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
Get access to the groups in the event system #5769
Comments
A test method can be marked a belonging to one or more groups. In the context of the event system, information about which groups a test belongs to is made available using a A collection of all If I understand your use case correctly, you want to run custom code before the first test is run contingent on whether there is at least a single test scheduled for execution that belongs to a specific group. The code shown below shows an example of a subscriber for the <?php declare(strict_types=1);
namespace PHPUnit\TestFixture\Issue5769;
use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Event\TestSuite\Started;
use PHPUnit\Event\TestSuite\StartedSubscriber as TestSuiteStartedSubscriber;
use PHPUnit\Metadata\Group;
final class Subscriber implements TestSuiteStartedSubscriber
{
private bool $isTopLevel = true;
public function notify(Started $event): void
{
if (!$this->isTopLevel) {
return;
}
$this->isTopLevel = false;
$groups = [];
foreach ($event->testSuite()->tests() as $test) {
if (!$test->isTestMethod()) {
continue;
}
assert($test instanceof TestMethod);
foreach ($test->metadata()->isGroup() as $group) {
assert($group instanceof Group);
$groups[] = $group->groupName();
}
}
var_dump(array_unique($groups));
}
} Does this help? |
thanks a lot for the detailed explanations 💙 indeed, that works perfectly. i was only looking in the Event namespace and saw nothing about groups there, did not realize its all done in Metadata. just out of curiosity: https://github.com/sebastianbergmann/phpunit/blob/10.5/src/Event/Value/TestSuite/TestSuiteBuilder.php#L34-L44 is indeed dead code, or does it have some side effect i am missing? |
Glad I could help!
You have found dead code, thank you for that. It's now gone. |
With the event system introduced in phpunit 10, i think it is no longer possible for listeners to know which groups are involved with tests. We used to check for a specific group to know if the listener needs to start a webserver in FOSHttpCache
it looks to me like the conversion of models to event value objects collects the groups but then does not do anything with them: https://github.com/sebastianbergmann/phpunit/blob/10.5/src/Event/Value/TestSuite/TestSuiteBuilder.php#L34-L44
The text was updated successfully, but these errors were encountered: