Skip to content

Commit 323c811

Browse files
authored
Merge pull request #11 from gacela-project/route-not-found-404
Trigger NotFound404Controller if no Route was found
2 parents 80dd2fa + 7857558 commit 323c811

16 files changed

+112
-104
lines changed

phpunit.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
</php>
1919

2020
<testsuites>
21-
<testsuite name="unit">
22-
<directory suffix="Test.php">tests/Unit</directory>
21+
<testsuite name="feature">
22+
<directory suffix="Test.php">tests/Feature</directory>
2323
</testsuite>
2424
</testsuites>
2525

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gacela\Router\Controllers;
6+
7+
final class NotFound404Controller
8+
{
9+
public function __invoke(): void
10+
{
11+
header('HTTP/1.0 404 Not Found');
12+
}
13+
}

src/Router/Router.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Gacela\Router;
66

7+
use Gacela\Router\Controllers\NotFound404Controller;
78
use Gacela\Router\Entities\Route;
89

910
final class Router
@@ -20,19 +21,17 @@ public static function configure(callable $fn): void
2021

2122
$route = self::findRoute($routerConfigurator);
2223

23-
if ($route) {
24-
echo $route->run($mappingInterfaces);
25-
}
24+
echo $route->run($mappingInterfaces);
2625
}
2726

28-
private static function findRoute(Routes $routerConfigurator): ?Route
27+
private static function findRoute(Routes $routerConfigurator): Route
2928
{
3029
foreach ($routerConfigurator->routes() as $route) {
3130
if ($route->requestMatches()) {
3231
return $route;
3332
}
3433
}
3534

36-
return null;
35+
return new Route('', '/', NotFound404Controller::class);
3736
}
3837
}

tests/Feature/HeaderTestCase.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GacelaTest\Feature;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
include_once __DIR__ . '/header.php';
10+
11+
abstract class HeaderTestCase extends TestCase
12+
{
13+
/**
14+
* @runInSeparateProcess
15+
*/
16+
protected function setUp(): void
17+
{
18+
global $testHeaders;
19+
20+
$testHeaders = null;
21+
}
22+
23+
protected function headers(): array
24+
{
25+
global $testHeaders;
26+
27+
return $testHeaders;
28+
}
29+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GacelaTest\Feature\Router;
6+
7+
use Gacela\Router\Entities\Request;
8+
use Gacela\Router\Router;
9+
use Gacela\Router\Routes;
10+
use GacelaTest\Feature\HeaderTestCase;
11+
use GacelaTest\Feature\Router\Fixtures\FakeController;
12+
13+
final class ErrorHandlingTest extends HeaderTestCase
14+
{
15+
public function test_match_does_not_matches_other_methods(): void
16+
{
17+
$_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
18+
$_SERVER['REQUEST_METHOD'] = 'GET';
19+
20+
$this->expectOutputString('');
21+
22+
Router::configure(static function (Routes $routes): void {
23+
$routes->post('expected/uri', FakeController::class, 'basicAction');
24+
});
25+
}
26+
27+
public function test_respond_status_404_when_the_uri_does_not_matches(): void
28+
{
29+
$_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri';
30+
$_SERVER['REQUEST_METHOD'] = Request::METHOD_OPTIONS;
31+
32+
Router::configure(static function (): void {
33+
});
34+
35+
self::assertSame([
36+
[
37+
'header' => 'HTTP/1.0 404 Not Found',
38+
'replace' => true,
39+
'response_code' => 0,
40+
],
41+
], $this->headers());
42+
}
43+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace GacelaTest\Unit\Router\Fake;
5+
namespace GacelaTest\Feature\Router\Fake;
66

77
final class Name implements NameInterface
88
{

tests/Unit/Router/Fake/NameInterface.php renamed to tests/Feature/Router/Fake/NameInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace GacelaTest\Unit\Router\Fake;
5+
namespace GacelaTest\Feature\Router\Fake;
66

77
interface NameInterface
88
{

tests/Unit/Router/Fixtures/FakeController.php renamed to tests/Feature/Router/Fixtures/FakeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace GacelaTest\Unit\Router\Fixtures;
5+
namespace GacelaTest\Feature\Router\Fixtures;
66

77
final class FakeController
88
{

tests/Unit/Router/Fixtures/FakeControllerWithDependencies.php renamed to tests/Feature/Router/Fixtures/FakeControllerWithDependencies.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
declare(strict_types=1);
44

5-
namespace GacelaTest\Unit\Router\Fixtures;
5+
namespace GacelaTest\Feature\Router\Fixtures;
66

7-
use GacelaTest\Unit\Router\Fake\NameInterface;
7+
use GacelaTest\Feature\Router\Fake\NameInterface;
88

99
final class FakeControllerWithDependencies
1010
{

tests/Unit/Router/Fixtures/FakeControllerWithRequest.php renamed to tests/Feature/Router/Fixtures/FakeControllerWithRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace GacelaTest\Unit\Router\Fixtures;
5+
namespace GacelaTest\Feature\Router\Fixtures;
66

77
use Gacela\Router\Entities\Request;
88

0 commit comments

Comments
 (0)