Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/http-interop-support' into develop
Browse files Browse the repository at this point in the history
Close #32
  • Loading branch information
weierophinney committed Jan 24, 2017
2 parents ac7dab2 + c027180 commit 8fc7784
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 14 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ All notable changes to this project will be documented in this file, in reverse

### Added

- Nothing.
- [#32](https://github.com/zendframework/zend-expressive-router/pull/32) adds
support for [http-interop/http-middleware](https://github.com/http-interop/http-middleware)
server middleware in `Route` instances.

### Deprecated

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"require": {
"php": "^5.6 || ^7.0",
"psr/http-message": "^1.0",
"fig/http-message-util": "^1.1"
"fig/http-message-util": "^1.1",
"http-interop/http-middleware": "^0.4.1"
},
"require-dev": {
"phpunit/phpunit": "^4.7 || ^5.6",
Expand Down
17 changes: 13 additions & 4 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Zend\Expressive\Router;

use Fig\Http\Message\RequestMethodInterface as RequestMethod;
use Interop\Http\ServerMiddleware\MiddlewareInterface;

/**
* Value object representing a single route.
Expand Down Expand Up @@ -68,7 +69,7 @@ class Route

/**
* @param string $path Path to match.
* @param string|callable $middleware Middleware to use when this route is matched.
* @param string|callable|MiddlewareInterface $middleware Middleware to use when this route is matched.
* @param int|array Allowed HTTP methods; defaults to HTTP_METHOD_ANY.
* @param string|null $name the route name
* @throws Exception\InvalidArgumentException for invalid path type.
Expand All @@ -81,8 +82,16 @@ public function __construct($path, $middleware, $methods = self::HTTP_METHOD_ANY
throw new Exception\InvalidArgumentException('Invalid path; must be a string');
}

if (! is_callable($middleware) && ! is_string($middleware) && ! is_array($middleware)) {
throw new Exception\InvalidArgumentException('Invalid middleware; must be callable or a service name');
if (! is_callable($middleware)
&& ! $middleware instanceof MiddlewareInterface
&& ! is_string($middleware)
&& ! is_array($middleware)
) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid middleware; must be a callable, a %s instance, a service name, '
. 'or an array of any of these types',
MiddlewareInterface::class
));
}

if ($methods !== self::HTTP_METHOD_ANY && ! is_array($methods)) {
Expand Down Expand Up @@ -136,7 +145,7 @@ public function getName()
}

/**
* @return string|callable|array
* @return string|callable|MiddlewareInterface
*/
public function getMiddleware()
{
Expand Down
10 changes: 7 additions & 3 deletions src/RouteResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Zend\Expressive\Router;

use Interop\Http\ServerMiddleware\MiddlewareInterface;

/**
* Value object representing the results of routing.
*
Expand Down Expand Up @@ -45,7 +47,7 @@ class RouteResult
private $matchedRouteName;

/**
* @var callable|string
* @var callable|string|MiddlewareInterface|array
*/
private $matchedMiddleware;

Expand Down Expand Up @@ -145,8 +147,10 @@ public function getMatchedRouteName()
/**
* Retrieve the matched middleware, if possible.
*
* @return false|callable|string|array Returns false if the result represents a
* failure; otherwise, a callable or a string service name.
* @return false|callable|string|MiddlewareInterface|array Returns false if
* the result represents a failure; otherwise, a callable, a string
* service name, a MiddlewareInterface instance, or array of any of
* these types.
*/
public function getMatchedMiddleware()
{
Expand Down
49 changes: 44 additions & 5 deletions test/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace ZendTest\Expressive\Router;

use Fig\Http\Message\RequestMethodInterface as RequestMethod;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\Expressive\Router\Exception\InvalidArgumentException;
use Zend\Expressive\Router\Route;
Expand Down Expand Up @@ -128,11 +129,7 @@ public function testThrowsExceptionDuringConstructionIfPathIsNotString()

public function testThrowsExceptionDuringConstructionOnInvalidMiddleware()
{
$this->setExpectedException(
InvalidArgumentException::class,
'Invalid middleware; must be callable or a service name'
);

$this->setExpectedException(InvalidArgumentException::class, 'Invalid middleware');
new Route('/foo', 12345);
}

Expand Down Expand Up @@ -208,4 +205,46 @@ public function testPassingWildcardMethodDoesNotMarkAsImplicit()
$this->assertFalse($route->implicitHead());
$this->assertFalse($route->implicitOptions());
}

public function testAllowsHttpInteropMiddleware()
{
$middleware = $this->prophesize(MiddlewareInterface::class)->reveal();
$route = new Route('/test', $middleware, Route::HTTP_METHOD_ANY);
$this->assertSame($middleware, $route->getMiddleware());
}

/**
* This is to allow passing an array of middleware for use in creating a MiddlewarePipe
* instance; Route should simply check if it's a non-callable array, and, if so, store
* the entry as it would a non-callable string.
*/
public function testAllowsNonCallableArraysAsMiddleware()
{
$middleware = ['Non', 'Callable', 'Middleware'];
$route = new Route('/test', $middleware, Route::HTTP_METHOD_ANY);
$this->assertSame($middleware, $route->getMiddleware());
}

public function invalidMiddleware()
{
// Strings are allowed, because they could be service names.
return [
'null' => [null],
'true' => [true],
'false' => [false],
'zero' => [0],
'int' => [1],
'int' => [1],
'non-callable-object' => [(object) ['handler' => 'foo']],
];
}

/**
* @dataProvider invalidMiddleware
*/
public function testConstructorRaisesExceptionForInvalidMiddleware($middleware)
{
$this->setExpectedException(InvalidArgumentException::class, 'Invalid middleware');
new Route('/test', $middleware);
}
}

0 comments on commit 8fc7784

Please sign in to comment.