From ff66548b5d219451a0218bd45177438abaa4994b Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Tue, 11 Oct 2016 18:01:26 +0200 Subject: [PATCH] Add separate tests for global definitions --- fixtures/global_references.php | 24 ++ fixtures/global_symbols.php | 53 +++ src/PhpDocument.php | 10 +- .../TextDocument/Definition/GlobalTest.php | 317 ++++++++++++++++++ .../NamespacedTest.php} | 10 +- .../NamespacedTest.php} | 10 +- 6 files changed, 407 insertions(+), 17 deletions(-) create mode 100644 fixtures/global_references.php create mode 100644 fixtures/global_symbols.php create mode 100644 tests/Server/TextDocument/Definition/GlobalTest.php rename tests/Server/TextDocument/{DefinitionTest.php => Definition/NamespacedTest.php} (98%) rename tests/Server/TextDocument/{ReferencesTest.php => References/NamespacedTest.php} (98%) diff --git a/fixtures/global_references.php b/fixtures/global_references.php new file mode 100644 index 0000000..ae9b627 --- /dev/null +++ b/fixtures/global_references.php @@ -0,0 +1,24 @@ +testMethod(); +echo $obj->testProperty; +TestClass::staticTestMethod(); +echo TestClass::$staticTestProperty; +echo TestClass::TEST_CLASS_CONST; +test_function(); + +$var = 123; +echo $var; + +function whatever(TestClass $param): TestClass { + echo $param; +} + +$fn = function() use ($var) { + echo $var; +}; + +echo TEST_CONST; diff --git a/fixtures/global_symbols.php b/fixtures/global_symbols.php new file mode 100644 index 0000000..a8e2765 --- /dev/null +++ b/fixtures/global_symbols.php @@ -0,0 +1,53 @@ +getAttribute('parentNode'); if ($n instanceof Node\Stmt\Namespace_) { - $namespacedName = (string)$n->name . '\\' . $name; - // If the namespaced version is defined, return that - // Otherwise fall back to global - if ($this->project->isDefined($namespacedName)) { - return $namespacedName; - } + return (string)$n->name . '\\' . $name; } } } diff --git a/tests/Server/TextDocument/Definition/GlobalTest.php b/tests/Server/TextDocument/Definition/GlobalTest.php new file mode 100644 index 0000000..120ca68 --- /dev/null +++ b/tests/Server/TextDocument/Definition/GlobalTest.php @@ -0,0 +1,317 @@ +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')); + } + + 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() + { + // $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 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)); + } + + 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)); + } + + 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)); + } + + 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) { + // 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)); + } + + 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)); + } + + 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)); + } +} diff --git a/tests/Server/TextDocument/DefinitionTest.php b/tests/Server/TextDocument/Definition/NamespacedTest.php similarity index 98% rename from tests/Server/TextDocument/DefinitionTest.php rename to tests/Server/TextDocument/Definition/NamespacedTest.php index f26bb49..48cad63 100644 --- a/tests/Server/TextDocument/DefinitionTest.php +++ b/tests/Server/TextDocument/Definition/NamespacedTest.php @@ -1,14 +1,14 @@ 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')); + $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')); } public function testDefinitionFileBeginning() { diff --git a/tests/Server/TextDocument/ReferencesTest.php b/tests/Server/TextDocument/References/NamespacedTest.php similarity index 98% rename from tests/Server/TextDocument/ReferencesTest.php rename to tests/Server/TextDocument/References/NamespacedTest.php index cecc729..def9b20 100644 --- a/tests/Server/TextDocument/ReferencesTest.php +++ b/tests/Server/TextDocument/References/NamespacedTest.php @@ -1,7 +1,7 @@ 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')); + $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, file_get_contents($this->referencesUri)); $project->loadDocument($this->symbolsUri, file_get_contents($this->symbolsUri)); $project->loadDocument($this->useUri, file_get_contents($this->useUri));