Skip to content

Commit

Permalink
Merge pull request #570 from FriendsOfSymfony/phpunit-check-group
Browse files Browse the repository at this point in the history
check webserver group with phpunit 10
  • Loading branch information
dbu committed Mar 28, 2024
2 parents 1be50e0 + be14f7a commit e7106a6
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/Test/WebServerSubscriber.php
Expand Up @@ -11,20 +11,30 @@

namespace FOS\HttpCache\Test;

use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Event\TestRunner\ExecutionStarted;
use PHPUnit\Event\TestRunner\ExecutionStartedSubscriber;
use PHPUnit\Event\TestSuite\TestSuite;
use PHPUnit\Metadata\Group;

class WebServerSubscriber implements ExecutionStartedSubscriber
{
/**
* PHP web server PID.
*/
private int $pid;
private bool $isTopLevel = true;

public function notify(ExecutionStarted $event): void
{
if (isset($this->pid)) {
// TODO: can we detect if 'webserver' is in the list of groups of the test suite?
if (!$this->isTopLevel) {
return;
}
$this->isTopLevel = false;

if (isset($this->pid)
|| !$this->hasTestsWithGroup($event->testSuite(), 'webserver')
) {
return;
}

Expand All @@ -35,6 +45,27 @@ public function notify(ExecutionStarted $event): void
});
}

private function hasTestsWithGroup(TestSuite $testSuite, string $group): bool
{
foreach ($testSuite->tests() as $test) {
if (!$test->isTestMethod()) {
continue;
}

assert($test instanceof TestMethod);

foreach ($test->metadata()->isGroup() as $testGroup) {
assert($testGroup instanceof Group);

if ($testGroup->groupName() === $group) {
return true;
}
}
}

return false;
}

/**
* Start PHP built-in web server.
*
Expand Down

0 comments on commit e7106a6

Please sign in to comment.