22
33namespace PHPStan \Analyser \ResultCache ;
44
5+ use Nette \Utils \Strings ;
56use Override ;
67use PHPStan \Analyser \AnalyserResult ;
78use PHPStan \Command \Output ;
2021use Symfony \Component \Console \Output \NullOutput ;
2122use Symfony \Component \Console \Style \SymfonyStyle as SymfonyConsoleStyle ;
2223use function array_keys ;
24+ use function basename ;
2325use function count ;
2426use function file_get_contents ;
2527use function file_put_contents ;
2628use function is_file ;
2729use function md5_file ;
2830use function mkdir ;
31+ use function ord ;
2932use function rmdir ;
30- use function str_replace ;
3133use function strlen ;
3234use function strpos ;
3335use function substr ;
@@ -47,11 +49,12 @@ class ResultCacheManagerCollectedDataTest extends PHPStanTestCase
4749 protected function setUp (): void
4850 {
4951 parent ::setUp ();
50- $ this ->directory = sys_get_temp_dir () . '/phpstan-collected-data- ' . uniqid ();
52+ $ fileHelper = self ::getContainer ()->getByType (FileHelper::class);
53+ $ this ->directory = $ fileHelper ->normalizePath (sys_get_temp_dir () . '/phpstan-collected-data- ' . uniqid ());
5154 // the cache file must not be in the analysed directory, or it counts as a scanned file
5255 mkdir ($ this ->directory . '/src ' , 0777 , true );
5356 foreach (['a ' , 'b ' , 'c ' ] as $ name ) {
54- $ file = $ this ->directory . '/src/ ' . $ name . '.php ' ;
57+ $ file = $ fileHelper -> normalizePath ( $ this ->directory . '/src/ ' . $ name . '.php ' ) ;
5558 file_put_contents ($ file , '<?php class Cache ' . $ name . " {} \n" );
5659 $ this ->files [] = $ file ;
5760 }
@@ -87,7 +90,7 @@ public function testCachedEntriesAreIndexedAndReadBack(): void
8790 $ this ->assertNotFalse ($ contents );
8891 foreach ($ lazy ->getCachedIndex () as $ file => [$ offset , $ length ]) {
8992 $ entry = substr ($ contents , $ offset , $ length );
90- $ this ->assertStringContainsString ('" ' . str_replace ( $ this -> directory . ' / ' , '' , $ file ) . '" ' , $ entry );
93+ $ this ->assertStringContainsString ('"src/ ' . basename ( $ file ) . '" ' , $ entry );
9194 $ this ->assertStringEndsWith ('} ' , $ entry );
9295 }
9396 }
@@ -123,9 +126,9 @@ public function testPartialSaveEqualsFreshSaveOfMergedData(): void
123126 $ expected [$ this ->files [1 ]] = $ freshB [$ this ->files [1 ]];
124127 $ this ->assertSame ($ expected , $ processed ->getAnalyserResult ()->getCollectedData ());
125128
126- $ partial = md5_file ( $ this ->cacheFilePath () );
129+ $ partial = $ this ->cacheContentsWithoutTime ( );
127130 $ this ->saveFullAnalysis ($ expected );
128- $ this ->assertSame ($ partial , md5_file ( $ this ->cacheFilePath () ));
131+ $ this ->assertSame ($ partial , $ this ->cacheContentsWithoutTime ( ));
129132 }
130133
131134 public function testDamagedValueDiscardsTheCacheWhenRead (): void
@@ -186,7 +189,7 @@ private function collectedData(array $names): array
186189 {
187190 $ data = [];
188191 foreach ($ names as $ name ) {
189- $ data [$ this ->directory . ' /src/ ' . $ name . ' .php ' ] = [MethodWithoutImpurePointsCollector::class => ['usage in ' . $ name ]];
192+ $ data [$ this ->files [ ord ( $ name) - ord ( ' a ' )] ] = [MethodWithoutImpurePointsCollector::class => ['usage in ' . $ name ]];
190193 }
191194
192195 return $ data ;
@@ -236,6 +239,17 @@ private function analyserResult(array $collectedData): AnalyserResult
236239 );
237240 }
238241
242+ /**
243+ * A full analysis records its own time, which a partial save keeps from the previous one.
244+ */
245+ private function cacheContentsWithoutTime (): string
246+ {
247+ $ contents = file_get_contents ($ this ->cacheFilePath ());
248+ $ this ->assertNotFalse ($ contents );
249+
250+ return Strings ::replace ($ contents , '~^lastFullAnalysisTime \d+\ni:\d+;~m ' , '' );
251+ }
252+
239253 private function breakFirstCollectedDataValue (): void
240254 {
241255 $ contents = file_get_contents ($ this ->cacheFilePath ());
0 commit comments