Repository navigation
Expand file tree
/
Copy pathChangedFilesDetectorTest.php
More file actions
182 lines (138 loc) · 6.55 KB
/
Copy pathChangedFilesDetectorTest.php
File metadata and controls
182 lines (138 loc) · 6.55 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
declare(strict_types=1);
namespace Rector\Tests\Caching\Detector;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
final class ChangedFilesDetectorTest extends AbstractLazyTestCase
{
private ChangedFilesDetector $changedFilesDetector;
/**
* @var mixed[]
*/
private array $originalRules = [];
/**
* @var mixed[]
*/
private array $originalSkip = [];
/**
* @var mixed[]
*/
private array $originalFileExtensions = [];
protected function setUp(): void
{
parent::setUp();
$this->changedFilesDetector = $this->make(ChangedFilesDetector::class);
// the parameter bag is a global static shared across the whole test process, restore it after
$this->originalRules = SimpleParameterProvider::provideArrayParameter(Option::REGISTERED_RECTOR_RULES);
$this->originalSkip = SimpleParameterProvider::provideArrayParameter(Option::SKIP);
$this->originalFileExtensions = SimpleParameterProvider::provideArrayParameter(Option::FILE_EXTENSIONS);
}
protected function tearDown(): void
{
SimpleParameterProvider::setParameter(Option::REGISTERED_RECTOR_RULES, $this->originalRules);
SimpleParameterProvider::setParameter(Option::SKIP, $this->originalSkip);
SimpleParameterProvider::setParameter(Option::FILE_EXTENSIONS, $this->originalFileExtensions);
$this->changedFilesDetector->clear();
}
public function testHasFileChanged(): void
{
$filePath = __DIR__ . '/Source/file.php';
$this->assertTrue($this->changedFilesDetector->hasFileChanged($filePath));
$this->changedFilesDetector->addCacheableFile($filePath);
$this->changedFilesDetector->cacheFile($filePath);
$this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath));
$this->changedFilesDetector->invalidateFile($filePath);
$this->assertTrue($this->changedFilesDetector->hasFileChanged($filePath));
}
public function testScopedRunReusesFullRunCache(): void
{
$filePath = __DIR__ . '/Source/file.php';
// full run caches the file as clean
$this->changedFilesDetector->setActiveScope(null, null);
$this->changedFilesDetector->addCacheableFile($filePath);
$this->changedFilesDetector->cacheFile($filePath);
$this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath));
// an --only run reuses the full-run cache instead of re-analysing the file
$this->changedFilesDetector->setActiveScope('Rector\\SomeRule', null);
$this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath));
}
public function testScopedCacheDoesNotLeakToFullRun(): void
{
$filePath = __DIR__ . '/Source/file.php';
// a scoped run only caches under its own key
$this->changedFilesDetector->setActiveScope('Rector\\SomeRule', null);
$this->changedFilesDetector->addCacheableFile($filePath);
$this->changedFilesDetector->cacheFile($filePath);
$this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath));
// a full run must not treat the file as cached, as only one rule ran
$this->changedFilesDetector->setActiveScope(null, null);
$this->assertTrue($this->changedFilesDetector->hasFileChanged($filePath));
}
public function testCacheKeptWhenRuleRemoved(): void
{
$filePath = $this->cacheFileUnderRules(['Rector\\RuleA', 'Rector\\RuleB']);
// removing a rule means strictly less work, cache stays valid
$this->applyRules(['Rector\\RuleA']);
$this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath));
}
public function testCacheClearedWhenRuleAdded(): void
{
$filePath = $this->cacheFileUnderRules(['Rector\\RuleA']);
// adding a rule may refactor files that were clean so far, cache must drop
$this->applyRules(['Rector\\RuleA', 'Rector\\RuleB']);
$this->assertTrue($this->changedFilesDetector->hasFileChanged($filePath));
}
public function testCacheKeptWhenSkipAdded(): void
{
SimpleParameterProvider::setParameter(Option::SKIP, ['Rector\\RuleA']);
$filePath = $this->cacheFileUnderRules(['Rector\\RuleA', 'Rector\\RuleB']);
// adding a skip means strictly less work, cache stays valid
SimpleParameterProvider::setParameter(Option::SKIP, ['Rector\\RuleA', 'Rector\\RuleB']);
$this->snapshotConfiguration();
$this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath));
}
public function testCacheClearedWhenSkipRemoved(): void
{
SimpleParameterProvider::setParameter(Option::SKIP, ['Rector\\RuleA', 'Rector\\RuleB']);
$filePath = $this->cacheFileUnderRules(['Rector\\RuleA', 'Rector\\RuleB']);
// removing a skip re-enables transformations, cache must drop
SimpleParameterProvider::setParameter(Option::SKIP, ['Rector\\RuleA']);
$this->snapshotConfiguration();
$this->assertTrue($this->changedFilesDetector->hasFileChanged($filePath));
}
public function testCacheClearedWhenOutputAffectingParameterChanged(): void
{
SimpleParameterProvider::setParameter(Option::FILE_EXTENSIONS, ['php']);
$filePath = $this->cacheFileUnderRules(['Rector\\RuleA']);
SimpleParameterProvider::setParameter(Option::FILE_EXTENSIONS, ['php', 'phtml']);
$this->snapshotConfiguration();
$this->assertTrue($this->changedFilesDetector->hasFileChanged($filePath));
}
/**
* @param string[] $rules
*/
private function cacheFileUnderRules(array $rules): string
{
$filePath = __DIR__ . '/Source/file.php';
$this->applyRules($rules);
$this->changedFilesDetector->addCacheableFile($filePath);
$this->changedFilesDetector->cacheFile($filePath);
// sanity: the file is cached as clean before the configuration change under test
$this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath));
return $filePath;
}
/**
* @param string[] $rules
*/
private function applyRules(array $rules): void
{
SimpleParameterProvider::setParameter(Option::REGISTERED_RECTOR_RULES, $rules);
$this->snapshotConfiguration();
}
private function snapshotConfiguration(): void
{
$this->changedFilesDetector->setFirstResolvedConfigFileInfo(__DIR__ . '/config.php');
}
}