1
0
Fork 0

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)
pull/682/head
Gabriel Noé González 2018-11-10 23:57:14 +01:00
parent e71e4e0ff0
commit 73ab1520a3
6 changed files with 220 additions and 23 deletions

View File

@ -5,4 +5,5 @@
"**/tests/Validation/cases": true
},
"files.trimTrailingWhitespace": true,
"files.eol": "\n",
}

View File

@ -0,0 +1,9 @@
<?php
class ThisChildClass extends TestClass {
public function canSeeMethod()
{
$this->
}
}

View File

@ -1,15 +0,0 @@
<?php
class Foo
{
public function canAccess()
{
}
private function cantAccess()
{
}
}

View File

@ -41,6 +41,20 @@ class TestClass implements TestInterface
*/
public $testProperty;
/**
* Reprehenderit magna velit mollit ipsum do.
*
* @var TestClass
*/
private $privateProperty;
/**
* Reprehenderit magna velit mollit ipsum do.
*
* @var TestClass
*/
protected $protectedProperty;
/**
* Do magna consequat veniam minim proident eiusmod incididunt aute proident.
*/
@ -59,6 +73,16 @@ class TestClass implements TestInterface
{
$this->testProperty = $testParameter;
}
private function privateTestMethod()
{
return $this->privateProperty;
}
protected function protectedTestMethod()
{
return $this->protectedProperty;
}
}
trait TestTrait

View File

@ -0,0 +1,178 @@
<?php
declare(strict_types = 1);
namespace LanguageServer\Tests\Server\TextDocument;
use LanguageServer\ContentRetriever\FileSystemContentRetriever;
use LanguageServer\DefinitionResolver;
use LanguageServer\Index\DependenciesIndex;
use LanguageServer\Index\Index;
use LanguageServer\Index\ProjectIndex;
use LanguageServer\LanguageClient;
use LanguageServer\PhpDocumentLoader;
use LanguageServer\Server\TextDocument;
use LanguageServer\Tests\MockProtocolStream;
use LanguageServerProtocol\CompletionItem;
use LanguageServerProtocol\CompletionItemKind;
use LanguageServerProtocol\CompletionList;
use LanguageServerProtocol\Position;
use LanguageServerProtocol\TextDocumentIdentifier;
use PHPUnit\Framework\TestCase;
use function file_get_contents;
use function LanguageServer\pathToUri;
/**
* Description of CompletionWithVisibilityTest
*
* @author Gabriel Noé <jnoe@itnow.externos.es>
*/
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);
}
}

View File

@ -1,5 +1,4 @@
<?php
declare(strict_types = 1);
namespace LanguageServer\Tests;
@ -22,6 +21,7 @@ use Microsoft\PhpParser;
class ValidationTest extends TestCase
{
public function validationTestProvider()
{
$testProviderArray = array();
@ -31,7 +31,7 @@ class ValidationTest extends TestCase
$disabled = json_decode(file_get_contents(__DIR__ . '/disabled.json'));
foreach (new RecursiveIteratorIterator($iterator) as $file) {
if (strpos(\strrev((string)$file), \strrev(".php")) === 0 && !\in_array(basename((string)$file), $disabled)) {
if (strpos(\strrev((string) $file), \strrev(".php")) === 0 && !\in_array(basename((string) $file), $disabled)) {
if ($file->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;