From 99668b6fbe920796290d0b2b64ae5c1edf270a99 Mon Sep 17 00:00:00 2001 From: Bugo Date: Wed, 4 Dec 2024 11:29:36 +0500 Subject: [PATCH] Update tests --- tests/Pest.php | 25 +++++++++++--- tests/Unit/DbFuncMapperTest.php | 59 +++++++++++++++++++++++++++++++-- tests/Unit/ErrorHandlerTest.php | 8 +++++ 3 files changed, 86 insertions(+), 6 deletions(-) diff --git a/tests/Pest.php b/tests/Pest.php index 0eec0cf..197b8e3 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -18,11 +18,10 @@ Config::$sourcedir = __DIR__ . DIRECTORY_SEPARATOR . 'files'; Utils::$context['css_header'] = []; - Utils::$context['javascript_inline'] = ['defer' => [], 'standard' => []]; + Utils::$context['num_errors'] = 0; Utils::$smcFunc['htmlspecialchars'] = fn(...$params) => htmlspecialchars(...$params); - Utils::$smcFunc['db_query'] = fn(...$params) => new \stdClass(); Utils::$smcFunc['db_fetch_row'] = fn(...$params) => []; Utils::$smcFunc['db_fetch_assoc'] = fn(...$params) => []; @@ -33,8 +32,17 @@ Utils::$smcFunc['db_transaction'] = fn(...$params) => true; Utils::$smcFunc['db_optimize_table'] = fn(...$params) => 0; Utils::$smcFunc['db_list_tables'] = fn(...$params) => []; - Utils::$smcFunc['db_get_version'] = fn(...$params) => ''; - Utils::$smcFunc['db_create_table'] = fn(...$params) => false; + Utils::$smcFunc['db_get_version'] = fn() => ''; + Utils::$smcFunc['db_get_vendor'] = fn() => ''; + Utils::$smcFunc['db_add_column'] = fn(...$params) => true; + Utils::$smcFunc['db_add_index'] = fn(...$params) => true; + Utils::$smcFunc['db_change_column'] = fn(...$params) => true; + Utils::$smcFunc['db_create_table'] = fn(...$params) => true; + Utils::$smcFunc['db_table_structure'] = fn(...$params) => []; + Utils::$smcFunc['db_list_columns'] = fn(...$params) => []; + Utils::$smcFunc['db_list_indexes'] = fn(...$params) => []; + Utils::$smcFunc['db_remove_column'] = fn(...$params) => true; + Utils::$smcFunc['db_remove_index'] = fn(...$params) => true; })->in(__DIR__); /* @@ -243,10 +251,19 @@ function fatal_lang_error(...$params): void if (! function_exists('log_error')) { function log_error(...$params): string { + Utils::$context['num_errors']++; + return ''; } } +if (! function_exists('display_db_error')) { + function display_db_error(): void + { + echo '

Connection Problems

'; + } +} + if (! function_exists('call_integration_hook')) { function call_integration_hook(string $hook, array $args = []): array { diff --git a/tests/Unit/DbFuncMapperTest.php b/tests/Unit/DbFuncMapperTest.php index 28b7ec6..0d1b357 100644 --- a/tests/Unit/DbFuncMapperTest.php +++ b/tests/Unit/DbFuncMapperTest.php @@ -55,12 +55,67 @@ ->toBeString(); }); +test('get_vendor method', function () { + expect($this->db->get_vendor()) + ->toBeString(); +}); + +test('add_column method', function () { + expect($this->db->add_column('', [])) + ->toBeTrue(); +}); + +test('add_index method', function () { + expect($this->db->add_index('', [])) + ->toBeTrue(); +}); + +test('change_column method', function () { + expect($this->db->change_column('', '', [])) + ->toBeTrue(); +}); + test('create_table method', function () { expect($this->db->create_table('', [])) - ->toBeFalse(); + ->toBeTrue(); +}); + +test('table_structure method', function () { + expect($this->db->table_structure('name')) + ->toBeArray(); +}); + +test('list_columns method', function () { + expect($this->db->list_columns('name')) + ->toBeArray(); +}); + +test('list_indexes method', function () { + expect($this->db->list_indexes('name')) + ->toBeArray(); +}); + +test('remove_column method', function () { + expect($this->db->remove_column('', '')) + ->toBeTrue(); +}); + +test('remove_index method', function () { + expect($this->db->remove_index('', '')) + ->toBeTrue(); +}); + +test('__call not implemented method', function () { + Utils::$smcFunc['db_some_func'] = fn(...$params) => [$params]; + + expect(Utils::$context['num_errors'])->toBe(0); + + $this->db->some_func($this->params); + + expect(Utils::$context['num_errors'])->toBe(1); }); -test('__call method', function () { +test('__call unknown method', function () { expect($this->db->unknown($this->params)) ->toBeFalse(); }); diff --git a/tests/Unit/ErrorHandlerTest.php b/tests/Unit/ErrorHandlerTest.php index d576d1b..be24597 100644 --- a/tests/Unit/ErrorHandlerTest.php +++ b/tests/Unit/ErrorHandlerTest.php @@ -18,3 +18,11 @@ test('log method', function () { expect(ErrorHandler::log('test'))->toBeString(); }); + +test('displayDbError method', function () { + ob_start(); + ErrorHandler::displayDbError(); + $result = ob_get_clean(); + + expect($result)->toContain('Connection Problems'); +});