-
Notifications
You must be signed in to change notification settings - Fork 23
/
Symfony.php
155 lines (130 loc) · 4.73 KB
/
Symfony.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types=1);
namespace Codeception\Lib\Connector;
use InvalidArgumentException;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use Symfony\Bundle\FrameworkBundle\Test\TestContainer;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelBrowser;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use function array_keys;
use function codecept_debug;
class Symfony extends HttpKernelBrowser
{
private bool $rebootable;
private bool $hasPerformedRequest = false;
private ?ContainerInterface $container;
public array $persistentServices = [];
/**
* Constructor.
*
* @param Kernel $kernel A booted HttpKernel instance
* @param array $services An injected services
* @param bool $rebootable
*/
public function __construct(Kernel $kernel, array $services = [], bool $rebootable = true)
{
parent::__construct($kernel);
$this->followRedirects(true);
$this->rebootable = $rebootable;
$this->persistentServices = $services;
$this->container = $this->getContainer();
$this->rebootKernel();
}
/**
* @param Request $request
* @return Response
*/
protected function doRequest($request): Response
{
if ($this->rebootable) {
if ($this->hasPerformedRequest) {
$this->rebootKernel();
} else {
$this->hasPerformedRequest = true;
}
}
return parent::doRequest($request);
}
/**
* Reboot kernel
*
* Services from the list of persistent services
* are updated from service container before kernel shutdown
* and injected into newly initialized container after kernel boot.
*/
public function rebootKernel(): void
{
if ($this->container) {
foreach (array_keys($this->persistentServices) as $serviceName) {
if ($service = $this->getService($serviceName)) {
$this->persistentServices[$serviceName] = $service;
}
}
}
$this->persistDoctrineConnections();
$this->kernel->reboot(null);
$this->container = $this->getContainer();
foreach ($this->persistentServices as $serviceName => $service) {
try {
$this->container->set($serviceName, $service);
} catch (InvalidArgumentException $e) {
//Private services can't be set in Symfony 4
codecept_debug("[Symfony] Can't set persistent service {$serviceName}: " . $e->getMessage());
}
}
if ($profiler = $this->getProfiler()) {
$profiler->enable();
}
}
private function getContainer(): ?ContainerInterface
{
/** @var ContainerInterface $container */
$container = $this->kernel->getContainer();
if ($container->has('test.service_container')) {
$container = $container->get('test.service_container');
}
return $container;
}
private function getProfiler(): ?Profiler
{
if ($this->container->has('profiler')) {
/** @var Profiler $profiler */
$profiler = $this->container->get('profiler');
return $profiler;
}
return null;
}
private function getService(string $serviceName): ?object
{
if ($this->container->has($serviceName)) {
return $this->container->get($serviceName);
}
return null;
}
private function persistDoctrineConnections(): void
{
if (!$this->container->hasParameter('doctrine.connections')) {
return;
}
if ($this->container instanceof TestContainer) {
$reflectedTestContainer = new ReflectionMethod($this->container, 'getPublicContainer');
$reflectedTestContainer->setAccessible(true);
$publicContainer = $reflectedTestContainer->invoke($this->container);
} else {
$publicContainer = $this->container;
}
$reflectedContainer = new ReflectionClass($publicContainer);
$reflectionTarget = $reflectedContainer->hasProperty('parameters') ? $publicContainer : $publicContainer->getParameterBag();
$reflectedParameters = new ReflectionProperty($reflectionTarget, 'parameters');
$reflectedParameters->setAccessible(true);
$parameters = $reflectedParameters->getValue($reflectionTarget);
unset($parameters['doctrine.connections']);
$reflectedParameters->setValue($reflectionTarget, $parameters);
}
}