From c8e25a5f97a603eb813a62eb8cd4588b14e59893 Mon Sep 17 00:00:00 2001 From: Mehmet Korkmaz Date: Thu, 3 Jan 2019 15:52:59 +0300 Subject: [PATCH] createWithServerRequestInterface named constructor added --- src/Router.php | 10 ++++++--- test/RouterTest.php | 52 +++++++++++++++++++++++---------------------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/Router.php b/src/Router.php index 1f6e484..178a904 100644 --- a/src/Router.php +++ b/src/Router.php @@ -107,10 +107,9 @@ final class Router */ public function __construct( int $defaultReturnType, - ServerRequestInterface $request + string $method, + string $requestedPath ) { - $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); @@ -120,6 +119,11 @@ public function __construct( $this->defaultReturnType = ($defaultReturnType >=1 && $defaultReturnType <=7) ? $defaultReturnType : self::HTML; } + public static function createWithServerRequestInterface(int $defaultReturnType, ServerRequestInterface $request) + { + return new self($defaultReturnType, $request->getMethod(), $request->getUri()->getPath()); + } + public function withDefaultReturnType(int $defaultReturnType) : self { $new = clone $this; diff --git a/test/RouterTest.php b/test/RouterTest.php index f00321c..de324fb 100644 --- a/test/RouterTest.php +++ b/test/RouterTest.php @@ -2,7 +2,7 @@ namespace tests; -use Psr\Http\Message\ServerRequestInterface; +use Selami\Router\Exceptions\InvalidRequestMethodException; use Selami\Router\Router; use Zend\Diactoros\ServerRequestFactory; use ReflectionObject; @@ -41,9 +41,6 @@ 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); } @@ -53,7 +50,7 @@ public function setUp() : void */ public function shouldExtractRouteFromURLSuccessfully($requestedPath, $folder, $expected) : void { - $router = new Router( + $router = Router::createWithServerRequestInterface( $this->config['default_return_type'], $this->request ); @@ -87,7 +84,7 @@ public function extractFolderDataProvider() : array */ public function shouldCacheRoutesSuccessfully() : void { - $router = new Router( + $router = Router::createWithServerRequestInterface( $this->config['default_return_type'], $this->request ); @@ -101,7 +98,7 @@ public function shouldCacheRoutesSuccessfully() : void 'Couldn\'t cache the file' ); // Rest of the test should run without throwing exception - $router = new Router( + $router = Router::createWithServerRequestInterface( $this->config['default_return_type'], $this->request ); @@ -116,7 +113,7 @@ public function shouldCacheRoutesSuccessfully() : void public function shouldThrowExceptionForInvalidCachedFile() : void { file_put_contents('/tmp/failed.cache', ''); - $router = new Router( + $router = Router::createWithServerRequestInterface( $this->config['default_return_type'], $this->request ); @@ -138,7 +135,7 @@ public function shouldThrowExceptionForInvalidCachedFile() : void */ public function shouldReadCacheRoutesSuccessfully($requestedPath, $folder, $expected) : void { - $router = new Router( + $router = Router::createWithServerRequestInterface( $this->config['default_return_type'], $this->request ); @@ -166,7 +163,7 @@ public function shouldReadCacheRoutesSuccessfully($requestedPath, $folder, $expe */ public function shouldCorrectlyInstantiateRouter() : void { - $router = new Router( + $router = Router::createWithServerRequestInterface( $this->config['default_return_type'], $this->request ); @@ -189,7 +186,7 @@ public function shouldCorrectlyInstantiateRouter() : void */ public function shouldCorrectlyReturnRouteAndRouteAliases() : void { - $router = new Router( + $router = Router::createWithServerRequestInterface( Router::JSON, $this->request->withUri(new Uri('/alias/123')) ); @@ -229,22 +226,23 @@ public function shouldThrowUnexpectedValueExceptionForCallMethod() : void { $router = new Router( $this->config['default_return_type'], - $this->request - + 'NON-HTTP-REQUEST', + $this->config['folder'] ); - $router = $router->withSubFolder($this->config['folder']); $router->nonAvalibleHTTPMethod('/', 'app/main', null, 'home'); } /** * @test - * @expectedException \TypeError + * @expectedException \Selami\Router\Exceptions\InvalidRequestMethodException */ public function shouldThrowUnexpectedValueExceptionForConstructorMethod() : void { new Router( $this->config['default_return_type'], - 'UNEXPECTED' + 'UNEXPECTEDVALUE', + $this->request->getUri()->getPath(), + $this->config['folder'] ); } @@ -256,9 +254,10 @@ public function shouldThrowUnexpectedValueExceptionForAddMethod() : void { $router = new Router( $this->config['default_return_type'], - $this->request + $this->request->getMethod(), + $this->request->getUri()->getPath(), + $this->config['folder'] ); - $router = $router->withSubFolder($this->config['folder']); $router->add('nonAvailableHTTPMethod', '/', 'app/main', null, 'home'); } @@ -266,13 +265,14 @@ public function shouldThrowUnexpectedValueExceptionForAddMethod() : void * @test * @expectedException \TypeError */ - public function shouldThrowInvalidArgumentExceptionForAddMethodIfRequestMethodIsNotStringOrArray() : void + public function shouldThrowInvalidArgumentExceptionForAddMethodIfRequestMethotIsNotStringOrArray() : void { $router = new Router( $this->config['default_return_type'], - $this->request + $this->request->getMethod(), + $this->request->getUri()->getPath(), + $this->config['folder'] ); - $router = $router->withSubFolder($this->config['folder']); $router->add(200, '/', 'app/main', null, 'home'); } @@ -285,9 +285,10 @@ public function shouldCorrectlyReturnMethodNotAllowed() : void $this->request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES); $router = new Router( $this->config['default_return_type'], - $this->request + $this->request->getMethod(), + $this->request->getUri()->getPath(), + $this->config['folder'] ); - $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); @@ -306,9 +307,10 @@ public function shouldCorrectlyReturnNotFound() : void $this->request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES); $router = new Router( $this->config['default_return_type'], - $this->request + $this->request->getMethod(), + $this->request->getUri()->getPath(), + $this->config['folder'] ); - $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);