|
| 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