|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Component\HttpKernel\Tests\DependencyInjection; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Symfony\Component\DependencyInjection\Argument\IteratorArgument; |
| 7 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 8 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 9 | +use Symfony\Component\DependencyInjection\Reference; |
| 10 | +use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass; |
| 11 | +use Symfony\Component\HttpKernel\EventListener\ServiceResetListener; |
| 12 | +use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableService; |
| 13 | +use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService; |
| 14 | + |
| 15 | +class ResettableServicePassTest extends TestCase |
| 16 | +{ |
| 17 | + public function testCompilerPass() |
| 18 | + { |
| 19 | + $container = new ContainerBuilder(); |
| 20 | + $container->register('one', ResettableService::class) |
| 21 | + ->addTag('kernel.reset', array('method' => 'reset')); |
| 22 | + $container->register('two', ClearableService::class) |
| 23 | + ->addTag('kernel.reset', array('method' => 'clear')); |
| 24 | + |
| 25 | + $container->register(ServiceResetListener::class) |
| 26 | + ->setArguments(array(null, array())); |
| 27 | + $container->addCompilerPass(new ResettableServicePass('kernel.reset')); |
| 28 | + |
| 29 | + $container->compile(); |
| 30 | + |
| 31 | + $definition = $container->getDefinition(ServiceResetListener::class); |
| 32 | + |
| 33 | + $this->assertEquals( |
| 34 | + array( |
| 35 | + new IteratorArgument(array( |
| 36 | + 'one' => new Reference('one', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE), |
| 37 | + 'two' => new Reference('two', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE), |
| 38 | + )), |
| 39 | + array( |
| 40 | + 'one' => 'reset', |
| 41 | + 'two' => 'clear', |
| 42 | + ), |
| 43 | + ), |
| 44 | + $definition->getArguments() |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException |
| 50 | + * @expectedExceptionMessage Tag kernel.reset requires the "method" attribute to be set. |
| 51 | + */ |
| 52 | + public function testMissingMethod() |
| 53 | + { |
| 54 | + $container = new ContainerBuilder(); |
| 55 | + $container->register(ResettableService::class) |
| 56 | + ->addTag('kernel.reset'); |
| 57 | + $container->register(ServiceResetListener::class) |
| 58 | + ->setArguments(array(null, array())); |
| 59 | + $container->addCompilerPass(new ResettableServicePass('kernel.reset')); |
| 60 | + |
| 61 | + $container->compile(); |
| 62 | + } |
| 63 | + |
| 64 | + public function testCompilerPassWithoutResetters() |
| 65 | + { |
| 66 | + $container = new ContainerBuilder(); |
| 67 | + $container->register(ServiceResetListener::class) |
| 68 | + ->setArguments(array(null, array())); |
| 69 | + $container->addCompilerPass(new ResettableServicePass()); |
| 70 | + |
| 71 | + $container->compile(); |
| 72 | + |
| 73 | + $this->assertFalse($container->has(ServiceResetListener::class)); |
| 74 | + } |
| 75 | + |
| 76 | + public function testCompilerPassWithoutListener() |
| 77 | + { |
| 78 | + $container = new ContainerBuilder(); |
| 79 | + $container->addCompilerPass(new ResettableServicePass()); |
| 80 | + |
| 81 | + $container->compile(); |
| 82 | + |
| 83 | + $this->assertFalse($container->has(ServiceResetListener::class)); |
| 84 | + } |
| 85 | +} |
0 commit comments