Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
createWithServerRequestInterface named constructor added
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorkmaz committed Jan 3, 2019
1 parent 9619df2 commit c8e25a5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
10 changes: 7 additions & 3 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
52 changes: 27 additions & 25 deletions test/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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
);
Expand Down Expand Up @@ -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
);
Expand All @@ -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
);
Expand All @@ -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
);
Expand All @@ -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
);
Expand Down Expand Up @@ -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
);
Expand All @@ -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'))
);
Expand Down Expand Up @@ -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']
);
}

Expand All @@ -256,23 +254,25 @@ 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');
}

/**
* @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');
}

Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit c8e25a5

Please sign in to comment.