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

204 lines
5.2 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests\LanguageServer;
use LanguageServerProtocol\Position;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Analyzer\FileAnalyzer;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Context;
use Psalm\Tests\TestConfig;
use Psalm\Tests\Internal\Provider;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Provider\Providers;
class CompletionTest extends \Psalm\Tests\TestCase
{
/**
* @return void
*/
public function setUp()
{
parent::setUp();
2018-11-06 03:57:36 +01:00
FileAnalyzer::clearCache();
$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()
);
2018-11-11 18:01:14 +01:00
$this->project_analyzer = new ProjectAnalyzer(
$config,
$providers,
false,
true,
2018-11-06 03:57:36 +01:00
ProjectAnalyzer::TYPE_CONSOLE,
1,
false
);
2018-11-11 18:01:14 +01:00
$this->project_analyzer->getCodebase()->server_mode = 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->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$this->assertSame(['B\A', '->'], $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->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$this->assertSame(['B\A', '->'], $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->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']);
$codebase->addFilesToAnalyze(['somefile.php' => 'somefile.php']);
2018-11-11 18:01:14 +01:00
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
$this->assertSame(['B\A', '->'], $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->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$this->assertSame(['B\C', '->'], $codebase->getCompletionDataAtPosition('somefile.php', new Position(16, 39)));
}
}