2016-11-19 04:06:24 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace LanguageServer\Tests\Server\TextDocument;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use LanguageServer\Tests\MockProtocolStream;
|
2016-11-24 11:27:55 +00:00
|
|
|
use LanguageServer\{Server, LanguageClient, Project, CompletionProvider};
|
|
|
|
use LanguageServer\Protocol\{
|
|
|
|
TextDocumentIdentifier,
|
|
|
|
TextEdit,
|
|
|
|
Range,
|
|
|
|
Position,
|
|
|
|
ClientCapabilities,
|
2016-11-28 17:47:56 +00:00
|
|
|
CompletionList,
|
2016-11-24 11:27:55 +00:00
|
|
|
CompletionItem,
|
|
|
|
CompletionItemKind
|
|
|
|
};
|
2016-11-19 04:06:24 +00:00
|
|
|
use function LanguageServer\pathToUri;
|
|
|
|
|
|
|
|
class CompletionTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Server\TextDocument
|
|
|
|
*/
|
|
|
|
private $textDocument;
|
|
|
|
|
|
|
|
/**
|
2016-11-19 13:02:43 +00:00
|
|
|
* @var Project
|
2016-11-19 04:06:24 +00:00
|
|
|
*/
|
2016-11-19 13:02:43 +00:00
|
|
|
private $project;
|
2016-11-19 04:06:24 +00:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$client = new LanguageClient(new MockProtocolStream, new MockProtocolStream);
|
2016-11-19 13:02:43 +00:00
|
|
|
$this->project = new Project($client, new ClientCapabilities);
|
|
|
|
$this->project->loadDocument(pathToUri(__DIR__ . '/../../../fixtures/global_symbols.php'))->wait();
|
2016-11-22 15:12:12 +00:00
|
|
|
$this->project->loadDocument(pathToUri(__DIR__ . '/../../../fixtures/symbols.php'))->wait();
|
2016-11-19 13:02:43 +00:00
|
|
|
$this->textDocument = new Server\TextDocument($this->project, $client);
|
2016-11-19 04:06:24 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 20:24:39 +00:00
|
|
|
public function testPropertyAndMethodWithPrefix()
|
2016-11-19 04:06:24 +00:00
|
|
|
{
|
2016-11-22 20:24:39 +00:00
|
|
|
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/property_with_prefix.php');
|
2016-11-19 13:02:43 +00:00
|
|
|
$this->project->openDocument($completionUri, file_get_contents($completionUri));
|
2016-11-19 04:06:24 +00:00
|
|
|
$items = $this->textDocument->completion(
|
2016-11-19 13:02:43 +00:00
|
|
|
new TextDocumentIdentifier($completionUri),
|
2016-11-19 04:06:24 +00:00
|
|
|
new Position(3, 7)
|
|
|
|
)->wait();
|
2016-11-28 17:47:56 +00:00
|
|
|
$this->assertEquals(new CompletionList([
|
2016-11-19 04:06:24 +00:00
|
|
|
new CompletionItem(
|
|
|
|
'testProperty',
|
|
|
|
CompletionItemKind::PROPERTY,
|
|
|
|
'\TestClass', // Type of the property
|
|
|
|
'Reprehenderit magna velit mollit ipsum do.'
|
|
|
|
),
|
|
|
|
new CompletionItem(
|
2016-11-22 20:24:39 +00:00
|
|
|
'testMethod',
|
|
|
|
CompletionItemKind::METHOD,
|
|
|
|
'\TestClass', // Return type of the method
|
|
|
|
'Non culpa nostrud mollit esse sunt laboris in irure ullamco cupidatat amet.'
|
|
|
|
)
|
2016-11-28 17:47:56 +00:00
|
|
|
]), $items);
|
2016-11-22 20:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPropertyAndMethodWithoutPrefix()
|
|
|
|
{
|
|
|
|
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/property.php');
|
|
|
|
$this->project->openDocument($completionUri, file_get_contents($completionUri));
|
|
|
|
$items = $this->textDocument->completion(
|
|
|
|
new TextDocumentIdentifier($completionUri),
|
|
|
|
new Position(3, 6)
|
|
|
|
)->wait();
|
2016-11-28 17:47:56 +00:00
|
|
|
$this->assertEquals(new CompletionList([
|
2016-11-22 20:24:39 +00:00
|
|
|
new CompletionItem(
|
|
|
|
'testProperty',
|
|
|
|
CompletionItemKind::PROPERTY,
|
|
|
|
'\TestClass', // Type of the property
|
|
|
|
'Reprehenderit magna velit mollit ipsum do.'
|
|
|
|
),
|
|
|
|
new CompletionItem(
|
2016-11-19 04:06:24 +00:00
|
|
|
'testMethod',
|
|
|
|
CompletionItemKind::METHOD,
|
|
|
|
'\TestClass', // Return type of the method
|
|
|
|
'Non culpa nostrud mollit esse sunt laboris in irure ullamco cupidatat amet.'
|
|
|
|
)
|
2016-11-28 17:47:56 +00:00
|
|
|
]), $items);
|
2016-11-19 04:06:24 +00:00
|
|
|
}
|
2016-11-20 17:53:03 +00:00
|
|
|
|
2016-11-22 15:12:12 +00:00
|
|
|
public function testVariable()
|
2016-11-20 17:53:03 +00:00
|
|
|
{
|
|
|
|
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/variable.php');
|
|
|
|
$this->project->openDocument($completionUri, file_get_contents($completionUri));
|
|
|
|
$items = $this->textDocument->completion(
|
|
|
|
new TextDocumentIdentifier($completionUri),
|
|
|
|
new Position(8, 5)
|
|
|
|
)->wait();
|
2016-11-28 17:47:56 +00:00
|
|
|
$this->assertEquals(new CompletionList([
|
2016-11-24 12:26:22 +00:00
|
|
|
new CompletionItem(
|
|
|
|
'$var',
|
|
|
|
CompletionItemKind::VARIABLE,
|
|
|
|
'int',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
new TextEdit(new Range(new Position(8, 5), new Position(8, 5)), 'var')
|
|
|
|
),
|
|
|
|
new CompletionItem(
|
|
|
|
'$param',
|
|
|
|
CompletionItemKind::VARIABLE,
|
|
|
|
'string|null',
|
|
|
|
'A parameter',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
new TextEdit(new Range(new Position(8, 5), new Position(8, 5)), 'param')
|
|
|
|
)
|
2016-11-28 17:47:56 +00:00
|
|
|
]), $items);
|
2016-11-20 17:53:03 +00:00
|
|
|
}
|
2016-11-22 15:12:12 +00:00
|
|
|
|
|
|
|
public function testVariableWithPrefix()
|
|
|
|
{
|
|
|
|
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/variable_with_prefix.php');
|
|
|
|
$this->project->openDocument($completionUri, file_get_contents($completionUri));
|
|
|
|
$items = $this->textDocument->completion(
|
|
|
|
new TextDocumentIdentifier($completionUri),
|
2016-11-24 12:26:22 +00:00
|
|
|
new Position(8, 6)
|
2016-11-22 15:12:12 +00:00
|
|
|
)->wait();
|
2016-11-28 17:47:56 +00:00
|
|
|
$this->assertEquals(new CompletionList([
|
2016-11-24 12:26:22 +00:00
|
|
|
new CompletionItem(
|
|
|
|
'$param',
|
|
|
|
CompletionItemKind::VARIABLE,
|
|
|
|
'string|null',
|
|
|
|
'A parameter',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
new TextEdit(new Range(new Position(8, 6), new Position(8, 6)), 'aram')
|
|
|
|
)
|
2016-11-28 17:47:56 +00:00
|
|
|
]), $items);
|
2016-11-22 15:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNewInNamespace()
|
|
|
|
{
|
|
|
|
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/used_new.php');
|
|
|
|
$this->project->openDocument($completionUri, file_get_contents($completionUri));
|
|
|
|
$items = $this->textDocument->completion(
|
|
|
|
new TextDocumentIdentifier($completionUri),
|
|
|
|
new Position(6, 10)
|
|
|
|
)->wait();
|
2016-11-28 17:47:56 +00:00
|
|
|
$this->assertEquals(new CompletionList([
|
2016-11-22 15:12:12 +00:00
|
|
|
// Global TestClass definition (inserted as \TestClass)
|
|
|
|
new CompletionItem(
|
|
|
|
'TestClass',
|
|
|
|
CompletionItemKind::CLASS_,
|
|
|
|
null,
|
|
|
|
'Pariatur ut laborum tempor voluptate consequat ea deserunt.',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'\TestClass'
|
|
|
|
),
|
|
|
|
// Namespaced, `use`d TestClass definition (inserted as TestClass)
|
|
|
|
new CompletionItem(
|
|
|
|
'TestClass',
|
|
|
|
CompletionItemKind::CLASS_,
|
|
|
|
'TestNamespace',
|
|
|
|
'Pariatur ut laborum tempor voluptate consequat ea deserunt.',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'TestClass'
|
|
|
|
),
|
2016-11-28 17:47:56 +00:00
|
|
|
]), $items);
|
2016-11-22 15:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUsedClass()
|
|
|
|
{
|
|
|
|
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/used_class.php');
|
|
|
|
$this->project->openDocument($completionUri, file_get_contents($completionUri));
|
|
|
|
$items = $this->textDocument->completion(
|
|
|
|
new TextDocumentIdentifier($completionUri),
|
|
|
|
new Position(6, 5)
|
|
|
|
)->wait();
|
2016-11-28 17:47:56 +00:00
|
|
|
$this->assertEquals(new CompletionList([
|
2016-11-22 15:12:12 +00:00
|
|
|
new CompletionItem(
|
|
|
|
'TestClass',
|
|
|
|
CompletionItemKind::CLASS_,
|
|
|
|
'TestNamespace',
|
|
|
|
'Pariatur ut laborum tempor voluptate consequat ea deserunt.'
|
|
|
|
)
|
2016-11-28 17:47:56 +00:00
|
|
|
]), $items);
|
2016-11-22 15:12:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 16:33:56 +00:00
|
|
|
public function testStaticPropertyWithPrefix()
|
|
|
|
{
|
|
|
|
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/static_property_with_prefix.php');
|
|
|
|
$this->project->openDocument($completionUri, file_get_contents($completionUri));
|
|
|
|
$items = $this->textDocument->completion(
|
|
|
|
new TextDocumentIdentifier($completionUri),
|
|
|
|
new Position(2, 14)
|
|
|
|
)->wait();
|
2016-11-28 17:47:56 +00:00
|
|
|
$this->assertEquals(new CompletionList([
|
2016-11-22 16:33:56 +00:00
|
|
|
new CompletionItem(
|
|
|
|
'staticTestProperty',
|
|
|
|
CompletionItemKind::PROPERTY,
|
|
|
|
'\TestClass[]',
|
2016-11-24 11:27:55 +00:00
|
|
|
'Lorem excepteur officia sit anim velit veniam enim.',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'$staticTestProperty'
|
2016-11-22 16:33:56 +00:00
|
|
|
)
|
2016-11-28 17:47:56 +00:00
|
|
|
]), $items);
|
2016-11-22 16:33:56 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 23:04:36 +00:00
|
|
|
public function testStaticWithoutPrefix()
|
|
|
|
{
|
|
|
|
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/static.php');
|
|
|
|
$this->project->openDocument($completionUri, file_get_contents($completionUri));
|
|
|
|
$items = $this->textDocument->completion(
|
|
|
|
new TextDocumentIdentifier($completionUri),
|
|
|
|
new Position(2, 11)
|
|
|
|
)->wait();
|
2016-11-28 17:47:56 +00:00
|
|
|
$this->assertEquals(new CompletionList([
|
2016-11-22 23:04:36 +00:00
|
|
|
new CompletionItem(
|
|
|
|
'TEST_CLASS_CONST',
|
|
|
|
CompletionItemKind::VARIABLE,
|
|
|
|
'int',
|
|
|
|
'Anim labore veniam consectetur laboris minim quis aute aute esse nulla ad.'
|
|
|
|
),
|
|
|
|
new CompletionItem(
|
|
|
|
'staticTestProperty',
|
|
|
|
CompletionItemKind::PROPERTY,
|
|
|
|
'\TestClass[]',
|
2016-11-24 11:27:55 +00:00
|
|
|
'Lorem excepteur officia sit anim velit veniam enim.',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'$staticTestProperty'
|
2016-11-22 23:04:36 +00:00
|
|
|
),
|
|
|
|
new CompletionItem(
|
|
|
|
'staticTestMethod',
|
|
|
|
CompletionItemKind::METHOD,
|
|
|
|
'mixed', // Method return type
|
|
|
|
'Do magna consequat veniam minim proident eiusmod incididunt aute proident.'
|
|
|
|
)
|
2016-11-28 17:47:56 +00:00
|
|
|
]), $items);
|
2016-11-22 23:04:36 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 16:33:56 +00:00
|
|
|
public function testStaticMethodWithPrefix()
|
|
|
|
{
|
|
|
|
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/static_method_with_prefix.php');
|
|
|
|
$this->project->openDocument($completionUri, file_get_contents($completionUri));
|
|
|
|
$items = $this->textDocument->completion(
|
|
|
|
new TextDocumentIdentifier($completionUri),
|
|
|
|
new Position(2, 13)
|
|
|
|
)->wait();
|
2016-11-28 17:47:56 +00:00
|
|
|
$this->assertEquals(new CompletionList([
|
2016-11-22 16:33:56 +00:00
|
|
|
new CompletionItem(
|
|
|
|
'staticTestMethod',
|
|
|
|
CompletionItemKind::METHOD,
|
|
|
|
'mixed', // Method return type
|
|
|
|
'Do magna consequat veniam minim proident eiusmod incididunt aute proident.'
|
|
|
|
)
|
2016-11-28 17:47:56 +00:00
|
|
|
]), $items);
|
2016-11-22 16:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testClassConstWithPrefix()
|
|
|
|
{
|
|
|
|
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/class_const_with_prefix.php');
|
|
|
|
$this->project->openDocument($completionUri, file_get_contents($completionUri));
|
|
|
|
$items = $this->textDocument->completion(
|
|
|
|
new TextDocumentIdentifier($completionUri),
|
|
|
|
new Position(2, 13)
|
|
|
|
)->wait();
|
2016-11-28 17:47:56 +00:00
|
|
|
$this->assertEquals(new CompletionList([
|
2016-11-22 16:33:56 +00:00
|
|
|
new CompletionItem(
|
|
|
|
'TEST_CLASS_CONST',
|
|
|
|
CompletionItemKind::VARIABLE,
|
|
|
|
'int',
|
|
|
|
'Anim labore veniam consectetur laboris minim quis aute aute esse nulla ad.'
|
|
|
|
)
|
2016-11-28 17:47:56 +00:00
|
|
|
]), $items);
|
2016-11-22 16:33:56 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 15:12:12 +00:00
|
|
|
public function testFullyQualifiedClass()
|
|
|
|
{
|
|
|
|
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/fully_qualified_class.php');
|
|
|
|
$this->project->openDocument($completionUri, file_get_contents($completionUri));
|
|
|
|
$items = $this->textDocument->completion(
|
|
|
|
new TextDocumentIdentifier($completionUri),
|
|
|
|
new Position(6, 6)
|
|
|
|
)->wait();
|
2016-11-28 17:47:56 +00:00
|
|
|
$this->assertEquals(new CompletionList([
|
2016-11-22 15:12:12 +00:00
|
|
|
new CompletionItem(
|
|
|
|
'TestClass',
|
|
|
|
CompletionItemKind::CLASS_,
|
|
|
|
null,
|
2016-11-22 23:04:36 +00:00
|
|
|
'Pariatur ut laborum tempor voluptate consequat ea deserunt.',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'TestClass'
|
2016-11-22 15:12:12 +00:00
|
|
|
)
|
2016-11-28 17:47:56 +00:00
|
|
|
]), $items);
|
2016-11-22 15:12:12 +00:00
|
|
|
}
|
2016-11-24 11:27:55 +00:00
|
|
|
|
|
|
|
public function testKeywords()
|
|
|
|
{
|
|
|
|
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/keywords.php');
|
|
|
|
$this->project->openDocument($completionUri, file_get_contents($completionUri));
|
|
|
|
$items = $this->textDocument->completion(
|
|
|
|
new TextDocumentIdentifier($completionUri),
|
|
|
|
new Position(2, 1)
|
|
|
|
)->wait();
|
2016-11-28 17:47:56 +00:00
|
|
|
$this->assertEquals(new CompletionList([
|
2016-11-24 11:27:55 +00:00
|
|
|
new CompletionItem('class', CompletionItemKind::KEYWORD, null, null, null, null, 'class '),
|
|
|
|
new CompletionItem('clone', CompletionItemKind::KEYWORD, null, null, null, null, 'clone ')
|
2016-11-28 17:47:56 +00:00
|
|
|
]), $items);
|
2016-11-24 11:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testHtmlWithoutPrefix()
|
|
|
|
{
|
|
|
|
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/html.php');
|
|
|
|
$this->project->openDocument($completionUri, file_get_contents($completionUri));
|
|
|
|
$items = $this->textDocument->completion(
|
|
|
|
new TextDocumentIdentifier($completionUri),
|
|
|
|
new Position(0, 0)
|
|
|
|
)->wait();
|
2016-11-28 17:47:56 +00:00
|
|
|
$this->assertEquals(new CompletionList([
|
2016-11-24 11:27:55 +00:00
|
|
|
new CompletionItem(
|
|
|
|
'<?php',
|
|
|
|
CompletionItemKind::KEYWORD,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
new TextEdit(new Range(new Position(0, 0), new Position(0, 0)), '<?php')
|
|
|
|
)
|
2016-11-28 17:47:56 +00:00
|
|
|
]), $items);
|
2016-11-24 11:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testHtmlWithPrefix()
|
|
|
|
{
|
|
|
|
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/html_with_prefix.php');
|
|
|
|
$this->project->openDocument($completionUri, file_get_contents($completionUri));
|
|
|
|
$items = $this->textDocument->completion(
|
|
|
|
new TextDocumentIdentifier($completionUri),
|
|
|
|
new Position(0, 1)
|
|
|
|
)->wait();
|
2016-11-28 17:47:56 +00:00
|
|
|
$this->assertEquals(new CompletionList([
|
2016-11-24 11:27:55 +00:00
|
|
|
new CompletionItem(
|
|
|
|
'<?php',
|
|
|
|
CompletionItemKind::KEYWORD,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
new TextEdit(new Range(new Position(0, 1), new Position(0, 1)), '?php')
|
|
|
|
)
|
2016-11-28 17:47:56 +00:00
|
|
|
]), $items);
|
2016-11-24 11:27:55 +00:00
|
|
|
}
|
2016-11-24 22:36:45 +00:00
|
|
|
|
|
|
|
public function testNamespace()
|
|
|
|
{
|
|
|
|
$completionUri = pathToUri(__DIR__ . '/../../../fixtures/completion/namespace.php');
|
|
|
|
$this->project->openDocument($completionUri, file_get_contents($completionUri));
|
|
|
|
$items = $this->textDocument->completion(
|
|
|
|
new TextDocumentIdentifier($completionUri),
|
|
|
|
new Position(4, 6)
|
|
|
|
)->wait();
|
2016-11-28 17:47:56 +00:00
|
|
|
$this->assertEquals(new CompletionList([
|
2016-11-24 22:36:45 +00:00
|
|
|
new CompletionItem(
|
|
|
|
'SomeNamespace',
|
|
|
|
CompletionItemKind::MODULE,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'SomeNamespace'
|
|
|
|
)
|
2016-11-28 17:47:56 +00:00
|
|
|
]), $items);
|
2016-11-24 22:36:45 +00:00
|
|
|
}
|
2016-11-19 04:06:24 +00:00
|
|
|
}
|