-
Notifications
You must be signed in to change notification settings - Fork 7
[Suggestion] provide a way to check valid EventSubscriberInterface implementations #7
Comments
The other solution could be to have this matcher in the core as it would benefit PhpSpec itself as well for instance. However, this could look too framework-specific to be in core (which is why I suggested it here first to avoid requests to include similar matchers in core for any library in the future). |
I think it might be an overstretch of this method's responsibility to verify that these methods are callable / public - surely that is going to be covered by other tests? |
@ciaranmcnulty if you don't write a spec yet for other methods of the class, you would have something passing by defining a private or an abstract method, while getting a fatal error when dispatching the event in your code. The expectation for an event subscriber is that the dispatcher is able to call the method, not only that the method exists. Otherwise, you would not be catching errors where you used the name of your private method instead of the name of the proper listener when both exist in the class. Note that I'm not trying to call the listener method itself. The fact that the listener method works should indeed be covered by other tests (I cannot call it anyway, as most listeners will not accept the base |
I see your point, I guess it's more that I think you're imposing false ignorance of what the rest of the class is going to do... For me I'd be happy to do:
In the example you've provided getSubscribedEvents could return an empty array and the test would not catch it. Or it could return an array handling events |
@ciaranmcnulty Asserting on which event you subscribe is a different expectation, which should appear on its own in the spec. It is part of your expectation of the class, not of the expectation of it being a valid implementation of the $this->getSubscribedEvents()->shouldReturn(['foo'=>'handleFoo']); will not test at all that your |
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:This implementation is incomplete:
0
for the method should check the index1
for 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 ?)
The text was updated successfully, but these errors were encountered: