1818use Symfony \Component \Config \Resource \ResourceInterface ;
1919use Symfony \Component \Console \Input \ArrayInput ;
2020use Symfony \Component \Console \Output \NullOutput ;
21+ use Symfony \Component \DependencyInjection \Container ;
2122use Symfony \Component \Filesystem \Filesystem ;
2223use Symfony \Component \Finder \Finder ;
23- use Symfony \Component \HttpKernel \KernelInterface ;
2424
2525class CacheClearCommandTest extends TestCase
2626{
@@ -41,65 +41,96 @@ protected function tearDown(): void
4141 $ this ->fs ->remove ($ this ->kernel ->getProjectDir ());
4242 }
4343
44- /** @dataProvider getKernel */
45- public function testCacheIsFreshAfterCacheClearedWithWarmup (KernelInterface $ kernel )
44+ public function testCacheIsFreshAfterCacheClearedWithWarmup ()
4645 {
46+ $ this ->fs ->mkdir ($ this ->kernel ->getProjectDir ());
47+
4748 $ input = new ArrayInput (['cache:clear ' ]);
48- $ application = new Application ($ kernel );
49+ $ application = new Application ($ this -> kernel );
4950 $ application ->setCatchExceptions (false );
5051
5152 $ application ->doRun ($ input , new NullOutput ());
5253
5354 // Ensure that all *.meta files are fresh
5455 $ finder = new Finder ();
55- $ metaFiles = $ finder ->files ()->in ($ kernel ->getCacheDir ())->name ('*.php.meta ' );
56+ $ metaFiles = $ finder ->files ()->in ($ this -> kernel ->getCacheDir ())->name ('*.php.meta ' );
5657 // check that cache is warmed up
5758 $ this ->assertNotEmpty ($ metaFiles );
5859 $ configCacheFactory = new ConfigCacheFactory (true );
5960
6061 foreach ($ metaFiles as $ file ) {
61- $ configCacheFactory ->cache (substr ($ file , 0 , -5 ), function () use ($ file ) {
62- $ this ->fail (sprintf ('Meta file "%s" is not fresh ' , (string ) $ file ));
63- });
62+ $ configCacheFactory ->cache (
63+ substr ($ file , 0 , -5 ),
64+ function () use ($ file ) {
65+ $ this ->fail (sprintf ('Meta file "%s" is not fresh ' , (string ) $ file ));
66+ }
67+ );
6468 }
6569
6670 // check that app kernel file present in meta file of container's cache
67- $ containerClass = $ kernel ->getContainer ()->getParameter ('kernel.container_class ' );
71+ $ containerClass = $ this -> kernel ->getContainer ()->getParameter ('kernel.container_class ' );
6872 $ containerRef = new \ReflectionClass ($ containerClass );
6973 $ containerFile = \dirname ($ containerRef ->getFileName (), 2 ).'/ ' .$ containerClass .'.php ' ;
7074 $ containerMetaFile = $ containerFile .'.meta ' ;
71- $ kernelRef = new \ReflectionObject ($ kernel );
72- $ kernelFile = $ kernelRef ->getFileName ();
75+ $ this -> kernelRef = new \ReflectionObject ($ this -> kernel );
76+ $ this -> kernelFile = $ this -> kernelRef ->getFileName ();
7377 /** @var ResourceInterface[] $meta */
7478 $ meta = unserialize (file_get_contents ($ containerMetaFile ));
7579 $ found = false ;
7680 foreach ($ meta as $ resource ) {
77- if ((string ) $ resource === $ kernelFile ) {
81+ if ((string ) $ resource === $ this -> kernelFile ) {
7882 $ found = true ;
7983 break ;
8084 }
8185 }
8286 $ this ->assertTrue ($ found , 'Kernel file should present as resource ' );
8387
8488 $ containerRef = new \ReflectionClass (require $ containerFile );
85- $ containerFile = str_replace ('tes_ ' .\DIRECTORY_SEPARATOR , 'test ' .\DIRECTORY_SEPARATOR , $ containerRef ->getFileName ());
86- $ this ->assertMatchesRegularExpression (sprintf ('/ \'kernel.container_class \'\s*=>\s* \'%s \'/ ' , $ containerClass ), file_get_contents ($ containerFile ), 'kernel.container_class is properly set on the dumped container ' );
89+ $ containerFile = str_replace (
90+ 'tes_ ' .\DIRECTORY_SEPARATOR ,
91+ 'test ' .\DIRECTORY_SEPARATOR ,
92+ $ containerRef ->getFileName ()
93+ );
94+ $ this ->assertMatchesRegularExpression (
95+ sprintf ('/ \'kernel.container_class \'\s*=>\s* \'%s \'/ ' , $ containerClass ),
96+ file_get_contents ($ containerFile ),
97+ 'kernel.container_class is properly set on the dumped container '
98+ );
8799 }
88100
89- public function getKernel ()
101+ public function testCacheIsWarmedWhenCalledTwice ()
90102 {
91- yield [new TestAppKernel ('test ' , true )];
92- yield [new NoBuildDirKernel ('test ' , true )];
103+ $ input = new ArrayInput (['cache:clear ' ]);
104+ $ application = new Application (clone $ this ->kernel );
105+ $ application ->setCatchExceptions (false );
106+ $ application ->doRun ($ input , new NullOutput ());
107+
108+ $ _SERVER ['REQUEST_TIME ' ] = time () + 1 ;
109+ $ application = new Application (clone $ this ->kernel );
110+ $ application ->setCatchExceptions (false );
111+ $ application ->doRun ($ input , new NullOutput ());
112+
113+ $ this ->assertTrue (is_file ($ this ->kernel ->getCacheDir ().'/annotations.php ' ));
93114 }
94- }
95115
96- class NoBuildDirKernel extends TestAppKernel
97- {
98- protected function getKernelParameters ()
116+ public function testCacheIsWarmedWithOldContainer ()
99117 {
100- $ parameters = parent ::getKernelParameters ();
101- unset($ parameters ['kernel.build_dir ' ]);
118+ $ kernel = clone $ this ->kernel ;
119+
120+ // Hack to get a dumped working container,
121+ // BUT without "kernel.build_dir" parameter (like an old dumped container)
122+ $ kernel ->boot ();
123+ $ container = $ kernel ->getContainer ();
124+ \Closure::bind (function (Container $ class ) {
125+ unset($ class ->loadedDynamicParameters ['kernel.build_dir ' ]);
126+ unset($ class ->parameters ['kernel.build_dir ' ]);
127+ }, null , \get_class ($ container ))($ container );
128+
129+ $ input = new ArrayInput (['cache:clear ' ]);
130+ $ application = new Application ($ kernel );
131+ $ application ->setCatchExceptions (false );
132+ $ application ->doRun ($ input , new NullOutput ());
102133
103- return $ parameters ;
134+ $ this -> expectNotToPerformAssertions () ;
104135 }
105136}
0 commit comments