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 | |
| parent | 0d83f0350991a0993234278ff822a5a9893802f5 (diff) | |
| download | the-epube-c2ce4528c4b671466291f7947c682b4adffa007f.tar.gz the-epube-c2ce4528c4b671466291f7947c682b4adffa007f.tar.xz | |
add some simple tests
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ConfigTest.php | 205 | ||||
| -rw-r--r-- | tests/DebugTest.php | 219 | ||||
| -rw-r--r-- | tests/RequestTest.php | 143 | ||||
| -rw-r--r-- | tests/ResponseTest.php | 153 |
4 files changed, 720 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); + } +} diff --git a/tests/DebugTest.php b/tests/DebugTest.php new file mode 100644 index 0000000..8156035 --- /dev/null +++ b/tests/DebugTest.php @@ -0,0 +1,219 @@ +<?php +use PHPUnit\Framework\TestCase; + +final class DebugTest extends TestCase +{ + private string $tmpLogfile = ''; + + protected function setUp(): void + { + // Reset Debug static state via public setters + Debug::set_enabled(false); + Debug::set_quiet(false); + Debug::set_logfile(''); + Debug::set_loglevel(0); + + // Create a temp log file for file-based tests + $this->tmpLogfile = tempnam(sys_get_temp_dir(), 'epube_debug_'); + } + + protected function tearDown(): void + { + // Clean up temp log file + if ($this->tmpLogfile && file_exists($this->tmpLogfile)) { + unlink($this->tmpLogfile); + } + + // Reset Debug static state via public setters + Debug::set_enabled(false); + Debug::set_quiet(false); + Debug::set_logfile(''); + Debug::set_loglevel(0); + } + + // ── Constants ──────────────────────────────────────────────────────────── + + public function test_constants_defined(): void + { + $this->assertEquals(-1, Debug::LOG_DISABLED); + $this->assertEquals(0, Debug::LOG_NORMAL); + $this->assertEquals(1, Debug::LOG_VERBOSE); + $this->assertEquals(2, Debug::LOG_EXTENDED); + } + + // ── set_enabled / enabled() ────────────────────────────────────────────── + + public function test_enabled_defaults_to_false(): void + { + $this->assertFalse(Debug::enabled()); + } + + public function test_set_enabled_true(): void + { + Debug::set_enabled(true); + $this->assertTrue(Debug::enabled()); + } + + public function test_set_enabled_false(): void + { + Debug::set_enabled(true); + Debug::set_enabled(false); + $this->assertFalse(Debug::enabled()); + } + + // ── set_loglevel / get_loglevel() ──────────────────────────────────────── + + public function test_loglevel_defaults_to_zero(): void + { + $this->assertEquals(0, Debug::get_loglevel()); + } + + public function test_set_loglevel(): void + { + Debug::set_loglevel(Debug::LOG_VERBOSE); + $this->assertEquals(Debug::LOG_VERBOSE, Debug::get_loglevel()); + } + + public function test_set_loglevel_extended(): void + { + Debug::set_loglevel(Debug::LOG_EXTENDED); + $this->assertEquals(Debug::LOG_EXTENDED, Debug::get_loglevel()); + } + + // ── log() — disabled ───────────────────────────────────────────────────── + + public function test_log_disabled_does_nothing(): void + { + Debug::set_enabled(false); + $this->expectOutputString(''); + Debug::log('test message', Debug::LOG_NORMAL); + } + + // ── log() — level filtering ────────────────────────────────────────────── + + public function test_log_level_filtering(): void + { + // loglevel < level means the message is skipped + // So with loglevel=0 (NORMAL), a message at level=1 (VERBOSE) is filtered + Debug::set_enabled(true); + Debug::set_loglevel(Debug::LOG_NORMAL); + Debug::set_quiet(true); + + ob_start(); + Debug::log('verbose message', Debug::LOG_VERBOSE); + $output = ob_get_clean(); + $this->assertEmpty($output, 'VERBOSE message should be filtered when loglevel is NORMAL'); + } + + public function test_log_level_normal_when_loglevel_zero(): void + { + Debug::set_enabled(true); + Debug::set_loglevel(Debug::LOG_NORMAL); + Debug::set_quiet(true); + + ob_start(); + Debug::log('normal message', Debug::LOG_NORMAL); + $output = ob_get_clean(); + $this->assertNotEmpty($output, 'NORMAL message should pass when loglevel is NORMAL'); + } + + // ── log() — file writing ───────────────────────────────────────────────── + + public function test_log_writes_to_file(): void + { + Debug::set_enabled(true); + Debug::set_logfile($this->tmpLogfile); + Debug::set_loglevel(Debug::LOG_NORMAL); + + Debug::log('file test message', Debug::LOG_NORMAL); + + $this->assertFileExists($this->tmpLogfile); + $content = file_get_contents($this->tmpLogfile); + $this->assertStringContainsString('file test message', $content); + } + + public function test_log_timestamp_in_file(): void + { + Debug::set_enabled(true); + Debug::set_logfile($this->tmpLogfile); + Debug::set_loglevel(Debug::LOG_NORMAL); + + Debug::log('timestamp test', Debug::LOG_NORMAL); + + $content = file_get_contents($this->tmpLogfile); + // Format: [HH:MM:SS/PID] message + $this->assertMatchesRegularExpression('/\[\d{2}:\d{2}:\d{2}\/\d+\]/', $content); + } + + public function test_log_multiple_messages_to_file(): void + { + Debug::set_enabled(true); + Debug::set_logfile($this->tmpLogfile); + Debug::set_loglevel(Debug::LOG_NORMAL); + + Debug::log('first message', Debug::LOG_NORMAL); + Debug::log('second message', Debug::LOG_NORMAL); + + $lines = array_filter(explode("\n", file_get_contents($this->tmpLogfile))); + $this->assertCount(2, $lines); + $this->assertStringContainsString('first message', $lines[0]); + $this->assertStringContainsString('second message', $lines[1]); + } + + // ── log() — quiet mode ─────────────────────────────────────────────────── + + public function test_quiet_mode_suppresses_stdout(): void + { + // quiet mode only suppresses stdout when a logfile is set + Debug::set_enabled(true); + Debug::set_loglevel(Debug::LOG_NORMAL); + Debug::set_quiet(true); + Debug::set_logfile($this->tmpLogfile); + + ob_start(); + Debug::log('quiet test', Debug::LOG_NORMAL); + $output = ob_get_clean(); + $this->assertEmpty($output, 'Quiet mode should suppress stdout when logfile is set'); + } + + public function test_non_quiet_mode_prints_to_stdout(): void + { + Debug::set_enabled(true); + Debug::set_loglevel(Debug::LOG_NORMAL); + Debug::set_quiet(false); + + ob_start(); + Debug::log('stdout test', Debug::LOG_NORMAL); + $output = ob_get_clean(); + $this->assertStringContainsString('stdout test', $output); + } + + // ── set_logfile ────────────────────────────────────────────────────────── + + public function test_set_logfile(): void + { + Debug::set_logfile('/tmp/test_debug.log'); + $ref = new ReflectionClass(Debug::class); + $prop = $ref->getProperty('logfile'); + $this->assertEquals('/tmp/test_debug.log', $prop->getValue(null)); + } + + // ── set_quiet ──────────────────────────────────────────────────────────── + + public function test_set_quiet_true(): void + { + Debug::set_quiet(true); + $ref = new ReflectionClass(Debug::class); + $prop = $ref->getProperty('quiet'); + $this->assertTrue($prop->getValue(null)); + } + + public function test_set_quiet_false(): void + { + Debug::set_quiet(true); + Debug::set_quiet(false); + $ref = new ReflectionClass(Debug::class); + $prop = $ref->getProperty('quiet'); + $this->assertFalse($prop->getValue(null)); + } +} diff --git a/tests/RequestTest.php b/tests/RequestTest.php new file mode 100644 index 0000000..1f45505 --- /dev/null +++ b/tests/RequestTest.php @@ -0,0 +1,143 @@ +<?php +use PHPUnit\Framework\TestCase; + +final class RequestTest extends TestCase +{ + // ── getQueryParams() ───────────────────────────────────────────────────── + + public function test_get_query_params_returns_get(): void + { + $_GET = ['foo' => 'bar', 'page' => '1']; + $request = new Request(); + $this->assertEquals(['foo' => 'bar', 'page' => '1'], $request->getQueryParams()); + } + + public function test_get_query_params_empty(): void + { + $_GET = []; + $request = new Request(); + $this->assertEquals([], $request->getQueryParams()); + } + + // ── getServerParams() ──────────────────────────────────────────────────── + + public function test_get_server_params_returns_server(): void + { + $_SERVER = ['REQUEST_METHOD' => 'POST', 'SCRIPT_NAME' => '/backend.php']; + $request = new Request(); + $this->assertEquals(['REQUEST_METHOD' => 'POST', 'SCRIPT_NAME' => '/backend.php'], $request->getServerParams()); + } + + // ── getCookieParams() ──────────────────────────────────────────────────── + + public function test_get_cookie_params_returns_cookies(): void + { + $_COOKIE = ['session' => 'abc123']; + $request = new Request(); + $this->assertEquals(['session' => 'abc123'], $request->getCookieParams()); + } + + // ── getParsedBody() ────────────────────────────────────────────────────── + + public function test_get_parsed_body_merges_get_and_post(): void + { + $_GET = ['op' => 'list']; + $_POST = ['limit' => '10']; + $request = new Request(); + $body = $request->getParsedBody(); + $this->assertEquals('list', $body['op']); + $this->assertEquals('10', $body['limit']); + } + + public function test_get_parsed_body_post_overrides_get(): void + { + $_GET = ['op' => 'list']; + $_POST = ['op' => 'getinfo']; + $request = new Request(); + $body = $request->getParsedBody(); + // array_merge: second array values override first + $this->assertEquals('getinfo', $body['op']); + } + + public function test_get_parsed_body_only_get(): void + { + $_GET = ['op' => 'list']; + $_POST = []; + $request = new Request(); + $this->assertEquals(['op' => 'list'], $request->getParsedBody()); + } + + public function test_get_parsed_body_only_post(): void + { + $_GET = []; + $_POST = ['op' => 'perform-login']; + $request = new Request(); + $this->assertEquals(['op' => 'perform-login'], $request->getParsedBody()); + } + + // ── getAttribute() ─────────────────────────────────────────────────────── + + public function test_get_attribute_returns_value_from_body(): void + { + $_POST = ['op' => 'list']; + $request = new Request(); + $this->assertEquals('list', $request->getAttribute('op')); + } + + public function test_get_attribute_returns_default_when_missing(): void + { + $_POST = []; + $request = new Request(); + $this->assertNull($request->getAttribute('missing')); + } + + public function test_get_attribute_returns_custom_default(): void + { + $_POST = []; + $request = new Request(); + $this->assertEquals('default_value', $request->getAttribute('missing', 'default_value')); + } + + // ── withAttribute() ────────────────────────────────────────────────────── + + public function test_with_attribute_returns_clone(): void + { + $_POST = ['op' => 'list']; + $request = new Request(); + $newRequest = $request->withAttribute('limit', 10); + $this->assertNotSame($request, $newRequest); + } + + public function test_with_attribute_sets_value(): void + { + $_POST = ['op' => 'list']; + $request = new Request(); + $newRequest = $request->withAttribute('limit', 10); + $this->assertEquals(10, $newRequest->getAttribute('limit')); + } + + public function test_with_attribute_does_not_mutate_original(): void + { + $_POST = ['op' => 'list']; + $request = new Request(); + $request->withAttribute('limit', 10); + $this->assertNull($request->getAttribute('limit')); + } + + public function test_with_attribute_preserves_existing_attributes(): void + { + $_POST = ['op' => 'list']; + $request = new Request(); + $newRequest = $request->withAttribute('limit', 10); + $this->assertEquals('list', $newRequest->getAttribute('op')); + $this->assertEquals(10, $newRequest->getAttribute('limit')); + } + + public function test_with_attribute_overwrites_existing(): void + { + $_POST = ['op' => 'list']; + $request = new Request(); + $newRequest = $request->withAttribute('op', 'getinfo'); + $this->assertEquals('getinfo', $newRequest->getAttribute('op')); + } +} diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php new file mode 100644 index 0000000..133b077 --- /dev/null +++ b/tests/ResponseTest.php @@ -0,0 +1,153 @@ +<?php +use PHPUnit\Framework\TestCase; + +final class ResponseTest extends TestCase +{ + private function createResponse(): Response + { + return new Response(); + } + + // ── Default state ──────────────────────────────────────────────────────── + + public function test_default_status_is_200(): void + { + $response = $this->createResponse(); + $clone = $response->withStatus(200); + $this->assertSame(200, (function () { return $this->status; })->call($clone)); + } + + public function test_default_body_is_empty(): void + { + $response = $this->createResponse(); + $this->assertEquals('', $response->getBody()); + } + + // ── withStatus() ───────────────────────────────────────────────────────── + + public function test_with_status_returns_clone(): void + { + $response = $this->createResponse(); + $newResponse = $response->withStatus(404); + $this->assertNotSame($response, $newResponse); + } + + public function test_with_status_sets_code(): void + { + $response = $this->createResponse(); + $newResponse = $response->withStatus(404); + $ref = new ReflectionClass($newResponse); + $prop = $ref->getProperty('status'); + $this->assertEquals(404, $prop->getValue($newResponse)); + } + + public function test_with_status_does_not_mutate_original(): void + { + $response = $this->createResponse(); + $response->withStatus(404); + $ref = new ReflectionClass($response); + $prop = $ref->getProperty('status'); + $this->assertEquals(200, $prop->getValue($response)); + } + + public function test_with_status_various_codes(): void + { + foreach ([200, 201, 301, 400, 401, 403, 404, 500, 503] as $code) { + $newResponse = $this->createResponse()->withStatus($code); + $ref = new ReflectionClass($newResponse); + $prop = $ref->getProperty('status'); + $this->assertEquals($code, $prop->getValue($newResponse)); + } + } + + // ── withHeader() ───────────────────────────────────────────────────────── + + public function test_with_header_returns_clone(): void + { + $response = $this->createResponse(); + $newResponse = $response->withHeader('Content-Type', 'text/html'); + $this->assertNotSame($response, $newResponse); + } + + public function test_with_header_sets_value(): void + { + $response = $this->createResponse(); + $newResponse = $response->withHeader('Content-Type', 'application/json'); + $ref = new ReflectionClass($newResponse); + $prop = $ref->getProperty('headers'); + $this->assertEquals(['Content-Type' => 'application/json'], $prop->getValue($newResponse)); + } + + public function test_with_header_does_not_mutate_original(): void + { + $response = $this->createResponse(); + $response->withHeader('X-Foo', 'bar'); + $ref = new ReflectionClass($response); + $prop = $ref->getProperty('headers'); + $this->assertEquals([], $prop->getValue($response)); + } + + public function test_with_header_overwrites_existing(): void + { + $response = $this->createResponse(); + $r1 = $response->withHeader('Content-Type', 'text/html'); + $r2 = $r1->withHeader('Content-Type', 'application/json'); + $ref = new ReflectionClass($r2); + $prop = $ref->getProperty('headers'); + $this->assertEquals(['Content-Type' => 'application/json'], $prop->getValue($r2)); + } + + public function test_with_header_multiple_headers(): void + { + $response = $this->createResponse(); + $newResponse = $response + ->withHeader('Content-Type', 'text/html') + ->withHeader('X-Custom', 'value'); + $ref = new ReflectionClass($newResponse); + $prop = $ref->getProperty('headers'); + $headers = $prop->getValue($newResponse); + $this->assertEquals('text/html', $headers['Content-Type']); + $this->assertEquals('value', $headers['X-Custom']); + } + + // ── withBody() ─────────────────────────────────────────────────────────── + + public function test_with_body_returns_clone(): void + { + $response = $this->createResponse(); + $newResponse = $response->withBody('hello'); + $this->assertNotSame($response, $newResponse); + } + + public function test_with_body_sets_content(): void + { + $response = $this->createResponse(); + $newResponse = $response->withBody('hello world'); + $this->assertEquals('hello world', $newResponse->getBody()); + } + + public function test_with_body_does_not_mutate_original(): void + { + $response = $this->createResponse(); + $response->withBody('changed'); + $this->assertEquals('', $response->getBody()); + } + + public function test_body_json_content(): void + { + $json = json_encode(['status' => 'ok']); + $response = $this->createResponse()->withBody($json); + $this->assertEquals($json, $response->getBody()); + } + + // ── send() ─────────────────────────────────────────────────────────────── + + public function test_send_does_not_throw(): void + { + $response = $this->createResponse()->withBody('test'); + // send() calls header() and http_response_code() which throw in CLI + // PHPUnit suppresses these in some versions, but we catch them + $this->expectNotToPerformAssertions(); + $response->send(); + } +} |
