diff options
author | minstel <minstel@yandex.ru> | 2016-10-13 14:12:52 +0300 |
---|---|---|
committer | minstel <minstel@yandex.ru> | 2016-10-13 14:12:52 +0300 |
commit | 5a72097af59eec035227db405ff023a0367c56df (patch) | |
tree | 0980eb40192d0f9edc1d1796c58ee51f7b8c6901 | |
parent | d9b8f6a9d645f2380f74b8c155429ae7e4793d4d (diff) | |
download | router-5a72097af59eec035227db405ff023a0367c56df.zip router-5a72097af59eec035227db405ff023a0367c56df.tar.gz router-5a72097af59eec035227db405ff023a0367c56df.tar.bz2 |
Fixes to prev commit
-rw-r--r-- | src/Router/Middleware/NotFound.php | 42 | ||||
-rw-r--r-- | tests/Router/Middleware/NotFoundTest.php | 41 |
2 files changed, 41 insertions, 42 deletions
diff --git a/src/Router/Middleware/NotFound.php b/src/Router/Middleware/NotFound.php index 56825a7..26b9770 100644 --- a/src/Router/Middleware/NotFound.php +++ b/src/Router/Middleware/NotFound.php @@ -2,6 +2,7 @@ namespace Jasny\Router\Middleware; +use Jasny\Router\Routes; use Jasny\Router\Routes\Glob; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; @@ -13,9 +14,9 @@ class NotFound { /** * Routes - * @var array + * @var Routes */ - protected $routes = []; + protected $routes = null; /** * Action for 'not found' case @@ -36,14 +37,14 @@ class NotFound * @param callback|int $notFound * @param callback|int $methodNotAllowed */ - public function __construct(array $routes, $notFound = 404, $methodNotAllowed = null) + public function __construct(Routes $routes, $notFound = 404, $methodNotAllowed = null) { - if (!in_array($notFound, [404, '404'], true) && !is_callable($notFound)) { - throw new \InvalidArgumentException("'Not found' parameter should be '404' or a callback"); + if (!(is_numeric($notFound) && $notFound >= 100 && $notFound <= 999) && !is_callable($notFound)) { + throw new \InvalidArgumentException("'Not found' parameter should be a code in range 100-999 or a callback"); } - if ($methodNotAllowed && !in_array($methodNotAllowed, [405, '405'], true) && !is_callable($methodNotAllowed)) { - throw new \InvalidArgumentException("'Method not allowed' parameter should be '405' or a callback"); + if ($methodNotAllowed && !(is_numeric($methodNotAllowed) && $methodNotAllowed >= 100 && $methodNotAllowed <= 999) && !is_callable($methodNotAllowed)) { + throw new \InvalidArgumentException("'Method not allowed' parameter should be a code in range 100-999 or a callback"); } $this->routes = $routes; @@ -65,25 +66,14 @@ class NotFound throw new \InvalidArgumentException("'next' should be a callback"); } - $glob = new Glob($this->routes); - - if ($this->methodNotAllowed) { - $notAllowed = !$glob->hasRoute($request) && $glob->hasRoute($request, false); - - if ($notAllowed) { - return is_numeric($this->methodNotAllowed) ? - $this->simpleResponse($response, $this->methodNotAllowed, 'Method Not Allowed') : - call_user_func($this->methodNotAllowed, $request, $response); - } - } - - if (!$glob->hasRoute($request)) { - return is_numeric($this->notFound) ? - $this->simpleResponse($response, $this->notFound, 'Not Found') : - call_user_func($this->notFound, $request, $response); + if ($this->routes->hasRoute($request)) { + return $next ? $next($request, $response) : $response; } - return $next ? $next($request, $response) : $response; + $status = $this->methodNotAllowed && $this->routes->hasRoute($request, false) ? + $this->methodNotAllowed : $this->notFound; + + return is_numeric($status) ? $this->simpleResponse($response, $status) : call_user_func($status, $request, $response); } /** @@ -94,8 +84,10 @@ class NotFound * @param string $message * @return */ - protected function simpleResponse(ResponseInterface $response, $code, $message) + protected function simpleResponse(ResponseInterface $response, $code) { + $message = 'Not Found'; + $body = $response->getBody(); $body->rewind(); $body->write($message); diff --git a/tests/Router/Middleware/NotFoundTest.php b/tests/Router/Middleware/NotFoundTest.php index fbeccea..62a94ac 100644 --- a/tests/Router/Middleware/NotFoundTest.php +++ b/tests/Router/Middleware/NotFoundTest.php @@ -1,5 +1,6 @@ <?php +use Jasny\Router\Routes\Glob; use Jasny\Router\Middleware\NotFound; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; @@ -13,12 +14,15 @@ class NotFoundTest extends PHPUnit_Framework_TestCase * @dataProvider constructProvider * @param string $notFound * @param string $notAllowed + * @param boolean $positive */ - public function testConstruct($notFound, $notAllowed) + public function testConstruct($notFound, $notAllowed, $positive) { - $this->expectException(\InvalidArgumentException::class); + if (!$positive) $this->expectException(\InvalidArgumentException::class); + + $middleware = new NotFound(new Glob([]), $notFound, $notAllowed); - $middleware = new NotFound([], $notFound, $notAllowed); + if ($positive) $this->skipTest(); } /** @@ -27,10 +31,14 @@ class NotFoundTest extends PHPUnit_Framework_TestCase public function constructProvider() { return [ - [404, 406], - [200, 405], - [null, 405], - [true, true] + [null, 405, false], + [true, true, false], + [99, null, false], + [1000, null, false], + [404, 99, false], + [404, 1000, false], + [200, 405, true], + [404, 200, true] ]; } @@ -39,11 +47,11 @@ class NotFoundTest extends PHPUnit_Framework_TestCase */ public function testInvokeInvalidNext() { - $middleware = new NotFound([], 404, 405); + $middleware = new NotFound(new Glob([]), 404, 405); list($request, $response) = $this->getRequests('/foo', 'POST'); $this->expectException(\InvalidArgumentException::class); - + $result = $middleware($request, $response, 'not_callable'); } @@ -62,9 +70,9 @@ class NotFoundTest extends PHPUnit_Framework_TestCase list($request, $response) = $this->getRequests('/foo', 'POST'); if (is_numeric($notAllowed)) { - $this->expectSimpleDeny($response, 405, 'Method Not Allowed'); + $this->expectSimpleDeny($response, $notAllowed); } elseif (!$notAllowed && is_numeric($notFound)) { - $this->expectSimpleDeny($response, 404, 'Not Found'); + $this->expectSimpleDeny($response, $notFound); } $result = $middleware($request, $response, $next); @@ -133,17 +141,16 @@ class NotFoundTest extends PHPUnit_Framework_TestCase * * @param ResponseInterface $response * @param int $code - * @param string $reasonPhrase */ - public function expectSimpleDeny(ResponseInterface $response, $code, $reasonPhrase) + public function expectSimpleDeny(ResponseInterface $response, $code) { $stream = $this->createMock(StreamInterface::class); $stream->expects($this->once())->method('rewind'); - $stream->expects($this->once())->method('write')->with($this->equalTo($reasonPhrase)); + $stream->expects($this->once())->method('write')->with($this->equalTo('Not Found')); $response->method('getBody')->will($this->returnValue($stream)); $response->expects($this->once())->method('withBody')->with($this->equalTo($stream))->will($this->returnSelf()); - $response->expects($this->once())->method('withStatus')->with($this->equalTo($code), $this->equalTo($reasonPhrase))->will($this->returnSelf()); + $response->expects($this->once())->method('withStatus')->with($this->equalTo($code), $this->equalTo('Not Found'))->will($this->returnSelf()); } /** @@ -171,13 +178,13 @@ class NotFoundTest extends PHPUnit_Framework_TestCase */ public function getRoutes() { - return [ + return new Glob([ '/' => ['controller' => 'test'], '/foo/bar' => ['controller' => 'test'], '/foo +GET' => ['controller' => 'test'], '/foo +OPTIONS' => ['controller' => 'test'], '/bar/foo/zet -POST' => ['controller' => 'test'] - ]; + ]); } /** |