Files
xamxam/tests/run-tests.php
Pontoporeia 71167b2cdf fix: remove DB_ENV auto-detection; require explicit DB_ENV=test for tests
src/config.php: remove the file-existence fallback that silently redirected
all requests to test.db whenever that file was present on disk. getDatabasePath()
now always returns the production DB unless DB_ENV=test is explicitly set.

tests/run-tests.php: putenv('DB_ENV=test') at the top so the suite always
targets test.db regardless of what is set in the shell environment.

tests/Unit/DatabaseTest.php, tests/Integration/SearchTest.php,
tests/Security/SecurityTest.php: same putenv() guard added to each file so
they work correctly when run standalone (e.g. just test-unit).

justfile: all test and DB-development recipes now prefix DB_ENV=test to their
php/sqlite3 commands, making the intent explicit in the recipe itself.

Fixes: a developer who ran the test suite and kept test.db on disk would
silently hit test data when browsing the local site with no DB_ENV set.
2026-03-28 13:43:04 +01:00

92 lines
3.3 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
/**
* Post-ERG Test Runner
* Runs all tests in the tests/ directory
*/
// Tests always run against the test database; require an explicit opt-in so
// that a stray test.db on disk never silently redirects a production session.
putenv('DB_ENV=test');
echo "╔════════════════════════════════════════════╗\n";
echo "║ Post-ERG Test Suite ║\n";
echo "╚════════════════════════════════════════════╝\n\n";
$testFiles = [
['name' => 'Database (Unit)', 'path' => __DIR__ . '/Unit/DatabaseTest.php'],
['name' => 'Rate Limit (Unit)', 'path' => __DIR__ . '/Unit/RateLimitTest.php'],
['name' => 'Search (Integration)', 'path' => __DIR__ . '/Integration/SearchTest.php'],
['name' => 'Security', 'path' => __DIR__ . '/Security/SecurityTest.php'],
];
$totalTests = 0;
$passedTests = 0;
$failedTests = 0;
$skippedTests = 0;
foreach ($testFiles as $test) {
echo "┌─────────────────────────────────────────┐\n";
echo "" . str_pad($test['name'], 41) . "\n";
echo "└─────────────────────────────────────────┘\n\n";
$totalTests++;
$path = $test['path'];
$file = basename($path);
if (!file_exists($path)) {
echo "⚠️ SKIP: $file (not found)\n\n";
$skippedTests++;
continue;
}
ob_start();
$exitCode = 0;
$testResult = false;
try {
$testResult = include $path;
// Check if test returned false or had error indicators in output
$output = ob_get_contents();
if ($testResult === false ||
strpos($output, '❌') !== false ||
strpos($output, 'FAIL:') !== false) {
$exitCode = 1;
}
} catch (Exception $e) {
$exitCode = 1;
echo "❌ EXCEPTION: " . $e->getMessage() . "\n";
}
$output = ob_get_clean();
echo $output;
if ($exitCode === 0 && $testResult !== false) {
echo "\n✅ TEST PASSED\n\n";
$passedTests++;
} else {
echo "\n❌ TEST FAILED\n\n";
$failedTests++;
}
}
echo "╔════════════════════════════════════════════╗\n";
echo "║ Test Summary ║\n";
echo "╠════════════════════════════════════════════╣\n";
echo "║ Total: " . str_pad($totalTests, 34) . "\n";
echo "║ Passed: " . str_pad($passedTests . "", 35) . "\n";
echo "║ Failed: " . str_pad($failedTests . ($failedTests > 0 ? "" : ""), 35) . "\n";
if ($skippedTests > 0) {
echo "║ Skipped: " . str_pad($skippedTests . " ⚠️", 36) . "\n";
}
echo "╚════════════════════════════════════════════╝\n\n";
if ($failedTests > 0) {
echo "❌ Some tests failed!\n";
exit(1);
} else {
echo "✅ All tests passed!\n";
exit(0);
}