1
0
Fork 0

Add separate tests for global definitions

pull/52/head
Felix Becker 2016-10-11 18:01:26 +02:00
parent 3a4fd9b1dd
commit ff66548b5d
6 changed files with 407 additions and 17 deletions

View File

@ -0,0 +1,24 @@
<?php
$obj = new TestClass();
$obj->testMethod();
echo $obj->testProperty;
TestClass::staticTestMethod();
echo TestClass::$staticTestProperty;
echo TestClass::TEST_CLASS_CONST;
test_function();
$var = 123;
echo $var;
function whatever(TestClass $param): TestClass {
echo $param;
}
$fn = function() use ($var) {
echo $var;
};
echo TEST_CONST;

View File

@ -0,0 +1,53 @@
<?php
const TEST_CONST = 123;
class TestClass implements TestInterface
{
const TEST_CLASS_CONST = 123;
public static $staticTestProperty;
public $testProperty;
public static function staticTestMethod()
{
}
public function testMethod($testParameter)
{
$testVariable = 123;
}
}
trait TestTrait
{
}
interface TestInterface
{
}
function test_function()
{
}
new class {
const TEST_CLASS_CONST = 123;
public static $staticTestProperty;
public $testProperty;
public static function staticTestMethod()
{
}
public function testMethod($testParameter)
{
$testVariable = 123;
}
};

View File

@ -412,7 +412,8 @@ class PhpDocument
return null; return null;
} }
// If the node is a function or constant, it could be namespaced, but PHP falls back to global // If the node is a function or constant, it could be namespaced, but PHP falls back to global
// The NameResolver therefor does not resolve these to namespaced names // The NameResolver therefor does not currently resolve these to namespaced names
// https://github.com/nikic/PHP-Parser/issues/236
// http://php.net/manual/en/language.namespaces.fallback.php // http://php.net/manual/en/language.namespaces.fallback.php
if ($parent instanceof Node\Expr\FuncCall || $parent instanceof Node\Expr\ConstFetch) { if ($parent instanceof Node\Expr\FuncCall || $parent instanceof Node\Expr\ConstFetch) {
// Find and try with namespace // Find and try with namespace
@ -420,12 +421,7 @@ class PhpDocument
while (isset($n)) { while (isset($n)) {
$n = $n->getAttribute('parentNode'); $n = $n->getAttribute('parentNode');
if ($n instanceof Node\Stmt\Namespace_) { if ($n instanceof Node\Stmt\Namespace_) {
$namespacedName = (string)$n->name . '\\' . $name; return (string)$n->name . '\\' . $name;
// If the namespaced version is defined, return that
// Otherwise fall back to global
if ($this->project->isDefined($namespacedName)) {
return $namespacedName;
}
} }
} }
} }

View File

