Skip to content

Commit be14f7a

Browse files
committed
check webserver group with phpunit 10
1 parent 1be50e0 commit be14f7a

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/Test/WebServerSubscriber.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,30 @@
1111

1212
namespace FOS\HttpCache\Test;
1313

14+
use PHPUnit\Event\Code\TestMethod;
1415
use PHPUnit\Event\TestRunner\ExecutionStarted;
1516
use PHPUnit\Event\TestRunner\ExecutionStartedSubscriber;
17+
use PHPUnit\Event\TestSuite\TestSuite;
18+
use PHPUnit\Metadata\Group;
1619

1720
class WebServerSubscriber implements ExecutionStartedSubscriber
1821
{
1922
/**
2023
* PHP web server PID.
2124
*/
2225
private int $pid;
26+
private bool $isTopLevel = true;
2327

2428
public function notify(ExecutionStarted $event): void
2529
{
26-
if (isset($this->pid)) {
27-
// TODO: can we detect if 'webserver' is in the list of groups of the test suite?
30+
if (!$this->isTopLevel) {
31+
return;
32+
}
33+
$this->isTopLevel = false;
34+
35+
if (isset($this->pid)
36+
|| !$this->hasTestsWithGroup($event->testSuite(), 'webserver')
37+
) {
2838
return;
2939
}
3040

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

48+
private function hasTestsWithGroup(TestSuite $testSuite, string $group): bool
49+
{
50+
foreach ($testSuite->tests() as $test) {
51+
if (!$test->isTestMethod()) {
52+
continue;
53+
}
54+
55+
assert($test instanceof TestMethod);
56+
57+
foreach ($test->metadata()->isGroup() as $testGroup) {
58+
assert($testGroup instanceof Group);
59+
60+
if ($testGroup->groupName() === $group) {
61+
return true;
62+
}
63+
}
64+
}
65+
66+
return false;
67+
}
68+
3869
/**
3970
* Start PHP built-in web server.
4071
*

0 commit comments

Comments
 (0)