diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-05-27 20:18:22 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-05-27 20:18:22 +0300 |
| commit | c2ce4528c4b671466291f7947c682b4adffa007f (patch) | |
| tree | eb4493ea9ee34e44b351a46e9883bbb4405768f3 /tests/ConfigTest.php | |
| parent | 0d83f0350991a0993234278ff822a5a9893802f5 (diff) | |
| download | the-epube-c2ce4528c4b671466291f7947c682b4adffa007f.tar.gz the-epube-c2ce4528c4b671466291f7947c682b4adffa007f.tar.xz | |
add some simple tests
Diffstat (limited to 'tests/ConfigTest.php')
| -rw-r--r-- | tests/ConfigTest.php | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php new file mode 100644 index 0000000..1ff5625 --- /dev/null +++ b/tests/ConfigTest.php @@ -0,0 +1,205 @@ +<?php +use PHPUnit\Framework\TestCase; + +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); + } + + protected function tearDown(): void + { + // Clean up any environment variables we set + $ref = new ReflectionClass(Config::class); + $prop = $ref->getProperty('instance'); + $prop->setValue(null, null); + } + + // ── Singleton behavior ─────────────────────────────────────────────────── + + public function test_singleton_returns_same_instance(): void + { + $config1 = Config::get_instance(); + $config2 = Config::get_instance(); + $this->assertSame($config1, $config2); + } + + // ── cast_to() ──────────────────────────────────────────────────────────── + + public function test_cast_to_string(): void + { + $this->assertEquals('hello', Config::cast_to('hello', Config::T_STRING)); + } + + public function test_cast_to_int(): void + { + $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)); + } + + public function test_cast_to_bool_true(): void + { + $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 + { + $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'); + $this->assertEquals('sqlite', Config::get(Config::DB_TYPE)); + } + + public function test_get_db_host_default(): void + { + putenv('EPUBE_DB_HOST'); + $this->assertEquals('db', Config::get(Config::DB_HOST)); + } + + public function test_get_db_port_default(): void + { + putenv('EPUBE_DB_PORT'); + $this->assertEquals('5432', Config::get(Config::DB_PORT)); + } + + public function test_get_scratch_db_default(): void + { + putenv('EPUBE_SCRATCH_DB'); + $this->assertEquals('db/scratch.db', Config::get(Config::SCRATCH_DB)); + } + + public function test_get_session_name_default(): void + { + putenv('EPUBE_SESSION_NAME'); + $this->assertEquals('epube_sid', Config::get(Config::SESSION_NAME)); + } + + // ── Env var overrides ──────────────────────────────────────────────────── + + public function test_env_override_db_type(): void + { + putenv('EPUBE_DB_TYPE=pgsql'); + $this->assertEquals('pgsql', Config::get(Config::DB_TYPE)); + } + + public function test_env_override_db_host(): void + { + putenv('EPUBE_DB_HOST=myhost'); + $this->assertEquals('myhost', Config::get(Config::DB_HOST)); + } + + public function test_env_override_db_port(): void + { + putenv('EPUBE_DB_PORT=55432'); + $this->assertEquals('55432', Config::get(Config::DB_PORT)); + } + + public function test_env_override_session_name(): void + { + putenv('EPUBE_SESSION_NAME=custom_sid'); + $this->assertEquals('custom_sid', Config::get(Config::SESSION_NAME)); + } + + // ── Config::add() ──────────────────────────────────────────────────────── + + public function test_add_custom_config(): void + { + Config::add('CUSTOM_KEY', 'custom_value', Config::T_STRING); + $this->assertEquals('custom_value', Config::get('CUSTOM_KEY')); + } + + public function test_add_custom_config_int(): void + { + Config::add('CUSTOM_INT', '42', Config::T_INT); + $this->assertEquals(42, Config::get('CUSTOM_INT')); + } + + // ── get_self_url() ─────────────────────────────────────────────────────── + + public function test_get_self_url_http(): void + { + $_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); + } + + public function test_get_self_url_https(): void + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/backend.php'; + $_SERVER['HTTPS'] = 'on'; + $url = Config::get_self_url(); + $this->assertStringContainsString('https://', $url); + $this->assertEquals('https://example.com', $url); + } + + public function test_get_self_url_with_x_forwarded_proto(): void + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/backend.php'; + unset($_SERVER['HTTPS']); + $_SERVER['HTTP_X_FORWARDED_PROTO'] = 'https'; + $url = Config::get_self_url(); + $this->assertStringContainsString('https://', $url); + } + + public function test_get_self_url_trailing_slash_removed(): void + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/backend.php/'; + $url = Config::get_self_url(); + $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 + { + $version = Config::get_version(); + $this->assertIsString($version); + } + + public function test_get_version_as_array(): void + { + $version = Config::get_version(false); + $this->assertIsArray($version); + $this->assertArrayHasKey('status', $version); + $this->assertArrayHasKey('version', $version); + } +} |