@ -0,0 +1,317 @@
<?php
declare(strict_types = 1);
namespace LanguageServer\Tests\Server\TextDocument\Definition;
use PHPUnit\Framework\TestCase;
use LanguageServer\Tests\MockProtocolStream;
use LanguageServer\{Server, LanguageClient, Project};
use LanguageServer\Protocol\{TextDocumentIdentifier, Position};
class GlobalTest extends TestCase
{
/**
* @var Server\TextDocument
*/
private $textDocument;
public function setUp()
{
$client = new LanguageClient(new MockProtocolStream());
$project = new Project($client);
$this->textDocument = new Server\TextDocument($project, $client);
$project->openDocument('references', file_get_contents(__DIR__ . '/../../../../fixtures/global_references.php'));
$project->openDocument('symbols', file_get_contents(__DIR__ . '/../../../../fixtures/global_symbols.php'));
}
public function testDefinitionFileBeginning() {
// |<?php
$result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(0, 0));
$this->assertEquals([], json_decode(json_encode($result), true));
}
public function testDefinitionEmptyResult() {
// namespace keyword
$result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(2, 4));
$this->assertEquals([], json_decode(json_encode($result), true));
}
public function testDefinitionForClassLike()
{
// $obj = new TestClass();
// Get definition for TestClass
$result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(4, 16));
$this->assertEquals([
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 6,
'character' => 0
],
'end' => [
'line' => 21,
'character' => 1
]
]
], json_decode(json_encode($result), true));
}
public function testDefinitionForImplements()
{
// class TestClass implements TestInterface
// Get definition for TestInterface
$result = $this->textDocument->definition(new TextDocumentIdentifier('symbols'), new Position(6, 33));
$this->assertEquals([
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 28,
'character' => 0
],
'end' => [
'line' => 31,
'character' => 1
]
]
], json_decode(json_encode($result), true));
}
public function testDefinitionForClassConstants()
{
// echo TestClass::TEST_CLASS_CONST;
// Get definition for TEST_CLASS_CONST
$result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(9, 21));
$this->assertEquals([
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 8,
'character' => 10
],
'end' => [
'line' => 8,
'character' => 32
]
]
], json_decode(json_encode($result), true));
}
public function testDefinitionForConstants()
{
// echo TEST_CONST;
// Get definition for TEST_CONST
$result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(23, 9));
$this->assertEquals([
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 4,
'character' => 6
],
'end' => [
'line' => 4,
'character' => 22
]
]
], json_decode(json_encode($result), true));
}
public function testDefinitionForStaticMethods()
{
// TestClass::staticTestMethod();
// Get definition for staticTestMethod
$result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(7, 20));
$this->assertEquals([
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 12,
'character' => 4
],
'end' => [
'line' => 15,
'character' => 5
]
]
], json_decode(json_encode($result), true));
}
public function testDefinitionForStaticProperties()
{
// echo TestClass::$staticTestProperty;
// Get definition for staticTestProperty
$result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(8, 25));
$this->assertEquals([
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 9,
'character' => 18
],
'end' => [
'line' => 9,
'character' => 37
]
]
], json_decode(json_encode($result), true));
}
public function testDefinitionForMethods()
{
// $obj->testMethod();
// Get definition for testMethod
$result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(5, 11));
$this->assertEquals([
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 17,
'character' => 4
],
'end' => [
'line' => 20,
'character' => 5
]
]
], json_decode(json_encode($result), true));
}
public function testDefinitionForProperties()
{
// echo $obj->testProperty;
// Get definition for testProperty
$result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(6, 18));
$this->assertEquals([
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 10,
'character' => 11
],
'end' => [
'line' => 10,
'character' => 24
]
]
], json_decode(json_encode($result), true));
}
public function testDefinitionForVariables()
{
// echo $var;
// Get definition for $var
$result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(13, 7));
$this->assertEquals([
'uri' => 'references',
'range' => [
'start' => [
'line' => 12,
'character' => 0
],
'end' => [
'line' => 12,
'character' => 10
]
]
], json_decode(json_encode($result), true));
}
public function testDefinitionForParamTypeHints()
{
// function whatever(TestClass $param) {
// Get definition for TestClass
$result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(15, 23));
$this->assertEquals([
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 6,
'character' => 0
],
'end' => [
'line' => 21,
'character' => 1
]
]
], json_decode(json_encode($result), true));
}
public function testDefinitionForReturnTypeHints()
{
// function whatever(TestClass $param) {
// Get definition for TestClass
$result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(15, 42));
$this->assertEquals([
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 6,
'character' => 0
],
'end' => [
'line' => 21,
'character' => 1
]
]
], json_decode(json_encode($result), true));
}
public function testDefinitionForParams()
{
// echo $param;
// Get definition for $param
$result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(16, 13));
$this->assertEquals([
'uri' => 'references',
'range' => [
'start' => [
'line' => 15,
'character' => 18
],
'end' => [
'line' => 15,
'character' => 34
]
]
], json_decode(json_encode($result), true));
}
public function testDefinitionForUsedVariables()
{
// echo $var;
// Get definition for $var
$result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(20, 11));
$this->assertEquals([
'uri' => 'references',
'range' => [
'start' => [
'line' => 19,
'character' => 22
],
'end' => [
'line' => 19,
'character' => 26
]
]
], json_decode(json_encode($result), true));
}
public function testDefinitionForFunctions()
{
// test_function();
// Get definition for test_function
$result = $this->textDocument->definition(new TextDocumentIdentifier('references'), new Position(10, 4));
$this->assertEquals([
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 33,
'character' => 0
],
'end' => [
'line' => 36,
'character' => 1
]
]
], json_decode(json_encode($result), true));
}
}

