Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error if required route params missing #3118

Merged
merged 6 commits into from
Oct 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/Http/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,17 @@ public function getRouteData()
return $this->dataGenerator->getData();
}

protected function fixPathPart(&$part, $key, array $parameters)
protected function fixPathPart($part, array $parameters, string $routeName)
{
if (is_array($part) && array_key_exists($part[0], $parameters)) {
$part = $parameters[$part[0]];
if (! is_array($part)) {
return $part;
}

if (! array_key_exists($part[0], $parameters)) {
throw new \InvalidArgumentException("Could not generate URL for route '$routeName': no value provided for required part '$part[0]'.");
}

return $parameters[$part[0]];
}

public function getPath($name, array $parameters = [])
Expand All @@ -151,9 +157,11 @@ public function getPath($name, array $parameters = [])
}
}

array_walk($matchingParts, [$this, 'fixPathPart'], $parameters);
$fixedParts = array_map(function ($part) use ($parameters, $name) {
return $this->fixPathPart($part, $parameters, $name);
}, $matchingParts);

return '/'.ltrim(implode('', $matchingParts), '/');
return '/'.ltrim(implode('', $fixedParts), '/');
}

throw new \RuntimeException("Route $name not found");
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/Http/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,41 @@ public function can_add_routes_late()

$this->assertEquals('/posts', $routeCollection->getPath('forum.posts.delete'));
}

/** @test */
public function must_provide_required_parameters()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage("Could not generate URL for route 'user': no value provided for required part 'user'.");

$routeCollection = (new RouteCollection)->addRoute('GET', '/user/{user}', 'user', function () {
echo 'user';
});

$routeCollection->getPath('user', []);
}

/** @test */
public function dont_need_to_provide_optional_parameters()
{
$routeCollection = (new RouteCollection)->addRoute('GET', '/user/{user}[/{test}]', 'user', function () {
echo 'user';
});

$path = $routeCollection->getPath('user', ['user' => 'SomeUser']);

$this->assertEquals('/user/SomeUser', $path);
}

/** @test */
public function can_provide_optional_parameters()
{
$routeCollection = (new RouteCollection)->addRoute('GET', '/user/{user}[/{test}]', 'user', function () {
echo 'user';
});

$path = $routeCollection->getPath('user', ['user' => 'SomeUser', 'test' => 'Flarum']);

$this->assertEquals('/user/SomeUser/Flarum', $path);
}
}