From 7c34f6e866ceaae45c1ea0b24db897d5f69a9c75 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Tue, 29 Sep 2020 23:35:47 +0200 Subject: [PATCH] [TwigBundle] Only remove kernel exception listener if twig is used --- .../Compiler/ExceptionListenerPass.php | 18 +++-- .../Compiler/ExceptionListenerPassTest.php | 79 +++++++++++++++++++ 2 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/ExceptionListenerPassTest.php diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExceptionListenerPass.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExceptionListenerPass.php index d06b8e8199..c21507dca8 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExceptionListenerPass.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExceptionListenerPass.php @@ -33,15 +33,19 @@ class ExceptionListenerPass implements CompilerPassInterface // register the exception listener only if it's currently used, else use the provided by FrameworkBundle if (null === $container->getParameter('twig.exception_listener.controller') && $container->hasDefinition('exception_listener')) { $container->removeDefinition('twig.exception_listener'); - } else { - $container->removeDefinition('exception_listener'); - if ($container->hasParameter('templating.engines')) { - $engines = $container->getParameter('templating.engines'); - if (!\in_array('twig', $engines, true)) { - $container->removeDefinition('twig.exception_listener'); - } + return; + } + + if ($container->hasParameter('templating.engines')) { + $engines = $container->getParameter('templating.engines'); + if (\in_array('twig', $engines, true)) { + $container->removeDefinition('exception_listener'); + + return; } } + + $container->removeDefinition('twig.exception_listener'); } } diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/ExceptionListenerPassTest.php b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/ExceptionListenerPassTest.php new file mode 100644 index 0000000000..cc7558f6e6 --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/ExceptionListenerPassTest.php @@ -0,0 +1,79 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Symfony\Bundle\TwigBundle\Tests\DependencyInjection\Compiler; + +use PHPUnit\Framework\TestCase; +use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\ExceptionListenerPass; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\Security\Http\Firewall\ExceptionListener; +use Twig\Environment; + +class ExceptionListenerPassTest extends TestCase +{ + public function testExitsWhenTwigIsNotAvailable(): void + { + $builder = new ContainerBuilder(); + $builder->register('exception_listener', ExceptionListener::class); + $builder->register('twig.exception_listener', ExceptionListener::class); + + ($pass = new ExceptionListenerPass())->process($builder); + + $this->assertTrue($builder->hasDefinition('exception_listener')); + $this->assertTrue($builder->hasDefinition('twig.exception_listener')); + } + + public function testRemovesTwigExceptionListenerWhenNoExceptionListenerControllerExists(): void + { + $builder = new ContainerBuilder(); + $builder->register('twig', Environment::class); + $builder->register('exception_listener', ExceptionListener::class); + $builder->register('twig.exception_listener', ExceptionListener::class); + $builder->setParameter('twig.exception_listener.controller', null); + + ($pass = new ExceptionListenerPass())->process($builder); + + $this->assertTrue($builder->hasDefinition('exception_listener')); + $this->assertFalse($builder->hasDefinition('twig.exception_listener')); + } + + public function testRemovesTwigExceptionListenerIfTwigIsNotUsedAsTemplateEngine(): void + { + $builder = new ContainerBuilder(); + $builder->register('twig', Environment::class); + $builder->register('exception_listener', ExceptionListener::class); + $builder->register('twig.exception_listener', ExceptionListener::class); + $builder->setParameter('twig.exception_listener.controller', 'exception_controller::showAction'); + $builder->setParameter('templating.engines', ['php']); + + ($pass = new ExceptionListenerPass())->process($builder); + + $this->assertTrue($builder->hasDefinition('exception_listener')); + $this->assertFalse($builder->hasDefinition('twig.exception_listener')); + } + + public function testRemovesKernelExceptionListenerIfTwigIsUsedAsTemplateEngine(): void + { + $builder = new ContainerBuilder(); + $builder->register('twig', Environment::class); + $builder->register('exception_listener', ExceptionListener::class); + $builder->register('twig.exception_listener', ExceptionListener::class); + $builder->setParameter('twig.exception_listener.controller', 'exception_controller::showAction'); + $builder->setParameter('templating.engines', ['twig']); + + ($pass = new ExceptionListenerPass())->process($builder); + + $this->assertFalse($builder->hasDefinition('exception_listener')); + $this->assertTrue($builder->hasDefinition('twig.exception_listener')); + } +}