forked from felixfbecker/php-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow getting type from define() node (felixfbecker#363)
* Allow getting type from define() node - fixes felixfbecker#364 * Add test case for DefinitionResolver
- Loading branch information
1 parent
b1cc7bf
commit 08cf1a3
Showing
4 changed files
with
108 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace LanguageServer\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use LanguageServer\Index\Index; | ||
use LanguageServer\{DefinitionResolver, Parser}; | ||
|
||
class DefinitionResolverTest extends TestCase | ||
{ | ||
public function testCreateDefinitionFromNode() | ||
{ | ||
$parser = new Parser; | ||
$stmts = $parser->parse("<?php\ndefine('TEST_DEFINE', true);"); | ||
$stmts[0]->setAttribute('ownerDocument', new MockPhpDocument); | ||
|
||
$index = new Index; | ||
$definitionResolver = new DefinitionResolver($index); | ||
$def = $definitionResolver->createDefinitionFromNode($stmts[0], '\TEST_DEFINE'); | ||
|
||
$this->assertInstanceOf(\phpDocumentor\Reflection\Types\Boolean::class, $def->type); | ||
} | ||
|
||
public function testGetTypeFromNode() | ||
{ | ||
$parser = new Parser; | ||
$stmts = $parser->parse("<?php\ndefine('TEST_DEFINE', true);"); | ||
$stmts[0]->setAttribute('ownerDocument', new MockPhpDocument); | ||
|
||
$index = new Index; | ||
$definitionResolver = new DefinitionResolver($index); | ||
$type = $definitionResolver->getTypeFromNode($stmts[0]); | ||
|
||
$this->assertInstanceOf(\phpDocumentor\Reflection\Types\Boolean::class, $type); | ||
} | ||
|
||
public function testGetDefinedFqnForIncompleteDefine() | ||
{ | ||
// define('XXX') (only one argument) must not introduce a new symbol | ||
$parser = new Parser; | ||
$stmts = $parser->parse("<?php\ndefine('TEST_DEFINE');"); | ||
$stmts[0]->setAttribute('ownerDocument', new MockPhpDocument); | ||
|
||
$index = new Index; | ||
$definitionResolver = new DefinitionResolver($index); | ||
$fqn = $definitionResolver->getDefinedFqn($stmts[0]); | ||
|
||
$this->assertNull($fqn); | ||
} | ||
|
||
public function testGetDefinedFqnForDefine() | ||
{ | ||
$parser = new Parser; | ||
$stmts = $parser->parse("<?php\ndefine('TEST_DEFINE', true);"); | ||
$stmts[0]->setAttribute('ownerDocument', new MockPhpDocument); | ||
|
||
$index = new Index; | ||
$definitionResolver = new DefinitionResolver($index); | ||
$fqn = $definitionResolver->getDefinedFqn($stmts[0]); | ||
|
||
$this->assertEquals('TEST_DEFINE', $fqn); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace LanguageServer\Tests; | ||
|
||
/** | ||
* A fake document for tests | ||
*/ | ||
class MockPhpDocument | ||
{ | ||
/** | ||
* Returns fake uri | ||
* | ||
* @return string | ||
*/ | ||
public function getUri() | ||
{ | ||
return 'file:///whatever'; | ||
} | ||
} |