Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomano committed Dec 17, 2024
1 parent 7b8290e commit cfccde0
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 33 deletions.
61 changes: 31 additions & 30 deletions tests/Handlers/ErrorPageHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Bugo\Compat\Board;
use Bugo\Compat\Config;
use Bugo\Compat\Lang;
use Bugo\Compat\Theme;
use Bugo\Compat\Utils;
use Bugo\Optimus\Handlers\ErrorPageHandler;
Expand All @@ -13,63 +14,63 @@
});

describe('handleWrongActions method', function () {
beforeEach(function () {
Config::$modSettings['optimus_errors_for_wrong_actions'] = false;
beforeEach(function () {
Config::$modSettings['optimus_errors_for_wrong_actions'] = false;

Theme::$current->settings['catch_action'] = [];
});
Theme::$current->settings['catch_action'] = [];
});

it('sets fatal_error template when setting is enabled', function () {
Config::$modSettings['optimus_errors_for_wrong_actions'] = true;
it('sets fatal_error template when setting is enabled', function () {
Config::$modSettings['optimus_errors_for_wrong_actions'] = true;

$this->handler->handleWrongActions();
$this->handler->handleWrongActions();

expect(Theme::$current->settings['catch_action']['sub_template'])
->toBe('fatal_error');
});
expect(Theme::$current->settings['catch_action']['sub_template'])
->toBe('fatal_error');
});

it('does not set template when setting is disabled', function () {
Config::$modSettings['optimus_errors_for_wrong_actions'] = false;
it('does not set template when setting is disabled', function () {
Config::$modSettings['optimus_errors_for_wrong_actions'] = false;

$this->handler->handleWrongActions();
$this->handler->handleWrongActions();

expect(Theme::$current->settings['catch_action'])
->toBeEmpty();
});
expect(Theme::$current->settings['catch_action'])
->toBeEmpty();
});

it('handles null catch_action settings', function () {
Config::$modSettings['optimus_errors_for_wrong_actions'] = true;
Theme::$current->settings['catch_action'] = null;
it('handles null catch_action settings', function () {
Config::$modSettings['optimus_errors_for_wrong_actions'] = true;
Theme::$current->settings['catch_action'] = null;

$this->handler->handleWrongActions();
$this->handler->handleWrongActions();

expect(Theme::$current->settings['catch_action'])
->toBeArray()
->toHaveKey('sub_template', 'fatal_error');
});
expect(Theme::$current->settings['catch_action'])
->toBeArray()
->toHaveKey('sub_template', 'fatal_error');
});
});

