From 691a0bddfe221b2912bcc066a1c3f37ff33e4b5e Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Tue, 18 Oct 2016 10:48:16 +0200 Subject: [PATCH] Refactor tests for easier changes to fixtures (#87) --- .../TextDocument/Definition/GlobalTest.php | 332 +++----------- .../Definition/NamespacedTest.php | 412 +----------------- .../References/GlobalFallbackTest.php | 42 +- .../TextDocument/References/GlobalTest.php | 317 ++------------ .../References/NamespacedTest.php | 358 +-------------- .../TextDocument/TextDocumentTestCase.php | 164 +++++++ 6 files changed, 295 insertions(+), 1330 deletions(-) create mode 100644 tests/Server/TextDocument/TextDocumentTestCase.php diff --git a/tests/Server/TextDocument/Definition/GlobalTest.php b/tests/Server/TextDocument/Definition/GlobalTest.php index 23013ea..d2ce222 100644 --- a/tests/Server/TextDocument/Definition/GlobalTest.php +++ b/tests/Server/TextDocument/Definition/GlobalTest.php @@ -3,380 +3,174 @@ declare(strict_types = 1); namespace LanguageServer\Tests\Server\TextDocument\Definition; -use PHPUnit\Framework\TestCase; -use LanguageServer\Tests\MockProtocolStream; -use LanguageServer\{Server, LanguageClient, Project}; -use LanguageServer\Protocol\{TextDocumentIdentifier, Position}; +use LanguageServer\Tests\Server\TextDocument\TextDocumentTestCase; +use LanguageServer\Protocol\{TextDocumentIdentifier, Position, Location, Range}; +use function LanguageServer\pathToUri; -class GlobalTest extends TestCase +class GlobalTest extends TextDocumentTestCase { - /** - * @var Server\TextDocument - */ - private $textDocument; - - public function setUp() - { - $client = new LanguageClient(new MockProtocolStream()); - $project = new Project($client); - $this->textDocument = new Server\TextDocument($project, $client); - $project->openDocument('references', file_get_contents(__DIR__ . '/../../../../fixtures/global_references.php')); - $project->openDocument('symbols', file_get_contents(__DIR__ . '/../../../../fixtures/global_symbols.php')); - // Load this to check that there are no conflicts - $project->openDocument('references_namespaced', file_get_contents(__DIR__ . '/../../../../fixtures/references.php')); - $project->openDocument('symbols_namespaced', file_get_contents(__DIR__ . '/../../../../fixtures/symbols.php')); - $project->openDocument('use', file_get_contents(__DIR__ . '/../../../../fixtures/use.php')); - } - public function testDefinitionFileBeginning() { // |textDocument->definition(new TextDocumentIdentifier('references'), new Position(0, 0)); - $this->assertEquals([], json_decode(json_encode($result), true)); + $result = $this->textDocument->definition(new TextDocumentIdentifier(pathToUri(realpath(__DIR__ . '/../../../../fixtures/references.php'))), new Position(0, 0)); + $this->assertEquals([], $result); } public function testDefinitionEmptyResult() { // namespace keyword - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(2, 4)); - $this->assertEquals([], json_decode(json_encode($result), true)); + $result = $this->textDocument->definition(new TextDocumentIdentifier(pathToUri(realpath(__DIR__ . '/../../../../fixtures/references.php'))), new Position(2, 4)); + $this->assertEquals([], $result); } public function testDefinitionForClassLike() { // $obj = new TestClass(); // Get definition for TestClass - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(4, 16)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 6, - 'character' => 0 - ], - 'end' => [ - 'line' => 21, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); + $reference = $this->getReferenceLocations('TestClass')[0]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->start); + $this->assertEquals($this->getDefinitionLocation('TestClass'), $result); } - public function testDefinitionForClassOnStaticMethodCall() { // $obj = new TestClass(); // Get definition for TestClass - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(7, 6)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 6, - 'character' => 0 - ], - 'end' => [ - 'line' => 21, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); + $reference = $this->getReferenceLocations('TestClass')[1]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->start); + $this->assertEquals($this->getDefinitionLocation('TestClass'), $result); } public function testDefinitionForClassOnStaticPropertyFetch() { - // $obj = new TestClass(); + // echo TestClass::$staticTestProperty; // Get definition for TestClass - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(8, 10)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 6, - 'character' => 0 - ], - 'end' => [ - 'line' => 21, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); + $reference = $this->getReferenceLocations('TestClass')[2]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->start); + $this->assertEquals($this->getDefinitionLocation('TestClass'), $result); } public function testDefinitionForClassOnConstFetch() { - // $obj = new TestClass(); + // TestClass::TEST_CLASS_CONST; // Get definition for TestClass - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(9, 10)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 6, - 'character' => 0 - ], - 'end' => [ - 'line' => 21, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); + $reference = $this->getReferenceLocations('TestClass')[3]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->start); + $this->assertEquals($this->getDefinitionLocation('TestClass'), $result); } public function testDefinitionForImplements() { // class TestClass implements TestInterface // Get definition for TestInterface - $result = $this->textDocument->definition(new TextDocumentIdentifier('symbols'), new Position(6, 33)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 28, - 'character' => 0 - ], - 'end' => [ - 'line' => 31, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); + $reference = $this->getReferenceLocations('TestInterface')[0]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->start); + $this->assertEquals($this->getDefinitionLocation('TestInterface'), $result); } public function testDefinitionForClassConstants() { // echo TestClass::TEST_CLASS_CONST; // Get definition for TEST_CLASS_CONST - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(9, 21)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 8, - 'character' => 10 - ], - 'end' => [ - 'line' => 8, - 'character' => 32 - ] - ] - ], json_decode(json_encode($result), true)); + $reference = $this->getReferenceLocations('TestClass::TEST_CLASS_CONST')[0]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->start); + $this->assertEquals($this->getDefinitionLocation('TestClass::TEST_CLASS_CONST'), $result); } public function testDefinitionForConstants() { // echo TEST_CONST; // Get definition for TEST_CONST - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(23, 9)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 4, - 'character' => 6 - ], - 'end' => [ - 'line' => 4, - 'character' => 22 - ] - ] - ], json_decode(json_encode($result), true)); + $reference = $this->getReferenceLocations('TEST_CONST')[1]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->start); + $this->assertEquals($this->getDefinitionLocation('TEST_CONST'), $result); } public function testDefinitionForStaticMethods() { // TestClass::staticTestMethod(); // Get definition for staticTestMethod - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(7, 20)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 12, - 'character' => 4 - ], - 'end' => [ - 'line' => 15, - 'character' => 5 - ] - ] - ], json_decode(json_encode($result), true)); + $reference = $this->getReferenceLocations('TestClass::staticTestMethod()')[0]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->end); + $this->assertEquals($this->getDefinitionLocation('TestClass::staticTestMethod()'), $result); } public function testDefinitionForStaticProperties() { // echo TestClass::$staticTestProperty; // Get definition for staticTestProperty - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(8, 25)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 9, - 'character' => 18 - ], - 'end' => [ - 'line' => 9, - 'character' => 37 - ] - ] - ], json_decode(json_encode($result), true)); + $reference = $this->getReferenceLocations('TestClass::staticTestProperty')[0]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->end); + $this->assertEquals($this->getDefinitionLocation('TestClass::staticTestProperty'), $result); } public function testDefinitionForMethods() { // $obj->testMethod(); // Get definition for testMethod - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(5, 11)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 17, - 'character' => 4 - ], - 'end' => [ - 'line' => 20, - 'character' => 5 - ] - ] - ], json_decode(json_encode($result), true)); + $reference = $this->getReferenceLocations('TestClass::testMethod()')[0]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->end); + $this->assertEquals($this->getDefinitionLocation('TestClass::testMethod()'), $result); } public function testDefinitionForProperties() { // echo $obj->testProperty; // Get definition for testProperty - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(6, 18)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 10, - 'character' => 11 - ], - 'end' => [ - 'line' => 10, - 'character' => 24 - ] - ] - ], json_decode(json_encode($result), true)); + $reference = $this->getReferenceLocations('TestClass::testProperty')[0]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->end); + $this->assertEquals($this->getDefinitionLocation('TestClass::testProperty'), $result); } public function testDefinitionForVariables() { // echo $var; // Get definition for $var - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(13, 7)); - $this->assertEquals([ - 'uri' => 'references', - 'range' => [ - 'start' => [ - 'line' => 12, - 'character' => 0 - ], - 'end' => [ - 'line' => 12, - 'character' => 10 - ] - ] - ], json_decode(json_encode($result), true)); + $uri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/references.php')); + $result = $this->textDocument->definition(new TextDocumentIdentifier($uri), new Position(13, 7)); + $this->assertEquals(new Location($uri, new Range(new Position(12, 0), new Position(12, 10))), $result); } public function testDefinitionForParamTypeHints() { // function whatever(TestClass $param) { // Get definition for TestClass - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(15, 23)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 6, - 'character' => 0 - ], - 'end' => [ - 'line' => 21, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); + $reference = $this->getReferenceLocations('TestClass')[4]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->start); + $this->assertEquals($this->getDefinitionLocation('TestClass'), $result); } + public function testDefinitionForReturnTypeHints() { - // function whatever(TestClass $param) { + // function whatever(TestClass $param): TestClass { // Get definition for TestClass - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(15, 42)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 6, - 'character' => 0 - ], - 'end' => [ - 'line' => 21, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); + $reference = $this->getReferenceLocations('TestClass')[5]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->start); + $this->assertEquals($this->getDefinitionLocation('TestClass'), $result); } public function testDefinitionForParams() { // echo $param; // Get definition for $param - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(16, 13)); - $this->assertEquals([ - 'uri' => 'references', - 'range' => [ - 'start' => [ - 'line' => 15, - 'character' => 18 - ], - 'end' => [ - 'line' => 15, - 'character' => 34 - ] - ] - ], json_decode(json_encode($result), true)); + $uri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/references.php')); + $result = $this->textDocument->definition(new TextDocumentIdentifier($uri), new Position(16, 13)); + $this->assertEquals(new Location($uri, new Range(new Position(15, 18), new Position(15, 34))), $result); } public function testDefinitionForUsedVariables() { // echo $var; // Get definition for $var - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(20, 11)); - $this->assertEquals([ - 'uri' => 'references', - 'range' => [ - 'start' => [ - 'line' => 19, - 'character' => 22 - ], - 'end' => [ - 'line' => 19, - 'character' => 26 - ] - ] - ], json_decode(json_encode($result), true)); + $uri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/references.php')); + $result = $this->textDocument->definition(new TextDocumentIdentifier($uri), new Position(20, 11)); + $this->assertEquals(new Location($uri, new Range(new Position(19, 22), new Position(19, 26))), $result); } public function testDefinitionForFunctions() { // test_function(); // Get definition for test_function - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(10, 4)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 33, - 'character' => 0 - ], - 'end' => [ - 'line' => 36, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); + $reference = $this->getReferenceLocations('test_function()')[0]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->start); + $this->assertEquals($this->getDefinitionLocation('test_function()'), $result); } } diff --git a/tests/Server/TextDocument/Definition/NamespacedTest.php b/tests/Server/TextDocument/Definition/NamespacedTest.php index 4d9dc2a..e796ef9 100644 --- a/tests/Server/TextDocument/Definition/NamespacedTest.php +++ b/tests/Server/TextDocument/Definition/NamespacedTest.php @@ -3,417 +3,45 @@ declare(strict_types = 1); namespace LanguageServer\Tests\Server\TextDocument\Definition; -use PHPUnit\Framework\TestCase; -use LanguageServer\Tests\MockProtocolStream; -use LanguageServer\{Server, LanguageClient, Project}; -use LanguageServer\Protocol\{TextDocumentIdentifier, Position}; +use LanguageServer\Protocol\{TextDocumentIdentifier, Location}; use function LanguageServer\pathToUri; -class NamespacedTest extends TestCase +class NamespacedTest extends GlobalTest { - /** - * @var Server\TextDocument - */ - private $textDocument; - - public function setUp() + public function getReferenceLocations(string $fqn): array { - $client = new LanguageClient(new MockProtocolStream()); - $project = new Project($client); - $this->textDocument = new Server\TextDocument($project, $client); - $project->openDocument('references', file_get_contents(__DIR__ . '/../../../../fixtures/references.php')); - $project->openDocument('symbols', file_get_contents(__DIR__ . '/../../../../fixtures/symbols.php')); - $project->openDocument('use', file_get_contents(__DIR__ . '/../../../../fixtures/use.php')); + return parent::getReferenceLocations('TestNamespace\\' . $fqn); } - public function testDefinitionFileBeginning() { - // |textDocument->definition(new TextDocumentIdentifier('references'), new Position(0, 0)); - $this->assertEquals([], json_decode(json_encode($result), true)); - } - - public function testDefinitionEmptyResult() { - // namespace keyword - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(2, 4)); - $this->assertEquals([], json_decode(json_encode($result), true)); - } - - public function testDefinitionForClassLike() + public function getDefinitionLocation(string $fqn): Location { - // $obj = new TestClass(); - // Get definition for TestClass - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(4, 16)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 6, - 'character' => 0 - ], - 'end' => [ - 'line' => 21, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testDefinitionForClassOnStaticMethodCall() - { - // $obj = new TestClass(); - // Get definition for TestClass - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(7, 6)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 6, - 'character' => 0 - ], - 'end' => [ - 'line' => 21, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testDefinitionForClassOnStaticPropertyFetch() - { - // $obj = new TestClass(); - // Get definition for TestClass - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(8, 10)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 6, - 'character' => 0 - ], - 'end' => [ - 'line' => 21, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testDefinitionForClassOnConstFetch() - { - // $obj = new TestClass(); - // Get definition for TestClass - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(9, 10)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 6, - 'character' => 0 - ], - 'end' => [ - 'line' => 21, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testDefinitionForClassLikeUseStatement() - { - // use TestNamespace\TestClass; - // Get definition for TestClass - $result = $this->textDocument->definition(new TextDocumentIdentifier('use'), new Position(4, 22)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 6, - 'character' => 0 - ], - 'end' => [ - 'line' => 21, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testDefinitionForClassLikeGroupUseStatement() - { - // use TestNamespace\{TestTrait, TestInterface}; - // Get definition for TestInterface - $result = $this->textDocument->definition(new TextDocumentIdentifier('use'), new Position(5, 37)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 28, - 'character' => 0 - ], - 'end' => [ - 'line' => 31, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testDefinitionForImplements() - { - // class TestClass implements TestInterface - // Get definition for TestInterface - $result = $this->textDocument->definition(new TextDocumentIdentifier('symbols'), new Position(6, 33)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 28, - 'character' => 0 - ], - 'end' => [ - 'line' => 31, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testDefinitionForClassConstants() - { - // echo TestClass::TEST_CLASS_CONST; - // Get definition for TEST_CLASS_CONST - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(9, 21)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 8, - 'character' => 10 - ], - 'end' => [ - 'line' => 8, - 'character' => 32 - ] - ] - ], json_decode(json_encode($result), true)); + return parent::getDefinitionLocation('TestNamespace\\' . $fqn); } public function testDefinitionForConstants() { // echo TEST_CONST; // Get definition for TEST_CONST - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(23, 9)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 4, - 'character' => 6 - ], - 'end' => [ - 'line' => 4, - 'character' => 22 - ] - ] - ], json_decode(json_encode($result), true)); + $reference = $this->getReferenceLocations('TEST_CONST')[0]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->start); + $this->assertEquals($this->getDefinitionLocation('TEST_CONST'), $result); } - public function testDefinitionForStaticMethods() + public function testDefinitionForClassLikeUseStatement() { - // TestClass::staticTestMethod(); - // Get definition for staticTestMethod - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(7, 20)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 12, - 'character' => 4 - ], - 'end' => [ - 'line' => 15, - 'character' => 5 - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testDefinitionForStaticProperties() - { - // echo TestClass::$staticTestProperty; - // Get definition for staticTestProperty - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(8, 25)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 9, - 'character' => 18 - ], - 'end' => [ - 'line' => 9, - 'character' => 37 - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testDefinitionForMethods() - { - // $obj->testMethod(); - // Get definition for testMethod - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(5, 11)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 17, - 'character' => 4 - ], - 'end' => [ - 'line' => 20, - 'character' => 5 - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testDefinitionForProperties() - { - // echo $obj->testProperty; - // Get definition for testProperty - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(6, 18)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 10, - 'character' => 11 - ], - 'end' => [ - 'line' => 10, - 'character' => 24 - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testDefinitionForVariables() - { - // echo $var; - // Get definition for $var - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(13, 7)); - $this->assertEquals([ - 'uri' => 'references', - 'range' => [ - 'start' => [ - 'line' => 12, - 'character' => 0 - ], - 'end' => [ - 'line' => 12, - 'character' => 10 - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testDefinitionForParamTypeHints() - { - // function whatever(TestClass $param) { + // use TestNamespace\TestClass; // Get definition for TestClass - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(15, 23)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 6, - 'character' => 0 - ], - 'end' => [ - 'line' => 21, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); - } - public function testDefinitionForReturnTypeHints() - { - // function whatever(TestClass $param) { - // Get definition for TestClass - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(15, 42)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 6, - 'character' => 0 - ], - 'end' => [ - 'line' => 21, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); + $reference = $this->getReferenceLocations('TestClass')[6]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->start); + $this->assertEquals($this->getDefinitionLocation('TestClass'), $result); } - public function testDefinitionForParams() + public function testDefinitionForClassLikeGroupUseStatement() { - // echo $param; - // Get definition for $param - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(16, 13)); - $this->assertEquals([ - 'uri' => 'references', - 'range' => [ - 'start' => [ - 'line' => 15, - 'character' => 18 - ], - 'end' => [ - 'line' => 15, - 'character' => 34 - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testDefinitionForUsedVariables() - { - // echo $var; - // Get definition for $var - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(20, 11)); - $this->assertEquals([ - 'uri' => 'references', - 'range' => [ - 'start' => [ - 'line' => 19, - 'character' => 22 - ], - 'end' => [ - 'line' => 19, - 'character' => 26 - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testDefinitionForFunctions() - { - // test_function(); - // Get definition for test_function - $result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(10, 4)); - $this->assertEquals([ - 'uri' => 'symbols', - 'range' => [ - 'start' => [ - 'line' => 33, - 'character' => 0 - ], - 'end' => [ - 'line' => 36, - 'character' => 1 - ] - ] - ], json_decode(json_encode($result), true)); + // use TestNamespace\{TestTrait, TestInterface}; + // Get definition for TestInterface + $reference = $this->getReferenceLocations('TestClass')[0]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->start); + $this->assertEquals($this->getDefinitionLocation('TestClass'), $result); } } diff --git a/tests/Server/TextDocument/References/GlobalFallbackTest.php b/tests/Server/TextDocument/References/GlobalFallbackTest.php index a501613..5ad5442 100644 --- a/tests/Server/TextDocument/References/GlobalFallbackTest.php +++ b/tests/Server/TextDocument/References/GlobalFallbackTest.php @@ -6,15 +6,11 @@ namespace LanguageServer\Tests\Server\TextDocument\References; use PHPUnit\Framework\TestCase; use LanguageServer\Tests\MockProtocolStream; use LanguageServer\{Server, LanguageClient, Project}; -use LanguageServer\Protocol\{TextDocumentIdentifier, Position, ReferenceContext}; +use LanguageServer\Protocol\{TextDocumentIdentifier, Position, ReferenceContext, Location, Range}; +use LanguageServer\Tests\Server\TextDocument\TextDocumentTestCase; -class GlobalFallbackTest extends TestCase +class GlobalFallbackTest extends TextDocumentTestCase { - /** - * @var Server\TextDocument - */ - private $textDocument; - public function setUp() { $client = new LanguageClient(new MockProtocolStream()); @@ -37,21 +33,7 @@ class GlobalFallbackTest extends TestCase // const TEST_CONST = 123; // Get references for TEST_CONST $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier('global_symbols'), new Position(4, 13)); - $this->assertEquals([ - [ - 'uri' => 'global_fallback', - 'range' => [ - 'start' => [ - 'line' => 6, - 'character' => 5 - ], - 'end' => [ - 'line' => 6, - 'character' => 15 - ] - ] - ] - ], json_decode(json_encode($result), true)); + $this->assertEquals([new Location('global_fallback', new Range(new Position(6, 5), new Position(6, 15)))], $result); } public function testFallsBackForFunctions() @@ -59,20 +41,6 @@ class GlobalFallbackTest extends TestCase // function test_function() // Get references for test_function $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier('global_symbols'), new Position(33, 16)); - $this->assertEquals([ - [ - 'uri' => 'global_fallback', - 'range' => [ - 'start' => [ - 'line' => 5, - 'character' => 0 - ], - 'end' => [ - 'line' => 5, - 'character' => 13 - ] - ] - ] - ], json_decode(json_encode($result), true)); + $this->assertEquals([new Location('global_fallback', new Range(new Position(5, 0), new Position(5, 13)))], $result); } } diff --git a/tests/Server/TextDocument/References/GlobalTest.php b/tests/Server/TextDocument/References/GlobalTest.php index 5e0e177..ea09266 100644 --- a/tests/Server/TextDocument/References/GlobalTest.php +++ b/tests/Server/TextDocument/References/GlobalTest.php @@ -3,347 +3,104 @@ declare(strict_types = 1); namespace LanguageServer\Tests\Server\TextDocument\References; -use PHPUnit\Framework\TestCase; -use LanguageServer\Tests\MockProtocolStream; -use LanguageServer\{Server, LanguageClient, Project}; -use LanguageServer\Protocol\{TextDocumentIdentifier, Position, ReferenceContext}; +use LanguageServer\Protocol\{TextDocumentIdentifier, Position, ReferenceContext, Location, Range}; +use LanguageServer\Tests\Server\TextDocument\TextDocumentTestCase; use function LanguageServer\pathToUri; -class GlobalTest extends TestCase +class GlobalTest extends TextDocumentTestCase { - /** - * @var Server\TextDocument - */ - private $textDocument; - - private $symbolsUri; - private $referencesUri; - - public function setUp() - { - $client = new LanguageClient(new MockProtocolStream()); - $project = new Project($client); - $this->textDocument = new Server\TextDocument($project, $client); - $this->symbolsUri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/global_symbols.php')); - $this->referencesUri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/global_references.php')); - $project->loadDocument($this->referencesUri); - $project->loadDocument($this->symbolsUri); - } - public function testReferencesForClassLike() { // class TestClass implements TestInterface // Get references for TestClass - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->symbolsUri), new Position(6, 9)); - $this->assertEquals([ - // $obj = new TestClass(); - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 4, - 'character' => 11 - ], - 'end' => [ - 'line' => 4, - 'character' => 20 - ] - ] - ], - // TestClass::staticTestMethod(); - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 7, - 'character' => 0 - ], - 'end' => [ - 'line' => 7, - 'character' => 9 - ] - ] - ], - // echo TestClass::$staticTestProperty; - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 8, - 'character' => 5 - ], - 'end' => [ - 'line' => 8, - 'character' => 14 - ] - ] - ], - // TestClass::TEST_CLASS_CONST; - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 9, - 'character' => 5 - ], - 'end' => [ - 'line' => 9, - 'character' => 14 - ] - ] - ], - // function whatever(TestClass $param) - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 15, - 'character' => 18 - ], - 'end' => [ - 'line' => 15, - 'character' => 27 - ] - ] - ], - // function whatever(TestClass $param): TestClass - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 15, - 'character' => 37 - ], - 'end' => [ - 'line' => 15, - 'character' => 46 - ] - ] - ] - ], json_decode(json_encode($result), true)); + $definition = $this->getDefinitionLocation('TestClass'); + $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($definition->uri), $definition->range->start); + $this->assertEquals($this->getReferenceLocations('TestClass'), $result); } public function testReferencesForClassConstants() { // const TEST_CLASS_CONST = 123; // Get references for TEST_CLASS_CONST - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->symbolsUri), new Position(8, 19)); - $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 9, - 'character' => 5 - ], - 'end' => [ - 'line' => 9, - 'character' => 32 - ] - ] - ] - ], json_decode(json_encode($result), true)); + $definition = $this->getDefinitionLocation('TestClass'); + $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($definition->uri), $definition->range->start); + $this->assertEquals($this->getReferenceLocations('TestClass'), $result); } public function testReferencesForConstants() { // const TEST_CONST = 123; // Get references for TEST_CONST - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->symbolsUri), new Position(4, 13)); - $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 23, - 'character' => 5 - ], - 'end' => [ - 'line' => 23, - 'character' => 15 - ] - ] - ] - ], json_decode(json_encode($result), true)); + $definition = $this->getDefinitionLocation('TEST_CONST'); + $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($definition->uri), $definition->range->start); + $this->assertEquals($this->getReferenceLocations('TEST_CONST'), $result); } public function testReferencesForStaticMethods() { // public static function staticTestMethod() // Get references for staticTestMethod - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->symbolsUri), new Position(12, 35)); - $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 7, - 'character' => 0 - ], - 'end' => [ - 'line' => 7, - 'character' => 29 - ] - ] - ] - ], json_decode(json_encode($result), true)); + $definition = $this->getDefinitionLocation('TestClass::staticTestMethod()'); + $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($definition->uri), $definition->range->start); + $this->assertEquals($this->getReferenceLocations('TestClass::staticTestMethod()'), $result); } public function testReferencesForStaticProperties() { // public static $staticTestProperty; // Get references for $staticTestProperty - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->symbolsUri), new Position(9, 27)); - $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 8, - 'character' => 5 - ], - 'end' => [ - 'line' => 8, - 'character' => 35 - ] - ] - ] - ], json_decode(json_encode($result), true)); + $definition = $this->getDefinitionLocation('TestClass::staticTestProperty'); + $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($definition->uri), $definition->range->start); + $this->assertEquals($this->getReferenceLocations('TestClass::staticTestProperty'), $result); } public function testReferencesForMethods() { // public function testMethod($testParameter) // Get references for testMethod - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->symbolsUri), new Position(17, 24)); - $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 5, - 'character' => 0 - ], - 'end' => [ - 'line' => 5, - 'character' => 18 - ] - ] - ] - ], json_decode(json_encode($result), true)); + $definition = $this->getDefinitionLocation('TestClass::testMethod()'); + $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($definition->uri), $definition->range->start); + $this->assertEquals($this->getReferenceLocations('TestClass::testMethod()'), $result); } public function testReferencesForProperties() { // public $testProperty; // Get references for testProperty - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->symbolsUri), new Position(10, 15)); - $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 6, - 'character' => 5 - ], - 'end' => [ - 'line' => 6, - 'character' => 23 - ] - ] - ] - ], json_decode(json_encode($result), true)); + $definition = $this->getDefinitionLocation('TestClass::testProperty'); + $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($definition->uri), $definition->range->start); + $this->assertEquals($this->getReferenceLocations('TestClass::testProperty'), $result); } public function testReferencesForVariables() { // $var = 123; // Get definition for $var - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->referencesUri), new Position(13, 7)); + $uri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/references.php')); + $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($uri), new Position(13, 7)); $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 12, - 'character' => 0 - ], - 'end' => [ - 'line' => 12, - 'character' => 4 - ] - ] - ], - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 13, - 'character' => 5 - ], - 'end' => [ - 'line' => 13, - 'character' => 9 - ] - ] - ], - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 20, - 'character' => 9 - ], - 'end' => [ - 'line' => 20, - 'character' => 13 - ] - ] - ] - ], json_decode(json_encode($result), true)); + new Location($uri, new Range(new Position(12, 0), new Position(12, 4))), + new Location($uri, new Range(new Position(13, 5), new Position(13, 9))), + new Location($uri, new Range(new Position(20, 9), new Position(20, 13))) + ], $result); } public function testReferencesForFunctionParams() { // function whatever(TestClass $param): TestClass // Get references for $param - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->referencesUri), new Position(15, 32)); - $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 16, - 'character' => 9 - ], - 'end' => [ - 'line' => 16, - 'character' => 15 - ] - ] - ] - ], json_decode(json_encode($result), true)); + $uri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/references.php')); + $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($uri), new Position(15, 32)); + $this->assertEquals([new Location($uri, new Range(new Position(16, 9), new Position(16, 15)))], $result); } public function testReferencesForFunctions() { // function test_function() // Get references for test_function - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->symbolsUri), new Position(33, 16)); - $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 10, - 'character' => 0 - ], - 'end' => [ - 'line' => 10, - 'character' => 13 - ] - ] - ] - ], json_decode(json_encode($result), true)); + $referencesUri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/references.php')); + $symbolsUri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/symbols.php')); + $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($symbolsUri), new Position(33, 16)); + $this->assertEquals([new Location($referencesUri, new Range(new Position(10, 0), new Position(10, 13)))], $result); } } diff --git a/tests/Server/TextDocument/References/NamespacedTest.php b/tests/Server/TextDocument/References/NamespacedTest.php index eed82ee..930a9c8 100644 --- a/tests/Server/TextDocument/References/NamespacedTest.php +++ b/tests/Server/TextDocument/References/NamespacedTest.php @@ -3,364 +3,18 @@ declare(strict_types = 1); namespace LanguageServer\Tests\Server\TextDocument\References; -use PHPUnit\Framework\TestCase; -use LanguageServer\Tests\MockProtocolStream; -use LanguageServer\{Server, LanguageClient, Project}; -use LanguageServer\Protocol\{TextDocumentIdentifier, Position, ReferenceContext}; +use LanguageServer\Protocol\Location; use function LanguageServer\pathToUri; -class NamespacedTest extends TestCase +class NamespacedTest extends GlobalTest { - /** - * @var Server\TextDocument - */ - private $textDocument; - - private $symbolsUri; - private $referencesUri; - private $useUri; - - public function setUp() + protected function getReferenceLocations(string $fqn): array { - $client = new LanguageClient(new MockProtocolStream()); - $project = new Project($client); - $this->textDocument = new Server\TextDocument($project, $client); - $this->symbolsUri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/symbols.php')); - $this->referencesUri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/references.php')); - $this->useUri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/use.php')); - $project->loadDocument($this->referencesUri); - $project->loadDocument($this->symbolsUri); - $project->loadDocument($this->useUri); + return parent::getReferenceLocations('TestNamespace\\' . $fqn); } - public function testReferencesForClassLike() + protected function getDefinitionLocation(string $fqn): Location { - // class TestClass implements TestInterface - // Get references for TestClass - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->symbolsUri), new Position(6, 9)); - $this->assertEquals([ - // $obj = new TestClass(); - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 4, - 'character' => 11 - ], - 'end' => [ - 'line' => 4, - 'character' => 20 - ] - ] - ], - // TestClass::staticTestMethod(); - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 7, - 'character' => 0 - ], - 'end' => [ - 'line' => 7, - 'character' => 9 - ] - ] - ], - // echo TestClass::$staticTestProperty; - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 8, - 'character' => 5 - ], - 'end' => [ - 'line' => 8, - 'character' => 14 - ] - ] - ], - // TestClass::TEST_CLASS_CONST; - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 9, - 'character' => 5 - ], - 'end' => [ - 'line' => 9, - 'character' => 14 - ] - ] - ], - // function whatever(TestClass $param) - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 15, - 'character' => 18 - ], - 'end' => [ - 'line' => 15, - 'character' => 27 - ] - ] - ], - // function whatever(TestClass $param): TestClass - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 15, - 'character' => 37 - ], - 'end' => [ - 'line' => 15, - 'character' => 46 - ] - ] - ], - // use TestNamespace\TestClass; - [ - 'uri' => $this->useUri, - 'range' => [ - 'start' => [ - 'line' => 4, - 'character' => 4 - ], - 'end' => [ - 'line' => 4, - 'character' => 27 - ] - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testReferencesForClassConstants() - { - // const TEST_CLASS_CONST = 123; - // Get references for TEST_CLASS_CONST - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->symbolsUri), new Position(8, 19)); - $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 9, - 'character' => 5 - ], - 'end' => [ - 'line' => 9, - 'character' => 32 - ] - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testReferencesForConstants() - { - // const TEST_CONST = 123; - // Get references for TEST_CONST - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->symbolsUri), new Position(4, 13)); - $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 23, - 'character' => 5 - ], - 'end' => [ - 'line' => 23, - 'character' => 15 - ] - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testReferencesForStaticMethods() - { - // public static function staticTestMethod() - // Get references for staticTestMethod - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->symbolsUri), new Position(12, 35)); - $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 7, - 'character' => 0 - ], - 'end' => [ - 'line' => 7, - 'character' => 29 - ] - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testReferencesForStaticProperties() - { - // public static $staticTestProperty; - // Get references for $staticTestProperty - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->symbolsUri), new Position(9, 27)); - $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 8, - 'character' => 5 - ], - 'end' => [ - 'line' => 8, - 'character' => 35 - ] - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testReferencesForMethods() - { - // public function testMethod($testParameter) - // Get references for testMethod - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->symbolsUri), new Position(17, 24)); - $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 5, - 'character' => 0 - ], - 'end' => [ - 'line' => 5, - 'character' => 18 - ] - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testReferencesForProperties() - { - // public $testProperty; - // Get references for testProperty - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->symbolsUri), new Position(10, 15)); - $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 6, - 'character' => 5 - ], - 'end' => [ - 'line' => 6, - 'character' => 23 - ] - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testReferencesForVariables() - { - // $var = 123; - // Get definition for $var - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->referencesUri), new Position(13, 7)); - $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 12, - 'character' => 0 - ], - 'end' => [ - 'line' => 12, - 'character' => 4 - ] - ] - ], - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 13, - 'character' => 5 - ], - 'end' => [ - 'line' => 13, - 'character' => 9 - ] - ] - ], - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 20, - 'character' => 9 - ], - 'end' => [ - 'line' => 20, - 'character' => 13 - ] - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testReferencesForFunctionParams() - { - // function whatever(TestClass $param): TestClass - // Get references for $param - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->referencesUri), new Position(15, 32)); - $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 16, - 'character' => 9 - ], - 'end' => [ - 'line' => 16, - 'character' => 15 - ] - ] - ] - ], json_decode(json_encode($result), true)); - } - - public function testReferencesForFunctions() - { - // function test_function() - // Get references for test_function - $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($this->symbolsUri), new Position(33, 16)); - $this->assertEquals([ - [ - 'uri' => $this->referencesUri, - 'range' => [ - 'start' => [ - 'line' => 10, - 'character' => 0 - ], - 'end' => [ - 'line' => 10, - 'character' => 13 - ] - ] - ] - ], json_decode(json_encode($result), true)); + return parent::getDefinitionLocation('TestNamespace\\' . $fqn); } } diff --git a/tests/Server/TextDocument/TextDocumentTestCase.php b/tests/Server/TextDocument/TextDocumentTestCase.php new file mode 100644 index 0000000..1733fea --- /dev/null +++ b/tests/Server/TextDocument/TextDocumentTestCase.php @@ -0,0 +1,164 @@ +project = new Project($client); + $this->textDocument = new Server\TextDocument($this->project, $client); + + $globalSymbolsUri = pathToUri(realpath(__DIR__ . '/../../../fixtures/global_symbols.php')); + $globalReferencesUri = pathToUri(realpath(__DIR__ . '/../../../fixtures/global_references.php')); + $symbolsUri = pathToUri(realpath(__DIR__ . '/../../../fixtures/symbols.php')); + $referencesUri = pathToUri(realpath(__DIR__ . '/../../../fixtures/references.php')); + $useUri = pathToUri(realpath(__DIR__ . '/../../../fixtures/use.php')); + + $this->project->loadDocument($symbolsUri); + $this->project->loadDocument($referencesUri); + $this->project->loadDocument($globalSymbolsUri); + $this->project->loadDocument($globalReferencesUri); + $this->project->loadDocument($useUri); + + $this->definitionLocations = [ + + // Global + 'TEST_CONST' => new Location($globalSymbolsUri, new Range(new Position( 4, 6), new Position(4, 22))), + 'TestClass' => new Location($globalSymbolsUri, new Range(new Position( 6, 0), new Position(21, 1))), + 'TestInterface' => new Location($globalSymbolsUri, new Range(new Position(28, 0), new Position(31, 1))), + 'TestClass::TEST_CLASS_CONST' => new Location($globalSymbolsUri, new Range(new Position( 8, 10), new Position(8, 32))), + 'TestClass::testProperty' => new Location($globalSymbolsUri, new Range(new Position(10, 11), new Position(10, 24))), + 'TestClass::staticTestProperty' => new Location($globalSymbolsUri, new Range(new Position( 9, 18), new Position(9, 37))), + 'TestClass::staticTestMethod()' => new Location($globalSymbolsUri, new Range(new Position(12, 4), new Position(15, 5))), + 'TestClass::testMethod()' => new Location($globalSymbolsUri, new Range(new Position(17, 4), new Position(20, 5))), + 'test_function()' => new Location($globalSymbolsUri, new Range(new Position(33, 0), new Position(36, 1))), + + // Namespaced + 'TestNamespace\\TEST_CONST' => new Location($symbolsUri, new Range(new Position( 4, 6), new Position(4, 22))), + 'TestNamespace\\TestClass' => new Location($symbolsUri, new Range(new Position( 6, 0), new Position(21, 1))), + 'TestNamespace\\TestInterface' => new Location($symbolsUri, new Range(new Position(28, 0), new Position(31, 1))), + 'TestNamespace\\TestClass::TEST_CLASS_CONST' => new Location($symbolsUri, new Range(new Position( 8, 10), new Position(8, 32))), + 'TestNamespace\\TestClass::testProperty' => new Location($symbolsUri, new Range(new Position(10, 11), new Position(10, 24))), + 'TestNamespace\\TestClass::staticTestProperty' => new Location($symbolsUri, new Range(new Position( 9, 18), new Position(9, 37))), + 'TestNamespace\\TestClass::staticTestMethod()' => new Location($symbolsUri, new Range(new Position(12, 4), new Position(15, 5))), + 'TestNamespace\\TestClass::testMethod()' => new Location($symbolsUri, new Range(new Position(17, 4), new Position(20, 5))), + 'TestNamespace\\test_function()' => new Location($symbolsUri, new Range(new Position(33, 0), new Position(36, 1))) + ]; + + $this->referenceLocations = [ + + // Namespaced + 'TestNamespace\\TEST_CONST' => [ + 0 => new Location($referencesUri, new Range(new Position(23, 5), new Position(23, 15))) + ], + 'TestNamespace\\TestClass' => [ + 0 => new Location($referencesUri, new Range(new Position( 4, 11), new Position( 4, 20))), // $obj = new TestClass(); + 1 => new Location($referencesUri, new Range(new Position( 7, 0), new Position( 7, 9))), // TestClass::staticTestMethod(); + 2 => new Location($referencesUri, new Range(new Position( 8, 5), new Position( 8, 14))), // echo TestClass::$staticTestProperty; + 3 => new Location($referencesUri, new Range(new Position( 9, 5), new Position( 9, 14))), // TestClass::TEST_CLASS_CONST; + 4 => new Location($referencesUri, new Range(new Position(15, 18), new Position(15, 27))), // function whatever(TestClass $param) + 5 => new Location($referencesUri, new Range(new Position(15, 37), new Position(15, 46))), // function whatever(TestClass $param): TestClass + 6 => new Location($useUri, new Range(new Position( 4, 4), new Position( 4, 27))), // use TestNamespace\TestClass; + ], + 'TestNamespace\\TestInterface' => [ + 0 => new Location($symbolsUri, new Range(new Position( 6, 27), new Position( 6, 40))) // class TestClass implements TestInterface + ], + 'TestNamespace\\TestClass::TEST_CLASS_CONST' => [ + 0 => new Location($referencesUri, new Range(new Position( 9, 16), new Position( 9, 32))) + ], + 'TestNamespace\\TestClass::testProperty' => [ + 0 => new Location($referencesUri, new Range(new Position( 6, 5), new Position( 6, 23))) + ], + 'TestNamespace\\TestClass::staticTestProperty' => [ + 0 => new Location($referencesUri, new Range(new Position( 8, 5), new Position( 8, 35))) + ], + 'TestNamespace\\TestClass::staticTestMethod()' => [ + 0 => new Location($referencesUri, new Range(new Position( 7, 0), new Position( 7, 29))) + ], + 'TestNamespace\\TestClass::testMethod()' => [ + 0 => new Location($referencesUri, new Range(new Position( 5, 0), new Position( 5, 18))) + ], + 'TestNamespace\\test_function()' => [ + 0 => new Location($referencesUri, new Range(new Position(10, 0), new Position(10, 13))) + ], + + // Global + 'TEST_CONST' => [ + 0 => new Location($referencesUri, new Range(new Position(23, 5), new Position(23, 15))), + 1 => new Location($globalReferencesUri, new Range(new Position(23, 5), new Position(23, 15))) + ], + 'TestClass' => [ + 0 => new Location($globalReferencesUri, new Range(new Position( 4, 11), new Position( 4, 20))), // $obj = new TestClass(); + 1 => new Location($globalReferencesUri, new Range(new Position( 7, 0), new Position( 7, 9))), // TestClass::staticTestMethod(); + 2 => new Location($globalReferencesUri, new Range(new Position( 8, 5), new Position( 8, 14))), // echo TestClass::$staticTestProperty; + 3 => new Location($globalReferencesUri, new Range(new Position( 9, 5), new Position( 9, 14))), // TestClass::TEST_CLASS_CONST; + 4 => new Location($globalReferencesUri, new Range(new Position(15, 18), new Position(15, 27))), // function whatever(TestClass $param) + 5 => new Location($globalReferencesUri, new Range(new Position(15, 37), new Position(15, 46))), // function whatever(TestClass $param): TestClass + ], + 'TestInterface' => [ + 0 => new Location($globalSymbolsUri, new Range(new Position( 6, 27), new Position( 6, 40))) // class TestClass implements TestInterface + ], + 'TestClass::TEST_CLASS_CONST' => [ + 0 => new Location($globalReferencesUri, new Range(new Position( 9, 16), new Position( 9, 32))) + ], + 'TestClass::testProperty' => [ + 0 => new Location($globalReferencesUri, new Range(new Position( 6, 5), new Position( 6, 23))) + ], + 'TestClass::staticTestProperty' => [ + 0 => new Location($globalReferencesUri, new Range(new Position( 8, 5), new Position( 8, 35))) + ], + 'TestClass::staticTestMethod()' => [ + 0 => new Location($globalReferencesUri, new Range(new Position( 7, 0), new Position( 7, 29))) + ], + 'TestClass::testMethod()' => [ + 0 => new Location($globalReferencesUri, new Range(new Position( 5, 0), new Position( 5, 18))) + ], + 'test_function()' => [ + 0 => new Location($globalReferencesUri, new Range(new Position(10, 0), new Position(10, 13))) + ] + ]; + } + + protected function getDefinitionLocation(string $fqn): Location + { + return $this->definitionLocations[$fqn]; + } + + protected function getReferenceLocations(string $fqn): array + { + return $this->referenceLocations[$fqn]; + } +}