From 9619df20fe373fd16bf5106b58b6e43995528ede Mon Sep 17 00:00:00 2001 From: Mehmet Korkmaz Date: Thu, 3 Jan 2019 15:46:30 +0300 Subject: [PATCH] Router parameter chaned with ServerRequestInterface --- src/Router.php | 9 ++++--- test/RouterTest.php | 62 +++++++++++++++++++-------------------------- 2 files changed, 31 insertions(+), 40 deletions(-) diff --git a/src/Router.php b/src/Router.php index 7f2ca3d..1f6e484 100644 --- a/src/Router.php +++ b/src/Router.php @@ -15,7 +15,7 @@ namespace Selami\Router; use Selami\Router\Exceptions\InvalidRequestMethodException; - +use Psr\Http\Message\ServerRequestInterface; /** * Router * @@ -107,9 +107,10 @@ final class Router */ public function __construct( int $defaultReturnType, - string $method, - string $requestedPath + ServerRequestInterface $request ) { + $method = $request->getMethod(); + $requestedPath = $request->getUri()->getPath(); if (!in_array($method, self::$validRequestMethods, true)) { $message = sprintf('%s is not valid Http request method.', $method); throw new InvalidRequestMethodException($message); @@ -133,7 +134,7 @@ public function withSubFolder(string $folder) : self return $new; } - public function withCacheFile(string $fileName) : self + public function withCacheFile(?string $fileName=null) : self { $new = clone $this; $new->cacheFile = $fileName; diff --git a/test/RouterTest.php b/test/RouterTest.php index 0441047..f00321c 100644 --- a/test/RouterTest.php +++ b/test/RouterTest.php @@ -2,11 +2,12 @@ namespace tests; -use Selami\Router\Exceptions\InvalidRequestMethodException; +use Psr\Http\Message\ServerRequestInterface; use Selami\Router\Router; use Zend\Diactoros\ServerRequestFactory; use ReflectionObject; use PHPUnit\Framework\TestCase; +use Zend\Diactoros\Uri; class RouterTest extends TestCase { @@ -40,6 +41,9 @@ public function setUp() : void $_SERVER['HTTPS'] = ''; $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; $_SERVER['REQUEST_TIME'] = time(); + /** + * @var ServerRequestInterface + */ $this->request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES); } @@ -51,8 +55,7 @@ public function shouldExtractRouteFromURLSuccessfully($requestedPath, $folder, $ { $router = new Router( $this->config['default_return_type'], - $this->request->getMethod(), - $this->request->getUri()->getPath() + $this->request ); $router= $router->withSubFolder($this->config['folder']); $router->add(Router::GET, '/', 'app/main', null, 'home'); @@ -86,8 +89,7 @@ public function shouldCacheRoutesSuccessfully() : void { $router = new Router( $this->config['default_return_type'], - $this->request->getMethod(), - $this->request->getUri()->getPath() + $this->request ); $router = $router->withSubFolder($this->config['folder']) ->withCacheFile($this->config['cache_file']); @@ -101,8 +103,7 @@ public function shouldCacheRoutesSuccessfully() : void // Rest of the test should run without throwing exception $router = new Router( $this->config['default_return_type'], - $this->request->getMethod(), - $this->request->getUri()->getPath() + $this->request ); $router = $router->withSubFolder($this->config['folder']) ->withCacheFile($this->config['cache_file']); @@ -117,8 +118,7 @@ public function shouldThrowExceptionForInvalidCachedFile() : void file_put_contents('/tmp/failed.cache', ''); $router = new Router( $this->config['default_return_type'], - $this->request->getMethod(), - $this->request->getUri()->getPath() + $this->request ); $router = $router->withSubFolder($this->config['folder']) ->withCacheFile('/tmp/failed.cache') @@ -140,8 +140,7 @@ public function shouldReadCacheRoutesSuccessfully($requestedPath, $folder, $expe { $router = new Router( $this->config['default_return_type'], - $this->request->getMethod(), - $this->request->getUri()->getPath() + $this->request ); $router = $router->withCacheFile($this->config['cache_file']) ->withSubFolder($this->config['folder']); @@ -169,8 +168,7 @@ public function shouldCorrectlyInstantiateRouter() : void { $router = new Router( $this->config['default_return_type'], - $this->request->getMethod(), - $this->request->getUri()->getPath() + $this->request ); $router = $router->withSubFolder($this->config['folder']); $router->add(Router::GET, '/', 'app/main', null, 'home'); @@ -193,9 +191,7 @@ public function shouldCorrectlyReturnRouteAndRouteAliases() : void { $router = new Router( Router::JSON, - $this->request->getMethod(), - '/alias/123', - $this->config['folder'] + $this->request->withUri(new Uri('/alias/123')) ); $router->get('/', 'app/main', null, 'home'); @@ -233,24 +229,22 @@ public function shouldThrowUnexpectedValueExceptionForCallMethod() : void { $router = new Router( $this->config['default_return_type'], - $this->request->getMethod(), - $this->request->getUri()->getPath(), - $this->config['folder'] + $this->request + ); + $router = $router->withSubFolder($this->config['folder']); $router->nonAvalibleHTTPMethod('/', 'app/main', null, 'home'); } /** * @test - * @expectedException \Selami\Router\Exceptions\InvalidRequestMethodException + * @expectedException \TypeError */ public function shouldThrowUnexpectedValueExceptionForConstructorMethod() : void { new Router( $this->config['default_return_type'], - 'UNEXPECTEDVALUE', - $this->request->getUri()->getPath(), - $this->config['folder'] + 'UNEXPECTED' ); } @@ -262,10 +256,9 @@ public function shouldThrowUnexpectedValueExceptionForAddMethod() : void { $router = new Router( $this->config['default_return_type'], - $this->request->getMethod(), - $this->request->getUri()->getPath(), - $this->config['folder'] + $this->request ); + $router = $router->withSubFolder($this->config['folder']); $router->add('nonAvailableHTTPMethod', '/', 'app/main', null, 'home'); } @@ -273,14 +266,13 @@ public function shouldThrowUnexpectedValueExceptionForAddMethod() : void * @test * @expectedException \TypeError */ - public function shouldThrowInvalidArgumentExceptionForAddMethodIfRequestMethotIsNotStringOrArray() : void + public function shouldThrowInvalidArgumentExceptionForAddMethodIfRequestMethodIsNotStringOrArray() : void { $router = new Router( $this->config['default_return_type'], - $this->request->getMethod(), - $this->request->getUri()->getPath(), - $this->config['folder'] + $this->request ); + $router = $router->withSubFolder($this->config['folder']); $router->add(200, '/', 'app/main', null, 'home'); } @@ -293,10 +285,9 @@ public function shouldCorrectlyReturnMethodNotAllowed() : void $this->request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES); $router = new Router( $this->config['default_return_type'], - $this->request->getMethod(), - $this->request->getUri()->getPath(), - $this->config['folder'] + $this->request ); + $router = $router->withSubFolder($this->config['folder']); $router->add(Router::GET, '/', 'app/main', null, 'home'); $router->add(Router::GET, '/json', 'app/json', Router::JSON); $router->add(Router::POST, '/json', 'app/redirect', Router::REDIRECT); @@ -315,10 +306,9 @@ public function shouldCorrectlyReturnNotFound() : void $this->request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES); $router = new Router( $this->config['default_return_type'], - $this->request->getMethod(), - $this->request->getUri()->getPath(), - $this->config['folder'] + $this->request ); + $router = $router->withSubFolder($this->config['folder']); $router->add(Router::GET, '/', 'app/main', null, 'home'); $router->add(Router::GET, '/json', 'app/json', Router::JSON); $router->add(Router::POST, '/json', 'app/redirect', Router::REDIRECT);