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

Commit c8e25a5

Browse files
committed
createWithServerRequestInterface named constructor added
1 parent 9619df2 commit c8e25a5

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

src/Router.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,9 @@ final class Router
107107
*/
108108
public function __construct(
109109
int $defaultReturnType,
110-
ServerRequestInterface $request
110+
string $method,
111+
string $requestedPath
111112
) {
112-
$method = $request->getMethod();
113-
$requestedPath = $request->getUri()->getPath();
114113
if (!in_array($method, self::$validRequestMethods, true)) {
115114
$message = sprintf('%s is not valid Http request method.', $method);
116115
throw new InvalidRequestMethodException($message);
@@ -120,6 +119,11 @@ public function __construct(
120119
$this->defaultReturnType = ($defaultReturnType >=1 && $defaultReturnType <=7) ? $defaultReturnType : self::HTML;
121120
}
122121

122+
public static function createWithServerRequestInterface(int $defaultReturnType, ServerRequestInterface $request)
123+
{
124+
return new self($defaultReturnType, $request->getMethod(), $request->getUri()->getPath());
125+
}
126+
123127
public function withDefaultReturnType(int $defaultReturnType) : self
124128
{
125129
$new = clone $this;

test/RouterTest.php

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace tests;
44

5-
use Psr\Http\Message\ServerRequestInterface;
5+
use Selami\Router\Exceptions\InvalidRequestMethodException;
66
use Selami\Router\Router;
77
use Zend\Diactoros\ServerRequestFactory;
88
use ReflectionObject;
@@ -41,9 +41,6 @@ public function setUp() : void
4141
$_SERVER['HTTPS'] = '';
4242
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
4343
$_SERVER['REQUEST_TIME'] = time();
44-
/**
45-
* @var ServerRequestInterface
46-
*/
4744
$this->request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
4845
}
4946

@@ -53,7 +50,7 @@ public function setUp() : void
5350
*/
5451
public function shouldExtractRouteFromURLSuccessfully($requestedPath, $folder, $expected) : void
5552
{
56-
$router = new Router(
53+
$router = Router::createWithServerRequestInterface(
5754
$this->config['default_return_type'],
5855
$this->request
5956
);
@@ -87,7 +84,7 @@ public function extractFolderDataProvider() : array
8784
*/
8885
public function shouldCacheRoutesSuccessfully() : void
8986
{
90-
$router = new Router(
87+
$router = Router::createWithServerRequestInterface(
9188
$this->config['default_return_type'],
9289
$this->request
9390
);
@@ -101,7 +98,7 @@ public function shouldCacheRoutesSuccessfully() : void
10198
'Couldn\'t cache the file'
10299
);
103100
// Rest of the test should run without throwing exception
104-
$router = new Router(
101+
$router = Router::createWithServerRequestInterface(
105102
$this->config['default_return_type'],
106103
$this->request
107104
);
@@ -116,7 +113,7 @@ public function shouldCacheRoutesSuccessfully() : void
116113
public function shouldThrowExceptionForInvalidCachedFile() : void
117114
{
118115
file_put_contents('/tmp/failed.cache', '');
119-
$router = new Router(
116+
$router = Router::createWithServerRequestInterface(
120117
$this->config['default_return_type'],
121118
$this->request
122119
);
@@ -138,7 +135,7 @@ public function shouldThrowExceptionForInvalidCachedFile() : void
138135
*/
139136
public function shouldReadCacheRoutesSuccessfully($requestedPath, $folder, $expected) : void
140137
{
141-
$router = new Router(
138+
$router = Router::createWithServerRequestInterface(
142139
$this->config['default_return_type'],
143140
$this->request
144141
);
@@ -166,7 +163,7 @@ public function shouldReadCacheRoutesSuccessfully($requestedPath, $folder, $expe
166163
*/
167164
public function shouldCorrectlyInstantiateRouter() : void
168165
{
169-
$router = new Router(
166+
$router = Router::createWithServerRequestInterface(
170167
$this->config['default_return_type'],
171168
$this->request
172169
);
@@ -189,7 +186,7 @@ public function shouldCorrectlyInstantiateRouter() : void
189186
*/
190187
public function shouldCorrectlyReturnRouteAndRouteAliases() : void
191188
{
192-
$router = new Router(
189+
$router = Router::createWithServerRequestInterface(
193190
Router::JSON,
194191
$this->request->withUri(new Uri('/alias/123'))
195192
);
@@ -229,22 +226,23 @@ public function shouldThrowUnexpectedValueExceptionForCallMethod() : void
229226
{
230227
$router = new Router(
231228
$this->config['default_return_type'],
232-
$this->request
233-
229+
'NON-HTTP-REQUEST',
230+
$this->config['folder']
234231
);
235-
$router = $router->withSubFolder($this->config['folder']);
236232
$router->nonAvalibleHTTPMethod('/', 'app/main', null, 'home');
237233
}
238234

239235
/**
240236
* @test
241-
* @expectedException \TypeError
237+
* @expectedException \Selami\Router\Exceptions\InvalidRequestMethodException
242238
*/
243239
public function shouldThrowUnexpectedValueExceptionForConstructorMethod() : void
244240
{
245241
new Router(
246242
$this->config['default_return_type'],
247-
'UNEXPECTED'
243+
'UNEXPECTEDVALUE',
244+
$this->request->getUri()->getPath(),
245+
$this->config['folder']
248246
);
249247
}
250248

@@ -256,23 +254,25 @@ public function shouldThrowUnexpectedValueExceptionForAddMethod() : void
256254
{
257255
$router = new Router(
258256
$this->config['default_return_type'],
259-
$this->request
257+
$this->request->getMethod(),
258+
$this->request->getUri()->getPath(),
259+
$this->config['folder']
260260
);
261-
$router = $router->withSubFolder($this->config['folder']);
262261
$router->add('nonAvailableHTTPMethod', '/', 'app/main', null, 'home');
263262
}
264263

265264
/**
266265
* @test
267266
* @expectedException \TypeError
268267
*/
269-
public function shouldThrowInvalidArgumentExceptionForAddMethodIfRequestMethodIsNotStringOrArray() : void
268+
public function shouldThrowInvalidArgumentExceptionForAddMethodIfRequestMethotIsNotStringOrArray() : void
270269
{
271270
$router = new Router(
272271
$this->config['default_return_type'],
273-
$this->request
272+
$this->request->getMethod(),
273+
$this->request->getUri()->getPath(),
274+
$this->config['folder']
274275
);
275-
$router = $router->withSubFolder($this->config['folder']);
276276
$router->add(200, '/', 'app/main', null, 'home');
277277
}
278278

@@ -285,9 +285,10 @@ public function shouldCorrectlyReturnMethodNotAllowed() : void
285285
$this->request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
286286
$router = new Router(
287287
$this->config['default_return_type'],
288-
$this->request
288+
$this->request->getMethod(),
289+
$this->request->getUri()->getPath(),
290+
$this->config['folder']
289291
);
290-
$router = $router->withSubFolder($this->config['folder']);
291292
$router->add(Router::GET, '/', 'app/main', null, 'home');
292293
$router->add(Router::GET, '/json', 'app/json', Router::JSON);
293294
$router->add(Router::POST, '/json', 'app/redirect', Router::REDIRECT);
@@ -306,9 +307,10 @@ public function shouldCorrectlyReturnNotFound() : void
306307
$this->request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
307308
$router = new Router(
308309
$this->config['default_return_type'],
309-
$this->request
310+
$this->request->getMethod(),
311+
$this->request->getUri()->getPath(),
312+
$this->config['folder']
310313
);
311-
$router = $router->withSubFolder($this->config['folder']);
312314
$router->add(Router::GET, '/', 'app/main', null, 'home');
313315
$router->add(Router::GET, '/json', 'app/json', Router::JSON);
314316
$router->add(Router::POST, '/json', 'app/redirect', Router::REDIRECT);

0 commit comments

Comments
 (0)