Adds a simple Core/App unit test
parent
1228abe6f2
commit
62b2242b75
|
|
@ -19,6 +19,20 @@ final class App
|
||||||
self::$services[$key] = $value;
|
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.
|
* Determine whether a value is registered.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,78 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\Core;
|
||||||
|
|
||||||
|
use App\App;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class AppTest extends TestCase
|
||||||
|
{
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
App::reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDown(): void
|
||||||
|
{
|
||||||
|
App::reset();
|
||||||
|
parent::tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Basic happy path: storing and retrieving services via the registry.
|
||||||
|
*/
|
||||||
|
public function testSetAndGetService(): void
|
||||||
|
{
|
||||||
|
App::set('foo', 'bar');
|
||||||
|
$this->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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -12,6 +12,9 @@ if (!headers_sent()) {
|
||||||
ini_set('session.gc_maxlifetime', 1440); // 24 minutes
|
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
|
// Load plugin Log model and IP helper early so fallback wrapper is bypassed
|
||||||
require_once __DIR__ . '/../app/helpers/ip_helper.php';
|
require_once __DIR__ . '/../app/helpers/ip_helper.php';
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue