Skip to content

Commit

Permalink
[EventDispatcher] [EventDispatcher] Throw exception when listener met…
Browse files Browse the repository at this point in the history
…hod cannot be resolved
  • Loading branch information
nikophil committed Jun 27, 2023
1 parent 1df20e4 commit 8ef62e1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
6 changes: 5 additions & 1 deletion DependencyInjection/RegisterListenersPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ public function process(ContainerBuilder $container)
], function ($matches) { return strtoupper($matches[0]); }, $event['event']);
$event['method'] = preg_replace('/[^a-z0-9]/i', '', $event['method']);

if (null !== ($class = $container->getDefinition($id)->getClass()) && ($r = $container->getReflectionClass($class, false)) && !$r->hasMethod($event['method']) && $r->hasMethod('__invoke')) {
if (null !== ($class = $container->getDefinition($id)->getClass()) && ($r = $container->getReflectionClass($class, false)) && !$r->hasMethod($event['method'])) {
if (!$r->hasMethod('__invoke')) {
throw new InvalidArgumentException(sprintf('None of the "%s" or "__invoke" methods exist for the service "foo". Please define the "method" attribute on "kernel.event_listener" tags.', $event['method'], $id, $this->listenerTag));
}

$event['method'] = '__invoke';
}
}
Expand Down
28 changes: 26 additions & 2 deletions Tests/DependencyInjection/RegisterListenersPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,20 @@ public function testEventSubscriberUnresolvableClassName()
public function testInvokableEventListener()
{
$container = new ContainerBuilder();
$container->register('foo', \stdClass::class)->addTag('kernel.event_listener', ['event' => 'foo.bar']);
$container->setParameter('event_dispatcher.event_aliases', [AliasedEvent::class => 'aliased_event']);

$container->register('foo', \get_class(new class() {
public function onFooBar()
{
}
}))->addTag('kernel.event_listener', ['event' => 'foo.bar']);
$container->register('bar', InvokableListenerService::class)->addTag('kernel.event_listener', ['event' => 'foo.bar']);
$container->register('baz', InvokableListenerService::class)->addTag('kernel.event_listener', ['event' => 'event']);
$container->register('zar', \stdClass::class)->addTag('kernel.event_listener', ['event' => 'foo.bar_zar']);
$container->register('zar', \get_class(new class() {
public function onFooBarZar()
{
}
}))->addTag('kernel.event_listener', ['event' => 'foo.bar_zar']);
$container->register('event_dispatcher', \stdClass::class);

$registerListenersPass = new RegisterListenersPass();
Expand Down Expand Up @@ -247,6 +257,20 @@ public function testInvokableEventListener()
$this->assertEquals($expectedCalls, $definition->getMethodCalls());
}

public function testItThrowsAnExceptionIfTagIsMissingMethodAndClassHasNoValidMethod()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('None of the "onFooBar" or "__invoke" methods exist for the service "foo". Please define the "method" attribute on "kernel.event_listener" tags.');

$container = new ContainerBuilder();

$container->register('foo', \stdClass::class)->addTag('kernel.event_listener', ['event' => 'foo.bar']);
$container->register('event_dispatcher', \stdClass::class);

$registerListenersPass = new RegisterListenersPass();
$registerListenersPass->process($container);
}

/**
* @requires PHP 8
*/
Expand Down

0 comments on commit 8ef62e1

Please sign in to comment.