2016-10-08 12:59:08 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace LanguageServer\Tests\Server\TextDocument;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-12-13 00:51:02 +00:00
|
|
|
use phpDocumentor\Reflection\DocBlockFactory;
|
2017-06-09 18:25:30 +00:00
|
|
|
use LanguageServer\{
|
|
|
|
DefinitionResolver, TreeAnalyzer
|
|
|
|
};
|
|
|
|
use LanguageServer\Index\{Index};
|
2016-10-11 12:42:56 +00:00
|
|
|
use function LanguageServer\pathToUri;
|
2017-06-09 18:25:30 +00:00
|
|
|
use Microsoft\PhpParser;
|
|
|
|
use Microsoft\PhpParser\Node;
|
2016-10-08 12:59:08 +00:00
|
|
|
|
|
|
|
class DefinitionCollectorTest extends TestCase
|
|
|
|
{
|
2016-10-09 13:48:08 +00:00
|
|
|
public function testCollectsSymbols()
|
2016-10-08 12:59:08 +00:00
|
|
|
{
|
2016-12-13 00:51:02 +00:00
|
|
|
$path = realpath(__DIR__ . '/../../fixtures/symbols.php');
|
2017-06-09 18:25:30 +00:00
|
|
|
$defNodes = $this->collectDefinitions($path);
|
2016-12-13 00:51:02 +00:00
|
|
|
|
2016-10-08 12:59:08 +00:00
|
|
|
$this->assertEquals([
|
2016-11-30 21:23:51 +00:00
|
|
|
'TestNamespace',
|
2016-10-08 12:59:08 +00:00
|
|
|
'TestNamespace\\TEST_CONST',
|
|
|
|
'TestNamespace\\TestClass',
|
|
|
|
'TestNamespace\\TestClass::TEST_CLASS_CONST',
|
2016-11-30 21:23:51 +00:00
|
|
|
'TestNamespace\\TestClass::$staticTestProperty',
|
|
|
|
'TestNamespace\\TestClass->testProperty',
|
2016-10-08 12:59:08 +00:00
|
|
|
'TestNamespace\\TestClass::staticTestMethod()',
|
2016-11-30 21:23:51 +00:00
|
|
|
'TestNamespace\\TestClass->testMethod()',
|
2016-10-08 12:59:08 +00:00
|
|
|
'TestNamespace\\TestTrait',
|
|
|
|
'TestNamespace\\TestInterface',
|
2016-12-16 00:40:17 +00:00
|
|
|
'TestNamespace\\test_function()',
|
2017-04-09 17:44:28 +00:00
|
|
|
'TestNamespace\\ChildClass',
|
|
|
|
'TestNamespace\\Example',
|
|
|
|
'TestNamespace\\Example->__construct()',
|
2018-11-11 02:47:57 +00:00
|
|
|
'TestNamespace\\Example->__destruct()',
|
|
|
|
'TestNamespace\\InnerNamespace',
|
|
|
|
'TestNamespace\\InnerNamespace\\InnerClass',
|
2016-11-18 14:22:24 +00:00
|
|
|
], array_keys($defNodes));
|
2017-06-09 18:25:30 +00:00
|
|
|
|
|
|
|
$this->assertInstanceOf(Node\ConstElement::class, $defNodes['TestNamespace\\TEST_CONST']);
|
|
|
|
$this->assertInstanceOf(Node\Statement\ClassDeclaration::class, $defNodes['TestNamespace\\TestClass']);
|
|
|
|
$this->assertInstanceOf(Node\ConstElement::class, $defNodes['TestNamespace\\TestClass::TEST_CLASS_CONST']);
|
|
|
|
// TODO - should we parse properties more strictly?
|
|
|
|
$this->assertInstanceOf(Node\Expression\Variable::class, $defNodes['TestNamespace\\TestClass::$staticTestProperty']);
|
|
|
|
$this->assertInstanceOf(Node\Expression\Variable::class, $defNodes['TestNamespace\\TestClass->testProperty']);
|
|
|
|
$this->assertInstanceOf(Node\MethodDeclaration::class, $defNodes['TestNamespace\\TestClass::staticTestMethod()']);
|
|
|
|
$this->assertInstanceOf(Node\MethodDeclaration::class, $defNodes['TestNamespace\\TestClass->testMethod()']);
|
|
|
|
$this->assertInstanceOf(Node\Statement\TraitDeclaration::class, $defNodes['TestNamespace\\TestTrait']);
|
|
|
|
$this->assertInstanceOf(Node\Statement\InterfaceDeclaration::class, $defNodes['TestNamespace\\TestInterface']);
|
|
|
|
$this->assertInstanceOf(Node\Statement\FunctionDeclaration::class, $defNodes['TestNamespace\\test_function()']);
|
|
|
|
$this->assertInstanceOf(Node\Statement\ClassDeclaration::class, $defNodes['TestNamespace\\ChildClass']);
|
|
|
|
$this->assertInstanceOf(Node\Statement\ClassDeclaration::class, $defNodes['TestNamespace\\Example']);
|
|
|
|
$this->assertInstanceOf(Node\MethodDeclaration::class, $defNodes['TestNamespace\\Example->__construct()']);
|
|
|
|
$this->assertInstanceOf(Node\MethodDeclaration::class, $defNodes['TestNamespace\\Example->__destruct()']);
|
2018-11-11 02:47:57 +00:00
|
|
|
$this->assertInstanceOf(Node\Statement\ClassDeclaration::class, $defNodes['TestNamespace\\InnerNamespace\\InnerClass']);
|
2016-10-08 12:59:08 +00:00
|
|
|
}
|
2016-10-09 13:48:08 +00:00
|
|
|
|
|
|
|
public function testDoesNotCollectReferences()
|
|
|
|
{
|
2016-12-13 00:51:02 +00:00
|
|
|
$path = realpath(__DIR__ . '/../../fixtures/references.php');
|
2017-06-09 18:25:30 +00:00
|
|
|
$defNodes = $this->collectDefinitions($path);
|
|
|
|
|
|
|
|
$this->assertEquals(['TestNamespace', 'TestNamespace\\whatever()'], array_keys($defNodes));
|
|
|
|
$this->assertInstanceOf(Node\Statement\NamespaceDefinition::class, $defNodes['TestNamespace']);
|
|
|
|
$this->assertInstanceOf(Node\Statement\FunctionDeclaration::class, $defNodes['TestNamespace\\whatever()']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $path
|
|
|
|
*/
|
|
|
|
private function collectDefinitions(string $path): array
|
|
|
|
{
|
2016-12-13 00:51:02 +00:00
|
|
|
$uri = pathToUri($path);
|
2017-06-09 18:25:30 +00:00
|
|
|
$parser = new PhpParser\Parser();
|
|
|
|
|
2016-12-13 00:51:02 +00:00
|
|
|
$docBlockFactory = DocBlockFactory::createInstance();
|
|
|
|
$index = new Index;
|
|
|
|
$definitionResolver = new DefinitionResolver($index);
|
|
|
|
$content = file_get_contents($path);
|
|
|
|
|
2017-06-09 18:25:30 +00:00
|
|
|
$treeAnalyzer = new TreeAnalyzer($parser, $content, $docBlockFactory, $definitionResolver, $uri);
|
|
|
|
return $treeAnalyzer->getDefinitionNodes();
|
2016-10-09 13:48:08 +00:00
|
|
|
}
|
2016-10-08 12:59:08 +00:00
|
|
|
}
|