|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\Tests\Caching; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Rector\Caching\Cache; |
| 9 | +use Rector\Caching\DryRunDiffCache; |
| 10 | +use Rector\Caching\FileDependencyCollector; |
| 11 | +use Rector\Caching\ValueObject\Storage\FileCacheStorage; |
| 12 | +use Rector\ChangesReporting\ValueObject\RectorWithLineChange; |
| 13 | +use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector; |
| 14 | +use Rector\Util\FileHasher; |
| 15 | +use Rector\ValueObject\Application\File; |
| 16 | +use Rector\ValueObject\Configuration; |
| 17 | +use Rector\ValueObject\FileProcessResult; |
| 18 | +use Rector\ValueObject\Reporting\FileDiff; |
| 19 | +use Symfony\Component\Filesystem\Filesystem; |
| 20 | + |
| 21 | +final class DryRunDiffCacheTest extends TestCase |
| 22 | +{ |
| 23 | + private string $cacheDirectory; |
| 24 | + |
| 25 | + private string $sourceFilePath; |
| 26 | + |
| 27 | + private string $dependencyFilePath; |
| 28 | + |
| 29 | + private FileDependencyCollector $fileDependencyCollector; |
| 30 | + |
| 31 | + private DryRunDiffCache $dryRunDiffCache; |
| 32 | + |
| 33 | + protected function setUp(): void |
| 34 | + { |
| 35 | + $this->cacheDirectory = sys_get_temp_dir() . '/rector_dry_run_diff_cache_test_' . getmypid(); |
| 36 | + |
| 37 | + $this->sourceFilePath = $this->cacheDirectory . '/Source.php'; |
| 38 | + $this->dependencyFilePath = $this->cacheDirectory . '/Dependency.php'; |
| 39 | + |
| 40 | + $filesystem = new Filesystem(); |
| 41 | + $filesystem->mkdir($this->cacheDirectory); |
| 42 | + $filesystem->dumpFile($this->sourceFilePath, "<?php\n\nclass Source\n{\n}\n"); |
| 43 | + $filesystem->dumpFile($this->dependencyFilePath, "<?php\n\nclass Dependency\n{\n}\n"); |
| 44 | + |
| 45 | + $fileHasher = new FileHasher(); |
| 46 | + $this->fileDependencyCollector = new FileDependencyCollector($fileHasher); |
| 47 | + |
| 48 | + $this->dryRunDiffCache = new DryRunDiffCache( |
| 49 | + new Cache(new FileCacheStorage($this->cacheDirectory, $filesystem)), |
| 50 | + $fileHasher, |
| 51 | + $this->fileDependencyCollector |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + protected function tearDown(): void |
| 56 | + { |
| 57 | + (new Filesystem())->remove($this->cacheDirectory); |
| 58 | + } |
| 59 | + |
| 60 | + public function testSaveLoadRoundTrip(): void |
| 61 | + { |
| 62 | + $file = $this->createFile(); |
| 63 | + $configuration = new Configuration(isDryRun: true); |
| 64 | + |
| 65 | + $this->assertNull($this->dryRunDiffCache->load($file, $configuration)); |
| 66 | + |
| 67 | + $this->fileDependencyCollector->record($this->sourceFilePath, $this->dependencyFilePath); |
| 68 | + $this->dryRunDiffCache->save($file, $configuration, $this->createFileDiff(), true); |
| 69 | + |
| 70 | + $cachedFileProcessResult = $this->dryRunDiffCache->load($file, $configuration); |
| 71 | + $this->assertInstanceOf(FileProcessResult::class, $cachedFileProcessResult); |
| 72 | + $this->assertTrue($cachedFileProcessResult->hasChanged()); |
| 73 | + |
| 74 | + $loadedFileDiff = $cachedFileProcessResult->getFileDiff(); |
| 75 | + $this->assertInstanceOf(FileDiff::class, $loadedFileDiff); |
| 76 | + $this->assertSame('some diff', $loadedFileDiff->getDiff()); |
| 77 | + $this->assertSame([RemoveUnusedPrivateMethodRector::class], $loadedFileDiff->getRectorClasses()); |
| 78 | + } |
| 79 | + |
| 80 | + public function testReplayKeepsUnchangedFlag(): void |
| 81 | + { |
| 82 | + $file = $this->createFile(); |
| 83 | + $configuration = new Configuration(isDryRun: true); |
| 84 | + |
| 85 | + // a rule can report line changes while the printed content stays identical |
| 86 | + $this->fileDependencyCollector->record($this->sourceFilePath, $this->dependencyFilePath); |
| 87 | + $this->dryRunDiffCache->save($file, $configuration, $this->createFileDiff(), false); |
| 88 | + |
| 89 | + $cachedFileProcessResult = $this->dryRunDiffCache->load($file, $configuration); |
| 90 | + $this->assertInstanceOf(FileProcessResult::class, $cachedFileProcessResult); |
| 91 | + $this->assertFalse($cachedFileProcessResult->hasChanged()); |
| 92 | + } |
| 93 | + |
| 94 | + public function testChangedOwnContentInvalidates(): void |
| 95 | + { |
| 96 | + $file = $this->createFile(); |
| 97 | + $configuration = new Configuration(isDryRun: true); |
| 98 | + $this->dryRunDiffCache->save($file, $configuration, $this->createFileDiff(), true); |
| 99 | + |
| 100 | + $changedFile = new File($this->sourceFilePath, "<?php\n\nclass Source\n{\n public int \$added;\n}\n"); |
| 101 | + $this->assertNull($this->dryRunDiffCache->load($changedFile, $configuration)); |
| 102 | + } |
| 103 | + |
| 104 | + public function testChangedDependencyInvalidates(): void |
| 105 | + { |
| 106 | + $file = $this->createFile(); |
| 107 | + $configuration = new Configuration(isDryRun: true); |
| 108 | + |
| 109 | + $this->fileDependencyCollector->record($this->sourceFilePath, $this->dependencyFilePath); |
| 110 | + $this->dryRunDiffCache->save($file, $configuration, $this->createFileDiff(), true); |
| 111 | + $this->assertInstanceOf(FileProcessResult::class, $this->dryRunDiffCache->load($file, $configuration)); |
| 112 | + |
| 113 | + (new Filesystem())->dumpFile( |
| 114 | + $this->dependencyFilePath, |
| 115 | + "<?php\n\nclass Dependency\n{\n public int \$added;\n}\n" |
| 116 | + ); |
| 117 | + |
| 118 | + // fresh collector = fresh process run, no memoized hashes |
| 119 | + $freshFileHasher = new FileHasher(); |
| 120 | + $freshDryRunDiffCache = new DryRunDiffCache( |
| 121 | + new Cache(new FileCacheStorage($this->cacheDirectory, new Filesystem())), |
| 122 | + $freshFileHasher, |
| 123 | + new FileDependencyCollector($freshFileHasher) |
| 124 | + ); |
| 125 | + |
| 126 | + $this->assertNull($freshDryRunDiffCache->load($file, $configuration)); |
| 127 | + } |
| 128 | + |
| 129 | + public function testNoDiffsConfigurationDoesNotCrossReplay(): void |
| 130 | + { |
| 131 | + $file = $this->createFile(); |
| 132 | + |
| 133 | + $this->dryRunDiffCache->save( |
| 134 | + $file, |
| 135 | + new Configuration(isDryRun: true, showDiffs: false), |
| 136 | + $this->createFileDiff(), |
| 137 | + true |
| 138 | + ); |
| 139 | + |
| 140 | + $this->assertNull($this->dryRunDiffCache->load($file, new Configuration(isDryRun: true, showDiffs: true))); |
| 141 | + } |
| 142 | + |
| 143 | + private function createFile(): File |
| 144 | + { |
| 145 | + return new File($this->sourceFilePath, (string) file_get_contents($this->sourceFilePath)); |
| 146 | + } |
| 147 | + |
| 148 | + private function createFileDiff(): FileDiff |
| 149 | + { |
| 150 | + return new FileDiff($this->sourceFilePath, 'some diff', 'some diff formatted', [ |
| 151 | + new RectorWithLineChange(RemoveUnusedPrivateMethodRector::class, 7), |
| 152 | + ]); |
| 153 | + } |
| 154 | +} |
0 commit comments