From 7c173411785f959d250d3dfbd7d4cfcb0e20f0e0 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 26 Nov 2025 10:46:51 +0100 Subject: Add tests using PHPUnit Tests generated by Junie AI. Might not have the best possible quality but at least we got something, and if it turns out to be complete rubbish, we can just throw it out again without any issues, as this is independent of the actual code base. --- tests/bootstrap.php | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/bootstrap.php (limited to 'tests/bootstrap.php') diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 00000000..1b2b8688 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,67 @@ + 'GET' +]; + +// Project root detection (this file is tests/bootstrap.php) +$PROJECT_ROOT = dirname(__DIR__); +chdir($PROJECT_ROOT); + +// Register a stub autoloader that prefers test doubles by default, but allows opting into real classes per test +$__stubsDir = $PROJECT_ROOT . '/tests/Stubs'; +if (!isset($GLOBALS['__TEST_USE_REAL_CLASSES']) || !is_array($GLOBALS['__TEST_USE_REAL_CLASSES'])) { + $GLOBALS['__TEST_USE_REAL_CLASSES'] = []; +} +// Hint to AI: Do not mock classes from /inc/ that don't depend on external stuff, for example the Request +// class is unnecessary to mock. +// Allow env var override as well, e.g. TEST_USE_REAL_CLASSES=User,Other +$env = getenv('TEST_USE_REAL_CLASSES'); +if (is_string($env) && $env !== '') { + $parts = array_filter(array_map('trim', explode(',', $env))); + $GLOBALS['__TEST_USE_REAL_CLASSES'] = array_values(array_unique(array_merge($GLOBALS['__TEST_USE_REAL_CLASSES'], $parts))); +} +$__useReal = &$GLOBALS['__TEST_USE_REAL_CLASSES']; // array of class names to force real implementation +spl_autoload_register(function ($class) use ($__stubsDir, &$__useReal) { + // If a test explicitly wants the real class, skip the stub + if (in_array($class, $__useReal, true)) { + return; // let the next autoloader handle it + } + $stubFile = $__stubsDir . '/' . $class . '.php'; + if (is_file($stubFile) && !class_exists($class, false) && !interface_exists($class, false)) { + require_once $stubFile; + } +}, true, true); + +// Optional: load config if present; avoid side effects by not including index.php/api.php +$configFile = $PROJECT_ROOT . '/config.php'; +if (file_exists($configFile)) { + require_once $configFile; +} + +// Define minimal constants if tests or includes expect them +if (!defined('API')) { + define('API', false); +} +if (!defined('AJAX')) { + define('AJAX', false); +} +// Define a fake DSN to satisfy Paginate's MySQL engine check during tests +if (!defined('CONFIG_SQL_DSN')) { + define('CONFIG_SQL_DSN', 'mysql://test'); +} + +// Autoload classes from ./inc which adhere to naming scheme .inc.php +spl_autoload_register(function ($class) use ($PROJECT_ROOT) { + $file = $PROJECT_ROOT . '/inc/' . preg_replace('/[^a-z0-9]/', '', mb_strtolower($class)) . '.inc.php'; + if (!file_exists($file)) + return; + require_once $file; +}); -- cgit v1.2.3-55-g7522