25
25
use Symfony \Component \DependencyInjection \ResettableContainerInterface ;
26
26
use Symfony \Component \DomCrawler \Crawler ;
27
27
use Symfony \Component \HttpFoundation \Response ;
28
+ use Symfony \Component \HttpKernel \KernelInterface ;
28
29
use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
29
30
use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
30
31
use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
@@ -40,10 +41,11 @@ class_alias(KernelBrowser::class, Client::class);
40
41
* @author Benjamin Eberlei <[email protected] >
41
42
*
42
43
* @method ContainerInterface getContainer()
44
+ * @property string $environment;
43
45
*/
44
46
abstract class WebTestCase extends BaseWebTestCase
45
47
{
46
- protected $ environment = 'test ' ;
48
+ protected static $ env = 'test ' ;
47
49
48
50
protected $ containers ;
49
51
@@ -70,7 +72,7 @@ abstract class WebTestCase extends BaseWebTestCase
70
72
*/
71
73
protected function getServiceMockBuilder (string $ id ): MockBuilder
72
74
{
73
- $ service = $ this ->getDependencyInjectionContainer ()->get ($ id );
75
+ $ service = $ this ->getContainer ()->get ($ id );
74
76
$ class = get_class ($ service );
75
77
76
78
return $ this ->getMockBuilder ($ class )->disableOriginalConstructor ();
@@ -83,13 +85,13 @@ protected function runCommand(string $name, array $params = [], bool $reuseKerne
83
85
{
84
86
if (!$ reuseKernel ) {
85
87
if (null !== static ::$ kernel ) {
86
- static ::$ kernel -> shutdown ();
88
+ static ::ensureKernelShutdown ();
87
89
}
88
90
89
- $ kernel = static ::$ kernel = static :: createKernel (['environment ' => $ this -> environment ]);
91
+ $ kernel = static ::bootKernel (['environment ' => self :: $ env ]);
90
92
$ kernel ->boot ();
91
93
} else {
92
- $ kernel = $ this ->getDependencyInjectionContainer ()->get ('kernel ' );
94
+ $ kernel = $ this ->getContainer ()->get ('kernel ' );
93
95
}
94
96
95
97
$ application = new Application ($ kernel );
@@ -129,7 +131,7 @@ protected function getVerbosityLevel(): int
129
131
// If `null`, is not yet set
130
132
if (null === $ this ->verbosityLevel ) {
131
133
// Set the global verbosity level that is set as NORMAL by the TreeBuilder in Configuration
132
- $ level = strtoupper ($ this ->getDependencyInjectionContainer ()->getParameter ('liip_functional_test.command_verbosity ' ));
134
+ $ level = strtoupper ($ this ->getContainer ()->getParameter ('liip_functional_test.command_verbosity ' ));
133
135
$ verbosity = '\Symfony\Component\Console\Output\StreamOutput::VERBOSITY_ ' .$ level ;
134
136
135
137
$ this ->verbosityLevel = constant ($ verbosity );
@@ -184,7 +186,7 @@ protected function getDecorated(): bool
184
186
{
185
187
if (null === $ this ->decorated ) {
186
188
// Set the global decoration flag that is set to `true` by the TreeBuilder in Configuration
187
- $ this ->decorated = $ this ->getDependencyInjectionContainer ()->getParameter ('liip_functional_test.command_decoration ' );
189
+ $ this ->decorated = $ this ->getContainer ()->getParameter ('liip_functional_test.command_decoration ' );
188
190
}
189
191
190
192
// Check the local decorated flag
@@ -204,12 +206,12 @@ public function isDecorated(bool $decorated): void
204
206
* Get an instance of the dependency injection container.
205
207
* (this creates a kernel *without* parameters).
206
208
*/
207
- protected function getDependencyInjectionContainer (): ContainerInterface
209
+ private function getDependencyInjectionContainer (): ContainerInterface
208
210
{
209
- $ cacheKey = $ this -> environment ;
211
+ $ cacheKey = self :: $ env ;
210
212
if (empty ($ this ->containers [$ cacheKey ])) {
211
213
$ kernel = static ::createKernel ([
212
- 'environment ' => $ this -> environment ,
214
+ 'environment ' => self :: $ env ,
213
215
]);
214
216
$ kernel ->boot ();
215
217
@@ -224,22 +226,60 @@ protected function getDependencyInjectionContainer(): ContainerInterface
224
226
return $ this ->containers [$ cacheKey ];
225
227
}
226
228
229
+ protected static function createKernel (array $ options = []): KernelInterface
230
+ {
231
+ if (!isset ($ options ['environment ' ])) {
232
+ $ options ['environment ' ] = self ::$ env ;
233
+ }
234
+
235
+ return parent ::createKernel ($ options );
236
+ }
237
+
227
238
/**
228
239
* Keep support of Symfony < 5.3.
229
240
*/
230
241
public function __call (string $ name , $ arguments )
231
242
{
232
243
if ('getContainer ' === $ name ) {
233
- if (method_exists ($ this , $ name )) {
234
- return self ::getContainer ();
235
- }
236
-
237
244
return $ this ->getDependencyInjectionContainer ();
238
245
}
239
246
240
247
throw new \Exception ("Method {$ name } is not supported. " );
241
248
}
242
249
250
+ public function __set ($ name , $ value )
251
+ {
252
+ if ($ name !== 'environment ' ) {
253
+ throw new \Exception (sprintf ('There is no property with name "%s" ' , $ name ));
254
+ }
255
+
256
+ @trigger_error ('Setting "environment" property is deprecated, please use self::$env. ' , \E_USER_DEPRECATED );
257
+
258
+ self ::$ env = $ value ;
259
+ }
260
+
261
+ public function __isset ($ name )
262
+ {
263
+ if ($ name !== 'environment ' ) {
264
+ throw new \Exception (sprintf ('There is no property with name "%s" ' , $ name ));
265
+ }
266
+
267
+ @trigger_error ('Checking "environment" property is deprecated, please use self::$env. ' , \E_USER_DEPRECATED );
268
+
269
+ return true ;
270
+ }
271
+
272
+ public function __get ($ name )
273
+ {
274
+ if ($ name !== 'environment ' ) {
275
+ throw new \Exception (sprintf ('There is no property with name "%s" ' , $ name ));
276
+ }
277
+
278
+ @trigger_error ('Getting "environment" property is deprecated, please use self::$env. ' , \E_USER_DEPRECATED );
279
+
280
+ return self ::$ env ;
281
+ }
282
+
243
283
/**
244
284
* Creates an instance of a lightweight Http client.
245
285
*
@@ -261,9 +301,9 @@ protected function makeClient(array $params = []): Client
261
301
*/
262
302
protected function makeAuthenticatedClient (array $ params = []): Client
263
303
{
264
- $ username = $ this ->getDependencyInjectionContainer ()
304
+ $ username = $ this ->getContainer ()
265
305
->getParameter ('liip_functional_test.authentication.username ' );
266
- $ password = $ this ->getDependencyInjectionContainer ()
306
+ $ password = $ this ->getContainer ()
267
307
->getParameter ('liip_functional_test.authentication.password ' );
268
308
269
309
return $ this ->createClientWithParams ($ params , $ username , $ password );
@@ -313,7 +353,7 @@ protected function createUserToken(UserInterface $user, string $firewallName): T
313
353
*/
314
354
protected function getUrl (string $ route , array $ params = [], int $ absolute = UrlGeneratorInterface::ABSOLUTE_PATH ): string
315
355
{
316
- return $ this ->getDependencyInjectionContainer ()->get ('router ' )->generate ($ route , $ params , $ absolute );
356
+ return $ this ->getContainer ()->get ('router ' )->generate ($ route , $ params , $ absolute );
317
357
}
318
358
319
359
/**
@@ -452,7 +492,11 @@ protected function createClientWithParams(array $params, ?string $username = nul
452
492
]);
453
493
}
454
494
455
- $ client = static ::createClient (['environment ' => $ this ->environment ], $ params );
495
+ if (static ::$ booted ) {
496
+ static ::ensureKernelShutdown ();
497
+ }
498
+
499
+ $ client = static ::createClient (['environment ' => self ::$ env ], $ params );
456
500
457
501
if ($ this ->firewallLogins ) {
458
502
// has to be set otherwise "hasPreviousSession" in Request returns false.
0 commit comments