1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 10:57:08 +01:00
psalm/tests/FileUpdates/CachedStorageTest.php

109 lines
3.6 KiB
PHP
Raw Normal View History

2018-10-12 05:00:32 +02:00
<?php
2018-10-12 05:00:32 +02:00
namespace Psalm\Tests\FileUpdates;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Provider\FakeFileProvider;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Provider\Providers;
2021-12-04 21:55:53 +01:00
use Psalm\Tests\Internal\Provider\ClassLikeStorageInstanceCacheProvider;
use Psalm\Tests\Internal\Provider\FakeFileReferenceCacheProvider;
use Psalm\Tests\Internal\Provider\FileStorageInstanceCacheProvider;
use Psalm\Tests\Internal\Provider\ParserInstanceCacheProvider;
use Psalm\Tests\Internal\Provider\ProjectCacheProvider;
2021-12-03 20:11:20 +01:00
use Psalm\Tests\TestCase;
2019-03-23 19:27:54 +01:00
use Psalm\Tests\TestConfig;
2021-06-08 04:55:21 +02:00
use function array_keys;
use function getcwd;
use function strpos;
2018-10-12 05:00:32 +02:00
2021-06-08 04:55:21 +02:00
use const DIRECTORY_SEPARATOR;
2021-12-03 20:11:20 +01:00
class CachedStorageTest extends TestCase
2018-10-12 05:00:32 +02:00
{
public function setUp(): void
2018-10-12 05:00:32 +02:00
{
parent::setUp();
$this->file_provider = new FakeFileProvider();
2018-10-12 05:00:32 +02:00
$config = new TestConfig();
$providers = new Providers(
$this->file_provider,
2021-12-04 21:55:53 +01:00
new ParserInstanceCacheProvider(),
new FileStorageInstanceCacheProvider(),
new ClassLikeStorageInstanceCacheProvider(),
new FakeFileReferenceCacheProvider(),
new ProjectCacheProvider()
2018-10-12 05:00:32 +02:00
);
2018-11-11 18:01:14 +01:00
$this->project_analyzer = new ProjectAnalyzer(
2018-10-12 05:00:32 +02:00
$config,
$providers
2018-10-12 05:00:32 +02:00
);
$this->project_analyzer->setPhpVersion('7.3', 'tests');
2018-10-12 05:00:32 +02:00
}
public function testValidInclude(): void
2018-10-12 05:00:32 +02:00
{
$test_name = $this->getTestName();
if (strpos($test_name, 'SKIPPED-') !== false) {
$this->markTestSkipped('Skipped due to a bug.');
}
2018-11-11 18:01:14 +01:00
$this->project_analyzer->getCodebase()->diff_methods = true;
2018-10-12 05:00:32 +02:00
2018-11-11 18:01:14 +01:00
$codebase = $this->project_analyzer->getCodebase();
2018-10-12 05:00:32 +02:00
$vendor_files = [
getcwd() . DIRECTORY_SEPARATOR . 'V1.php' => '<?php
namespace AnotherPackage;
interface StorageInterface {
public function getRecord(): OperationInterface;
}',
getcwd() . DIRECTORY_SEPARATOR . 'V2.php' => '<?php
namespace AnotherPackage;
interface OperationInterface {
public function getResult(): ResultInterface;
}',
getcwd() . DIRECTORY_SEPARATOR . 'V3.php' => '<?php
namespace AnotherPackage;
interface ResultInterface {}',
];
$analyzable_files = [
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
use AnotherPackage\StorageInterface;
class C {
/** @var ?StorageInterface */
private $storage;
public function zugzug() : void {
if (!$this->storage) {
return;
}
$result = $this->storage->getRecord()->getResult();
}
}',
];
foreach ($vendor_files as $file_path => $contents) {
$this->file_provider->registerFile($file_path, $contents);
$codebase->scanner->addFilesToShallowScan([$file_path => $file_path]);
}
foreach ($analyzable_files as $file_path => $contents) {
$this->file_provider->registerFile($file_path, $contents);
$codebase->addFilesToAnalyze([$file_path => $file_path]);
}
$codebase->scanFiles();
2018-11-11 18:01:14 +01:00
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
2018-10-12 05:00:32 +02:00
2018-11-11 18:01:14 +01:00
$codebase->reloadFiles($this->project_analyzer, array_keys($analyzable_files + $vendor_files));
2018-10-12 05:00:32 +02:00
2018-11-11 18:01:14 +01:00
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
2018-10-12 05:00:32 +02:00
}
}