Skip to content

Commit

Permalink
Merge pull request #11 from gacela-project/route-not-found-404
Browse files Browse the repository at this point in the history
Trigger NotFound404Controller if no Route was found
  • Loading branch information
Chemaclass authored Apr 18, 2023
2 parents 80dd2fa + 7857558 commit 323c811
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 104 deletions.
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</php>

<testsuites>
<testsuite name="unit">
<directory suffix="Test.php">tests/Unit</directory>
<testsuite name="feature">
<directory suffix="Test.php">tests/Feature</directory>
</testsuite>
</testsuites>

Expand Down
13 changes: 13 additions & 0 deletions src/Router/Controllers/NotFound404Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Gacela\Router\Controllers;

final class NotFound404Controller
{
public function __invoke(): void
{
header('HTTP/1.0 404 Not Found');
}
}
9 changes: 4 additions & 5 deletions src/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Gacela\Router;

use Gacela\Router\Controllers\NotFound404Controller;
use Gacela\Router\Entities\Route;

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

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

if ($route) {
echo $route->run($mappingInterfaces);
}
echo $route->run($mappingInterfaces);
}

private static function findRoute(Routes $routerConfigurator): ?Route
private static function findRoute(Routes $routerConfigurator): Route
{
foreach ($routerConfigurator->routes() as $route) {
if ($route->requestMatches()) {
return $route;
}
}

return null;
return new Route('', '/', NotFound404Controller::class);
}
}
29 changes: 29 additions & 0 deletions tests/Feature/HeaderTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace GacelaTest\Feature;

use PHPUnit\Framework\TestCase;

include_once __DIR__ . '/header.php';

abstract class HeaderTestCase extends TestCase
{
/**
* @runInSeparateProcess
*/
protected function setUp(): void
{
global $testHeaders;

$testHeaders = null;
}

protected function headers(): array
{
global $testHeaders;

return $testHeaders;
}
}
43 changes: 43 additions & 0 deletions tests/Feature/Router/ErrorHandlingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace GacelaTest\Feature\Router;

use Gacela\Router\Entities\Request;
use Gacela\Router\Router;
use Gacela\Router\Routes;
use GacelaTest\Feature\HeaderTestCase;
use GacelaTest\Feature\Router\Fixtures\FakeController;

final class ErrorHandlingTest extends HeaderTestCase
{
public function test_match_does_not_matches_other_methods(): void
{
$_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
$_SERVER['REQUEST_METHOD'] = 'GET';

$this->expectOutputString('');

Router::configure(static function (Routes $routes): void {
$routes->post('expected/uri', FakeController::class, 'basicAction');
});
}

public function test_respond_status_404_when_the_uri_does_not_matches(): void
{
$_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri';
$_SERVER['REQUEST_METHOD'] = Request::METHOD_OPTIONS;

Router::configure(static function (): void {
});

self::assertSame([
[
'header' => 'HTTP/1.0 404 Not Found',
'replace' => true,
'response_code' => 0,
],
], $this->headers());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace GacelaTest\Unit\Router\Fake;
namespace GacelaTest\Feature\Router\Fake;

final class Name implements NameInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace GacelaTest\Unit\Router\Fake;
namespace GacelaTest\Feature\Router\Fake;

interface NameInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace GacelaTest\Unit\Router\Fixtures;
namespace GacelaTest\Feature\Router\Fixtures;

final class FakeController
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace GacelaTest\Unit\Router\Fixtures;
namespace GacelaTest\Feature\Router\Fixtures;

use GacelaTest\Unit\Router\Fake\NameInterface;
use GacelaTest\Feature\Router\Fake\NameInterface;

final class FakeControllerWithDependencies
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace GacelaTest\Unit\Router\Fixtures;
namespace GacelaTest\Feature\Router\Fixtures;

use Gacela\Router\Entities\Request;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

declare(strict_types=1);

namespace GacelaTest\Unit\Router;
namespace GacelaTest\Feature\Router;

use Gacela\Router\MappingInterfaces;
use Gacela\Router\Router;
use Gacela\Router\Routes;
use GacelaTest\Unit\Router\Fake\Name;
use GacelaTest\Unit\Router\Fake\NameInterface;
use GacelaTest\Unit\Router\Fixtures\FakeControllerWithDependencies;
use GacelaTest\Unit\Router\Fixtures\FakeControllerWithRequest;
use GacelaTest\Feature\Router\Fake\Name;
use GacelaTest\Feature\Router\Fake\NameInterface;
use GacelaTest\Feature\Router\Fixtures\FakeControllerWithDependencies;
use GacelaTest\Feature\Router\Fixtures\FakeControllerWithRequest;
use PHPUnit\Framework\TestCase;

final class RouterInjectionTest extends TestCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

namespace GacelaTest\Unit\Router;
namespace GacelaTest\Feature\Router;

use Gacela\Router\Entities\Request;
use Gacela\Router\Exceptions\UnsupportedHttpMethodException;
use Gacela\Router\Router;
use Gacela\Router\Routes;
use GacelaTest\Unit\Router\Fixtures\FakeController;
use GacelaTest\Feature\Router\Fixtures\FakeController;
use Generator;
use PHPUnit\Framework\TestCase;

Expand All @@ -26,30 +26,6 @@ public function test_respond_when_everything_matches(): void
});
}

