4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\EventDispatcher\Tests\DependencyInjection;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17 use Symfony\Component\DependencyInjection\Reference;
18 use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
20 class RegisterListenersPassTest extends TestCase
23 * Tests that event subscribers not implementing EventSubscriberInterface
24 * trigger an exception.
26 * @expectedException \InvalidArgumentException
28 public function testEventSubscriberWithoutInterface()
30 $builder = new ContainerBuilder();
31 $builder->register('event_dispatcher');
32 $builder->register('my_event_subscriber', 'stdClass')
33 ->addTag('kernel.event_subscriber');
35 $registerListenersPass = new RegisterListenersPass();
36 $registerListenersPass->process($builder);
39 public function testValidEventSubscriber()
42 'my_event_subscriber' => array(0 => array()),
45 $builder = new ContainerBuilder();
46 $eventDispatcherDefinition = $builder->register('event_dispatcher');
47 $builder->register('my_event_subscriber', 'Symfony\Component\EventDispatcher\Tests\DependencyInjection\SubscriberService')
48 ->addTag('kernel.event_subscriber');
50 $registerListenersPass = new RegisterListenersPass();
51 $registerListenersPass->process($builder);
53 $expectedCalls = array(
58 array(new ServiceClosureArgument(new Reference('my_event_subscriber')), 'onEvent'),
63 $this->assertEquals($expectedCalls, $eventDispatcherDefinition->getMethodCalls());
67 * @expectedException \InvalidArgumentException
68 * @expectedExceptionMessage The service "foo" tagged "kernel.event_listener" must not be abstract.
70 public function testAbstractEventListener()
72 $container = new ContainerBuilder();
73 $container->register('foo', 'stdClass')->setAbstract(true)->addTag('kernel.event_listener', array());
74 $container->register('event_dispatcher', 'stdClass');
76 $registerListenersPass = new RegisterListenersPass();
77 $registerListenersPass->process($container);
81 * @expectedException \InvalidArgumentException
82 * @expectedExceptionMessage The service "foo" tagged "kernel.event_subscriber" must not be abstract.
84 public function testAbstractEventSubscriber()
86 $container = new ContainerBuilder();
87 $container->register('foo', 'stdClass')->setAbstract(true)->addTag('kernel.event_subscriber', array());
88 $container->register('event_dispatcher', 'stdClass');
90 $registerListenersPass = new RegisterListenersPass();
91 $registerListenersPass->process($container);
94 public function testEventSubscriberResolvableClassName()
96 $container = new ContainerBuilder();
98 $container->setParameter('subscriber.class', 'Symfony\Component\EventDispatcher\Tests\DependencyInjection\SubscriberService');
99 $container->register('foo', '%subscriber.class%')->addTag('kernel.event_subscriber', array());
100 $container->register('event_dispatcher', 'stdClass');
102 $registerListenersPass = new RegisterListenersPass();
103 $registerListenersPass->process($container);
105 $definition = $container->getDefinition('event_dispatcher');
106 $expectedCalls = array(
111 array(new ServiceClosureArgument(new Reference('foo')), 'onEvent'),
116 $this->assertEquals($expectedCalls, $definition->getMethodCalls());
119 public function testHotPathEvents()
121 $container = new ContainerBuilder();
123 $container->register('foo', SubscriberService::class)->addTag('kernel.event_subscriber', array());
124 $container->register('event_dispatcher', 'stdClass');
126 (new RegisterListenersPass())->setHotPathEvents(array('event'))->process($container);
128 $this->assertTrue($container->getDefinition('foo')->hasTag('container.hot_path'));
132 * @expectedException \InvalidArgumentException
133 * @expectedExceptionMessage You have requested a non-existent parameter "subscriber.class"
135 public function testEventSubscriberUnresolvableClassName()
137 $container = new ContainerBuilder();
138 $container->register('foo', '%subscriber.class%')->addTag('kernel.event_subscriber', array());
139 $container->register('event_dispatcher', 'stdClass');
141 $registerListenersPass = new RegisterListenersPass();
142 $registerListenersPass->process($container);
146 class SubscriberService implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
148 public static function getSubscribedEvents()
151 'event' => 'onEvent',