2016-10-18 08:48:16 +00:00
|
|
|
|
<?php
|
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
2016-10-18 21:09:51 +00:00
|
|
|
|
namespace LanguageServer\Tests\Server;
|
2016-10-18 08:48:16 +00:00
|
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
use LanguageServer\Tests\MockProtocolStream;
|
2016-12-13 00:51:02 +00:00
|
|
|
|
use LanguageServer\{Server, LanguageClient, PhpDocumentLoader, DefinitionResolver};
|
|
|
|
|
use LanguageServer\Index\{ProjectIndex, StubsIndex, GlobalIndex, DependenciesIndex, Index};
|
2016-12-08 01:33:48 +00:00
|
|
|
|
use LanguageServer\ContentRetriever\FileSystemContentRetriever;
|
2016-11-14 09:25:44 +00:00
|
|
|
|
use LanguageServer\Protocol\{Position, Location, Range, ClientCapabilities};
|
2016-10-18 08:48:16 +00:00
|
|
|
|
use function LanguageServer\pathToUri;
|
2016-11-14 09:25:44 +00:00
|
|
|
|
use Sabre\Event\Promise;
|
2016-10-18 08:48:16 +00:00
|
|
|
|
|
2016-10-18 21:09:51 +00:00
|
|
|
|
abstract class ServerTestCase extends TestCase
|
2016-10-18 08:48:16 +00:00
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @var Server\TextDocument
|
|
|
|
|
*/
|
|
|
|
|
protected $textDocument;
|
|
|
|
|
|
2016-10-18 21:09:51 +00:00
|
|
|
|
/**
|
|
|
|
|
* @var Server\Workspace
|
|
|
|
|
*/
|
|
|
|
|
protected $workspace;
|
|
|
|
|
|
2016-10-18 08:48:16 +00:00
|
|
|
|
/**
|
2016-12-13 00:51:02 +00:00
|
|
|
|
* @var PhpDocumentLoader
|
2016-10-18 08:48:16 +00:00
|
|
|
|
*/
|
2016-12-13 00:51:02 +00:00
|
|
|
|
protected $documentLoader;
|
2016-10-18 08:48:16 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Map from FQN to Location of definition
|
|
|
|
|
*
|
|
|
|
|
* @var Location[]
|
|
|
|
|
*/
|
|
|
|
|
private $definitionLocations;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Map from FQN to array of reference Locations
|
|
|
|
|
*
|
|
|
|
|
* @var Location[][]
|
|
|
|
|
*/
|
|
|
|
|
private $referenceLocations;
|
|
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
|
{
|
2017-01-11 01:08:52 +00:00
|
|
|
|
$sourceIndex = new Index;
|
|
|
|
|
$dependenciesIndex = new DependenciesIndex;
|
|
|
|
|
$projectIndex = new ProjectIndex($sourceIndex, $dependenciesIndex);
|
2016-12-13 00:51:02 +00:00
|
|
|
|
|
|
|
|
|
$definitionResolver = new DefinitionResolver($projectIndex);
|
|
|
|
|
$client = new LanguageClient(new MockProtocolStream, new MockProtocolStream);
|
|
|
|
|
$this->documentLoader = new PhpDocumentLoader(new FileSystemContentRetriever, $projectIndex, $definitionResolver);
|
|
|
|
|
$this->textDocument = new Server\TextDocument($this->documentLoader, $definitionResolver, $client, $projectIndex);
|
2017-01-11 01:08:52 +00:00
|
|
|
|
$this->workspace = new Server\Workspace($projectIndex, $dependenciesIndex, $sourceIndex, null, $this->documentLoader);
|
2016-10-18 08:48:16 +00:00
|
|
|
|
|
2016-10-18 21:09:51 +00:00
|
|
|
|
$globalSymbolsUri = pathToUri(realpath(__DIR__ . '/../../fixtures/global_symbols.php'));
|
|
|
|
|
$globalReferencesUri = pathToUri(realpath(__DIR__ . '/../../fixtures/global_references.php'));
|
|
|
|
|
$symbolsUri = pathToUri(realpath(__DIR__ . '/../../fixtures/symbols.php'));
|
|
|
|
|
$referencesUri = pathToUri(realpath(__DIR__ . '/../../fixtures/references.php'));
|
|
|
|
|
$useUri = pathToUri(realpath(__DIR__ . '/../../fixtures/use.php'));
|
2016-10-18 08:48:16 +00:00
|
|
|
|
|
2016-12-13 00:51:02 +00:00
|
|
|
|
$this->documentLoader->load($symbolsUri)->wait();
|
|
|
|
|
$this->documentLoader->load($referencesUri)->wait();
|
|
|
|
|
$this->documentLoader->load($globalSymbolsUri)->wait();
|
|
|
|
|
$this->documentLoader->load($globalReferencesUri)->wait();
|
|
|
|
|
$this->documentLoader->load($useUri)->wait();
|
2016-10-18 08:48:16 +00:00
|
|
|
|
|
2016-10-24 17:35:37 +00:00
|
|
|
|
// @codingStandardsIgnoreStart
|
2016-10-18 08:48:16 +00:00
|
|
|
|
$this->definitionLocations = [
|
|
|
|
|
|
|
|
|
|
// Global
|
2016-10-19 10:31:32 +00:00
|
|
|
|
'TEST_CONST' => new Location($globalSymbolsUri, new Range(new Position( 9, 6), new Position( 9, 22))),
|
|
|
|
|
'TestClass' => new Location($globalSymbolsUri, new Range(new Position(20, 0), new Position(61, 1))),
|
2016-12-16 00:40:17 +00:00
|
|
|
|
'ChildClass' => new Location($globalSymbolsUri, new Range(new Position(99, 0), new Position(99, 37))),
|
2016-10-19 10:31:32 +00:00
|
|
|
|
'TestTrait' => new Location($globalSymbolsUri, new Range(new Position(63, 0), new Position(66, 1))),
|
|
|
|
|
'TestInterface' => new Location($globalSymbolsUri, new Range(new Position(68, 0), new Position(71, 1))),
|
|
|
|
|
'TestClass::TEST_CLASS_CONST' => new Location($globalSymbolsUri, new Range(new Position(27, 10), new Position(27, 32))),
|
|
|
|
|
'TestClass::testProperty' => new Location($globalSymbolsUri, new Range(new Position(41, 11), new Position(41, 24))),
|
|
|
|
|
'TestClass::staticTestProperty' => new Location($globalSymbolsUri, new Range(new Position(34, 18), new Position(34, 37))),
|
|
|
|
|
'TestClass::staticTestMethod()' => new Location($globalSymbolsUri, new Range(new Position(46, 4), new Position(49, 5))),
|
|
|
|
|
'TestClass::testMethod()' => new Location($globalSymbolsUri, new Range(new Position(57, 4), new Position(60, 5))),
|
|
|
|
|
'test_function()' => new Location($globalSymbolsUri, new Range(new Position(78, 0), new Position(81, 1))),
|
|
|
|
|
'whatever()' => new Location($globalReferencesUri, new Range(new Position(21, 0), new Position(23, 1))),
|
2016-10-18 08:48:16 +00:00
|
|
|
|
|
|
|
|
|
// Namespaced
|
2016-12-17 02:46:08 +00:00
|
|
|
|
'TestNamespace' => new Location($symbolsUri, new Range(new Position( 2, 10), new Position( 2, 23))),
|
|
|
|
|
'SecondTestNamespace' => new Location($useUri, new Range(new Position( 2, 10), new Position( 2, 29))),
|
2016-10-19 10:31:32 +00:00
|
|
|
|
'TestNamespace\\TEST_CONST' => new Location($symbolsUri, new Range(new Position( 9, 6), new Position( 9, 22))),
|
|
|
|
|
'TestNamespace\\TestClass' => new Location($symbolsUri, new Range(new Position(20, 0), new Position(61, 1))),
|
2016-12-16 00:40:17 +00:00
|
|
|
|
'TestNamespace\\ChildClass' => new Location($symbolsUri, new Range(new Position(99, 0), new Position(99, 37))),
|
2016-10-19 10:31:32 +00:00
|
|
|
|
'TestNamespace\\TestTrait' => new Location($symbolsUri, new Range(new Position(63, 0), new Position(66, 1))),
|
|
|
|
|
'TestNamespace\\TestInterface' => new Location($symbolsUri, new Range(new Position(68, 0), new Position(71, 1))),
|
|
|
|
|
'TestNamespace\\TestClass::TEST_CLASS_CONST' => new Location($symbolsUri, new Range(new Position(27, 10), new Position(27, 32))),
|
|
|
|
|
'TestNamespace\\TestClass::testProperty' => new Location($symbolsUri, new Range(new Position(41, 11), new Position(41, 24))),
|
|
|
|
|
'TestNamespace\\TestClass::staticTestProperty' => new Location($symbolsUri, new Range(new Position(34, 18), new Position(34, 37))),
|
|
|
|
|
'TestNamespace\\TestClass::staticTestMethod()' => new Location($symbolsUri, new Range(new Position(46, 4), new Position(49, 5))),
|
|
|
|
|
'TestNamespace\\TestClass::testMethod()' => new Location($symbolsUri, new Range(new Position(57, 4), new Position(60, 5))),
|
|
|
|
|
'TestNamespace\\test_function()' => new Location($symbolsUri, new Range(new Position(78, 0), new Position(81, 1))),
|
|
|
|
|
'TestNamespace\\whatever()' => new Location($referencesUri, new Range(new Position(21, 0), new Position(23, 1)))
|
2016-10-18 08:48:16 +00:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$this->referenceLocations = [
|
|
|
|
|
|
|
|
|
|
// Namespaced
|
2016-12-17 02:46:08 +00:00
|
|
|
|
'TestNamespace' => [
|
|
|
|
|
0 => new Location($referencesUri, new Range(new Position(31, 13), new Position(31, 40))), // use function TestNamespace\test_function;
|
|
|
|
|
1 => new Location($useUri, new Range(new Position( 4, 4), new Position( 4, 27))), // use TestNamespace\TestClass;
|
|
|
|
|
2 => new Location($useUri, new Range(new Position( 5, 4), new Position( 5, 17))) // use TestNamespace\{TestTrait, TestInterface};
|
|
|
|
|
],
|
2016-10-18 08:48:16 +00:00
|
|
|
|
'TestNamespace\\TEST_CONST' => [
|
2016-10-19 10:31:32 +00:00
|
|
|
|
0 => new Location($referencesUri, new Range(new Position(29, 5), new Position(29, 15)))
|
2016-10-18 08:48:16 +00:00
|
|
|
|
],
|
|
|
|
|
'TestNamespace\\TestClass' => [
|
2016-12-16 00:40:17 +00:00
|
|
|
|
0 => new Location($symbolsUri , new Range(new Position(99, 25), new Position(99, 34))), // class ChildClass extends TestClass {}
|
|
|
|
|
1 => new Location($referencesUri, new Range(new Position( 4, 11), new Position( 4, 20))), // $obj = new TestClass();
|
|
|
|
|
2 => new Location($referencesUri, new Range(new Position( 7, 0), new Position( 7, 9))), // TestClass::staticTestMethod();
|
|
|
|
|
3 => new Location($referencesUri, new Range(new Position( 8, 5), new Position( 8, 14))), // echo TestClass::$staticTestProperty;
|
|
|
|
|
4 => new Location($referencesUri, new Range(new Position( 9, 5), new Position( 9, 14))), // TestClass::TEST_CLASS_CONST;
|
|
|
|
|
5 => new Location($referencesUri, new Range(new Position(21, 18), new Position(21, 27))), // function whatever(TestClass $param)
|
|
|
|
|
6 => new Location($referencesUri, new Range(new Position(21, 37), new Position(21, 46))), // function whatever(TestClass $param): TestClass
|
|
|
|
|
7 => new Location($referencesUri, new Range(new Position(39, 0), new Position(39, 9))), // TestClass::$staticTestProperty[123]->testProperty;
|
|
|
|
|
8 => new Location($useUri, new Range(new Position( 4, 4), new Position( 4, 27))), // use TestNamespace\TestClass;
|
|
|
|
|
],
|
|
|
|
|
'TestNamespace\\TestChild' => [
|
|
|
|
|
0 => new Location($referencesUri, new Range(new Position(42, 5), new Position(42, 25))), // echo $child->testProperty;
|
2016-10-18 08:48:16 +00:00
|
|
|
|
],
|
|
|
|
|
'TestNamespace\\TestInterface' => [
|
2016-10-21 14:51:11 +00:00
|
|
|
|
0 => new Location($symbolsUri, new Range(new Position(20, 27), new Position(20, 40))), // class TestClass implements TestInterface
|
2016-10-26 18:56:02 +00:00
|
|
|
|
1 => new Location($symbolsUri, new Range(new Position(57, 48), new Position(57, 61))), // public function testMethod($testParameter): TestInterface
|
|
|
|
|
2 => new Location($referencesUri, new Range(new Position(33, 20), new Position(33, 33))) // if ($abc instanceof TestInterface)
|
2016-10-18 08:48:16 +00:00
|
|
|
|
],
|
|
|
|
|
'TestNamespace\\TestClass::TEST_CLASS_CONST' => [
|
2016-10-19 22:18:36 +00:00
|
|
|
|
0 => new Location($symbolsUri, new Range(new Position(48, 13), new Position(48, 35))), // echo self::TEST_CLASS_CONSTANT
|
|
|
|
|
1 => new Location($referencesUri, new Range(new Position( 9, 5), new Position( 9, 32)))
|
2016-10-18 08:48:16 +00:00
|
|
|
|
],
|
|
|
|
|
'TestNamespace\\TestClass::testProperty' => [
|
2016-10-19 22:10:47 +00:00
|
|
|
|
0 => new Location($symbolsUri, new Range(new Position(59, 8), new Position(59, 27))), // $this->testProperty = $testParameter;
|
2016-11-18 14:22:24 +00:00
|
|
|
|
1 => new Location($referencesUri, new Range(new Position( 6, 5), new Position( 6, 23))), // echo $obj->testProperty;
|
|
|
|
|
2 => new Location($referencesUri, new Range(new Position(38, 0), new Position(38, 18))), // $obj->testProperty->testMethod();
|
|
|
|
|
3 => new Location($referencesUri, new Range(new Position(39, 0), new Position(39, 49))) // TestClass::$staticTestProperty[123]->testProperty;
|
2016-10-18 08:48:16 +00:00
|
|
|
|
],
|
|
|
|
|
'TestNamespace\\TestClass::staticTestProperty' => [
|
2016-11-18 14:22:24 +00:00
|
|
|
|
0 => new Location($referencesUri, new Range(new Position( 8, 5), new Position( 8, 35))), // echo TestClass::$staticTestProperty;
|
|
|
|
|
1 => new Location($referencesUri, new Range(new Position(39, 0), new Position(39, 30))) // TestClass::$staticTestProperty[123]->testProperty;
|
2016-10-18 08:48:16 +00:00
|
|
|
|
],
|
|
|
|
|
'TestNamespace\\TestClass::staticTestMethod()' => [
|
|
|
|
|
0 => new Location($referencesUri, new Range(new Position( 7, 0), new Position( 7, 29)))
|
|
|
|
|
],
|
|
|
|
|
'TestNamespace\\TestClass::testMethod()' => [
|
2016-11-18 14:22:24 +00:00
|
|
|
|
0 => new Location($referencesUri, new Range(new Position( 5, 0), new Position( 5, 18))), // $obj->testMethod();
|
2016-12-16 00:40:17 +00:00
|
|
|
|
1 => new Location($referencesUri, new Range(new Position(38, 0), new Position(38, 32))), // $obj->testProperty->testMethod();
|
|
|
|
|
2 => new Location($referencesUri, new Range(new Position(42, 5), new Position(42, 25))) // $child->testMethod();
|
2016-10-18 08:48:16 +00:00
|
|
|
|
],
|
|
|
|
|
'TestNamespace\\test_function()' => [
|
2016-10-26 09:47:02 +00:00
|
|
|
|
0 => new Location($referencesUri, new Range(new Position(10, 0), new Position(10, 13))),
|
|
|
|
|
1 => new Location($referencesUri, new Range(new Position(31, 13), new Position(31, 40)))
|
2016-10-18 08:48:16 +00:00
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
// Global
|
|
|
|
|
'TEST_CONST' => [
|
2016-10-19 10:31:32 +00:00
|
|
|
|
0 => new Location($referencesUri, new Range(new Position(29, 5), new Position(29, 15))),
|
|
|
|
|
1 => new Location($globalReferencesUri, new Range(new Position(29, 5), new Position(29, 15)))
|
2016-10-18 08:48:16 +00:00
|
|
|
|
],
|
|
|
|
|
'TestClass' => [
|
2016-12-16 00:40:17 +00:00
|
|
|
|
0 => new Location($globalSymbolsUri, new Range(new Position(99, 25), new Position(99, 34))), // class ChildClass extends TestClass {}
|
|
|
|
|
1 => new Location($globalReferencesUri, new Range(new Position( 4, 11), new Position( 4, 20))), // $obj = new TestClass();
|
|
|
|
|
2 => new Location($globalReferencesUri, new Range(new Position( 7, 0), new Position( 7, 9))), // TestClass::staticTestMethod();
|
|
|
|
|
3 => new Location($globalReferencesUri, new Range(new Position( 8, 5), new Position( 8, 14))), // echo TestClass::$staticTestProperty;
|
|
|
|
|
4 => new Location($globalReferencesUri, new Range(new Position( 9, 5), new Position( 9, 14))), // TestClass::TEST_CLASS_CONST;
|
|
|
|
|
5 => new Location($globalReferencesUri, new Range(new Position(21, 18), new Position(21, 27))), // function whatever(TestClass $param)
|
|
|
|
|
6 => new Location($globalReferencesUri, new Range(new Position(21, 37), new Position(21, 46))), // function whatever(TestClass $param): TestClass
|
|
|
|
|
7 => new Location($globalReferencesUri, new Range(new Position(39, 0), new Position(39, 9))), // TestClass::$staticTestProperty[123]->testProperty;
|
|
|
|
|
],
|
|
|
|
|
'TestChild' => [
|
|
|
|
|
0 => new Location($globalReferencesUri, new Range(new Position(42, 5), new Position(42, 25))), // echo $child->testProperty;
|
2016-10-18 08:48:16 +00:00
|
|
|
|
],
|
|
|
|
|
'TestInterface' => [
|
2016-10-21 14:51:11 +00:00
|
|
|
|
0 => new Location($globalSymbolsUri, new Range(new Position(20, 27), new Position(20, 40))), // class TestClass implements TestInterface
|
2016-10-26 18:56:02 +00:00
|
|
|
|
1 => new Location($globalSymbolsUri, new Range(new Position(57, 48), new Position(57, 61))), // public function testMethod($testParameter): TestInterface
|
|
|
|
|
2 => new Location($globalReferencesUri, new Range(new Position(33, 20), new Position(33, 33))) // if ($abc instanceof TestInterface)
|
2016-10-18 08:48:16 +00:00
|
|
|
|
],
|
|
|
|
|
'TestClass::TEST_CLASS_CONST' => [
|
2016-10-19 22:18:36 +00:00
|
|
|
|
0 => new Location($globalSymbolsUri, new Range(new Position(48, 13), new Position(48, 35))), // echo self::TEST_CLASS_CONSTANT
|
|
|
|
|
1 => new Location($globalReferencesUri, new Range(new Position( 9, 5), new Position( 9, 32)))
|
2016-10-18 08:48:16 +00:00
|
|
|
|
],
|
|
|
|
|
'TestClass::testProperty' => [
|
2016-10-19 22:10:47 +00:00
|
|
|
|
0 => new Location($globalSymbolsUri, new Range(new Position(59, 8), new Position(59, 27))), // $this->testProperty = $testParameter;
|
2016-11-18 14:22:24 +00:00
|
|
|
|
1 => new Location($globalReferencesUri, new Range(new Position( 6, 5), new Position( 6, 23))), // echo $obj->testProperty;
|
|
|
|
|
2 => new Location($globalReferencesUri, new Range(new Position(38, 0), new Position(38, 18))), // $obj->testProperty->testMethod();
|
|
|
|
|
3 => new Location($globalReferencesUri, new Range(new Position(39, 0), new Position(39, 49))) // TestClass::$staticTestProperty[123]->testProperty;
|
2016-10-18 08:48:16 +00:00
|
|
|
|
],
|
|
|
|
|
'TestClass::staticTestProperty' => [
|
2016-11-18 14:22:24 +00:00
|
|
|
|
0 => new Location($globalReferencesUri, new Range(new Position( 8, 5), new Position( 8, 35))), // echo TestClass::$staticTestProperty;
|
|
|
|
|
1 => new Location($globalReferencesUri, new Range(new Position(39, 0), new Position(39, 30))) // TestClass::$staticTestProperty[123]->testProperty;
|
2016-10-18 08:48:16 +00:00
|
|
|
|
],
|
|
|
|
|
'TestClass::staticTestMethod()' => [
|
|
|
|
|
0 => new Location($globalReferencesUri, new Range(new Position( 7, 0), new Position( 7, 29)))
|
|
|
|
|
],
|
|
|
|
|
'TestClass::testMethod()' => [
|
2016-11-18 14:22:24 +00:00
|
|
|
|
0 => new Location($globalReferencesUri, new Range(new Position( 5, 0), new Position( 5, 18))), // $obj->testMethod();
|
2016-12-16 00:40:17 +00:00
|
|
|
|
1 => new Location($globalReferencesUri, new Range(new Position(38, 0), new Position(38, 32))), // $obj->testProperty->testMethod();
|
|
|
|
|
2 => new Location($globalReferencesUri, new Range(new Position(42, 5), new Position(42, 25))) // $child->testMethod();
|
2016-10-18 08:48:16 +00:00
|
|
|
|
],
|
|
|
|
|
'test_function()' => [
|
2016-10-26 09:47:02 +00:00
|
|
|
|
0 => new Location($globalReferencesUri, new Range(new Position(10, 0), new Position(10, 13))),
|
|
|
|
|
1 => new Location($globalReferencesUri, new Range(new Position(31, 13), new Position(31, 40)))
|
2016-10-18 08:48:16 +00:00
|
|
|
|
]
|
|
|
|
|
];
|
2016-10-24 17:35:37 +00:00
|
|
|
|
// @codingStandardsIgnoreEnd
|
2016-10-18 08:48:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getDefinitionLocation(string $fqn): Location
|
|
|
|
|
{
|
|
|
|
|
return $this->definitionLocations[$fqn];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getReferenceLocations(string $fqn): array
|
|
|
|
|
{
|
|
|
|
|
return $this->referenceLocations[$fqn];
|
|
|
|
|
}
|
|
|
|
|
}
|