Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomano committed Dec 20, 2024
1 parent c9df15c commit ea334e4
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 18 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"scripts": {
"check": "vendor/bin/rector process --dry-run --clear-cache",
"tests": "vendor/bin/pest --colors=always",
"tests-coverage": "vendor/bin/pest --colors=always --coverage --min=70",
"tests-coverage-clover": "vendor/bin/pest --colors=always --min=70 --coverage-clover coverage.xml",
"tests-coverage-html": "vendor/bin/pest --colors=always --min=70 --coverage-html coverage",
"tests-coverage": "vendor/bin/pest --colors=always --coverage --min=90",
"tests-coverage-clover": "vendor/bin/pest --colors=always --min=90 --coverage-clover coverage.xml",
"tests-coverage-html": "vendor/bin/pest --colors=always --min=90 --coverage-html coverage",
"post-update-cmd": "cd src/Sources/Optimus && composer update --no-dev -o"
},
"config": {
Expand Down
25 changes: 19 additions & 6 deletions tests/Handlers/SearchTermHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,26 @@ public function insert(
});
});

it('checks showChart method', function () {
Config::$modSettings['optimus_log_search'] = true;
describe('showChart method', function () {
it('checks case with disabled optimus_log_search', function () {
Utils::$context['template_layers'] = [];

Utils::$context['search_terms'] = [['test']];
Config::$modSettings['optimus_log_search'] = false;

$showChart = new ReflectionMethod($this->handler, 'showChart');
$showChart->invoke($this->handler);

expect(Utils::$context['template_layers'])->toBeEmpty();
});

$showChart = new ReflectionMethod($this->handler, 'showChart');
$showChart->invoke($this->handler);
it('checks normal case', function () {
Config::$modSettings['optimus_log_search'] = true;

Utils::$context['search_terms'] = [['test']];

expect(Utils::$context['template_layers'])->toContain('search_terms');
$showChart = new ReflectionMethod($this->handler, 'showChart');
$showChart->invoke($this->handler);

expect(Utils::$context['template_layers'])->toContain('search_terms');
});
});
87 changes: 81 additions & 6 deletions tests/Handlers/TagHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,36 @@ public function testQuery($query, $params = []): array
];
}

if (str_contains($query, 'SELECT name')) {
return ['Keyword 1'];
}

if (str_contains($query, 'WHERE name LIKE {string:search}')) {
if (str_contains($query, 'SELECT id, name')) {
return [
['id' => '1', 'name' => 'Keyword 1'],
['id' => '2', 'name' => 'Keyword 2'],
];
}

if (str_contains($query, 'SELECT name')) {
return ['Keyword 1'];
}

if (str_contains($query, 'WHERE name = {string:name}')) {
return ['id' => '1'];
}

/* if (str_contains($query, 'DELETE FROM {db_prefix}optimus_log_keywords')) {
return ['1'];
} */

return [];
}

public function fetch_row($result): array|false|null
{
if ($result === ['id' => '1']) {
return ['1'];
}

return $result ?? false;
}
};
});

Expand Down Expand Up @@ -191,6 +204,7 @@ public function testQuery($query, $params = []): array

test('prepareDisplayContext method', function () {
Config::$modSettings['optimus_show_keywords_block'] = false;
Config::$modSettings['optimus_use_color_tags'] = true;

expect($this->handler->prepareDisplayContext([]))->toBeNull();

Expand All @@ -211,7 +225,11 @@ public function testQuery($query, $params = []): array
});

test('createTopic method', function () {
expect($this->handler->createTopic([], [], []))
Config::$modSettings['optimus_allow_change_topic_keywords'] = true;

$_REQUEST['optimus_keywords'] = 'key_1,key_2';

expect($this->handler->createTopic([], ['id' => 1], ['id' => 1]))
->toBeNull();
});

Expand All @@ -234,6 +252,7 @@ public function testQuery($query, $params = []): array
Config::$modSettings['optimus_allow_change_topic_keywords'] = true;

Utils::$context['is_new_topic'] = true;
Utils::$context['is_first_post'] = true;

