1
0
Fork 0
php-language-server/tests/NodeVisitor/DefinitionCollectorTest.php

98 lines
4.4 KiB
PHP
Raw Normal View History

2016-10-08 12:59:08 +00:00
<?php
2018-11-27 16:51:40 +00:00
declare(strict_types=1);
2016-10-08 12:59:08 +00:00
namespace LanguageServer\Tests\Server\TextDocument;
use PHPUnit\Framework\TestCase;
2016-12-13 00:51:02 +00:00
use phpDocumentor\Reflection\DocBlockFactory;
2018-11-27 16:51:40 +00:00
use LanguageServer\{DefinitionResolver, TreeAnalyzer};
use LanguageServer\Index\{Index};
use function LanguageServer\pathToUri;
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');
$defNodes = $this->collectDefinitions($path);
2016-12-13 00:51:02 +00:00
2018-11-27 16:51:40 +00:00
$this->assertEquals(
[
'TestNamespace',
'TestNamespace\\TEST_CONST',
'TestNamespace\\TestClass',
'TestNamespace\\TestClass::TEST_CLASS_CONST',
'TestNamespace\\TestClass::$staticTestProperty',
'TestNamespace\\TestClass->testProperty',
'TestNamespace\\TestClass::staticTestMethod()',
'TestNamespace\\TestClass->testMethod()',
'TestNamespace\\TestTrait',
'TestNamespace\\TestInterface',
'TestNamespace\\test_function()',
'TestNamespace\\ChildClass',
'TestNamespace\\Example',
'TestNamespace\\Example->__construct()',
'TestNamespace\\Example->__destruct()',
'TestNamespace\\InnerNamespace',
'TestNamespace\\InnerNamespace\\InnerClass'
],
array_keys($defNodes)
);
$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?
2018-11-27 16:51:40 +00:00
$this->assertInstanceOf(
Node\Expression\Variable::class,
$defNodes['TestNamespace\\TestClass::$staticTestProperty']
);
$this->assertInstanceOf(Node\Expression\Variable::class, $defNodes['TestNamespace\\TestClass->testProperty']);
2018-11-27 16:51:40 +00:00
$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-27 16:51:40 +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');
$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);
$parser = new PhpParser\Parser();
2016-12-13 00:51:02 +00:00
$docBlockFactory = DocBlockFactory::createInstance();
2018-11-27 16:51:40 +00:00
$index = new Index();
2016-12-13 00:51:02 +00:00
$definitionResolver = new DefinitionResolver($index);
$content = file_get_contents($path);
$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
}