1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-12 09:19:40 +01:00
psalm/tests/LanguageServer/FileMapTest.php
Joe Hoyle ba43e6d0f3
Remove file map cache when re-analyzing files (#5084)
* Remove file map cache when re-analyzing files

When the project is re-analyzed under the single thread condition (not using a process pool), the filemaps are not cleared before re-analyzing files. This means that file maps only get appended to. If you delete the contents of a file via the LSP, the file map will still be populated with all the old values for example.

In doing this I had to write a few more tests to check my assumptions too, so adding those additional tests.

* Rename test

* Formatting

* Formatting again!
2021-01-22 16:05:28 -05:00

104 lines
2.9 KiB
PHP

<?php
namespace Psalm\Tests\LanguageServer;
use LanguageServerProtocol\Position;
use Psalm\Context;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Provider\Providers;
use Psalm\Tests\Internal\Provider;
use Psalm\Tests\TestConfig;
class FileMapTest extends \Psalm\Tests\TestCase
{
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()
);
$this->project_analyzer = new ProjectAnalyzer(
$config,
$providers
);
$this->project_analyzer->setPhpVersion('7.3');
$this->project_analyzer->getCodebase()->store_node_types = true;
}
public function testMapIsUpdatedOnReloadFiles(): void
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
class A {
function __construct( string $var ) {
}
}
$a = new A( "foo" );'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
[ $type_map ] = $codebase->analyzer->getMapsForFile('somefile.php');
$this->assertTrue(!empty($type_map));
$codebase->file_provider->setOpenContents('somefile.php', '');
$codebase->reloadFiles($this->project_analyzer, ['somefile.php']);
[ $type_map ] = $codebase->analyzer->getMapsForFile('somefile.php');
$this->assertSame([], $type_map);
}
public function testGetTypeMap(): void
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
class A {
function __construct( string $var ) {
}
}
$a = new A( "foo" );'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
[ $type_map ] = $codebase->analyzer->getMapsForFile('somefile.php');
$this->assertSame(
[
155 => [
156,
'A',
],
146 => [
148,
'146-147:A',
],
],
$type_map
);
}
}