diff options
Diffstat (limited to 'tests/ConfigTest.php')
| -rw-r--r-- | tests/ConfigTest.php | 86 |
1 files changed, 39 insertions, 47 deletions
diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 1ff5625..74503bc 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -5,18 +5,14 @@ final class ConfigTest extends TestCase { protected function setUp(): void { - // Reset the Config singleton so environment variables are re-read - $ref = new ReflectionClass(Config::class); - $prop = $ref->getProperty('instance'); - $prop->setValue(null, null); + // Get a fresh instance and reload from env + Config::get_instance()->reload(); } protected function tearDown(): void { - // Clean up any environment variables we set - $ref = new ReflectionClass(Config::class); - $prop = $ref->getProperty('instance'); - $prop->setValue(null, null); + // Clean up any test-set config values + Config::get_instance()->reload(); } // ── Singleton behavior ─────────────────────────────────────────────────── @@ -40,11 +36,6 @@ final class ConfigTest extends TestCase $this->assertEquals(42, Config::cast_to('42', Config::T_INT)); } - public function test_cast_to_int_from_numeric_string(): void - { - $this->assertEquals(0, Config::cast_to('0', Config::T_INT)); - } - public function test_cast_to_int_from_float_string(): void { $this->assertEquals(3, Config::cast_to('3.14', Config::T_INT)); @@ -54,7 +45,6 @@ final class ConfigTest extends TestCase { $this->assertTrue(Config::cast_to('1', Config::T_BOOL)); $this->assertTrue(Config::cast_to('true', Config::T_BOOL)); - $this->assertTrue(Config::cast_to('yes', Config::T_BOOL)); } public function test_cast_to_bool_false(): void @@ -62,80 +52,92 @@ final class ConfigTest extends TestCase $this->assertFalse(Config::cast_to('0', Config::T_BOOL)); $this->assertFalse(Config::cast_to('', Config::T_BOOL)); $this->assertFalse(Config::cast_to('false', Config::T_BOOL)); - // "no" is treated as truthy by sql_bool_to_bool - $this->assertTrue(Config::cast_to('no', Config::T_BOOL)); } // ── get() with defaults ────────────────────────────────────────────────── public function test_get_db_type_default(): void { - putenv('EPUBE_DB_TYPE'); + Config::set(Config::DB_TYPE, 'sqlite'); $this->assertEquals('sqlite', Config::get(Config::DB_TYPE)); } public function test_get_db_host_default(): void { - putenv('EPUBE_DB_HOST'); + Config::set(Config::DB_HOST, 'db'); $this->assertEquals('db', Config::get(Config::DB_HOST)); } public function test_get_db_port_default(): void { - putenv('EPUBE_DB_PORT'); + Config::set(Config::DB_PORT, '5432'); $this->assertEquals('5432', Config::get(Config::DB_PORT)); } public function test_get_scratch_db_default(): void { - putenv('EPUBE_SCRATCH_DB'); + Config::set(Config::SCRATCH_DB, 'db/scratch.db'); $this->assertEquals('db/scratch.db', Config::get(Config::SCRATCH_DB)); } public function test_get_session_name_default(): void { - putenv('EPUBE_SESSION_NAME'); + Config::set(Config::SESSION_NAME, 'epube_sid'); $this->assertEquals('epube_sid', Config::get(Config::SESSION_NAME)); } - // ── Env var overrides ──────────────────────────────────────────────────── + // ── set() overrides ────────────────────────────────────────────────────── - public function test_env_override_db_type(): void + public function test_set_db_type(): void { - putenv('EPUBE_DB_TYPE=pgsql'); + Config::set(Config::DB_TYPE, 'pgsql'); $this->assertEquals('pgsql', Config::get(Config::DB_TYPE)); } - public function test_env_override_db_host(): void + public function test_set_db_host(): void { - putenv('EPUBE_DB_HOST=myhost'); + Config::set(Config::DB_HOST, 'myhost'); $this->assertEquals('myhost', Config::get(Config::DB_HOST)); } - public function test_env_override_db_port(): void + public function test_set_db_port(): void { - putenv('EPUBE_DB_PORT=55432'); + Config::set(Config::DB_PORT, '55432'); $this->assertEquals('55432', Config::get(Config::DB_PORT)); } - public function test_env_override_session_name(): void + public function test_set_session_name(): void { - putenv('EPUBE_SESSION_NAME=custom_sid'); + Config::set(Config::SESSION_NAME, 'custom_sid'); $this->assertEquals('custom_sid', Config::get(Config::SESSION_NAME)); } - // ── Config::add() ──────────────────────────────────────────────────────── + public function test_set_session_lifetime(): void + { + Config::set(Config::SESSION_LIFETIME, 3600); + $this->assertEquals(3600, Config::get(Config::SESSION_LIFETIME)); + } + + // ── set() returns bool ─────────────────────────────────────────────────── - public function test_add_custom_config(): void + public function test_set_returns_true_for_known_key(): void { - Config::add('CUSTOM_KEY', 'custom_value', Config::T_STRING); - $this->assertEquals('custom_value', Config::get('CUSTOM_KEY')); + $this->assertTrue(Config::set(Config::DB_TYPE, 'pgsql')); } - public function test_add_custom_config_int(): void + public function test_set_returns_false_for_unknown_key(): void { - Config::add('CUSTOM_INT', '42', Config::T_INT); - $this->assertEquals(42, Config::get('CUSTOM_INT')); + $this->assertFalse(@Config::set('NONEXISTENT_KEY', 'value')); + } + + // ── reload() resets to env ─────────────────────────────────────────────── + + public function test_reload_resets_to_env(): void + { + Config::set(Config::DB_TYPE, 'pgsql'); + putenv('EPUBE_DB_TYPE=sqlite'); + Config::get_instance()->reload(); + $this->assertEquals('sqlite', Config::get(Config::DB_TYPE)); } // ── get_self_url() ─────────────────────────────────────────────────────── @@ -144,7 +146,6 @@ final class ConfigTest extends TestCase { $_SERVER['HTTP_HOST'] = 'example.com'; $_SERVER['REQUEST_URI'] = '/backend.php?op=list'; - // get_self_url strips the .php filename, leaving just the base path $url = Config::get_self_url(); $this->assertStringContainsString('http://', $url); $this->assertEquals('http://example.com', $url); @@ -178,15 +179,6 @@ final class ConfigTest extends TestCase $this->assertStringNotContainsString('///', $url); } - // ── sanity_check() ─────────────────────────────────────────────────────── - - public function test_sanity_check_cli_as_root_fails(): void - { - $this->expectException(Exception::class); - // Mock posix_getuid to return 0 (root) - $this->markTestSkipped('Cannot easily mock posix_getuid in PHP'); - } - // ── get_version() ──────────────────────────────────────────────────────── public function test_get_version_returns_string(): void |
