Skip to content

Commit b421340

Browse files
committed
tests(unit): ConfigTest, DocsLogicTest and TranslatorTest
1 parent d018b10 commit b421340

File tree

10 files changed

+199
-22
lines changed

10 files changed

+199
-22
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ app/cache/
99
biome.*
1010
.phpunit.result.cache
1111
node_modules/
12+
tests/cache/
13+
.coverage

app/utils/DocsLogic.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@
55
use app\middleware\HeaderSecurityMiddleware;
66
use DOMDocument;
77
use DOMXPath;
8+
use flight\Engine;
89

910
class DocsLogic {
10-
11-
/**
12-
* DocsLogic constructor.
13-
*
14-
* @param CustomEngine $app Flight Engine
15-
*/
16-
public function __construct(protected $app) {
17-
11+
public function __construct(protected Engine $app) {
12+
//
1813
}
1914

2015
/**
@@ -198,4 +193,4 @@ protected function wrapContentInDiv(string $html): string {
198193

199194
return $d;
200195
}
201-
}
196+
}

tests/Feature/StatusCheckTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
use PHPUnit\Framework\Attributes\Test;
88

99
final class StatusCheckTest extends FeatureTestCase {
10-
#[Test]
11-
public function apiIsRunning(): void {
12-
$response = self::$client->get('./api/status');
10+
#[Test]
11+
public function apiIsRunning(): void {
12+
$response = self::$client->get('./api/status');
1313

14-
self::assertSame(200, $response->getStatusCode());
14+
self::assertSame(200, $response->getStatusCode());
1515

16-
self::assertSame(
17-
'application/json',
18-
mb_strtolower($response->getHeaderLine('content-type'))
19-
);
16+
self::assertSame(
17+
'application/json',
18+
mb_strtolower($response->getHeaderLine('content-type'))
19+
);
2020

21-
self::assertSame('{"status":"ok"}', $response->getBody()->getContents());
22-
}
21+
self::assertSame('{"status":"ok"}', $response->getBody()->getContents());
22+
}
2323
}

tests/Unit/ConfigTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit;
6+
7+
use app\utils\Config;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\Attributes\Test;
10+
11+
#[CoversClass(Config::class)]
12+
final class ConfigTest extends UnitTestCase {
13+
#[Test]
14+
function it_can_set_and_get_a_config(): void {
15+
$dsn = 'sqlite::memory:';
16+
17+
$config = new Config([
18+
'PDO_DSN' => $dsn
19+
]);
20+
21+
self::assertSame($dsn, $config['PDO_DSN']);
22+
}
23+
}

tests/Unit/DocsLogicTest.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit;
6+
7+
use app\utils\DocsLogic;
8+
use DOMDocument;
9+
use flight\Cache;
10+
use flight\Container;
11+
use flight\Engine;
12+
use Latte\Engine as LatteEngine;
13+
use Latte\Essential\TranslatorExtension;
14+
use Latte\Loaders\FileLoader;
15+
use Parsedown;
16+
use PHPUnit\Framework\Attributes\CoversClass;
17+
use PHPUnit\Framework\Attributes\Test;
18+
use Tests\Utils\CustomTranslator;
19+
20+
#[CoversClass(DocsLogic::class)]
21+
final class DocsLogicTest extends UnitTestCase {
22+
private Container $container;
23+
private DocsLogic $docsLogic;
24+
private Engine $engine;
25+
26+
function setUp(): void {
27+
$this->container = new Container;
28+
29+
$this->container->set(
30+
Engine::class,
31+
function (): Engine {
32+
$this->engine = new Engine;
33+
$this->engine->register('translator', CustomTranslator::class);
34+
35+
$this->engine->register(
36+
'cache',
37+
Cache::class,
38+
[__DIR__ . '/../cache/']
39+
);
40+
41+
$this->engine->register('parsedown', Parsedown::class);
42+
43+
$this->engine->register(
44+
'latte',
45+
LatteEngine::class,
46+
[],
47+
function (LatteEngine $latte): void {
48+
$latte->setTempDirectory(__DIR__ . '/../cache/');
49+
$latte->setLoader(new FileLoader(__DIR__ . '/../views/'));
50+
51+
$latte->addExtension(new TranslatorExtension(
52+
$this
53+
->container
54+
->get(CustomTranslator::class)
55+
->translate(...),
56+
));
57+
}
58+
);
59+
60+
return $this->engine;
61+
}
62+
);
63+
64+
$this->docsLogic = $this->container->get(DocsLogic::class);
65+
$_SERVER['HTTP_HOST'] = 'localhost';
66+
}
67+
68+
#[Test]
69+
function it_renders_a_page(): void {
70+
$this->engine->request()->url = '/test?var=value';
71+
72+
self::expectOutputString(<<<'html'
73+
<a href="http://localhost/test"></a>
74+
<h1>value</h1>
75+
76+
html);
77+
78+
$this->docsLogic->renderPage('test.latte', ['variable' => 'value']);
79+
}
80+
81+
#[Test]
82+
function it_can_setup_translator(): void {
83+
$translator = $this->docsLogic->setupTranslatorService('es', 'v3');
84+
85+
assert($translator instanceof CustomTranslator);
86+
87+
self::assertSame('es', $translator->getLanguage());
88+
self::assertSame('v3', $translator->getVersion());
89+
}
90+
91+
#[Test]
92+
function it_compiles_single_page(): void {
93+
ob_start();
94+
$this->docsLogic->compileSinglePage('es', 'v3', 'about');
95+
$html = ob_get_clean();
96+
97+
self::assertStringContainsString('composer require flightphp/core', $html);
98+
}
99+
100+
#[Test]
101+
function it_compiles_scrollspy_page(): void {
102+
ob_start();
103+
$this->docsLogic->compileScrollspyPage('es', 'v3', 'install', 'install');
104+
$html = new DOMDocument;
105+
@$html->loadHTML(ob_get_clean());
106+
$navLinks = $html->getElementsByTagName('a');
107+
$target = $navLinks->item(0)->getAttribute('data-target');
108+
$id = $navLinks->item(0)->getAttribute('id');
109+
110+
self::assertTrue($navLinks->count() >= 1);
111+
self::assertStringStartsWith('#', $target);
112+
self::assertStringStartsWith('#', $id);
113+
}
114+
}

tests/Unit/UnitTestCase.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PHPUnit\Framework\TestCase;
88

9-
abstract class UnitTestCase extends TestCase
10-
{
11-
//
9+
abstract class UnitTestCase extends TestCase {
10+
//
1211
}

tests/Utils/CustomTranslator.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Utils;
6+
7+
use app\utils\Translator;
8+
9+
final class CustomTranslator extends Translator {
10+
function getLanguage(): string {
11+
return $this->language;
12+
}
13+
14+
function getVersion(): string {
15+
return $this->version;
16+
}
17+
}

tests/views/single_page.latte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{$markdown|noescape}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<div class="row">
2+
<div class="col-md-3">
3+
<nav id="navbar-learn" class="sticky-top flex-column align-items-stretch pe-4 border-end">
4+
<nav class="nav nav-pills flex-column">
5+
{foreach $heading_data as $heading}
6+
<a
7+
class="nav-link" href="#{$heading['id']}"
8+
data-target="#{$heading['id']}">
9+
{$heading['title']}
10+
</a>
11+
{/foreach}
12+
</nav>
13+
</nav>
14+
</div>
15+
16+
<div class="col-md-9">
17+
<div
18+
data-bs-spy="scroll"
19+
data-bs-target="#navbar-learn"
20+
data-bs-smooth-scroll="true"
21+
tabindex="0">
22+
</div>
23+
</div>
24+
</div>

tests/views/test.latte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<a href="{$url}"></a>
2+
<h1>{$variable}</h1>

0 commit comments

Comments
 (0)