1
0
Fork 0
php-language-server/tests/Server/TextDocument/ParseErrorsTest.php

166 lines
6.0 KiB
PHP
Raw Normal View History

<?php
2018-11-27 16:51:40 +00:00
declare(strict_types=1);
namespace LanguageServer\Tests\Server\TextDocument;
use PHPUnit\Framework\TestCase;
use LanguageServer\Tests\MockProtocolStream;
2018-11-27 16:51:40 +00:00
use LanguageServer\{Server, Client, LanguageClient, ClientHandler, PhpDocumentLoader, DefinitionResolver};
2016-12-13 00:51:02 +00:00
use LanguageServer\Index\{Index, ProjectIndex, DependenciesIndex};
2016-12-08 01:33:48 +00:00
use LanguageServer\ContentRetriever\FileSystemContentRetriever;
use LanguageServerProtocol\{TextDocumentItem, DiagnosticSeverity};
2016-10-31 10:47:21 +00:00
use Sabre\Event\Promise;
use JsonMapper;
class ParseErrorsTest extends TestCase
{
/**
* @var Server\TextDocument
*/
private $textDocument;
private $args;
public function setUp()
{
2018-11-27 16:51:40 +00:00
$client = new LanguageClient(new MockProtocolStream(), new MockProtocolStream());
$client->textDocument = new class($this->args) extends Client\TextDocument {
private $args;
public function __construct(&$args)
{
2018-11-27 16:51:40 +00:00
parent::__construct(
new ClientHandler(new MockProtocolStream(), new MockProtocolStream()),
new JsonMapper()
);
$this->args = &$args;
}
2016-10-31 10:47:21 +00:00
public function publishDiagnostics(string $uri, array $diagnostics): Promise
{
$this->args = func_get_args();
2016-10-31 10:47:21 +00:00
return Promise\resolve(null);
}
};
2018-11-27 16:51:40 +00:00
$projectIndex = new ProjectIndex(new Index(), new DependenciesIndex());
2016-12-13 00:51:02 +00:00
$definitionResolver = new DefinitionResolver($projectIndex);
2018-11-27 16:51:40 +00:00
$loader = new PhpDocumentLoader(new FileSystemContentRetriever(), $projectIndex, $definitionResolver);
2016-12-13 00:51:02 +00:00
$this->textDocument = new Server\TextDocument($loader, $definitionResolver, $client, $projectIndex);
}
private function openFile($file)
{
$textDocumentItem = new TextDocumentItem();
$textDocumentItem->uri = 'whatever';
$textDocumentItem->languageId = 'php';
$textDocumentItem->version = 1;
$textDocumentItem->text = file_get_contents($file);
$this->textDocument->didOpen($textDocumentItem);
}
public function testParseErrorsArePublishedAsDiagnostics()
{
$this->openFile(__DIR__ . '/../../../fixtures/invalid_file.php');
2018-11-27 16:51:40 +00:00
$this->assertEquals(
[
2018-11-27 16:51:40 +00:00
'whatever',
[
[
'range' => [
'start' => [
'line' => 2,
'character' => 9
],
'end' => [
'line' => 2,
'character' => 9
]
],
'severity' => DiagnosticSeverity::ERROR,
'code' => null,
'source' => 'php',
'message' => "'Name' expected."
],
2018-11-27 16:51:40 +00:00
[
'range' => [
'start' => [
'line' => 2,
'character' => 9
],
'end' => [
'line' => 2,
'character' => 9
]
],
'severity' => DiagnosticSeverity::ERROR,
'code' => null,
'source' => 'php',
'message' => "'{' expected."
],
2018-11-27 16:51:40 +00:00
[
'range' => [
'start' => [
'line' => 2,
'character' => 9
],
'end' => [
'line' => 2,
'character' => 9
]
],
'severity' => DiagnosticSeverity::ERROR,
'code' => null,
'source' => 'php',
'message' => "'}' expected."
],
2018-11-27 16:51:40 +00:00
[
'range' => [
'start' => [
'line' => 2,
'character' => 15
],
'end' => [
'line' => 2,
'character' => 15
]
],
'severity' => DiagnosticSeverity::ERROR,
'code' => null,
'source' => 'php',
'message' => "'Name' expected."
]
2018-11-27 16:51:40 +00:00
]
],
json_decode(json_encode($this->args), true)
);
}
public function testParseErrorsWithOnlyStartLine()
{
$this->markTestIncomplete('This diagnostic not yet implemented in tolerant-php-parser');
$this->openFile(__DIR__ . '/../../../fixtures/namespace_not_first.php');
2018-11-27 16:51:40 +00:00
$this->assertEquals(
[
'whatever',
[
[
'range' => [
'start' => [
'line' => 4,
'character' => 0
],
'end' => [
'line' => 4,
'character' => 0
]
],
'severity' => DiagnosticSeverity::ERROR,
'code' => null,
'source' => 'php',
'message' => "Namespace declaration statement has to be the very first statement in the script"
]
2018-11-27 16:51:40 +00:00
]
],
json_decode(json_encode($this->args), true)
);
}
}