From 73ab1520a37a92f789142c55f1cd0b6d8d1c692e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20No=C3=A9=20Gonz=C3=A1lez?= Date: Sat, 10 Nov 2018 23:57:14 +0100 Subject: [PATCH] Visibility Test and Changing line ending from CRLF -> LF Added CompletionWithVisibilityTest.php - checks visibility of a property or method call (only public) - checks visibility of a Class (can see all methods and properties) - checks visibility of a child class (public and protected) --- .vscode/settings.json | 1 + .../completion/child_class_visibility.php | 9 + fixtures/completion/public_only.php | 15 -- fixtures/global_symbols.php | 26 ++- .../CompletionWithVisibilityTest.php | 178 ++++++++++++++++++ tests/Validation/ValidationTest.php | 14 +- 6 files changed, 220 insertions(+), 23 deletions(-) create mode 100644 fixtures/completion/child_class_visibility.php delete mode 100644 fixtures/completion/public_only.php create mode 100644 tests/Server/TextDocument/CompletionWithVisibilityTest.php diff --git a/.vscode/settings.json b/.vscode/settings.json index 63af6ee..4897ad8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,4 +5,5 @@ "**/tests/Validation/cases": true }, "files.trimTrailingWhitespace": true, + "files.eol": "\n", } diff --git a/fixtures/completion/child_class_visibility.php b/fixtures/completion/child_class_visibility.php new file mode 100644 index 0000000..44a177d --- /dev/null +++ b/fixtures/completion/child_class_visibility.php @@ -0,0 +1,9 @@ + + } +} \ No newline at end of file diff --git a/fixtures/completion/public_only.php b/fixtures/completion/public_only.php deleted file mode 100644 index aadb96c..0000000 --- a/fixtures/completion/public_only.php +++ /dev/null @@ -1,15 +0,0 @@ -testProperty = $testParameter; } + + private function privateTestMethod() + { + return $this->privateProperty; + } + + protected function protectedTestMethod() + { + return $this->protectedProperty; + } } trait TestTrait @@ -114,6 +138,6 @@ class UnusedClass public $unusedProperty; public function unusedMethod() - { + { } } diff --git a/tests/Server/TextDocument/CompletionWithVisibilityTest.php b/tests/Server/TextDocument/CompletionWithVisibilityTest.php new file mode 100644 index 0000000..63151d6 --- /dev/null +++ b/tests/Server/TextDocument/CompletionWithVisibilityTest.php @@ -0,0 +1,178 @@ + + */ +class CompletionWithVisibilityTest extends TestCase +{ + + /** + * @var TextDocument + */ + private $textDocument; + + /** + * @var PhpDocumentLoader + */ + private $loader; + + /** + * + * @var string + */ + private $fixturesPath; + + public function __construct($name = null, array $data = array(), $dataName = '') + { + parent::__construct($name, $data, $dataName); + $this->fixturesPath = __DIR__ . '/../../../fixtures'; + } + + public function setUp() + { + $client = new LanguageClient(new MockProtocolStream, new MockProtocolStream); + $projectIndex = new ProjectIndex(new Index, new DependenciesIndex); + $definitionResolver = new DefinitionResolver($projectIndex); + $contentRetriever = new FileSystemContentRetriever; + $this->loader = new PhpDocumentLoader($contentRetriever, $projectIndex, $definitionResolver); + $this->loader->load(pathToUri($this->fixturesPath . '/global_symbols.php'))->wait(); + $this->loader->load(pathToUri($this->fixturesPath . '/symbols.php'))->wait(); + $this->textDocument = new TextDocument($this->loader, $definitionResolver, $client, $projectIndex); + } + + /** + * Can access only to public properties and methods + */ + public function testVisibilityFromCall() + { + $items = $this->getCompletion('/completion/property.php', 3, 6); + // doesn't contain any of these properties and methods + $this->assertCompletionsListSubsetNotContains(new CompletionList([ + new CompletionItem( + 'privateProperty', CompletionItemKind::PROPERTY, '\TestClass', + 'Reprehenderit magna velit mollit ipsum do.' + ), + new CompletionItem( + 'privateTestMethod', CompletionItemKind::METHOD, 'mixed' // Return type of the method + ), + new CompletionItem( + 'protectedProperty', CompletionItemKind::PROPERTY, '\TestClass', + 'Reprehenderit magna velit mollit ipsum do.' + ), + new CompletionItem( + 'protectedTestMethod', CompletionItemKind::METHOD, 'mixed' // Return type of the method + ) + ], true), $items); + } + + /** + * From a Child class only public and protected properties and methods are + * visible + * + */ + public function testVisibilityInsideADescendantClassMethod() + { + $items = $this->getCompletion('/completion/child_class_visibility.php', 6, 16); + // doesn't contain any of these properties and methods + $this->assertCompletionsListSubsetNotContains(new CompletionList([ + new CompletionItem( + 'privateProperty', CompletionItemKind::PROPERTY, '\TestClass', + 'Reprehenderit magna velit mollit ipsum do.' + ), + new CompletionItem( + 'privateTestMethod', CompletionItemKind::METHOD, 'mixed' // Return type of the method + ) + ], true), $items); + } + + public function testVisibilityInsideClassMethod() + { + $items = $this->getCompletion('/global_symbols.php', 73, 15); + // can see all properties and methods + $this->assertCompletionsListSubset(new CompletionList([ + new CompletionItem( + 'privateProperty', CompletionItemKind::PROPERTY, '\TestClass', + 'Reprehenderit magna velit mollit ipsum do.' + ), + new CompletionItem( + 'privateTestMethod', CompletionItemKind::METHOD, 'mixed' // Return type of the method + ), + new CompletionItem( + 'protectedProperty', CompletionItemKind::PROPERTY, '\TestClass', + 'Reprehenderit magna velit mollit ipsum do.' + ), + new CompletionItem( + 'protectedTestMethod', CompletionItemKind::METHOD, 'mixed' // Return type of the method + ), + new CompletionItem( + 'testProperty', + CompletionItemKind::PROPERTY, + '\TestClass', // Type of the property + 'Reprehenderit magna velit mollit ipsum do.' + ), + new CompletionItem( + 'testMethod', + CompletionItemKind::METHOD, + '\TestClass', // Return type of the method + 'Non culpa nostrud mollit esse sunt laboris in irure ullamco cupidatat amet.' + ) + ], true), $items); + + } + + /** + * + * @param string $fixtureFile + * @param int $line + * @param int $char + * @return CompletionList + */ + private function getCompletion(string $fixtureFile, int $line, int $char) + { + $completionUri = pathToUri($this->fixturesPath . $fixtureFile); + $this->loader->open($completionUri, file_get_contents($completionUri)); + return $this->textDocument->completion( + new TextDocumentIdentifier($completionUri), new Position($line, $char) + )->wait(); + } + + private function assertCompletionsListSubset(CompletionList $subsetList, CompletionList $list) + { + foreach ($subsetList->items as $expectedItem) { + $this->assertContains($expectedItem, $list->items, null, null, false); + } + + $this->assertEquals($subsetList->isIncomplete, $list->isIncomplete); + } + + private function assertCompletionsListSubsetNotContains(CompletionList $subsetList, CompletionList $list) + { + foreach ($subsetList->items as $expectedItem) { + $this->assertNotContains($expectedItem, $list->items, null, null, false); + } + $this->assertEquals($subsetList->isIncomplete, $list->isIncomplete); + } +} diff --git a/tests/Validation/ValidationTest.php b/tests/Validation/ValidationTest.php index 2c3efaf..62b27eb 100644 --- a/tests/Validation/ValidationTest.php +++ b/tests/Validation/ValidationTest.php @@ -1,5 +1,4 @@ getSize() < 100000) { $testProviderArray[] = [$file->getPathname()]; } @@ -57,17 +57,17 @@ class ValidationTest extends TestCase $outputFile = getExpectedValuesFile($testCaseFile); if (!file_exists($outputFile)) { - file_put_contents($outputFile, json_encode($actualValues, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)); + file_put_contents($outputFile, json_encode($actualValues, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); } - $expectedValues = (array)json_decode(file_get_contents($outputFile)); + $expectedValues = (array) json_decode(file_get_contents($outputFile)); try { $this->assertEquals($expectedValues['definitions'], $actualValues['definitions']); - $this->assertEquals((array)$expectedValues['references'], (array)$actualValues['references'], sprintf('references match in "%s"', $outputFile)); + $this->assertEquals((array) $expectedValues['references'], (array) $actualValues['references'], sprintf('references match in "%s"', $outputFile)); } catch (\Throwable $e) { $outputFile = getExpectedValuesFile($testCaseFile); - file_put_contents($outputFile, json_encode($actualValues, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)); + file_put_contents($outputFile, json_encode($actualValues, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); throw $e; } @@ -135,7 +135,7 @@ class ValidationTest extends TestCase } elseif ($propertyName === 'extends') { $definition->$propertyName = $definition->$propertyName ?? []; } elseif ($propertyName === 'type' && $definition->type !== null) { - $defsForAssert[$fqn]['type__tostring'] = (string)$definition->type; + $defsForAssert[$fqn]['type__tostring'] = (string) $definition->type; } $defsForAssert[$fqn][$propertyName] = $definition->$propertyName;