Skip to content

Commit

Permalink
filter empty and replace with defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
iwyg committed Apr 5, 2016
1 parent b20b69a commit cc01859
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Matcher/MatcherTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ private function getMatchedVars(RouteInterface $route, array $matches)
$vars = $route->getContext()->getVars();
$params = array_merge(
$route->getDefaults(),
array_map(
array_filter(array_map(
[$this, 'getValue'],
array_intersect_key(
$matches,
$t = array_combine($vars, array_pad([], count($vars), null))
)
)
))
);

return array_intersect_key($params, $t);
Expand Down
23 changes: 23 additions & 0 deletions tests/Matcher/RequestMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ public function itShouldReturnCorrectMissmatchReason()
$this->assertTrue($result->isSchemeMissmatch(), 'Reason should be scheme missmatch.');
}

/** @test */
public function itShouldMapDefaultValues()
{
$matcher = new Matcher;
$routes = new Routes;

$routes->add(
'user',
new Route('/user/{id?}', 'getUserAction', ['GET'], null, ['id' => 12])
);

$request = $this->mockRequest();
$request->method('getMethod')->willReturn('GET');
$request->method('getPath')->willReturn('/user');
$request->method('getScheme')->willReturn('http');

$result = $matcher->matchRequest($request, $routes);

$vars = $result->getVars();
$this->assertArrayHasKey('id', $vars);
$this->assertSame(12, $vars['id']);
}

private function mockRoute()
{
return $this->getMockbuilder('Lucid\Mux\Route')
Expand Down

0 comments on commit cc01859

Please sign in to comment.