From 62b2242b75c04a82a3f82ac8b879530a3d244c61 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Tue, 13 Jan 2026 12:04:13 +0200 Subject: [PATCH] Adds a simple Core/App unit test --- app/core/App.php | 14 +++++++ tests/Unit/Core/AppTest.php | 78 +++++++++++++++++++++++++++++++++++++ tests/bootstrap.php | 3 ++ 3 files changed, 95 insertions(+) create mode 100644 tests/Unit/Core/AppTest.php diff --git a/app/core/App.php b/app/core/App.php index 738c7fa..baf7c5c 100644 --- a/app/core/App.php +++ b/app/core/App.php @@ -19,6 +19,20 @@ final class App self::$services[$key] = $value; } + /** + * Clear one or all registered services. + * + * Primarily used by unit tests to avoid cross-test pollution. + */ + public static function reset(?string $key = null): void + { + if ($key === null) { + self::$services = []; + return; + } + unset(self::$services[$key]); + } + /** * Determine whether a value is registered. */ diff --git a/tests/Unit/Core/AppTest.php b/tests/Unit/Core/AppTest.php new file mode 100644 index 0000000..f1cbab7 --- /dev/null +++ b/tests/Unit/Core/AppTest.php @@ -0,0 +1,78 @@ +assertTrue(App::has('foo')); + $this->assertSame('bar', App::get('foo')); + } + + /** + * Missing services should fall back to the provided default value. + */ + public function testGetUsesDefaultWhenMissing(): void + { + $this->assertFalse(App::has('missing')); + $this->assertSame('fallback', App::get('missing', 'fallback')); + } + + /** + * Resetting a specific key only clears that one service. + */ + public function testResetClearsSpecificService(): void + { + App::set('foo', 'bar'); + App::set('baz', 'qux'); + App::reset('foo'); + + $this->assertNull(App::get('foo')); + $this->assertSame('qux', App::get('baz')); + } + + /** + * Reset without arguments should clear the entire registry. + */ + public function testResetClearsAllServices(): void + { + App::set('foo', 'bar'); + App::set('baz', 'qux'); + App::reset(); + + $this->assertNull(App::get('foo')); + $this->assertNull(App::get('baz')); + } + + /** + * When nothing is registered, App helpers should still read legacy globals. + */ + public function testFallbackReadsLegacyGlobals(): void + { + $GLOBALS['config'] = ['foo' => 'bar']; + $GLOBALS['db'] = new \stdClass(); + + $this->assertSame('bar', App::config()['foo']); + $this->assertInstanceOf(\stdClass::class, App::db()); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 66df34a..550906b 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -12,6 +12,9 @@ if (!headers_sent()) { ini_set('session.gc_maxlifetime', 1440); // 24 minutes } +// load the main App registry +require_once __DIR__ . '/../app/core/App.php'; + // Load plugin Log model and IP helper early so fallback wrapper is bypassed require_once __DIR__ . '/../app/helpers/ip_helper.php';