Add priority for event subscribers
Currently subscribers don't have priority, so their priority depends on the order the extensions were added in the configuration, or the order the subscribers are added within the extension.
What do you think to add a way to define a priority?
There are two possibilities: create an interface PrioritizedSubscriber or add a new int $priority parameter to the method Runner\Extension\Facade::registerSubscriber()
Could you please explain why you think you need prioritised subscribers?
From the perspective of a PHPUnit extension, the event system is consume-only:
- Events are immutable
- Extensions cannot emit their own events
- Extensions cannot change the outcome of test execution, etc.
Therefore, the order in which subscribers of an event are notified should not matter.
yes but extensions can have extra side effects
for instance, in https://github.com/zenstruck/foundry we "reset" the database (ie: truncate) before the tests and we're planning to do it relying on a PHPUnit subscriber.
But we want to be compatible with https://github.com/dmaicher/doctrine-test-bundle (which also relies on PHPUnit subscribers). And having priorities would ease this compatibility.
Thank you for explaining.
I am not convinced that subscriber priorities can always solve the problem you describe. When different extensions subscribe to the same event using the same priority, we are back to square one. This could be solved by letting PHPUnit assign priorities based on the order in which the extensions are configured in the XML configuration file. I am not yet convinced that this is worth the additional complexity.
Since then, I have realised that we already have subscriber priorities of a sort. Extensions are registered in the order in which they appear in the PHPUnit XML configuration file. If one extension must be registered before another, simply change their order in the file.