1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00
psalm/tests/LanguageServer/CompletionTest.php

832 lines
24 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests\LanguageServer;
use LanguageServerProtocol\Position;
2019-03-23 19:27:54 +01:00
use Psalm\Context;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Analyzer\FileAnalyzer;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Provider\Providers;
Test parallelization (#4045) * Run tests in random order Being able to run tests in any order is a pre-requisite for being able to run them in parallel. * Reset type coverage between tests, fix affected tests * Reset parser and lexer between test runs and on php version change Previously lexer was reset, but parser kept the reference to the old one, and reference to the parser was kept by StatementsProvider. This resulted in order-dependent tests - if the parser was first initialized with phpVersion set to 7.4 then arrow functions worked fine, but were failing when the parser was initially constructed with settings for 7.3 This can be demonstrated on current master by upgrading to nikic/php-parser:4.9 and running: ``` vendor/bin/phpunit --no-coverage --filter="inferredArgArrowFunction" tests/ClosureTest.php ``` Now all tests using PHP 7.4 features must set the PHP version accordingly. * Marked more tests using 7.4 syntax * Reset newline-between-annotation flag between tests * Resolve real paths before passing them to checkPaths When checkPaths is called from psalm.php the paths are resolved, so we just mimicking SUT behaviour here. * Restore newline-between-annotations in DocCommentTest * Tweak Appveyor caches * Tweak TravisCI caches * Tweak CircleCI caches * Run tests in parallel Use `vendor/bin/paratest` instead of `vendor/bin/phpunit` * Use default paratest runner on Windows WrapperRunner is not supported on Windows. * TRAVIS_TAG could be empty * Restore appveyor conditional caching
2020-08-23 16:32:07 +02:00
use Psalm\Internal\RuntimeCaches;
2019-03-23 19:27:54 +01:00
use Psalm\Tests\Internal\Provider;
use Psalm\Tests\TestConfig;
class CompletionTest extends \Psalm\Tests\TestCase
{
2019-05-17 00:36:36 +02:00
public function setUp() : void
{
parent::setUp();
$this->file_provider = new \Psalm\Tests\Internal\Provider\FakeFileProvider();
$config = new TestConfig();
$providers = new Providers(
$this->file_provider,
new \Psalm\Tests\Internal\Provider\ParserInstanceCacheProvider(),
null,
null,
new Provider\FakeFileReferenceCacheProvider(),
new \Psalm\Tests\Internal\Provider\ProjectCacheProvider()
);
2018-11-11 18:01:14 +01:00
$this->project_analyzer = new ProjectAnalyzer(
$config,
$providers
);
2019-02-07 21:27:43 +01:00
$this->project_analyzer->setPhpVersion('7.3');
2019-02-24 07:33:25 +01:00
$this->project_analyzer->getCodebase()->store_node_types = true;
}
/**
* @return void
*/
public function testCompletionOnThisWithNoAssignment()
{
2018-11-11 18:01:14 +01:00
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
/** @var int|null */
protected $a;
public function foo() {
$this->
}
}'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
2020-03-25 14:18:49 +01:00
$this->assertSame(['B\A&static', '->', 213], $codebase->getCompletionDataAtPosition('somefile.php', new Position(8, 31)));
}
/**
* @return void
*/
public function testCompletionOnThisWithAssignmentBelow()
{
2018-11-11 18:01:14 +01:00
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
/** @var int|null */
protected $a;
public function foo() : self {
$this->
$a = "foo";
}
}'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
2020-03-25 14:18:49 +01:00
$this->assertSame(['B\A&static', '->', 220], $codebase->getCompletionDataAtPosition('somefile.php', new Position(8, 31)));
}
/**
* @return void
*/
public function testCompletionOnThisWithIfBelow()
{
2018-11-11 18:01:14 +01:00
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
/** @var int|null */
protected $a;
public function foo() : self {
$this
if(rand(0, 1)) {}
}
}'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$codebase->addTemporaryFileChanges(
'somefile.php',
'<?php
namespace B;
class A {
/** @var int|null */
protected $a;
public function foo() : self {
$this->
if(rand(0, 1)) {}
}
}'
);
$codebase->reloadFiles($this->project_analyzer, ['somefile.php']);
2018-11-11 18:01:14 +01:00
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
2020-03-25 14:18:49 +01:00
$this->assertSame(['B\A&static', '->', 220], $codebase->getCompletionDataAtPosition('somefile.php', new Position(8, 31)));
}
/**
* @return void
*/
public function testCompletionOnThisProperty()
{
2018-11-11 18:01:14 +01:00
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class C {
public function otherFunction() : void
}
class A {
/** @var C */
protected $cee_me;
public function __construct() {
$this->cee_me = new C();
}
public function foo() : void {
$this->cee_me->
}
}'
);
2018-11-11 18:01:14 +01:00
$codebase = $this->project_analyzer->getCodebase();
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$this->assertSame(['B\C', '->', 454], $codebase->getCompletionDataAtPosition('somefile.php', new Position(16, 39)));
}
/**
* @return void
*/
public function testCompletionOnThisPropertyWithCharacter()
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class C {
public function otherFunction() : void
}
class A {
/** @var C */
protected $cee_me;
public function __construct() {
$this->cee_me = new C();
}
public function foo() : void {
$this->cee_me->o
}
}'
);
$codebase = $this->project_analyzer->getCodebase();
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$this->assertSame(['B\C', '->', 455], $codebase->getCompletionDataAtPosition('somefile.php', new Position(16, 40)));
}
/**
* @return void
*/
public function testCompletionOnThisPropertyWithAnotherCharacter()
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class C {
public function otherFunction() : void
}
class A {
/** @var C */
protected $cee_me;
public function __construct() {
$this->cee_me = new C();
}
public function foo() : void {
$this->cee_me->ot
}
}'
);
$codebase = $this->project_analyzer->getCodebase();
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
2019-07-05 22:24:00 +02:00
$this->assertNull($codebase->getCompletionDataAtPosition('somefile.php', new Position(16, 41)));
}
/**
* @return void
*/
public function testCompletionOnTemplatedThisProperty()
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
/** @template T */
class C {
/** @var T */
private $t;
/** @param T $t */
public function __construct($t) {
$this->t = $t;
}
public function otherFunction() : void
}
class A {
/** @var C<string> */
protected $cee_me;
public function __construct() {
$this->cee_me = new C("hello");
}
public function foo() : void {
$this->cee_me->
}
}'
);
$codebase = $this->project_analyzer->getCodebase();
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$completion_data = $codebase->getCompletionDataAtPosition('somefile.php', new Position(25, 39));
$this->assertSame(['B\C<string>', '->', 726], $completion_data);
$completion_items = $codebase->getCompletionItemsForClassishThing($completion_data[0], $completion_data[1]);
$this->assertCount(3, $completion_items);
}
/**
* @return void
*/
public function testCompletionOnMethodReturnValue()
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
public function foo() : self {
return $this;
}
}
2019-06-12 19:34:49 +02:00
function foo(A $a) {
$a->foo()->
}
'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$this->assertSame(['B\A', '->', 259], $codebase->getCompletionDataAtPosition('somefile.php', new Position(9, 31)));
}
/**
* @return void
*/
public function testCompletionOnMethodArgument()
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
public function foo(A $a) : self {
return $this;
}
}
class C {}
2019-06-12 19:34:49 +02:00
function bar(A $a, C $c) {
$a->foo($c->)
}
'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$this->assertSame(['B\C', '->', 298], $codebase->getCompletionDataAtPosition('somefile.php', new Position(11, 32)));
}
/**
* @return void
*/
public function testCompletionOnMethodReturnValueWithArgument()
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
public function foo(A $a) : self {
return $this;
}
}
class C {}
2019-06-12 19:34:49 +02:00
function bar(A $a, C $c) {
$a->foo($c)->
}
'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$this->assertSame(['B\A', '->', 299], $codebase->getCompletionDataAtPosition('somefile.php', new Position(11, 33)));
}
/**
* @return void
*/
public function testCompletionOnVariableWithWhitespace()
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {}
2019-06-12 19:34:49 +02:00
function bar(A $a) {
$a ->
2019-06-12 19:34:49 +02:00
}'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$this->assertSame(['B\A', '->', 126], $codebase->getCompletionDataAtPosition('somefile.php', new Position(6, 25)));
2019-06-12 19:34:49 +02:00
}
/**
* @return void
*/
public function testCompletionOnVariableWithWhitespaceAndReturn()
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {}
2019-06-12 19:34:49 +02:00
function baz(A $a) {
$a
->
}
'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$this->assertSame(['B\A', '->', 150], $codebase->getCompletionDataAtPosition('somefile.php', new Position(7, 26)));
}
/**
* @return void
*/
public function testCompletionOnMethodReturnValueWithWhitespace()
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
public function foo() : self {
return $this;
}
}
2019-06-12 19:34:49 +02:00
function bar(A $a) {
$a->foo() ->
}
2019-06-12 19:34:49 +02:00
'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$this->assertSame(['B\A', '->', 261], $codebase->getCompletionDataAtPosition('somefile.php', new Position(10, 32)));
2019-06-12 19:34:49 +02:00
}
/**
* @return void
*/
public function testCompletionOnMethodReturnValueWithWhitespaceAndReturn()
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
public function foo() : self {
return $this;
}
}
2019-06-12 19:34:49 +02:00
function baz(A $a) {
$a->foo()
->
}
'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$this->assertSame(['B\A', '->', 285], $codebase->getCompletionDataAtPosition('somefile.php', new Position(11, 26)));
2019-06-12 19:34:49 +02:00
}
/**
* @return void
*/
public function testCompletionOnMethodReturnValueWhereParamIsClosure()
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class Collection {
public function map(callable $mapper) : self {
return $this;
}
}
function bar(Collection $a) {
$a->map(function ($foo) {})->
}'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$this->assertSame(['B\Collection', '->', 312], $codebase->getCompletionDataAtPosition('somefile.php', new Position(10, 49)));
2019-06-12 19:34:49 +02:00
}
/**
* @return void
*/
public function testCompletionOnMethodReturnValueWhereParamIsClosureWithStmt()
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class Collection {
public function map(callable $mapper) : self {
return $this;
}
}
function baz(Collection $a) {
$a->map(function ($foo) {return $foo;})->
}'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$this->assertSame(['B\Collection', '->', 324], $codebase->getCompletionDataAtPosition('somefile.php', new Position(10, 61)));
}
public function testCursorPositionOnMethodCompletion(): void
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
public function bar(string $a) {
$this->
}
public function baz() {}
}'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$completion_data = $codebase->getCompletionDataAtPosition('somefile.php', new Position(5, 31));
2020-03-25 14:18:49 +01:00
$this->assertSame(['B\A&static', '->', 146], $completion_data);
$completion_items = $codebase->getCompletionItemsForClassishThing($completion_data[0], $completion_data[1]);
$this->assertCount(2, $completion_items);
2019-07-05 22:24:00 +02:00
$this->assertSame('bar($0)', $completion_items[0]->insertText);
$this->assertSame('baz()', $completion_items[1]->insertText);
}
/**
* @return void
*/
public function testCompletionOnNewExceptionWithoutNamespace()
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
function foo() : void {
throw new Ex
}'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$this->assertSame(['*Ex', 'symbol', 78], $codebase->getCompletionDataAtPosition('somefile.php', new Position(2, 32)));
}
/**
* @return void
*/
public function testCompletionOnNewExceptionWithNamespaceNoUse()
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace Bar;
function foo() : void {
throw new Ex
}'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$completion_data = $codebase->getCompletionDataAtPosition('somefile.php', new Position(4, 32));
$this->assertSame(
[
'*Ex',
'symbol',
2019-07-05 22:24:00 +02:00
110,
],
$completion_data
);
$completion_items = $codebase->getCompletionItemsForPartialSymbol($completion_data[0], $completion_data[2], 'somefile.php');
$this->assertNotEmpty($completion_items);
$this->assertSame('Exception', $completion_items[0]->label);
$this->assertSame('Exception', $completion_items[0]->insertText);
$this->assertNotNull($completion_items[0]->additionalTextEdits);
$this->assertCount(1, $completion_items[0]->additionalTextEdits);
2019-07-03 22:58:27 +02:00
$this->assertSame('use Exception;' . "\n" . "\n", $completion_items[0]->additionalTextEdits[0]->newText);
$this->assertSame(3, $completion_items[0]->additionalTextEdits[0]->range->start->line);
$this->assertSame(16, $completion_items[0]->additionalTextEdits[0]->range->start->character);
$this->assertSame(3, $completion_items[0]->additionalTextEdits[0]->range->end->line);
$this->assertSame(16, $completion_items[0]->additionalTextEdits[0]->range->end->character);
}
/**
* @return void
*/
public function testCompletionOnNewExceptionWithNamespaceAndUse()
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace Bar;
use LogicException as LogEx;
class Alpha {}
class Antelope {}
function foo() : void {
new ArrayO
}'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$completion_data = $codebase->getCompletionDataAtPosition('somefile.php', new Position(9, 30));
$this->assertSame(
[
'*ArrayO',
'symbol',
220,
],
$completion_data
);
$completion_items = $codebase->getCompletionItemsForPartialSymbol($completion_data[0], $completion_data[2], 'somefile.php');
$this->assertCount(1, $completion_items);
$this->assertNotNull($completion_items[0]->additionalTextEdits);
$this->assertCount(1, $completion_items[0]->additionalTextEdits);
2019-07-03 22:58:27 +02:00
$this->assertSame("\n" . 'use ArrayObject;', $completion_items[0]->additionalTextEdits[0]->newText);
$this->assertSame(3, $completion_items[0]->additionalTextEdits[0]->range->start->line);
$this->assertSame(44, $completion_items[0]->additionalTextEdits[0]->range->start->character);
$this->assertSame(3, $completion_items[0]->additionalTextEdits[0]->range->end->line);
$this->assertSame(44, $completion_items[0]->additionalTextEdits[0]->range->end->character);
}
/**
* @return void
*/
public function testCompletionOnInstanceofWithNamespaceAndUse()
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace Bar;
use LogicException as LogEx;
class Alpha {}
class Antelope {}
class Anteater {}
function foo($a) : void {
if ($a instanceof Ant) {}
}'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$completion_data = $codebase->getCompletionDataAtPosition('somefile.php', new Position(10, 41));
$this->assertSame(
[
'*Ant',
'symbol',
267,
],
$completion_data
);
$completion_items = $codebase->getCompletionItemsForPartialSymbol($completion_data[0], $completion_data[2], 'somefile.php');
$this->assertCount(2, $completion_items);
}
}