forked from vimeo/psalm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CacheTest.php
159 lines (141 loc) · 4.93 KB
/
CacheTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
declare(strict_types=1);
namespace Psalm\Tests\Cache;
use Psalm\Config;
use Psalm\Internal\Analyzer\IssueData;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\IncludeCollector;
use Psalm\Internal\Provider\FakeFileProvider;
use Psalm\Internal\Provider\Providers;
use Psalm\Internal\RuntimeCaches;
use Psalm\IssueBuffer;
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;
use Psalm\Tests\TestCase;
use function str_replace;
use const DIRECTORY_SEPARATOR;
class CacheTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
RuntimeCaches::clearAll();
}
public function tearDown(): void
{
RuntimeCaches::clearAll();
parent::tearDown();
}
/**
* @param array<string, list<IssueData>> $issue_data
* @return array<string, list<string>>
*/
private static function normalizeIssueData(array $issue_data): array
{
$return = [];
foreach ($issue_data as $issue_data_per_file) {
foreach ($issue_data_per_file as $one_issue_data) {
$file_name = str_replace(DIRECTORY_SEPARATOR, '/', $one_issue_data->file_name);
$return[$file_name][] = $one_issue_data->type . ': ' . $one_issue_data->message;
}
}
return $return;
}
/**
* @param list<array{
* files: array<string, string|null>,
* issues?: array<string, list<string>>,
* }> $interactions
* @dataProvider provideCacheInteractions
*/
public function testCacheInteractions(
array $interactions
): void {
$config = Config::loadFromXML(
__DIR__ . DIRECTORY_SEPARATOR . 'test_base_dir',
<<<'XML'
<?xml version="1.0"?>
<psalm>
<projectFiles>
<directory name="src" />
</projectFiles>
</psalm>
XML,
);
$config->setIncludeCollector(new IncludeCollector());
$file_provider = new FakeFileProvider();
$providers = new Providers(
$file_provider,
new ParserInstanceCacheProvider(),
new FileStorageInstanceCacheProvider(),
new ClassLikeStorageInstanceCacheProvider(),
new FakeFileReferenceCacheProvider(),
new ProjectCacheProvider(),
);
foreach ($interactions as $interaction) {
foreach ($interaction['files'] as $file_path => $file_contents) {
$file_path = $config->base_dir . str_replace('/', DIRECTORY_SEPARATOR, $file_path);
if ($file_contents === null) {
$file_provider->deleteFile($file_path);
} else {
$file_provider->registerFile($file_path, $file_contents);
}
}
RuntimeCaches::clearAll();
$project_analyzer = new ProjectAnalyzer($config, $providers);
$project_analyzer->check($config->base_dir, true);
$issues = self::normalizeIssueData(IssueBuffer::getIssuesData());
self::assertSame($interaction['issues'] ?? [], $issues);
}
}
/**
* @return iterable<string, list{
* list<array{
* files: array<string, string|null>,
* issues?: array<string, list<string>>,
* }>,
* }>
*/
public static function provideCacheInteractions(): iterable
{
yield 'deletedFileInvalidatesReferencingMethod' => [
[
[
'files' => [
'/src/A.php' => <<<'PHP'
<?php
class A {
public function do(B $b): void
{
$b->do();
}
}
PHP,
'/src/B.php' => <<<'PHP'
<?php
class B {
public function do(): void
{
echo 'B';
}
}
PHP,
],
],
[
'files' => [
'/src/B.php' => null,
],
'issues' => [
'/src/A.php' => [
'UndefinedClass: Class, interface or enum named B does not exist',
],
],
],
],
];
}
}