Skip to content

Commit 5c1121f

Browse files
authored
Test: Add unit tests for Translator, NavigationBuilder and TOC Plugin (#1)
- Added Pest PHP, PHPStan and PHP-CS-Fixer configuration - Added unit tests for NavigationBuilder - Added unit tests for TableOfContentsPlugin (Regex logic) - Added CI workflow for quality checks
1 parent 383ac08 commit 5c1121f

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ parameters:
33
paths:
44
- src
55
- bin
6+
- tests
67
excludePaths:
78
- vendor
89
tmpDir: var/cache/phpstan
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use LiteDocs\Core\NavigationBuilder;
6+
7+
test('navigation builder converts flat config to tree', function () {
8+
$builder = new NavigationBuilder();
9+
10+
$config = [
11+
'Introduction' => 'index.md',
12+
'Guide' => [
13+
'Installation' => 'install.md'
14+
]
15+
];
16+
17+
expect($builder)->toBeInstanceOf(NavigationBuilder::class);
18+
});
19+
20+
test('getFlatList flattens a tree correctly', function () {
21+
$builder = new NavigationBuilder();
22+
23+
$tree = [
24+
[
25+
'title' => 'Home',
26+
'url' => 'index.html',
27+
],
28+
[
29+
'title' => 'Guide',
30+
'children' => [
31+
[
32+
'title' => 'Install',
33+
'url' => 'install.html'
34+
]
35+
]
36+
]
37+
];
38+
39+
$flat = $builder->getFlatList($tree);
40+
41+
expect($flat)->toBeArray();
42+
expect($flat)->toHaveCount(2);
43+
expect($flat[0]['url'])->toBe('index.html');
44+
expect($flat[1]['url'])->toBe('install.html');
45+
});

tests/Unit/Core/TranslatorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use LiteDocs\Core\Translator;
6+
7+
test('translator can be instantiated', function () {
8+
$translator = new Translator(__DIR__, __DIR__, 'lite');
9+
10+
expect($translator)->toBeInstanceOf(Translator::class);
11+
});
12+
13+
test('translator returns array', function () {
14+
$translator = new Translator(__DIR__, __DIR__, 'lite');
15+
$result = $translator->getTranslations('en');
16+
17+
expect($result)->toBeArray();
18+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use LiteDocs\Event\PageEvent;
6+
use LiteDocs\Plugin\TableOfContentsPlugin;
7+
8+
test('toc plugin extracts h2 headers and generates ids', function () {
9+
$plugin = new TableOfContentsPlugin();
10+
11+
$html = '<h1>Main Title</h1> <h2>Introduction</h2> <p>Text</p> <h2>Conclusion</h2>';
12+
13+
$event = new PageEvent($html, 'test.md', []);
14+
15+
$plugin->onAfterParse($event);
16+
17+
$newHtml = $event->content;
18+
19+
expect($newHtml)->toContain('id="introduction"');
20+
expect($newHtml)->toContain('id="conclusion"');
21+
22+
$context = $event->getContext();
23+
24+
expect($context)->toHaveKey('toc');
25+
expect($context['toc'])->toBeArray();
26+
expect($context['toc'])->toHaveCount(2);
27+
28+
expect($context['toc'][0]['title'])->toBe('Introduction');
29+
expect($context['toc'][0]['url'])->toBe('#introduction');
30+
31+
expect($context['toc'][1]['title'])->toBe('Conclusion');
32+
expect($context['toc'][1]['url'])->toBe('#conclusion');
33+
});
34+
35+
test('toc plugin handles duplicate headers correctly', function () {
36+
$plugin = new TableOfContentsPlugin();
37+
38+
$html = '<h2>Installation</h2> <p>...</p> <h2>Installation</h2>';
39+
40+
$event = new PageEvent($html, 'test.md', []);
41+
$plugin->onAfterParse($event);
42+
43+
$context = $event->getContext();
44+
45+
expect($context['toc'][0]['url'])->toBe('#installation');
46+
47+
expect($context['toc'][1]['url'])->not->toBe('#installation');
48+
expect($context['toc'][1]['url'])->toContain('installation-');
49+
});

0 commit comments

Comments
 (0)