Skip to content

Commit 95ea67a

Browse files
committed
bug #69 fix: use the right argument index for Authorization cookie lifetime (chalasr)
This PR was merged into the 0.3.x-dev branch. Discussion ---------- fix: use the right argument index for `Authorization` cookie lifetime Spotted in #68. Also embeds #68 (with test case) Commits ------- 561a376 fix: use the right argument index for `Authorization` cookie lifetime
2 parents 1c04180 + 561a376 commit 95ea67a

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": ">=7.1.3",
2020
"lcobucci/jwt": "^3.4|^4.0",
2121
"symfony/config": "^4.4|^5.0|^6.0",
22-
"symfony/dependency-injection": "^4.4|^5.0|^6.0",
22+
"symfony/dependency-injection": "^4.4|^5.4|^6.0",
2323
"symfony/http-kernel": "^4.4|^5.0|^6.0",
2424
"symfony/mercure": "^0.6.1",
2525
"symfony/web-link": "^4.4|^5.0|^6.0"

src/MercureBundle.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@ public function build(ContainerBuilder $container): void
3131
$container->addCompilerPass(new class() implements CompilerPassInterface {
3232
public function process(ContainerBuilder $container): void
3333
{
34+
if (!$container->hasDefinition(Authorization::class)) {
35+
return;
36+
}
37+
3438
$definition = $container->getDefinition(Authorization::class);
3539
if (
3640
null === $definition->getArgument(1) &&
3741
$container->hasParameter('session.storage.options')
3842
) {
3943
$definition->setArgument(
40-
2,
44+
1,
4145
$container->getParameter('session.storage.options')['cookie_lifetime'] ?? null
4246
);
4347
}

tests/MercureBundleTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Mercure Component project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Symfony\Bundle\MercureBundle\Tests;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\Bundle\MercureBundle\DependencyInjection\MercureExtension;
18+
use Symfony\Bundle\MercureBundle\MercureBundle;
19+
use Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass;
20+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
21+
use Symfony\Component\DependencyInjection\Compiler\InlineServiceDefinitionsPass;
22+
use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass;
23+
use Symfony\Component\DependencyInjection\ContainerBuilder;
24+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
25+
use Symfony\Component\Mercure\Authorization;
26+
27+
class MercureBundleTest extends TestCase
28+
{
29+
public function testBuildSetsAuthorizationCookieLifetime(): void
30+
{
31+
$config = [
32+
'mercure' => [
33+
'hubs' => [
34+
'default' => [
35+
'url' => 'https://demo.mercure.rocks/hub',
36+
'jwt' => 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.HB0k08BaV8KlLZ3EafCRlTDGbkd9qdznCzJQ_l8ELTU',
37+
],
38+
],
39+
],
40+
];
41+
42+
$container = new ContainerBuilder(new ParameterBag([
43+
'kernel.debug' => false,
44+
'session.storage.options' => ['cookie_lifetime' => 60],
45+
]));
46+
47+
(new MercureExtension())->load($config, $container);
48+
(new MercureBundle())->build($container);
49+
50+
// prevent unused services removal/inlining and missing optional services errors
51+
$container->getCompilerPassConfig()->setRemovingPasses(array_filter($container->getCompilerPassConfig()->getRemovingPasses(), function (CompilerPassInterface $pass) {
52+
return !(
53+
$pass instanceof RemoveUnusedDefinitionsPass ||
54+
$pass instanceof CheckExceptionOnInvalidReferenceBehaviorPass ||
55+
$pass instanceof InlineServiceDefinitionsPass
56+
);
57+
}));
58+
59+
$container->compile();
60+
61+
$this->assertSame(60, $container->getDefinition(Authorization::class)->getArgument(1));
62+
}
63+
64+
public function testBuildSkipsSettingAuthorizationCookieLifetimeIfNotWired(): void
65+
{
66+
$config = ['mercure' => ['hubs' => []]];
67+
68+
$container = new ContainerBuilder(new ParameterBag([
69+
'kernel.debug' => false,
70+
]));
71+
72+
(new MercureExtension())->load($config, $container);
73+
(new MercureBundle())->build($container);
74+
75+
// prevent unused services removal/inlining and missing optional services errors
76+
$container->getCompilerPassConfig()->setRemovingPasses(array_filter($container->getCompilerPassConfig()->getRemovingPasses(), function (CompilerPassInterface $pass) {
77+
return !(
78+
$pass instanceof RemoveUnusedDefinitionsPass ||
79+
$pass instanceof CheckExceptionOnInvalidReferenceBehaviorPass ||
80+
$pass instanceof InlineServiceDefinitionsPass
81+
);
82+
}));
83+
84+
$container->compile();
85+
86+
$this->assertFalse($container->hasDefinition(Authorization::class));
87+
}
88+
}

0 commit comments

Comments
 (0)