Skip to content
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

Closed
dbu opened this issue Mar 26, 2024 · 3 comments
Closed

Get access to the groups in the event system #5769

dbu opened this issue Mar 26, 2024 · 3 comments
Labels
feature/events Issues related to PHPUnit's event system

Comments

@dbu
Copy link

dbu commented Mar 26, 2024

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

@dbu dbu added the type/enhancement A new idea that should be implemented label Mar 26, 2024
@sebastianbergmann sebastianbergmann added feature/events Issues related to PHPUnit's event system and removed type/enhancement A new idea that should be implemented labels Mar 28, 2024
@sebastianbergmann
Copy link
Owner

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 MetadataCollection object that can be accessed using the TestMethod::metadata() method on the value object that represents a test. This value object is passed to all subscribers for events that are related to tests, for instance to subscribers of the Test\Prepared event.

A collection of all TestMethod objects representing all test methods scheduled for execution (ie. not filtered away, etc.) is passed to the TestSuite\Started event for the top-level test suite.

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 TestSuite\Started event as well as how to query the groups of the tests in the test suite.

<?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?

@dbu
Copy link
Author

dbu commented Mar 28, 2024

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.
the Started event does not have the test suite, i am using the ExecutionStarted: FriendsOfSymfony/FOSHttpCache#570

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?

@dbu dbu closed this as completed Mar 28, 2024
@sebastianbergmann
Copy link
Owner

thanks a lot for the detailed explanations 💙

Glad I could help!

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?

You have found dead code, thank you for that. It's now gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/events Issues related to PHPUnit's event system
Projects
None yet
Development

No branches or pull requests

2 participants