Skip to content

Commit 9ecad0a

Browse files
gdmfxNaktibalda
authored andcommitted
[Symfony] Fix "already initialized service", "reboot kernel issue" (#5262)
I have an issue. When i work with DataFactory and Doctrine2 modules, and when my testing services uses EntityManager - the instances of entity managers are different. Therefore DataFactory EntityManager knows about created entities through have() method. But my EntityManager is not. Also Doctrine2 cleanup service is not working, because all of them (DataFactory, Doctrine2, my service) use different connections and transactions. Because of this i have alot of bugs during testing. I have investigated the issue. The reason is - codeception Symfony module rebooted the kernel and on first request services that was in kernel's container are lost. But this kernel's container still uses for App services. There is code for persistent services, but, this is not works for first request. Because of Symfony module $this->container is empty, and kernel's container services are not saving. I'm suggest initialize $this->container variable from kernel first, and then perform rebootKernel method. Thus all persistent services from initial kernel can be saved and re-seted. This patch works and all bugs are gone. I spend a lot of time on these bugs. Now works DataFactory and Doctrine2 "cleanup" options together perfect. Also i had another one bug. This is described in Codeception/Codeception#4806. I'm suggest check that service is already initialized by perform "initialize" method. It works perfect. Also can i know why need reboot kernel on first request in Symfony module contructor?
1 parent 2df6d18 commit 9ecad0a

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/Codeception/Lib/Connector/Symfony.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function __construct(\Symfony\Component\HttpKernel\Kernel $kernel, array
3636
$this->followRedirects(true);
3737
$this->rebootable = (boolean)$rebootable;
3838
$this->persistentServices = $services;
39+
$this->container = $this->kernel->getContainer();
3940
$this->rebootKernel();
4041
}
4142

@@ -76,7 +77,9 @@ public function rebootKernel()
7677
$this->container = $this->kernel->getContainer();
7778

7879
foreach ($this->persistentServices as $serviceName => $service) {
79-
$this->container->set($serviceName, $service);
80+
if (!$this->container->initialized($serviceName)) {
81+
$this->container->set($serviceName, $service);
82+
}
8083
}
8184

8285
if ($this->container->has('profiler')) {

0 commit comments

Comments
 (0)