44
55use PHPStan \Testing \PHPStanTestCase ;
66use function count ;
7+ use function file_get_contents ;
8+ use function file_put_contents ;
9+ use function glob ;
10+ use function mkdir ;
11+ use function rmdir ;
12+ use function sprintf ;
13+ use function sys_get_temp_dir ;
14+ use function uniqid ;
15+ use function unlink ;
716
817/**
918 * @phpstan-type ContainerContents array{parameters?: ParameterMap, services?: ServiceMap}
1019 */
1120final class SymfonyContainerResultCacheMetaExtensionTest extends PHPStanTestCase
1221{
1322
23+ private string $ tmpDir ;
24+
25+ protected function setUp (): void
26+ {
27+ parent ::setUp ();
28+ $ this ->tmpDir = sys_get_temp_dir () . '/phpstan-symfony-test- ' . uniqid ('' , true );
29+ mkdir ($ this ->tmpDir , 0777 , true );
30+ }
31+
32+ protected function tearDown (): void
33+ {
34+ $ cacheFiles = glob ($ this ->tmpDir . '/*.hash ' );
35+ if ($ cacheFiles !== false ) {
36+ foreach ($ cacheFiles as $ file ) {
37+ unlink ($ file );
38+ }
39+ }
40+ rmdir ($ this ->tmpDir );
41+ parent ::tearDown ();
42+ }
43+
44+ public function testHashIsCalculatedAndWrittenToCacheFileOnCacheMiss (): void
45+ {
46+ $ containerXmlPath = __DIR__ . '/container.xml ' ;
47+
48+ $ extension = new SymfonyContainerResultCacheMetaExtension (
49+ new DefaultParameterMap ([]),
50+ new DefaultServiceMap ([]),
51+ $ this ->tmpDir ,
52+ $ containerXmlPath ,
53+ );
54+
55+ $ hash = $ extension ->getHash ();
56+
57+ $ resultCacheHashFile = sprintf ('%s/symfonyDiContainer-container.xml-result-cache-meta.hash ' , $ this ->tmpDir );
58+ self ::assertFileExists ($ resultCacheHashFile );
59+ self ::assertSame ($ hash , file_get_contents ($ resultCacheHashFile ));
60+
61+ $ xmlHashFile = sprintf ('%s/symfonyDiContainer-container.xml.hash ' , $ this ->tmpDir );
62+ self ::assertFileExists ($ xmlHashFile );
63+ self ::assertSame ('c55d6ac45b535d6ecc9402cbb93825c38ec7b11b03f66577d0d3549b3d9ef75f ' , file_get_contents ($ xmlHashFile ));
64+ }
65+
66+ public function testCachedHashIsReturnedOnCacheHit (): void
67+ {
68+ $ containerXmlPath = __DIR__ . '/container.xml ' ;
69+ $ xmlHashFile = sprintf ('%s/symfonyDiContainer-container.xml.hash ' , $ this ->tmpDir );
70+ file_put_contents ($ xmlHashFile , 'c55d6ac45b535d6ecc9402cbb93825c38ec7b11b03f66577d0d3549b3d9ef75f ' );
71+ $ resultCacheHashFile = sprintf ('%s/symfonyDiContainer-container.xml-result-cache-meta.hash ' , $ this ->tmpDir );
72+ file_put_contents ($ resultCacheHashFile , 'pre-computed-hash ' );
73+
74+ $ extension = new SymfonyContainerResultCacheMetaExtension (
75+ new DefaultParameterMap ([]),
76+ new DefaultServiceMap ([]),
77+ $ this ->tmpDir ,
78+ $ containerXmlPath ,
79+ );
80+
81+ self ::assertSame ('pre-computed-hash ' , $ extension ->getHash ());
82+ }
83+
1484 /**
1585 * @param list<ContainerContents> $sameHashContents
1686 * @param ContainerContents $invalidatingContent
@@ -30,6 +100,8 @@ public function testContainerHashIsCalculatedCorrectly(
30100 $ currentHash = (new SymfonyContainerResultCacheMetaExtension (
31101 $ content ['parameters ' ] ?? new DefaultParameterMap ([]),
32102 $ content ['services ' ] ?? new DefaultServiceMap ([]),
103+ __DIR__ . '/../../tmp ' ,
104+ null ,
33105 ))->getHash ();
34106
35107 if ($ hash === null ) {
@@ -44,6 +116,8 @@ public function testContainerHashIsCalculatedCorrectly(
44116 (new SymfonyContainerResultCacheMetaExtension (
45117 $ invalidatingContent ['parameters ' ] ?? new DefaultParameterMap ([]),
46118 $ invalidatingContent ['services ' ] ?? new DefaultServiceMap ([]),
119+ __DIR__ . '/../../tmp ' ,
120+ null ,
47121 ))->getHash (),
48122 );
49123 }
0 commit comments