$this->request = Request::createFromGlobals();
$this->request->request->set('optimus_keywords', 'bar');
Expand Down Expand Up @@ -330,3 +349,59 @@ public function testQuery($query, $params = []): array
expect(isset(Utils::$context['optimus_keywords']))->toBeFalse();
});
});

test('getAllKeywords method', function () {
$getAllKeywords = new ReflectionMethod($this->handler, 'getAllKeywords');
$result = $getAllKeywords->invoke($this->handler);

expect($result)->toBeArray();
});

test('loadAssets method', function () {
$loadAssets = new ReflectionMethod($this->handler, 'loadAssets');
$loadAssets->invoke($this->handler);

expect(Utils::$context['css_files'])->toHaveKey('virtual-select.min_css')
->and(Utils::$context['javascript_files'])->toHaveKey('virtual-select.min_js');
});

test('getIdByName method', function () {
$getIdByName = new ReflectionMethod($this->handler, 'getIdByName');
$result = $getIdByName->invoke($this->handler, 'id');

expect($result)->toBe(1);
});

test('addToDatabase method', function () {
$addToDatabase = new ReflectionMethod($this->handler, 'addToDatabase');
$result = $addToDatabase->invoke($this->handler, 'test');

expect($result)->toBe(1);
});

test('preparedKeywords method', function () {
$_REQUEST['optimus_keywords'] = 'foo,bar';

$preparedKeywords = new ReflectionMethod($this->handler, 'preparedKeywords');
$result = $preparedKeywords->invoke($this->handler);

expect($result)->toBe(['foo', 'bar']);
});

test('modify method', function () {
Config::$modSettings['optimus_allow_change_topic_keywords'] = true;

$_REQUEST['optimus_keywords'] = 'key_1,key_2';

$modify = new ReflectionMethod($this->handler, 'modify');
$result = $modify->invoke($this->handler, 1, 1);

expect($result)->toBeNull();
});

test('remove method', function () {
$remove = new ReflectionMethod($this->handler, 'remove');
$result = $remove->invoke($this->handler, [1, 2], 1);

expect($result)->toBeNull();
});
2 changes: 1 addition & 1 deletion tests/Handlers/TopicHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function testQuery($query, $params = []): array
});

test('modifyPost method', function () {
expect($this->handler->modifyPost([], [], [], []))->toBeNull();
expect($this->handler->modifyPost([], [], ['id' => 10], ['id' => 1, 'first_msg' => 10]))->toBeNull();
});

test('postEnd method - create', function () {
Expand Down
5 changes: 5 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
User::$info['language'] = 'english';

Lang::$txt['lang_dictionary'] = 'en';
Lang::$txt['no_matches'] = 'No matches';
Lang::$txt['search'] = 'Search';
Lang::$txt['remove'] = 'Remove';

Config::$boardurl = 'https://example.com';
Config::$scripturl = Config::$boardurl . '/index.php';
Expand All @@ -62,6 +65,7 @@

Utils::$context['forum_name'] = 'Test Forum';
Utils::$context['admin_menu_name'] = 'admin';
Utils::$context['right_to_left'] = false;

Utils::$smcFunc['substr'] = fn($string, $offset, $length) => substr($string, $offset, $length);
Utils::$smcFunc['strlen'] = fn($string) => strlen($string);
Expand Down Expand Up @@ -132,6 +136,7 @@ function cache_get_data(string $key, int $ttl = 120): ?array
{
if ($key == 'optimus_search_terms') return null;
if ($key == 'optimus_topic_keywords') return null;
if ($key == 'optimus_all_keywords') return null;

return [];
}
Expand Down
9 changes: 7 additions & 2 deletions tests/Utils/CopyrightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@

$link = Copyright::getLink();

expect($link)->toContain('https://dragomano.ru/mods/optimus')
->and($link)->toContain(' © 2010–' . date('Y') . ', Bugo');
expect($link)->toContain('https://dragomano.ru/mods/optimus');

unset(Lang::$txt['lang_dictionary']);
});

it('gets years', function () {
$link = Copyright::getYears();

expect($link)->toContain(' © 2010–' . date('Y') . ', Bugo');
});

0 comments on commit ea334e4

Please sign in to comment.