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

check webserver group with phpunit 10 #570

Merged
merged 1 commit into from Mar 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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