Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
added check for single method as string in Route constructor, updated…
… docblock
  • Loading branch information
llvdl committed May 23, 2016
commit 1ac64601141105f25e91deab22000f43ea57b8a6
14 changes: 7 additions & 7 deletions Slim/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ class Route extends Routable implements RouteInterface
/**
* Create new route
*
* @param string[] $methods The route HTTP methods
* @param string $pattern The route pattern
* @param callable $callable The route callable
* @param RouteGroup[] $groups The parent route groups
* @param int $identifier The route identifier
* @param string|string[] $methods The route HTTP methods
* @param string $pattern The route pattern
* @param callable $callable The route callable
* @param RouteGroup[] $groups The parent route groups
* @param int $identifier The route identifier
*/
public function __construct(array $methods, $pattern, $callable, $groups = [], $identifier = 0)
public function __construct($methods, $pattern, $callable, $groups = [], $identifier = 0)
{
$this->methods = $methods;
$this->methods = is_string($methods) ? [$methods] : $methods;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add support for string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because FastRoute supports both a string parameter and an array of string. Because there is no array typehint on $methods, string was already supported. I updated the docblock to reflect this.

Also, when setting Route::$methods in the constructor, the value is set to an array, so Route::getMethods() always returns an array as stated in the docblock.

$this->pattern = $pattern;
$this->callable = $callable;
$this->groups = $groups;
Expand Down
11 changes: 10 additions & 1 deletion tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,20 @@ public function testConstructor()
$this->assertAttributeEquals($callable, 'callable', $route);
}

public function testGetMethodsReturnsArrayWhenContructedWithString()
{
$route = new Route('GET', '/hello', function ($req, $res, $args) {
// Do something
});

$this->assertEquals(['GET'], $route->getMethods());
}

public function testGetMethods()
{
$this->assertEquals(['GET', 'POST'], $this->routeFactory()->getMethods());
}

public function testGetPattern()
{
$this->assertEquals('/hello/{name}', $this->routeFactory()->getPattern());
Expand Down