Files
appwrite/tests/extensions/TestFinishedSubscriber.php
Chirag Aggarwal 3930df146c (test): report true per-test duration and de-duplicate variable build tests (#12637)
* (test): report true per-test duration and de-duplicate variable build tests

The PHPUnit test reporter printed `durationSinceStart()` — wall-clock
elapsed since the run began — and labeled it as per-test duration. That
made the numbers climb monotonically across the run and flagged nearly
every test past the 15s mark as "slow", even pure CRUD tests that finish
in well under a second.

Capture an elapsed-since-start stamp when each test is prepared and
subtract it at finish, so the reported time and the "slow" warning are
the test's own duration.

Separately, the variable-in-build integration tests
(testProjectVariableInFunction, testProjectVariableInSite) lived in the
shared VariablesBase trait and therefore ran in both the console-client
and custom-server suites — 4 real Docker/npm builds where 2 suffice. The
contract is auth-side independent, so move them into
VariablesCustomServerTest only and keep the cheap CRUD coverage shared.

* (test): satisfy rector string-cast rule and document refactor pre-check

Rector's StringCastAssertStringContainsStringRector requires (string)
casts on the assertStringContains(Not) arguments in the moved build
tests. Apply them, and add composer refactor / refactor:check to the
AGENTS.md command table so the CI "Refactor" check is run before pushing.

* fix: purge project cache after config updates

* (test): stamp test start at PreparationStarted, not Prepared

Prepared fires after setUp/fixtures run, so expensive e2e setup was
excluded from the reported per-test duration and the slow-test warning.
Listen on PreparationStarted (before preparation begins) instead, and
rename the subscriber to match.

* (ci): bump php-retry to node24 runtime commit

itznotabug/php-retry#15 migrated the action runtime from the deprecated
node20 to node24. The v3 tag has not been re-pointed yet, so pin the
merged commit (d4500b9) directly to clear the "Node 20 is being
deprecated" warning on the unit-test retry step. Updates all 5 pins.

* (ci): combine PHP check jobs into a single Checks job

* (test): use full-precision duration in per-test reporter

durationSinceStart()->seconds() returns only the whole-second part, so
the finish-minus-start delta dropped all sub-second time: a 10.9s→11.1s
test reported 1000ms instead of ~200ms, and most sub-second tests
reported 0ms. Use Duration::asFloat() (seconds + nanoseconds) for both
the start stamp and the finish elapsed, and format output as rounded
integer ms / 2-decimal seconds.

* fix: remove project cache purge calls
2026-06-22 10:10:47 +05:30

37 lines
1012 B
PHP

<?php
namespace Appwrite\Tests;
use PHPUnit\Event\Test\Finished;
use PHPUnit\Event\Test\FinishedSubscriber;
class TestFinishedSubscriber implements FinishedSubscriber
{
public function __construct(
private readonly float $maxSecondsAllowed
) {
}
public function notify(Finished $event): void
{
$test = $event->test()->name();
$testId = $event->test()->id();
$elapsed = $event->telemetryInfo()->durationSinceStart()->asFloat();
$start = TestPreparationStartedSubscriber::$startSeconds[$testId] ?? null;
$time = $start === null ? $elapsed : $elapsed - $start;
unset(TestPreparationStartedSubscriber::$startSeconds[$testId]);
printf(
"%s ended in %d milliseconds\n",
$test,
(int) round($time * 1000)
);
if ($time > $this->maxSecondsAllowed) {
fwrite(STDOUT, sprintf("\e[31mThe %s test is slow, it took %.2f seconds!\n\e[0m", $test, $time));
}
}
}