summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorminstel <minstel@yandex.ru>2016-10-19 14:43:27 +0300
committerminstel <minstel@yandex.ru>2016-10-19 14:43:27 +0300
commitf55bccf1c0fb060f548db7838c533755f21f493d (patch)
treee909698f4bfa46433b3270f3a7b20b4203418c91
parentea5556fe7059fe07bd6fe85c9a656f637d04309b (diff)
downloadrouter-f55bccf1c0fb060f548db7838c533755f21f493d.zip
router-f55bccf1c0fb060f548db7838c533755f21f493d.tar.gz
router-f55bccf1c0fb060f548db7838c533755f21f493d.tar.bz2
Fixes
-rw-r--r--src/Router/Middleware/BasePath.php17
-rw-r--r--tests/Router/Middleware/BasePathTest.php95
2 files changed, 31 insertions, 81 deletions
diff --git a/src/Router/Middleware/BasePath.php b/src/Router/Middleware/BasePath.php
index a209a1f..a80d029 100644
--- a/src/Router/Middleware/BasePath.php
+++ b/src/Router/Middleware/BasePath.php
@@ -48,9 +48,9 @@ class BasePath
* @param callback $next
* @return ResponseInterface
*/
- public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next = null)
+ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
{
- if ($next && !is_callable($next)) {
+ if (!is_callable($next)) {
throw new \InvalidArgumentException("'next' should be a callback");
}
@@ -58,11 +58,10 @@ class BasePath
$path = $this->normalizePath($uri->getPath());
if (!$this->hasBasePath($path)) return $this->setError($response);
- if (!$next) return $response;
$noBase = $this->getBaselessPath($path);
- $uri = $uri->withPath($noBase);
- $request = $request->withUri($uri)->withAttribute('original_uri', $path);
+ $noBaseUri = $uri->withPath($noBase);
+ $request = $request->withUri($noBaseUri)->withAttribute('original_uri', $uri);
return call_user_func($next, $request, $response);
}
@@ -75,9 +74,7 @@ class BasePath
*/
protected function getBaselessPath($path)
{
- $path = preg_replace('|^' . preg_quote($this->getBasePath()) . '|i', '', $path);
-
- return $path ?: '/';
+ return substr($path, strlen($this->getBasePath())) ?: '/';
}
/**
@@ -88,7 +85,7 @@ class BasePath
*/
protected function normalizePath($path)
{
- return '/' . trim($path, '/');
+ return '/' . ltrim($path, '/');
}
/**
@@ -99,7 +96,7 @@ class BasePath
*/
protected function hasBasePath($path)
{
- return preg_match('#^' . preg_quote($this->getBasePath()) . '(\/|$)#i', $path);
+ return strpos($path . '/', $this->getBasePath() . '/') === 0;
}
/**
diff --git a/tests/Router/Middleware/BasePathTest.php b/tests/Router/Middleware/BasePathTest.php
index 4edaba0..eb9a2e8 100644
--- a/tests/Router/Middleware/BasePathTest.php
+++ b/tests/Router/Middleware/BasePathTest.php
@@ -44,12 +44,12 @@ class BasePathTest extends PHPUnit_Framework_TestCase
* @dataProvider validConstructProvider
* @param string $basePath
*/
- public function testValidConstruct($basePath)
+ public function testValidConstruct($basePath, $validBasePath)
{
$pathHandler = new BasePath($basePath);
$this->assertNotEmpty($pathHandler->getBasePath(), "Empty base path");
- $this->assertEquals($this->normalizePath($basePath), $pathHandler->getBasePath(), "Base path was not set correctly");
+ $this->assertEquals($validBasePath, $pathHandler->getBasePath(), "Base path was not set correctly");
}
/**
@@ -60,13 +60,13 @@ class BasePathTest extends PHPUnit_Framework_TestCase
public function validConstructProvider()
{
return [
- ['/foo'],
- ['/foo/'],
- ['foo/'],
- ['/foo/bar'],
- ['foo/bar'],
- ['/foo/bar/'],
- ['/foo/bar-zet/']
+ ['/foo', '/foo'],
+ ['/foo/', '/foo/'],
+ ['foo/', '/foo/'],
+ ['/foo/bar', '/foo/bar'],
+ ['foo/bar', '/foo/bar'],
+ ['/foo/bar/', '/foo/bar/'],
+ ['/foo/bar-zet/', '/foo/bar-zet/']
];
}
@@ -133,13 +133,14 @@ class BasePathTest extends PHPUnit_Framework_TestCase
* @dataProvider foundProvider
* @param string $basePath
* @param string $path
+ * @param string $noBasePath
*/
- public function testFound($basePath, $path)
+ public function testFound($basePath, $path, $noBasePath)
{
$middleware = new BasePath($basePath);
list($request, $response) = $this->getRequests();
- $this->expectRequestSetBasePath($request, $basePath, $path);
+ $this->expectRequestSetBasePath($request, $basePath, $path, $noBasePath);
$result = $middleware($request, $response, function($request, $response) {
$response->nextCalled = true;
@@ -151,26 +152,6 @@ class BasePathTest extends PHPUnit_Framework_TestCase
$this->assertTrue($response->nextCalled, "'next' was not called");
}
-
- /**
- * Test correct case, when path contains base path, with no 'next' callback
- *
- * @dataProvider foundProvider
- * @param string $basePath
- * @param string $path
- */
- public function testFoundNoNext($basePath, $path)
- {
- $middleware = new BasePath($basePath);
- list($request, $response) = $this->getRequests();
-
- $this->expectRequestGetPath($request, $path);
-
- $result = $middleware($request, $response);
-
- $this->assertEquals($response, $result, "Middleware should return response object");
- }
-
/**
* Provide data for testing BasePath creation
*
@@ -179,45 +160,18 @@ class BasePathTest extends PHPUnit_Framework_TestCase
public function foundProvider()
{
return [
- ['/foo', '/foo'],
- ['foo', '/foo'],
- ['/foo', '/foo/bar'],
- ['/foo/bar', '/foo/bar'],
- ['foo/bar', '/foo/bar'],
- ['/foo/bar', '/foo/bar/zet'],
- ['/f', '/f/foo'],
- ['f', '/f/foo'],
+ ['/foo', '/foo', '/'],
+ ['foo', '/foo', '/'],
+ ['/foo', '/foo/bar', '/bar'],
+ ['/foo/bar', '/foo/bar', '/'],
+ ['foo/bar', '/foo/bar', '/'],
+ ['/foo/bar', '/foo/bar/zet', '/zet'],
+ ['/f', '/f/foo', '/foo'],
+ ['f', '/f/foo', '/foo'],
];
}
/**
- * Normalize path
- *
- * @param string $path
- * @return string
- */
- public function normalizePath($path)
- {
- return '/' . trim($path, '/');
- }
-
- /**
- * Remove base path from given path
- *
- * @param string $path
- * @return string
- */
- protected function getBaselessPath($basePath, $path)
- {
- $basePath = $this->normalizePath($basePath);
- $path = $this->normalizePath($path);
-
- $path = preg_replace('|^' . preg_quote($basePath) . '|i', '', $path);
-
- return $path ?: '/';
- }
-
- /**
* Get requests for testing
*
* @param string $path
@@ -250,18 +204,17 @@ class BasePathTest extends PHPUnit_Framework_TestCase
* @param ServerRequestInterface $request
* @param string $basePath
* @param string $path
+ * @param string $noBasePath
*/
- public function expectRequestSetBasePath(ServerRequestInterface $request, $basePath, $path)
+ public function expectRequestSetBasePath(ServerRequestInterface $request, $basePath, $path, $noBasePath)
{
- $noBase = $this->getBaselessPath($basePath, $path);
-
$uri = $this->createMock(UriInterface::class);
$uri->expects($this->once())->method('getPath')->will($this->returnValue($path));
- $uri->expects($this->once())->method('withPath')->with($this->equalTo($noBase))->will($this->returnSelf());
+ $uri->expects($this->once())->method('withPath')->with($this->equalTo($noBasePath))->will($this->returnSelf());
$request->expects($this->once())->method('getUri')->will($this->returnValue($uri));
$request->expects($this->once())->method('withUri')->with($this->equalTo($uri))->will($this->returnSelf());
- $request->expects($this->once())->method('withAttribute')->with($this->equalTo('original_uri'), $this->equalTo($path))->will($this->returnSelf());
+ $request->expects($this->once())->method('withAttribute')->with($this->equalTo('original_uri'), $this->equalTo($uri))->will($this->returnSelf());
}
/**