Skip to content

Commit 46c7666

Browse files
authored
Added a fix for symfony 5.3 container. (#593)
* Added a fix for symfony 5.3 container. * Travis should be happy with php8.0+symfony5.3
1 parent e37469d commit 46c7666

File tree

7 files changed

+99
-27
lines changed

7 files changed

+99
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Removed
66
* Drop support of `run:paratest`, if you still need it, define the [`paratest:run` command](https://github.com/liip/LiipFunctionalTestBundle/blob/c732089d9ad32372db4cbee1a5a3c3b53bd40ff6/src/Command/RunParatestCommand.php) in your project
7+
* Deprecated usage of `environment` property. Use the `static::$env` property instead.
78

89
## 3.0
910
This new major version introduces a number of breaking changes; see the [upgrade guide](UPGRADE-3.0.md) for more details.

src/Test/WebTestCase.php

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Component\DependencyInjection\ResettableContainerInterface;
2626
use Symfony\Component\DomCrawler\Crawler;
2727
use Symfony\Component\HttpFoundation\Response;
28+
use Symfony\Component\HttpKernel\KernelInterface;
2829
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2930
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
3031
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
@@ -40,10 +41,11 @@ class_alias(KernelBrowser::class, Client::class);
4041
* @author Benjamin Eberlei <[email protected]>
4142
*
4243
* @method ContainerInterface getContainer()
44+
* @property string $environment;
4345
*/
4446
abstract class WebTestCase extends BaseWebTestCase
4547
{
46-
protected $environment = 'test';
48+
protected static $env = 'test';
4749

4850
protected $containers;
4951

@@ -70,7 +72,7 @@ abstract class WebTestCase extends BaseWebTestCase
7072
*/
7173
protected function getServiceMockBuilder(string $id): MockBuilder
7274
{
73-
$service = $this->getDependencyInjectionContainer()->get($id);
75+
$service = $this->getContainer()->get($id);
7476
$class = get_class($service);
7577

7678
return $this->getMockBuilder($class)->disableOriginalConstructor();
@@ -83,13 +85,13 @@ protected function runCommand(string $name, array $params = [], bool $reuseKerne
8385
{
8486
if (!$reuseKernel) {
8587
if (null !== static::$kernel) {
86-
static::$kernel->shutdown();
88+
static::ensureKernelShutdown();
8789
}
8890

89-
$kernel = static::$kernel = static::createKernel(['environment' => $this->environment]);
91+
$kernel = static::bootKernel(['environment' => self::$env]);
9092
$kernel->boot();
9193
} else {
92-
$kernel = $this->getDependencyInjectionContainer()->get('kernel');
94+
$kernel = $this->getContainer()->get('kernel');
9395
}
9496

9597
$application = new Application($kernel);
@@ -129,7 +131,7 @@ protected function getVerbosityLevel(): int
129131
// If `null`, is not yet set
130132
if (null === $this->verbosityLevel) {
131133
// 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'));
133135
$verbosity = '\Symfony\Component\Console\Output\StreamOutput::VERBOSITY_'.$level;
134136

135137
$this->verbosityLevel = constant($verbosity);
@@ -184,7 +186,7 @@ protected function getDecorated(): bool
184186
{
185187
if (null === $this->decorated) {
186188
// 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');
188190
}
189191

190192
// Check the local decorated flag
@@ -204,12 +206,12 @@ public function isDecorated(bool $decorated): void
204206
* Get an instance of the dependency injection container.
205207
* (this creates a kernel *without* parameters).
206208
*/
207-
protected function getDependencyInjectionContainer(): ContainerInterface
209+
private function getDependencyInjectionContainer(): ContainerInterface
208210
{
209-
$cacheKey = $this->environment;
211+
$cacheKey = self::$env;
210212
if (empty($this->containers[$cacheKey])) {
211213
$kernel = static::createKernel([
212-
'environment' => $this->environment,
214+
'environment' => self::$env,
213215
]);
214216
$kernel->boot();
215217

@@ -224,22 +226,60 @@ protected function getDependencyInjectionContainer(): ContainerInterface
224226
return $this->containers[$cacheKey];
225227
}
226228

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+
227238
/**
228239
* Keep support of Symfony < 5.3.
229240
*/
230241
public function __call(string $name, $arguments)
231242
{
232243
if ('getContainer' === $name) {
233-
if (method_exists($this, $name)) {
234-
return self::getContainer();
235-
}
236-
237244
return $this->getDependencyInjectionContainer();
238245
}
239246

240247
throw new \Exception("Method {$name} is not supported.");
241248
}
242249

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+
243283
/**
244284
* Creates an instance of a lightweight Http client.
245285
*
@@ -261,9 +301,9 @@ protected function makeClient(array $params = []): Client
261301
*/
262302
protected function makeAuthenticatedClient(array $params = []): Client
263303
{
264-
$username = $this->getDependencyInjectionContainer()
304+
$username = $this->getContainer()
265305
->getParameter('liip_functional_test.authentication.username');
266-
$password = $this->getDependencyInjectionContainer()
306+
$password = $this->getContainer()
267307
->getParameter('liip_functional_test.authentication.password');
268308

269309
return $this->createClientWithParams($params, $username, $password);
@@ -313,7 +353,7 @@ protected function createUserToken(UserInterface $user, string $firewallName): T
313353
*/
314354
protected function getUrl(string $route, array $params = [], int $absolute = UrlGeneratorInterface::ABSOLUTE_PATH): string
315355
{
316-
return $this->getDependencyInjectionContainer()->get('router')->generate($route, $params, $absolute);
356+
return $this->getContainer()->get('router')->generate($route, $params, $absolute);
317357
}
318358

319359
/**
@@ -452,7 +492,11 @@ protected function createClientWithParams(array $params, ?string $username = nul
452492
]);
453493
}
454494

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);
456500

457501
if ($this->firewallLogins) {
458502
// has to be set otherwise "hasPreviousSession" in Request returns false.

tests/App/Resources/views/layout.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<body>
1111
<p id="user">
12-
{%- if (app.user is null) -%}
12+
{%- if not is_granted('IS_AUTHENTICATED_REMEMBERED') -%}
1313
Not logged in.
1414
{%- else -%}
1515
Logged in as {{ app.user.username }}.

tests/App/config.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ framework:
77
router: { resource: "%kernel.project_dir%/routing.yml" }
88
form: true
99
csrf_protection: true
10+
property_access: ~
1011
session:
1112
# handler_id set to null will use default session handler from php.ini
1213
handler_id: ~
@@ -66,7 +67,7 @@ security:
6667
firewalls:
6768
secured_area:
6869
pattern: ^/
69-
anonymous: true
70+
anonymous: lazy
7071
http_basic:
7172
realm: "Admin Area"
7273
provider: chain_provider
@@ -86,5 +87,7 @@ services:
8687
Liip\Acme\Tests\App\Controller\DefaultController:
8788
tags: ['controller.service_arguments']
8889

90+
Symfony\Component\DependencyInjection\ContainerInterface: '@service_container'
91+
8992
twig:
9093
default_path: '%kernel.project_dir%/Resources/views/'

tests/Command/CommandTest.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,17 @@ public function testRunCommandWithInputs(): void
7070
$this->assertStringContainsString('Value of answer: AcmeDemoBundle', $this->commandTester->getDisplay());
7171
}
7272

73-
public function testRunCommandWithoutOptionsAndNotReuseKernel(): void
73+
/**
74+
* @dataProvider useEnvProvider
75+
*/
76+
public function testRunCommandWithoutOptionsAndNotReuseKernel(bool $useEnv): void
7477
{
78+
if ($useEnv) {
79+
self::$env = 'test';
80+
} else {
81+
$this->environment = 'test';
82+
}
83+
7584
// Run command without options
7685
$this->commandTester = $this->runCommand('liipfunctionaltestbundle:test');
7786

@@ -85,8 +94,15 @@ public function testRunCommandWithoutOptionsAndNotReuseKernel(): void
8594
$this->assertIsBool($this->getDecorated());
8695
$this->assertTrue($this->getDecorated());
8796

88-
// Run command and not reuse kernel
89-
$this->environment = 'prod';
97+
// Run command and reuse kernel
98+
if ($useEnv) {
99+
self::$env = 'prod';
100+
} else {
101+
$this->environment = 'prod';
102+
}
103+
104+
self::ensureKernelShutdown();
105+
$this->getContainer();
90106
$this->commandTester = $this->runCommand('liipfunctionaltestbundle:test', [], true);
91107

92108
$this->assertInstanceOf(CommandTester::class, $this->commandTester);
@@ -95,6 +111,14 @@ public function testRunCommandWithoutOptionsAndNotReuseKernel(): void
95111
$this->assertStringContainsString('Verbosity level: NORMAL', $this->commandTester->getDisplay());
96112
}
97113

114+
public function useEnvProvider(): array
115+
{
116+
return [
117+
[true],
118+
[false],
119+
];
120+
}
121+
98122
public function testRunCommandWithoutDecoration(): void
99123
{
100124
// Set `decorated` to false

tests/Test/WebTestCaseConfigTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ public function testIndexAuthenticationLoginAs(): void
150150
*/
151151
public function testIndexAuthenticationLoginClient(): void
152152
{
153-
$this->client = static::makeClient();
154-
155153
$this->schemaUpdate();
156154
$user = $this->loadTestFixtures();
157155

156+
$this->client = static::makeClient();
157+
158158
$this->loginClient($this->client, $user, 'secured_area');
159159

160160
$path = '/';

tests/Traits/LiipAcmeFixturesTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ trait LiipAcmeFixturesTrait
2222
public function schemaUpdate(): void
2323
{
2424
// Create database
25-
$kernel = $this->getDependencyInjectionContainer()->get('kernel');
25+
$kernel = $this->getContainer()->get('kernel');
2626

2727
$application = new Application($kernel);
2828

@@ -46,7 +46,7 @@ public function loadTestFixtures(): User
4646
$user1->setEnabled(true);
4747
$user1->setConfirmationToken(null);
4848

49-
$manager = $this->getDependencyInjectionContainer()->get('doctrine')->getManager();
49+
$manager = $this->getContainer()->get('doctrine')->getManager();
5050
$manager->persist($user1);
5151

5252
$user2 = clone $user1;

0 commit comments

Comments
 (0)