View File

@ -1,14 +1,14 @@
<?php <?php
declare(strict_types = 1); declare(strict_types = 1);
namespace LanguageServer\Tests\Server\TextDocument; namespace LanguageServer\Tests\Server\TextDocument\Definition;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use LanguageServer\Tests\MockProtocolStream; use LanguageServer\Tests\MockProtocolStream;
use LanguageServer\{Server, LanguageClient, Project}; use LanguageServer\{Server, LanguageClient, Project};
use LanguageServer\Protocol\{TextDocumentIdentifier, Position}; use LanguageServer\Protocol\{TextDocumentIdentifier, Position};
class DefinitionTest extends TestCase class NamespacedTest extends TestCase
{ {
/** /**
* @var Server\TextDocument * @var Server\TextDocument
@ -20,9 +20,9 @@ class DefinitionTest extends TestCase
$client = new LanguageClient(new MockProtocolStream()); $client = new LanguageClient(new MockProtocolStream());
$project = new Project($client); $project = new Project($client);
$this->textDocument = new Server\TextDocument($project, $client); $this->textDocument = new Server\TextDocument($project, $client);
$project->openDocument('references', file_get_contents(__DIR__ . '/../../../fixtures/references.php')); $project->openDocument('references', file_get_contents(__DIR__ . '/../../../../fixtures/references.php'));
$project->openDocument('symbols', file_get_contents(__DIR__ . '/../../../fixtures/symbols.php')); $project->openDocument('symbols', file_get_contents(__DIR__ . '/../../../../fixtures/symbols.php'));
$project->openDocument('use', file_get_contents(__DIR__ . '/../../../fixtures/use.php')); $project->openDocument('use', file_get_contents(__DIR__ . '/../../../../fixtures/use.php'));
} }
public function testDefinitionFileBeginning() { public function testDefinitionFileBeginning() {

View File

@ -1,7 +1,7 @@
<?php <?php
declare(strict_types = 1); declare(strict_types = 1);
namespace LanguageServer\Tests\Server\TextDocument; namespace LanguageServer\Tests\Server\TextDocument\References;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use LanguageServer\Tests\MockProtocolStream; use LanguageServer\Tests\MockProtocolStream;
@ -9,7 +9,7 @@ use LanguageServer\{Server, LanguageClient, Project};
use LanguageServer\Protocol\{TextDocumentIdentifier, Position, ReferenceContext}; use LanguageServer\Protocol\{TextDocumentIdentifier, Position, ReferenceContext};
use function LanguageServer\pathToUri; use function LanguageServer\pathToUri;
class ReferencesTest extends TestCase class NamespacedTest extends TestCase
{ {
/** /**
* @var Server\TextDocument * @var Server\TextDocument
@ -25,9 +25,9 @@ class ReferencesTest extends TestCase
$client = new LanguageClient(new MockProtocolStream()); $client = new LanguageClient(new MockProtocolStream());
$project = new Project($client); $project = new Project($client);
$this->textDocument = new Server\TextDocument($project, $client); $this->textDocument = new Server\TextDocument($project, $client);
$this->symbolsUri = pathToUri(realpath(__DIR__ . '/../../../fixtures/symbols.php')); $this->symbolsUri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/symbols.php'));
$this->referencesUri = pathToUri(realpath(__DIR__ . '/../../../fixtures/references.php')); $this->referencesUri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/references.php'));
$this->useUri = pathToUri(realpath(__DIR__ . '/../../../fixtures/use.php')); $this->useUri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/use.php'));
$project->loadDocument($this->referencesUri, file_get_contents($this->referencesUri)); $project->loadDocument($this->referencesUri, file_get_contents($this->referencesUri));
$project->loadDocument($this->symbolsUri, file_get_contents($this->symbolsUri)); $project->loadDocument($this->symbolsUri, file_get_contents($this->symbolsUri));
$project->loadDocument($this->useUri, file_get_contents($this->useUri)); $project->loadDocument($this->useUri, file_get_contents($this->useUri));