57 lines
2.5 KiB
PHP
57 lines
2.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Führt alle PHP-Tests aus tests/ nacheinander aus (je Test ein eigener Prozess) und fasst das Ergebnis zusammen.
|
|
* Aufruf: php tests/run_all.php alle Tests
|
|
* php tests/run_all.php plugin nur Tests, deren Dateiname "plugin" enthält
|
|
* Exit-Code 0 = alles grün, 1 = mindestens ein Test fehlgeschlagen (für CI).
|
|
*/
|
|
|
|
// Tests dürfen nie eine echte Datenbank aus config/config.php benutzen (dort kann die Produktiv-MySQL stehen): sie laufen gegen SQLite.
|
|
putenv('DESKHUB_TESTS_FORCE_SQLITE=1');
|
|
|
|
$filter = $argv[1] ?? '';
|
|
$timeout = 600;
|
|
$files = glob(__DIR__ . '/*.php') ?: [];
|
|
sort($files);
|
|
$files = array_values(array_filter($files, fn($f) => basename($f) !== 'run_all.php' && ($filter === '' || str_contains(basename($f), $filter))));
|
|
if (!$files) { fwrite(STDERR, "Keine Tests gefunden.\n"); exit(1); }
|
|
|
|
$failed = [];
|
|
$started = microtime(true);
|
|
foreach ($files as $file) {
|
|
$name = basename($file, '.php');
|
|
$t0 = microtime(true);
|
|
$proc = proc_open([PHP_BINARY, $file], [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes, dirname(__DIR__));
|
|
stream_set_blocking($pipes[1], false);
|
|
stream_set_blocking($pipes[2], false);
|
|
$out = '';
|
|
$err = '';
|
|
$timedOut = false;
|
|
while (true) {
|
|
$out .= (string) stream_get_contents($pipes[1]);
|
|
$err .= (string) stream_get_contents($pipes[2]);
|
|
$status = proc_get_status($proc);
|
|
if (!$status['running']) break;
|
|
if (microtime(true) - $t0 > $timeout) { proc_terminate($proc, 9); $timedOut = true; break; }
|
|
usleep(50000);
|
|
}
|
|
$out .= (string) stream_get_contents($pipes[1]);
|
|
$err .= (string) stream_get_contents($pipes[2]);
|
|
$code = $timedOut ? -1 : $status['exitcode'];
|
|
proc_close($proc);
|
|
|
|
$lines = array_values(array_filter(array_map('trim', explode("\n", trim($out)))));
|
|
$last = $lines ? end($lines) : '';
|
|
$ok = $code === 0 && !$timedOut;
|
|
printf("%s %-32s %5.1fs %s\n", $ok ? 'OK ' : 'FAIL', $name, microtime(true) - $t0, $ok ? $last : ($timedOut ? 'Zeitüberschreitung' : "Exit-Code {$code}"));
|
|
if (!$ok) {
|
|
$failed[] = $name;
|
|
$detail = trim($err !== '' ? $err : $out);
|
|
echo ' ' . str_replace("\n", "\n ", mb_substr($detail, 0, 3000)) . "\n";
|
|
}
|
|
}
|
|
printf("\n%d Tests in %.0f s: %d bestanden, %d fehlgeschlagen%s\n", count($files), microtime(true) - $started, count($files) - count($failed), count($failed), $failed ? ' (' . implode(', ', $failed) . ')' : '');
|
|
exit($failed ? 1 : 0);
|