Repository navigation
Expand file tree
/
Copy pathApplicationFileProcessorTest.php
More file actions
136 lines (102 loc) · 5.25 KB
/
Copy pathApplicationFileProcessorTest.php
File metadata and controls
136 lines (102 loc) · 5.25 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
<?php
declare(strict_types=1);
namespace Rector\Tests\Application\ApplicationFileProcessor;
use Rector\Application\ApplicationFileProcessor;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Rector\ValueObject\Configuration;
final class ApplicationFileProcessorTest extends AbstractLazyTestCase
{
private ApplicationFileProcessor $applicationFileProcessor;
private ChangedFilesDetector $changedFilesDetector;
protected function setUp(): void
{
parent::setUp();
// reset the container to boot an isolated cache dir, so parallel test processes cannot
// wipe the shared default cache mid-run and flip the caching assertions below
self::$rectorConfig = null;
$this->bootFromConfigFiles([__DIR__ . '/config.php']);
$this->applicationFileProcessor = $this->make(ApplicationFileProcessor::class);
$this->changedFilesDetector = $this->make(ChangedFilesDetector::class);
}
protected function tearDown(): void
{
$this->changedFilesDetector->clear();
}
public function testCleanFileIsCachedAsUnchanged(): void
{
$filePath = __DIR__ . '/Source/CleanFile.php';
$this->applicationFileProcessor->processFiles([$filePath], new Configuration(isDryRun: true));
$this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath));
}
public function testDryRunCachesFileWhoseReportedChangesLeaveContentUnchanged(): void
{
self::$rectorConfig = null;
$this->bootFromConfigFiles([__DIR__ . '/config-import-names.php']);
$applicationFileProcessor = $this->make(ApplicationFileProcessor::class);
$changedFilesDetector = $this->make(ChangedFilesDetector::class);
$filePath = __DIR__ . '/Source/WithAliasedImportOfSameShortName.php';
// name importing reports line changes here, but the printed file is identical, so the diff is empty
$processResult = $applicationFileProcessor->processFiles([$filePath], new Configuration(isDryRun: true));
$fileDiffs = $processResult->getFileDiffs(onlyWithChanges: false);
$this->assertCount(1, $fileDiffs);
$this->assertSame('', $fileDiffs[0]->getDiff());
$this->assertFalse($changedFilesDetector->hasFileChanged($filePath));
$changedFilesDetector->clear();
}
public function testDryRunDoesNotCacheFileWithPendingChanges(): void
{
self::$rectorConfig = null;
$this->bootFromConfigFiles([__DIR__ . '/config-max-changes.php']);
$applicationFileProcessor = $this->make(ApplicationFileProcessor::class);
$changedFilesDetector = $this->make(ChangedFilesDetector::class);
$filePath = __DIR__ . '/Source/WithTwoClosuresFirst.php';
$applicationFileProcessor->processFiles([$filePath], new Configuration(isDryRun: true));
$this->assertTrue($changedFilesDetector->hasFileChanged($filePath));
$changedFilesDetector->clear();
}
public function testOnlyRuleRunCachesUnderOwnScopeWithoutPoisoningFullRun(): void
{
$filePath = __DIR__ . '/Source/CleanFile.php';
$this->applicationFileProcessor->processFiles([$filePath], new Configuration(
isDryRun: true,
onlyRules: [RemoveEmptyClassMethodRector::class]
));
// a repeated --only run hits its own scoped cache entry
$this->changedFilesDetector->setActiveScope([RemoveEmptyClassMethodRector::class], null);
$this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath));
// a full run uses a different scope key, so it is not poisoned
$this->changedFilesDetector->setActiveScope([], null);
$this->assertTrue($this->changedFilesDetector->hasFileChanged($filePath));
}
public function testOnlySuffixRunCachesUnderOwnScopeWithoutPoisoningFullRun(): void
{
$filePath = __DIR__ . '/Source/CleanFile.php';
$this->applicationFileProcessor->processFiles([$filePath], new Configuration(
isDryRun: true,
onlySuffix: 'Controller.php'
));
$this->changedFilesDetector->setActiveScope([], 'Controller.php');
$this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath));
$this->changedFilesDetector->setActiveScope([], null);
$this->assertTrue($this->changedFilesDetector->hasFileChanged($filePath));
}
public function testMaxChangesStopsAfterLimit(): void
{
self::$rectorConfig = null;
$this->bootFromConfigFiles([__DIR__ . '/config-max-changes.php']);
$applicationFileProcessor = $this->make(ApplicationFileProcessor::class);
$filePaths = [
__DIR__ . '/Source/WithTwoClosuresFirst.php',
__DIR__ . '/Source/WithTwoClosuresSecond.php',
];
// each file holds 2 closures = 2 changes; the first file alone reaches the limit of 2,
// so the run stops before touching the second file - proving changes are counted, not files
$processResult = $applicationFileProcessor->processFiles($filePaths, new Configuration(
isDryRun: true,
maxChanges: 2,
));
$this->assertCount(1, $processResult->getFileDiffs());
}
}