Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[EventDispatcher] Add missing checks to RegisterListenersPass #13293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ public function process(ContainerBuilder $container)
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event subscribers are lazy-loaded.', $id));
}

if ($def->isAbstract()) {
throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as event subscribers are lazy-loaded.', $id));
}

// We must assume that the class value has been correctly filled, even if the service is created by a factory
$class = $def->getClass();
$class = $container->getParameterBag()->resolveValue($def->getClass());

$refClass = new \ReflectionClass($class);
$interface = 'Symfony\Component\EventDispatcher\EventSubscriberInterface';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,58 @@ public function testAbstractEventListener()
$registerListenersPass = new RegisterListenersPass();
$registerListenersPass->process($container);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The service "foo" must not be abstract as event subscribers are lazy-loaded.
*/
public function testAbstractEventSubscriber()
{
$container = new ContainerBuilder();
$container->register('foo', 'stdClass')->setAbstract(true)->addTag('kernel.event_subscriber', array());
$container->register('event_dispatcher', 'stdClass');

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

public function testEventSubscriberResolvableClassName()
{
$container = new ContainerBuilder();

$container->setParameter('subscriber.class', 'Symfony\Component\EventDispatcher\Tests\DependencyInjection\SubscriberService');
$container->register('foo', '%subscriber.class%')->addTag('kernel.event_subscriber', array());
$container->register('event_dispatcher', 'stdClass');

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

$definition = $container->getDefinition('event_dispatcher');
$expected_calls = array(
array(
'addSubscriberService',
array(
'foo',
'Symfony\Component\EventDispatcher\Tests\DependencyInjection\SubscriberService',
),
),
);
$this->assertSame($expected_calls, $definition->getMethodCalls());
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage You have requested a non-existent parameter "subscriber.class"
*/
public function testEventSubscriberUnresolvableClassName()
{
$container = new ContainerBuilder();
$container->register('foo', '%subscriber.class%')->addTag('kernel.event_subscriber', array());
$container->register('event_dispatcher', 'stdClass');

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

class SubscriberService implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expand Down