From be14f7a310928878bc1af3d9f62cc3dd1b51e76d Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Thu, 28 Mar 2024 10:11:43 +0100 Subject: [PATCH] check webserver group with phpunit 10 --- src/Test/WebServerSubscriber.php | 35 ++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Test/WebServerSubscriber.php b/src/Test/WebServerSubscriber.php index 2e9fbc78..8d25bb19 100644 --- a/src/Test/WebServerSubscriber.php +++ b/src/Test/WebServerSubscriber.php @@ -11,8 +11,11 @@ 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 { @@ -20,11 +23,18 @@ 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; } @@ -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. *