Repository navigation
Expand file tree
/
Copy pathCacheFileDependencyTest.php
More file actions
108 lines (84 loc) · 3.61 KB
/
Copy pathCacheFileDependencyTest.php
File metadata and controls
108 lines (84 loc) · 3.61 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
<?php
declare(strict_types=1);
namespace Rector\Tests\Application\ApplicationFileProcessor;
use Nette\Utils\FileSystem;
use Rector\Application\ApplicationFileProcessor;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\StaticReflection\DynamicSourceLocatorDecorator;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Rector\ValueObject\Configuration;
/**
* Reproduces the cross-file cache false negative of https://github.com/rectorphp/rector-src/issues/8493
*
* A child's output depends on its parent, but the cache keys the child only by its own contents.
* When the parent changes and the child does not, the child's entry still looks valid, so the child
* is skipped and the transformation it now needs is silently missed.
*/
final class CacheFileDependencyTest extends AbstractLazyTestCase
{
private ApplicationFileProcessor $applicationFileProcessor;
private ChangedFilesDetector $changedFilesDetector;
private string $temporaryDirectory;
protected function setUp(): void
{
parent::setUp();
self::$rectorConfig = null;
$this->bootFromConfigFiles([__DIR__ . '/config-parent-return-type.php']);
$this->applicationFileProcessor = $this->make(ApplicationFileProcessor::class);
$this->changedFilesDetector = $this->make(ChangedFilesDetector::class);
$this->temporaryDirectory = sys_get_temp_dir() . '/_rector_cache_file_dependency_test_src';
}
protected function tearDown(): void
{
$this->changedFilesDetector->clear();
FileSystem::delete($this->temporaryDirectory);
}
public function testChildIsReconsideredWhenParentChanges(): void
{
$parentFilePath = $this->temporaryDirectory . '/ParentClass.php';
$childFilePath = $this->temporaryDirectory . '/ChildClass.php';
// parent method has no return type yet, so the child gets nothing to copy
FileSystem::write($parentFilePath, $this->createParentContents(''));
FileSystem::write($childFilePath, $this->createChildContents());
// register the files for static reflection, as the process command does for a real run
$this->make(DynamicSourceLocatorDecorator::class)
->addPaths([$this->temporaryDirectory]);
$this->applicationFileProcessor->processFiles(
[$parentFilePath, $childFilePath],
new Configuration(isDryRun: true)
);
// both files are clean on this run and cached as such
$this->assertFalse($this->changedFilesDetector->hasFileChanged($childFilePath));
// the parent now declares a return type the child should inherit on the next run
FileSystem::write($parentFilePath, $this->createParentContents(': string'));
// the child's own contents are untouched, yet its output now depends on the changed parent,
// so it must be reconsidered rather than served from the stale cache entry (#8493)
$this->assertTrue($this->changedFilesDetector->hasFileChanged($childFilePath));
}
private function createParentContents(string $returnType): string
{
return <<<PHP
<?php
namespace RectorCacheDependency;
class ParentClass
{
public function run(){$returnType}
{
}
}
PHP;
}
private function createChildContents(): string
{
return <<<'PHP'
<?php
namespace RectorCacheDependency;
class ChildClass extends ParentClass
{
public function run()
{
}
}
PHP;
}
}