-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Event System for extending PHPUnit #4676
Comments
Hi Sebastian, Factory methods for value objects of TestSuite and TestCase classes make TestCase and TestSuite non-extensible. phpunit/src/Event/Value/TestSuite/TestSuite.php Lines 55 to 145 in 1c56cf3
phpunit/src/Event/Value/Test/TestMethod.php Lines 43 to 67 in 1c56cf3
Would it be possible to implement mechanism for extending class to provide factory method for value objects? |
Please explain why you want to extend these classes. |
No matter if hook, or event. I don't think that any of this can help for situations like #4960 And should not be used as an excuse to close feature-requests. |
@sebastianbergmann this closing means that the event system is now stable? I saw that you heavily refactored it, creating a distinction between outcomes and other events (good choice!), is that permanent now? Can we start building on top of that? |
I have closed this issue because the initial work has been completed by @theseer and @localheinz a long time ago and their work has been merged a long time ago. Since then, with the exception of #5040, everything in PHPUnit itself has been migrated from I am reluctant to call the current situation "stable", though, and cannot guarantee that there will be no major changes until we approach a release. |
Shall we still use hooks in the meantime? I didn't find documentation regarding the new event system, if I understand correctly
It's a bit annoying to build something new when we know it is deprecated but it seems that is the way to go |
The In the meantime, you can look at, for instance, these examples: |
Okay, thank you for those information! |
@sebastianbergmann you mentioned that the new event system would be documented before 10 was released. I see some documentation here: https://phpunit.readthedocs.io/en/10.0/extending-phpunit.html#phpunit-s-event-system However, it seems to be missing the basic example of how to use these new events. How does one refactor:
I'm guessing it has something to do with:
But the implementation is not clear to me. They don't look like attributes... so? |
The documentation has not been updated for PHPUnit 10 yet. The JUnit logger is a good starting point to see how events are used. |
okay, i'll have a look at JUnit logger EDIT: this write-up is very helpful, for anyone else who sees this: https://localheinz.com/articles/2023/02/14/extending-phpunit-with-its-new-event-system/ |
I think it is safe to say that this is a topic for 90% of us because of removed functionality that we relied upon. This will probably help too https://docs.phpunit.de/en/10.3/extending-phpunit.html#extending-the-test-runner |
As for me the biggest issue with New Event System, is that event is immutable and I cannot alter results. Example:
and fortunately or unfortunately... it's impossible. |
@oleg-andreyev what you probably want to try is Paraunit (mine) or Paratest. |
History and Background
The
TestListener
InterfaceFirst, there was the
PHPUnit\Framework\TestListener
interface:The
TestListener
interface violates the Interface Segregation Principle, the "I" in "SOLID". This means that it requires the implementation of many methods, even if the client is not interested in the events they represent.But the
TestListener
interface also has a fundamental design flaw: it passes around theTest
andTestSuite
objects, allowing clients to manipulate the outcome of a test run, for instance. To make things worse, client implementations ofTestListener
exist that bypass the public API ofTest
andTestSuite
to perform such manipulation. In short,TestListener
implementations were use to do more than "just listen".The
TestListener
interface is deprecated since PHPUnit 8, it will be removed in PHPUnit 10.The
Hook
InterfacesBack in 2018 and with PHPUnit 7.1, we attempted to make extending PHPUnit's test runner easier with the introduction of the
PHPUnit\Runner\Hook
interfaces. Here is an example of one of these interfaces:As you can see, we learned from the painful experience we made with the
TestListener
interface. ThePHPUnit\Runner\Hook
interfaces follow the Interface Segregation Principle and clients no longer get access to the real test objects that are used by PHPUnit to run the tests.However, we did not "think big enough" and the
PHPUnit\Runner\Hook
interfaces were too limited. Even we did not manage to migrate PHPUnit's ownTestListener
implementations to thePHPUnit\Runner\Hook
interfaces. They were a step in the right direction, but they fell short of actually being useful.The
PHPUnit\Runner\Hook
interfaces will be removed in PHPUnit 11.The New Event System
At the EU-FOSSA 2 Hackathon in October 2019, Sebastian Bergmann, Ewout Pieter den Ouden, Andreas Möller, Arne Blankerts, and Stefan Priebsch got together and designed a new system for extending PHPUnit based on events.
Since then, Arne Blankerts and Andreas Möller worked on implementing this new event-based system in a branch. This issue is created as we are getting close to be able to merge this branch.
The text was updated successfully, but these errors were encountered: