Skip to content

Commit

Permalink
Merge pull request #200 from hydephp/throw-exception-if-route-is-not-…
Browse files Browse the repository at this point in the history
…found

Throw exception if route is not found hydephp/develop@a4f1046
  • Loading branch information
github-actions committed Jul 9, 2022
1 parent 1238bcc commit 219b05d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/Facades/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@
class Route implements RouteFacadeContract
{
/** @inheritDoc */
public static function get(string $routeKey): ?RouteModel
public static function get(string $routeKey): RouteModel
{
return RouteModel::get($routeKey);
}

/** @inheritDoc */
public static function getFromKey(string $routeKey): ?RouteModel
public static function getFromKey(string $routeKey): RouteModel
{
return RouteModel::getFromKey($routeKey);
}

/** @inheritDoc */
public static function getFromSource(string $sourceFilePath): ?RouteModel
public static function getFromSource(string $sourceFilePath): RouteModel
{
return RouteModel::getFromSource($sourceFilePath);
}

/** @inheritDoc */
public static function getFromModel(PageContract $page): ?RouteModel
public static function getFromModel(PageContract $page): RouteModel
{
return RouteModel::getFromModel($page);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Modules/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,23 @@ protected function constructRouteKey(): string
}

/** @inheritDoc */
public static function get(string $routeKey): ?static
public static function get(string $routeKey): static
{
return static::getFromKey($routeKey);
}

/** @inheritDoc */
public static function getFromKey(string $routeKey): ?static
public static function getFromKey(string $routeKey): static
{
return Router::getInstance()->getRoutes()->get($routeKey);
return Router::getInstance()->getRoutes()->get($routeKey) ?? throw new RouteNotFoundException($routeKey);
}

/** @inheritDoc */
public static function getFromSource(string $sourceFilePath): ?static
public static function getFromSource(string $sourceFilePath): static
{
return Router::getInstance()->getRoutes()->first(function (RouteContract $route) use ($sourceFilePath) {
return $route->getSourceFilePath() === $sourceFilePath;
});
}) ?? throw new RouteNotFoundException($sourceFilePath);
}

/** @inheritDoc */
Expand Down
8 changes: 8 additions & 0 deletions src/Modules/Routing/RouteContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

use Hyde\Framework\Contracts\PageContract;

/**
* This contract defines the methods that a Route object must implement.
* These methods are each applied to the single route instance.
*
* In Hyde, the route models also serve as a facade for all routes, see the dedicated interface:
*
* @see \Hyde\Framework\Modules\Routing\RouteFacadeContract for the static facade methods.
*/
interface RouteContract
{
/**
Expand Down
29 changes: 21 additions & 8 deletions src/Modules/Routing/RouteFacadeContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
use Hyde\Framework\Contracts\PageContract;
use Illuminate\Support\Collection;

/**
* This contract defines the static facade methods for the Route class.
*
* @see \Hyde\Framework\Modules\Routing\RouteContract for the interface that each route model must implement.
*/
interface RouteFacadeContract
{
/**
Expand All @@ -13,33 +18,41 @@ interface RouteFacadeContract
* Alias for static::getFromKey().
*
* @param string $routeKey Example: posts/foo.md
* @return \Hyde\Framework\Modules\Routing\RouteContract|null
* @return \Hyde\Framework\Modules\Routing\RouteContract
*
* @throws \Hyde\Framework\Modules\Routing\RouteNotFoundException
*/
public static function get(string $routeKey): ?RouteContract;
public static function get(string $routeKey): RouteContract;

/**
* Get a route from the Router index for the specified route key.
*
* @param string $routeKey Example: posts/foo.md
* @return \Hyde\Framework\Modules\Routing\RouteContract|null
* @return \Hyde\Framework\Modules\Routing\RouteContract
*
* @throws \Hyde\Framework\Modules\Routing\RouteNotFoundException
*/
public static function getFromKey(string $routeKey): ?RouteContract;
public static function getFromKey(string $routeKey): RouteContract;

/**
* Get a route from the Router index for the specified source file path.
*
* @param string $sourceFilePath Example: _posts/foo.md
* @return \Hyde\Framework\Modules\Routing\RouteContract|null
* @return \Hyde\Framework\Modules\Routing\RouteContract
*
* @throws \Hyde\Framework\Modules\Routing\RouteNotFoundException
*/
public static function getFromSource(string $sourceFilePath): ?RouteContract;
public static function getFromSource(string $sourceFilePath): RouteContract;

/**
* Get a route from the Router index for the supplied page model.
*
* @param \Hyde\Framework\Contracts\PageContract $page
* @return \Hyde\Framework\Modules\Routing\RouteContract|null
* @return \Hyde\Framework\Modules\Routing\RouteContract
*
* @throws \Hyde\Framework\Modules\Routing\RouteNotFoundException
*/
public static function getFromModel(PageContract $page): ?RouteContract;
public static function getFromModel(PageContract $page): RouteContract;

/**
* Get all routes from the Router index.
Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/DarkmodeFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function test_layout_has_toggle_button_and_script_when_enabled()
{
Config::set('hyde.features', [
Features::markdownPages(),
Features::bladePages(),
Features::darkmode(),
]);

Expand Down Expand Up @@ -74,6 +75,7 @@ public function test_dark_mode_theme_button_is_hidden_in_layouts_when_disabled()
{
Config::set('hyde.features', [
Features::markdownPages(),
Features::bladePages(),
]);

$view = view('hyde::layouts/page')->with([
Expand Down
9 changes: 6 additions & 3 deletions tests/Feature/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Hyde\Framework\Models\Pages\MarkdownPost;
use Hyde\Framework\Modules\Routing\Route;
use Hyde\Framework\Modules\Routing\RouteContract;
use Hyde\Framework\Modules\Routing\RouteNotFoundException;
use Hyde\Framework\Modules\Routing\Router;
use Hyde\Testing\TestCase;

Expand Down Expand Up @@ -77,9 +78,10 @@ public function test_get_from_key_returns_route_from_router_index()
$this->assertInstanceOf(RouteContract::class, Route::get('index'));
}

public function test_get_from_key_returns_null_if_route_is_not_found()
public function test_get_from_key_throws_exception_if_route_is_not_found()
{
$this->assertNull(Route::get('not-found'));
$this->expectException(RouteNotFoundException::class);
Route::get('not-found');
}

public function test_get_from_source_returns_route_from_router_index()
Expand All @@ -90,7 +92,8 @@ public function test_get_from_source_returns_route_from_router_index()

public function test_get_from_source_returns_null_if_route_is_not_found()
{
$this->assertNull(Route::getFromSource('not-found'));
$this->expectException(RouteNotFoundException::class);
Route::getFromSource('not-found');
}

public function test_get_from_source_can_find_blade_pages()
Expand Down

0 comments on commit 219b05d

Please sign in to comment.