describe('handleWrongBoardsTopics method', function () {
it('checks case when board_info[error] = exist', function () {
it('checks case when board does not exist', function () {
Config::$modSettings['optimus_errors_for_wrong_boards_topics'] = true;

Board::$info['error'] = 'exist';

$this->handler->handleWrongBoardsTopics();

expect(Utils::$context['error_link'])
->toBe('javascript:history.go(-1)');
expect(Utils::$context['page_title'])
->toBe(Lang::$txt["optimus_404_page_title"]);
});

it('checks case when board_info[error] = access', function () {
it('checks case when board has no access', function () {
Config::$modSettings['optimus_errors_for_wrong_boards_topics'] = true;

Board::$info['error'] = 'access';

$this->handler->handleWrongBoardsTopics();

expect(Utils::$context['error_link'])
->toBe('javascript:history.go(-1)');
expect(Utils::$context['page_title'])
->toBe(Lang::$txt["optimus_403_page_title"]);
});

it('checks case with disabled setting', function () {
Expand Down
5 changes: 3 additions & 2 deletions tests/Services/FileSystemTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php declare(strict_types=1);

use Bugo\Optimus\Services\FileSystem;
use Bugo\Optimus\Services\FileSystemException;

beforeEach(function () {
$this->tempDir = sys_get_temp_dir() . '/optimus_test_' . uniqid();
Expand Down Expand Up @@ -28,7 +29,7 @@
});

it('writes gzipped file successfully', function () {
if (!function_exists('gzopen')) {
if (! function_exists('gzopen')) {
$this->markTestSkipped('Gzip functions are not available');
}

Expand All @@ -51,7 +52,7 @@

try {
expect(fn() => $fileSystem->writeFile('test.txt', 'content'))
->toThrow(RuntimeException::class);
->toThrow(FileSystemException::class);
} finally {
restore_error_handler();
}
Expand Down
1 change: 0 additions & 1 deletion tests/Services/SitemapDataServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ public function testQuery($query, $params = []): array

$topics = $topicsProperty->getValue($this->sitemapDataService);


expect($topics)->toHaveCount(2)
->and($topics[1])->toHaveKeys(['url', 'last_date', 'num_replies', 'subject'])
->and($topics[1]['subject'])->toBe('Test Topic')
Expand Down
65 changes: 65 additions & 0 deletions tests/Services/XmlGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php declare(strict_types=1);

use Bugo\Optimus\Services\XmlGenerator;
use Bugo\Optimus\Services\XmlGeneratorException;

beforeEach(function () {
$this->xmlGenerator = new XmlGenerator('https://example.com');
});

it('generates XML from array with default root element', function () {
$data = [
['loc' => 'https://example.com/item1', 'lastmod' => date('Y-m-d')],
['loc' => 'https://example.com/item2', 'lastmod' => date('Y-m-d')],
];

$xml = $this->xmlGenerator->generate($data);

expect($xml)->toContain('<?xml version="1.0" encoding="UTF-8"?>')
->and($xml)->toContain('<?xml-stylesheet type="text/xsl" href="https://example.com?action=sitemap_xsl"?>')
->and($xml)->toContain('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">')
->and($xml)->toContain('<loc>https://example.com/item1</loc>')
->and($xml)->toContain('<lastmod>' . date('Y-m-d') . '</lastmod>')
->and($xml)->toContain('<loc>https://example.com/item2</loc>')
->and($xml)->toContain('<lastmod>' . date('Y-m-d') . '</lastmod>')
->and($xml)->toContain('</urlset>');
});

it('generates XML with custom root element', function () {
$data = [
['loc' => 'https://example.com/item1', 'lastmod' => date('Y-m-d')],
];

$xml = $this->xmlGenerator->generate($data, ['isIndex' => true]);

expect($xml)->toContain('<?xml version="1.0" encoding="UTF-8"?>')
->and($xml)->toContain('<?xml-stylesheet type="text/xsl" href="https://example.com?action=sitemap_xsl"?>')
->and($xml)->toContain('<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">')
->and($xml)->toContain('<loc>https://example.com/item1</loc>')
->and($xml)->toContain('<lastmod>' . date('Y-m-d') . '</lastmod>')
->and($xml)->toContain('</sitemapindex>');
});

it('generates XML with extended data types', function () {
$data = [
[
'loc' => 'https://example.com/item1',
'image:image' => ['image:loc' => 'https://example.com/image.png'],
'mobile:mobile' => ['mobile:loc' => 'https://example.com/mobile'],
],
];

$xml = $this->xmlGenerator->generate($data, ['mobile' => true, 'images' => true]);

expect($xml)->toContain('http://www.google.com/schemas/sitemap-mobile/1.0')
->and($xml)->toContain('http://www.google.com/schemas/sitemap-image/1.1')
->and($xml)->toContain('<image:loc>https://example.com/image.png</image:loc>')
->and($xml)->toContain('<mobile:loc>https://example.com/mobile</mobile:loc>');
});

it('generates empty XML node for invalid data', function () {
$data = [['one', 'two', 'three']];

expect($this->xmlGenerator->generate($data, ['isIndex' => true]))
->toThrow(XmlGeneratorException::class);
})->throws(XmlGeneratorException::class);

0 comments on commit cfccde0

Please sign in to comment.