public function test_not_respond_when_the_uri_does_not_matches(): void
{
$_SERVER['REQUEST_URI'] = 'https://example.org/unexpected/uri';
$_SERVER['REQUEST_METHOD'] = 'GET';

$this->expectOutputString('');

Router::configure(static function (Routes $routes): void {
$routes->get('other/uri', FakeController::class, 'basicAction');
});
}

public function test_not_respond_when_the_method_does_not_matches(): void
{
$_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
$_SERVER['REQUEST_METHOD'] = 'GET';

$this->expectOutputString('');

Router::configure(static function (Routes $routes): void {
$routes->post('expected/uri', FakeController::class, 'basicAction');
});
}

public function test_respond_only_the_first_match(): void
{
$_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace GacelaTest\Unit\Router;
namespace GacelaTest\Feature\Router;

use Gacela\Router\Router;
use Gacela\Router\Routes;
use GacelaTest\Unit\Router\Fixtures\FakeController;
use GacelaTest\Feature\Router\Fixtures\FakeController;
use Generator;
use PHPUnit\Framework\TestCase;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,20 @@

declare(strict_types=1);

namespace GacelaTest\Unit\Router;
namespace GacelaTest\Feature\Router;

use Gacela\Router\Entities\Request;
use Gacela\Router\Router;
use Gacela\Router\Routes;
use GacelaTest\Unit\Router\Fixtures\HeadersTearDown;
use PHPUnit\Framework\TestCase;
use GacelaTest\Feature\HeaderTestCase;

include_once __DIR__ . '/Fake/header.php';

final class RouterRedirectTest extends TestCase
final class RouterRedirectTest extends HeaderTestCase
{
use HeadersTearDown;

/**
* @runInSeparateProcess
*/
protected function setUp(): void
{
}

/**
* @dataProvider destinationProvider
*/
public function test_simple_redirect(string $destination): void
{
global $testHeaders;

$_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri';
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;

Expand All @@ -43,16 +29,14 @@ public function test_simple_redirect(string $destination): void
'replace' => true,
'response_code' => 302,
],
], $testHeaders);
], $this->headers());
}

/**
* @dataProvider destinationProvider
*/
public function test_redirect_with_status_code(string $destination, int $statusCode): void
{
global $testHeaders;

$_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri';
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;

Expand All @@ -66,16 +50,14 @@ public function test_redirect_with_status_code(string $destination, int $statusC
'replace' => true,
'response_code' => $statusCode,
],
], $testHeaders);
], $this->headers());
}

/**
* @dataProvider destinationProvider
*/
public function test_redirect_with_custom_method(string $destination, int $statusCode, string $method): void
{
global $testHeaders;

$_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri';
$_SERVER['REQUEST_METHOD'] = $method;

Expand All @@ -91,26 +73,7 @@ static function (Routes $routes) use ($destination, $statusCode, $method): void
'replace' => true,
'response_code' => $statusCode,
],
], $testHeaders);
}

/**
* @dataProvider destinationProvider
*/
public function test_not_redirect_non_registered_method(string $destination, int $statusCode, string $method): void
{
global $testHeaders;

$_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri';
$_SERVER['REQUEST_METHOD'] = Request::METHOD_OPTIONS;

Router::configure(
static function (Routes $routes) use ($destination, $statusCode, $method): void {
$routes->redirect('optional/uri', $destination, $statusCode, $method);
},
);

self::assertNull($testHeaders);
], $this->headers());
}

public function destinationProvider(): iterable
Expand Down
File renamed without changes.
15 changes: 0 additions & 15 deletions tests/Unit/Router/Fixtures/HeadersTearDown.php

This file was deleted.

0 comments on commit 323c811

Please sign in to comment.