|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\TwigBundle\Tests\DependencyInjection\Compiler; |
| 15 | + |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\ExceptionListenerPass; |
| 18 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 19 | +use Symfony\Component\Security\Http\Firewall\ExceptionListener; |
| 20 | +use Twig\Environment; |
| 21 | + |
| 22 | +class ExceptionListenerPassTest extends TestCase |
| 23 | +{ |
| 24 | + public function testExitsWhenTwigIsNotAvailable(): void |
| 25 | + { |
| 26 | + $builder = new ContainerBuilder(); |
| 27 | + $builder->register('exception_listener', ExceptionListener::class); |
| 28 | + $builder->register('twig.exception_listener', ExceptionListener::class); |
| 29 | + |
| 30 | + ($pass = new ExceptionListenerPass())->process($builder); |
| 31 | + |
| 32 | + $this->assertTrue($builder->hasDefinition('exception_listener')); |
| 33 | + $this->assertTrue($builder->hasDefinition('twig.exception_listener')); |
| 34 | + } |
| 35 | + |
| 36 | + public function testRemovesTwigExceptionListenerWhenNoExceptionListenerControllerExists(): void |
| 37 | + { |
| 38 | + $builder = new ContainerBuilder(); |
| 39 | + $builder->register('twig', Environment::class); |
| 40 | + $builder->register('exception_listener', ExceptionListener::class); |
| 41 | + $builder->register('twig.exception_listener', ExceptionListener::class); |
| 42 | + $builder->setParameter('twig.exception_listener.controller', null); |
| 43 | + |
| 44 | + ($pass = new ExceptionListenerPass())->process($builder); |
| 45 | + |
| 46 | + $this->assertTrue($builder->hasDefinition('exception_listener')); |
| 47 | + $this->assertFalse($builder->hasDefinition('twig.exception_listener')); |
| 48 | + } |
| 49 | + |
| 50 | + public function testRemovesTwigExceptionListenerIfTwigIsNotUsedAsTemplateEngine(): void |
| 51 | + { |
| 52 | + $builder = new ContainerBuilder(); |
| 53 | + $builder->register('twig', Environment::class); |
| 54 | + $builder->register('exception_listener', ExceptionListener::class); |
| 55 | + $builder->register('twig.exception_listener', ExceptionListener::class); |
| 56 | + $builder->setParameter('twig.exception_listener.controller', 'exception_controller::showAction'); |
| 57 | + $builder->setParameter('templating.engines', ['php']); |
| 58 | + |
| 59 | + ($pass = new ExceptionListenerPass())->process($builder); |
| 60 | + |
| 61 | + $this->assertTrue($builder->hasDefinition('exception_listener')); |
| 62 | + $this->assertFalse($builder->hasDefinition('twig.exception_listener')); |
| 63 | + } |
| 64 | + |
| 65 | + public function testRemovesKernelExceptionListenerIfTwigIsUsedAsTemplateEngine(): void |
| 66 | + { |
| 67 | + $builder = new ContainerBuilder(); |
| 68 | + $builder->register('twig', Environment::class); |
| 69 | + $builder->register('exception_listener', ExceptionListener::class); |
| 70 | + $builder->register('twig.exception_listener', ExceptionListener::class); |
| 71 | + $builder->setParameter('twig.exception_listener.controller', 'exception_controller::showAction'); |
| 72 | + $builder->setParameter('templating.engines', ['twig']); |
| 73 | + |
| 74 | + ($pass = new ExceptionListenerPass())->process($builder); |
| 75 | + |
| 76 | + $this->assertFalse($builder->hasDefinition('exception_listener')); |
| 77 | + $this->assertTrue($builder->hasDefinition('twig.exception_listener')); |
| 78 | + } |
| 79 | +} |
0 commit comments