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

Commit

Permalink
Router parameter chaned with ServerRequestInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorkmaz committed Jan 3, 2019
1 parent 9f23b95 commit 9619df2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 40 deletions.
9 changes: 5 additions & 4 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace Selami\Router;

use Selami\Router\Exceptions\InvalidRequestMethodException;

use Psr\Http\Message\ServerRequestInterface;
/**
* Router
*
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
62 changes: 26 additions & 36 deletions test/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}

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

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

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

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

0 comments on commit 9619df2

Please sign in to comment.