Skip to content

Commit a4aa344

Browse files
committed
tests(e2e): add tests for bootstrap, routes and about
1 parent 635d6f0 commit a4aa344

File tree

6 files changed

+124
-1
lines changed

6 files changed

+124
-1
lines changed

app/config/routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
// Pick up old routes that didn't use to have a language and version header
4848
$app->route('/@section:[\w\-]{3,}(/@sub_section:[\w\-]{3,})', function (string $section, ?string $sub_section = '') use ($app): void {
4949
$language = Translator::getLanguageFromRequest();
50-
$app->redirect("/{$language}/v3/$section/$sub_section");
50+
$app->redirect("/{$language}/v3/$section/$sub_section/");
5151
});
5252

5353
/*

app/utils/CustomFlight.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* @method LatteEngine latte()
1616
* @method Parsedown parsedown()
1717
* @method Translator translator()
18+
* @deprecated
1819
*/
1920
class CustomEngine extends Engine {
2021
}

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
44
bootstrap="vendor/autoload.php"
55
colors="true"
6+
failOnWarning="true"
67
>
78
<testsuites>
89
<testsuite name="Unit">

tests/Feature/AboutTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Feature;
6+
7+
use DOMDocument;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\Attributes\Test;
10+
11+
final class AboutTest extends FeatureTestCase {
12+
#[Test]
13+
#[DataProvider('aboutPageUrisDataProvider')]
14+
function it_should_return_the_about_page(string $uri): void {
15+
$response = self::$client->get(str_replace('//', '/', "./es/v3/$uri"));
16+
$html = new DOMDocument;
17+
@$html->loadHTML($response->getBody()->getContents());
18+
$title = $html->getElementsByTagName('title')->item(0);
19+
20+
self::assertSame(200, $response->getStatusCode());
21+
self::assertStringContainsString('Acerca de', $title->textContent);
22+
23+
self::assertStringContainsString(
24+
'Flight PHP Framework',
25+
$title->textContent
26+
);
27+
}
28+
29+
static function aboutPageUrisDataProvider(): array {
30+
return [
31+
['/'],
32+
['/about'],
33+
];
34+
}
35+
}

tests/Feature/BootstrapTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Feature;
6+
7+
use GuzzleHttp\Exception\GuzzleException;
8+
use PHPUnit\Framework\Attributes\Test;
9+
10+
final class BootstrapTest extends FeatureTestCase {
11+
#[Test]
12+
function it_returns_500_when_no_config_file_exists(): void {
13+
$configFilePath = dirname(__DIR__, 2) . '/app/config/config.php';
14+
$backupConfigFilePath = "$configFilePath.backup";
15+
16+
if (file_exists($configFilePath)) {
17+
rename($configFilePath, $backupConfigFilePath);
18+
}
19+
20+
try {
21+
self::$client->get('./');
22+
} catch (GuzzleException $exception) {
23+
$message = $exception->getMessage();
24+
25+
self::assertStringContainsString('500', $message);
26+
27+
self::assertStringContainsString(
28+
'Config file not found. Please create a config.php file in the app/config directory to get started.',
29+
$message
30+
);
31+
}
32+
33+
rename($backupConfigFilePath, $configFilePath);
34+
}
35+
}

tests/Feature/RedirectionsTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Feature;
6+
7+
use PHPUnit\Framework\Attributes\Test;
8+
9+
final class RedirectionsTest extends FeatureTestCase {
10+
#[Test]
11+
function it_redirects_to_default_language_and_version_when_nothing_is_specified(): void {
12+
$response = self::$client->get('./', [
13+
'allow_redirects' => false
14+
]);
15+
16+
self::assertSame(303, $response->getStatusCode());
17+
18+
self::assertStringContainsString(
19+
'/en/v3/',
20+
$response->getHeaderLine('location')
21+
);
22+
}
23+
24+
#[Test]
25+
function it_redirects_to_version_when_only_language_is_specified(): void {
26+
$response = self::$client->get('/en', [
27+
'allow_redirects' => false
28+
]);
29+
30+
self::assertSame(303, $response->getStatusCode());
31+
32+
self::assertStringContainsString(
33+
'/en/v3/',
34+
$response->getHeaderLine('location')
35+
);
36+
}
37+
38+
#[Test]
39+
function it_redirects_to_default_language_and_version_when_only_section_and_subsection_is_specified(): void {
40+
$response = self::$client->get('/section/subsection', [
41+
'allow_redirects' => false
42+
]);
43+
44+
self::assertSame(303, $response->getStatusCode());
45+
46+
self::assertStringContainsString(
47+
'/en/v3/section/subsection/',
48+
$response->getHeaderLine('location')
49+
);
50+
}
51+
}

0 commit comments

Comments
 (0)