18
18
use Symfony \Component \Config \Resource \ResourceInterface ;
19
19
use Symfony \Component \Console \Input \ArrayInput ;
20
20
use Symfony \Component \Console \Output \NullOutput ;
21
+ use Symfony \Component \DependencyInjection \Container ;
21
22
use Symfony \Component \Filesystem \Filesystem ;
22
23
use Symfony \Component \Finder \Finder ;
23
- use Symfony \Component \HttpKernel \KernelInterface ;
24
24
25
25
class CacheClearCommandTest extends TestCase
26
26
{
@@ -41,65 +41,96 @@ protected function tearDown(): void
41
41
$ this ->fs ->remove ($ this ->kernel ->getProjectDir ());
42
42
}
43
43
44
- /** @dataProvider getKernel */
45
- public function testCacheIsFreshAfterCacheClearedWithWarmup (KernelInterface $ kernel )
44
+ public function testCacheIsFreshAfterCacheClearedWithWarmup ()
46
45
{
46
+ $ this ->fs ->mkdir ($ this ->kernel ->getProjectDir ());
47
+
47
48
$ input = new ArrayInput (['cache:clear ' ]);
48
- $ application = new Application ($ kernel );
49
+ $ application = new Application ($ this -> kernel );
49
50
$ application ->setCatchExceptions (false );
50
51
51
52
$ application ->doRun ($ input , new NullOutput ());
52
53
53
54
// Ensure that all *.meta files are fresh
54
55
$ finder = new Finder ();
55
- $ metaFiles = $ finder ->files ()->in ($ kernel ->getCacheDir ())->name ('*.php.meta ' );
56
+ $ metaFiles = $ finder ->files ()->in ($ this -> kernel ->getCacheDir ())->name ('*.php.meta ' );
56
57
// check that cache is warmed up
57
58
$ this ->assertNotEmpty ($ metaFiles );
58
59
$ configCacheFactory = new ConfigCacheFactory (true );
59
60
60
61
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
+ );
64
68
}
65
69
66
70
// 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 ' );
68
72
$ containerRef = new \ReflectionClass ($ containerClass );
69
73
$ containerFile = \dirname ($ containerRef ->getFileName (), 2 ).'/ ' .$ containerClass .'.php ' ;
70
74
$ 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 ();
73
77
/** @var ResourceInterface[] $meta */
74
78
$ meta = unserialize (file_get_contents ($ containerMetaFile ));
75
79
$ found = false ;
76
80
foreach ($ meta as $ resource ) {
77
- if ((string ) $ resource === $ kernelFile ) {
81
+ if ((string ) $ resource === $ this -> kernelFile ) {
78
82
$ found = true ;
79
83
break ;
80
84
}
81
85
}
82
86
$ this ->assertTrue ($ found , 'Kernel file should present as resource ' );
83
87
84
88
$ 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
+ );
87
99
}
88
100
89
- public function getKernel ()
101
+ public function testCacheIsWarmedWhenCalledTwice ()
90
102
{
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 ' ));
93
114
}
94
- }
95
115
96
- class NoBuildDirKernel extends TestAppKernel
97
- {
98
- protected function getKernelParameters ()
116
+ public function testCacheIsWarmedWithOldContainer ()
99
117
{
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 ());
102
133
103
- return $ parameters ;
134
+ $ this -> expectNotToPerformAssertions () ;
104
135
}
105
136
}
0 commit comments