1414use Symfony \Component \HttpFoundation \Response ;
1515use Symfony \Component \HttpKernel \HttpKernelBrowser ;
1616use Symfony \Component \HttpKernel \Kernel ;
17+ use Symfony \Contracts \Service \ResetInterface ;
1718use Symfony \Component \HttpKernel \Profiler \Profiler ;
1819use function array_keys ;
1920use function codecept_debug ;
@@ -31,7 +32,7 @@ public function __construct(
3132 parent ::__construct ($ kernel );
3233 $ this ->followRedirects ();
3334 $ this ->container = $ this ->getContainer ();
34- $ this ->rebootKernel (); // Ensure the profiler exists
35+ $ this ->rebootKernel ();
3536 }
3637
3738 /** @param Request $request */
@@ -47,7 +48,7 @@ protected function doRequest(object $request): Response
4748 }
4849
4950 /**
50- * Reboot kernel
51+ * Reboots the kernel.
5152 *
5253 * Services from the list of persistent services
5354 * are updated from service container before kernel shutdown
@@ -64,8 +65,7 @@ public function rebootKernel(): void
6465 }
6566
6667 $ this ->persistDoctrineConnections ();
67- $ this ->kernel ->boot ();
68- $ this ->kernel ->shutdown ();
68+ $ this ->ensureKernelShutdown ();
6969 $ this ->kernel ->boot ();
7070 $ this ->container = $ this ->getContainer ();
7171
@@ -82,6 +82,22 @@ public function rebootKernel(): void
8282 }
8383 }
8484
85+ protected function ensureKernelShutdown (): void
86+ {
87+ $ this ->kernel ->boot ();
88+ $ container = $ this ->kernel ->getContainer ();
89+
90+ if ($ container ->has ('services_resetter ' )) {
91+ $ container ->get ('services_resetter ' );
92+ }
93+
94+ $ this ->kernel ->shutdown ();
95+
96+ if ($ container instanceof ResetInterface) {
97+ $ container ->reset ();
98+ }
99+ }
100+
85101 private function getContainer (): ?ContainerInterface
86102 {
87103 /** @var ContainerInterface $container */
@@ -120,7 +136,9 @@ private function persistDoctrineConnections(): void
120136 }
121137
122138 $ reflectedContainer = new ReflectionClass ($ publicContainer );
123- $ reflectionTarget = $ reflectedContainer ->hasProperty ('parameters ' ) ? $ publicContainer : $ publicContainer ->getParameterBag ();
139+ $ reflectionTarget = $ reflectedContainer ->hasProperty ('parameters ' )
140+ ? $ publicContainer
141+ : $ publicContainer ->getParameterBag ();
124142
125143 $ reflectedParameters = new ReflectionProperty ($ reflectionTarget , 'parameters ' );
126144 $ reflectedParameters ->setAccessible (true );
0 commit comments