You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 14, 2021. It is now read-only.
The EventSubscriberInterface asks you to return an array of events for which the class wants to listen. Validating the proper implementation of this interface is a bit complex. Here is an example doing part of it:
<?phpnamespacespec\Acme\DemoBundle\Activity;
usePhpSpec\Exception\Example\FailureException;
usePhpSpec\ObjectBehavior;
useProphecy\Argument;
class MyListenerSpec extends ObjectBehavior
{
functionit_is_an_event_subscriber()
{
$this->shouldHaveType('Symfony\Component\EventDispatcher\EventSubscriberInterface');
$this->getSubscribedEvents()->shouldBeArray();
foreach ($this->getSubscribedEvents()->getWrappedObject() as$event => $listeners) {
if (is_string($listeners)) {
$this->shouldHaveListenerMethod($listeners);
continue;
}
if (!is_array($listeners)) {
thrownewFailureException(sprintf('The listener registered for %s must be a string or an array of listeners', $event));
}
if (is_string($listeners[0])) {
$this->shouldHaveListenerMethod($listeners[0]);
continue;
}
foreach ($listenersas$i => $listener) {
if (!is_array($listener)) {
thrownewFailureException(sprintf('The listener %s registered for %s must be an array (method, priority)', $i, $event));
}
$this->shouldHaveListenerMethod($listener[0]);
}
}
}
publicfunctiongetMatchers()
{
returnarray(
'haveListenerMethod' => function ($subject, $method) {
if (!method_exists($subject, $method)) {
returnfalse;
}
$refl = new \ReflectionMethod($subject, $method);
return$refl->isPublic() && !$refl->isAbstract();
}
);
}
}
This implementation is incomplete:
it does not verify that the priority is returned properly in cases where it is expected (both cases where I access the index 0 for the method should check the index 1 for an integer)
it misses some checks for missing indexes in arrays
it is not implemented as a matcher on its own
This implementation most checks that the registered method actually exist (which avoids mistakes like doing a typo in the method name)
Having to repeat this complex validation in each subscriber spec would be a huge pain, which is why I would prefer to register it as a matcher and write something like $this->shouldBeAnEventSubscriber() or something like that.
Do you think such matcher could fit in the Symfony2Extension ? Or is the extension aimed at integrating with the fullstack and component-specific matchers should better go in a separate extension easier to use separately ? I haven't tried to use the Symfony2Extension yet in my project, so I don't know how intrusive it is (i.e. is it possible to register it in a project not using the fullstack to benefit from such a matcher ?)
The
EventSubscriberInterfaceasks you to return an array of events for which the class wants to listen. Validating the proper implementation of this interface is a bit complex. Here is an example doing part of it:This implementation is incomplete:
0for the method should check the index1for an integer)This implementation most checks that the registered method actually exist (which avoids mistakes like doing a typo in the method name)
Having to repeat this complex validation in each subscriber spec would be a huge pain, which is why I would prefer to register it as a matcher and write something like
$this->shouldBeAnEventSubscriber()or something like that.Do you think such matcher could fit in the Symfony2Extension ? Or is the extension aimed at integrating with the fullstack and component-specific matchers should better go in a separate extension easier to use separately ? I haven't tried to use the Symfony2Extension yet in my project, so I don't know how intrusive it is (i.e. is it possible to register it in a project not using the fullstack to benefit from such a matcher ?)