Skip to content

Commit

Permalink
Well, okay, maybe that was too quick.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin de Graaf committed Apr 7, 2019
1 parent 4bdddf1 commit 2be5764
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Parable Routing

## 0.2.1

_Changes_

Well, that had unintentional effects, didn't it? The whole idea of named routes was that names were unique. So requiring an HTTP method to work with them that way is against that exact notion.

The following methods _obviously_ don't require passing in the HTTP method:
- `getRouteByName(string $httpMethod, string $name): ?Route`
- `buildRouteUrl(string $httpMethod, string $name, array $parameters = []): string`

Ahh, the beauty of pre-release software 😅

## 0.2.0

_Changes_
Expand Down
16 changes: 9 additions & 7 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,22 @@ public function getRoutes(string $httpMethod): array
return $this->routes[$httpMethod] ?? [];
}

public function getRouteByName(string $httpMethod, string $name): ?Route
public function getRouteByName(string $name): ?Route
{
$routeUrl = $this->routeNames[$httpMethod][$name] ?? null;
foreach ($this->routeNames as $httpMethod => $routeNames) {
$routeUrl = $routeNames[$name] ?? null;

if ($routeUrl === null) {
return null;
if ($routeUrl !== null) {
return $this->routes[$httpMethod][$routeUrl] ?? null;
}
}

return $this->routes[$httpMethod][$routeUrl] ?? null;
return null;
}

public function buildRouteUrl(string $httpMethod, string $name, array $parameters = []): string
public function buildRouteUrl(string $name, array $parameters = []): string
{
$route = $this->getRouteByName($httpMethod, $name);
$route = $this->getRouteByName($name);

if ($route === null) {
throw new Exception(sprintf("Route '%s' not found.", $name));
Expand Down
16 changes: 8 additions & 8 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function testAddRouteAndGetRouteByName()
{
$this->setUpDefaultRoutesAndAssert();

$route = $this->router->getRouteByName('GET', 'simple');
$route = $this->router->getRouteByName('simple');

self::assertSame(['GET'], $route->getHttpMethods());
self::assertSame('/simple', $route->getUrl());
Expand All @@ -68,7 +68,7 @@ public function testAddRouteAndGetRouteByName()

public function testInvalidGetRouteByNameReturnsNull()
{
self::assertNull($this->router->getRouteByName('GET', 'la-dee-dah'));
self::assertNull($this->router->getRouteByName('la-dee-dah'));
}

public function testInvalidMatchReturnsNull()
Expand Down Expand Up @@ -252,7 +252,7 @@ public function testBuildRouteUrl()
{
$this->setUpDefaultRoutesAndAssert();

$route = $this->router->buildRouteUrl('GET', 'complex', ['id' => 2, 'name' => 'stuff']);
$route = $this->router->buildRouteUrl('complex', ['id' => 2, 'name' => 'stuff']);
self::assertSame("/complex/2/stuff", $route);
}

Expand All @@ -261,7 +261,7 @@ public function testBuildRouteUrlThrowsOnUnknownName()
$this->expectException(Exception::class);
$this->expectExceptionMessage("Route 'nope' not found.");

$this->router->buildRouteUrl('GET', 'nope', ['id' => 2, 'name' => 'stuff']);
$this->router->buildRouteUrl('nope', ['id' => 2, 'name' => 'stuff']);
}

public function testBuildRouteUrlThrowsOnUrlWithWrongParameters()
Expand All @@ -271,7 +271,7 @@ public function testBuildRouteUrlThrowsOnUrlWithWrongParameters()
$this->expectException(Exception::class);
$this->expectExceptionMessage("Parameter 'id2' not found in url '/complex/{id}/{name}'.");

$this->router->buildRouteUrl('GET', 'complex', ['id2' => 2, 'name2' => 'stuff']);
$this->router->buildRouteUrl('complex', ['id2' => 2, 'name2' => 'stuff']);
}

public function testRouteReturnsNullOnNonExistingValueKey()
Expand All @@ -289,9 +289,9 @@ public function testRouteBuildUrlWithOrWithoutParameters()
{
$this->setUpDefaultRoutesAndAssert();

self::assertSame('/simple', $this->router->buildRouteUrl('GET', 'simple'));
self::assertSame('/simple', $this->router->buildRouteUrl('GET', 'simple', []));
self::assertSame('/simple', $this->router->buildRouteUrl('GET', 'simple', ['id2' => 2, 'name2' => 'stuff']));
self::assertSame('/simple', $this->router->buildRouteUrl('simple'));
self::assertSame('/simple', $this->router->buildRouteUrl('simple', []));
self::assertSame('/simple', $this->router->buildRouteUrl('simple', ['id2' => 2, 'name2' => 'stuff']));
}

public function testGetRoutesReturnsCorrectNumberOfRoutes()
Expand Down

0 comments on commit 2be5764

Please sign in to comment.