Skip to content

Commit

Permalink
ensure default schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
iwyg committed Jan 26, 2016
1 parent ed0033d commit 9c3dc45
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/RouteCollectionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,12 @@ public function endGroup()
private function parseRequirements(array $rq, $methods)
{
$keys = [];
$constr = array_filter($this->mergeDefaults($rq), function ($val, $key) use (&$keys, $methods) {
$constr = array_filter($this->mergeDefaults($rq, $methods), function ($val, $key) use (&$keys, $methods) {
if (!in_array($key, self::$keys)) {
return true;
}
if (self::K_NAME === $key && null === $val) {
$val = $this->generateName($methods);
}
$keys[$key] = $val;

return false;
}, ARRAY_FILTER_USE_BOTH);

Expand All @@ -230,9 +228,19 @@ private function parseRequirements(array $rq, $methods)
*
* @return array
*/
private function mergeDefaults(array $given)
private function mergeDefaults(array $given, $methods)
{
return array_merge(array_combine(self::$keys, array_pad([], count(self::$keys), null)), $given);
$defaults = array_merge(array_combine(self::$keys, array_pad([], count(self::$keys), null)), $given);

if (null === $defaults[self::K_SCHEME]) {
$defaults[self::K_SCHEME] = 'http|https';
}

if (null === $defaults[self::K_NAME]) {
$defaults[self::K_NAME] = $this->generateName($methods);
}

return $defaults;
}

/**
Expand Down
29 changes: 28 additions & 1 deletion tests/RouteCollectionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,31 @@ public function itShouldAutoGenerateNames()
$this->assertStringStartsWith('route_GET_', $name);

$builder = $this->newBuilder();
$builder->post('/app/{user}/{area}', 'action', ['user' => '\w+', 'area' => '\d+']);
$builder->post('/app/{user}/{area}', 'action', $c = ['user' => '\w+', 'area' => '\d+']);

$name = current(array_keys($builder->getCollection()->all()));

$this->assertStringStartsWith('route_POST_', $name);
}

/** @test */
public function itShouldObtainDefaults()
{
$builder = $this->newBuilder();
$builder->get('/{foo}', 'action', [Builder::K_NAME => 'index'], ['foo' => 'bar']);

$this->assertSame('bar', $builder->getCollection()->get('index')->getDefault('foo'));
}

/** @test */
public function itShouldObtainConstraints()
{
$builder = $this->newBuilder();
$builder->get('/{foo}', 'action', [Builder::K_NAME => 'index', 'foo' => '\w+']);

$this->assertSame('\w+', $builder->getCollection()->get('index')->getConstraint('foo'));
}

/**
* @test
* @dataProvider methodProvider
Expand All @@ -49,6 +67,15 @@ public function itShouldCreateRoutesWithMethodShortCuts($method, $args, $expecte
}
}

/** @test */
public function itShouldObtainDefaultSchemes()
{
$builder = $this->newBuilder();
$builder->get('/', 'action');

$this->assertEquals(['http', 'https'], current($builder->getCollection()->all())->getSchemes());
}

/** @test */
public function itShouldBuildGroups()
{
Expand Down

0 comments on commit 9c3dc45

Please sign in to comment.