summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArnold Daniels <arnold@jasny.net>2016-10-21 00:25:42 +0200
committerArnold Daniels <arnold@jasny.net>2016-10-21 00:25:42 +0200
commit30df753c53fceb7c55ab742a86a987def0361a1b (patch)
tree0c0bb9113336bb49c521691a2fa4e9e372766d7a
parentde341ffb8554ee160ef0ba6e6f2b13794cd99ed8 (diff)
downloadrouter-30df753c53fceb7c55ab742a86a987def0361a1b.zip
router-30df753c53fceb7c55ab742a86a987def0361a1b.tar.gz
router-30df753c53fceb7c55ab742a86a987def0361a1b.tar.bz2
Minor fixes for Middleware\BasePath tests
-rw-r--r--src/Router/Middleware/BasePath.php24
-rw-r--r--tests/Router/Middleware/BasePathTest.php142
2 files changed, 83 insertions, 83 deletions
diff --git a/src/Router/Middleware/BasePath.php b/src/Router/Middleware/BasePath.php
index a80d029..f356aed 100644
--- a/src/Router/Middleware/BasePath.php
+++ b/src/Router/Middleware/BasePath.php
@@ -57,13 +57,15 @@ class BasePath
$uri = $request->getUri();
$path = $this->normalizePath($uri->getPath());
- if (!$this->hasBasePath($path)) return $this->setError($response);
+ if (!$this->hasBasePath($path)) {
+ return $this->notFound($request, $response);
+ }
$noBase = $this->getBaselessPath($path);
$noBaseUri = $uri->withPath($noBase);
- $request = $request->withUri($noBaseUri)->withAttribute('original_uri', $uri);
+ $rewrittenRequest = $request->withUri($noBaseUri)->withAttribute('original_uri', $uri);
- return call_user_func($next, $request, $response);
+ return $next($rewrittenRequest, $response);
}
/**
@@ -100,19 +102,17 @@ class BasePath
}
/**
- * Set error response
+ * Respond with 404 Not Found
*
- * @param ResponseInterface $response
+ * @param ServerRequestInterface $request
+ * @param ResponseInterface $response
* @return ResponseInterface
*/
- protected function setError($response)
+ protected function notFound(ServerRequestInterface $request, ResponseInterface $response)
{
- $message = 'Not Found';
-
- $body = $response->getBody();
- $body->rewind();
- $body->write($message);
+ $notFound = $response->withStatus(404);
+ $notFound->getBody()->write('Not Found');
- return $response->withStatus(404, $message)->withBody($body);
+ return $notFound;
}
}
diff --git a/tests/Router/Middleware/BasePathTest.php b/tests/Router/Middleware/BasePathTest.php
index eb9a2e8..d615d70 100644
--- a/tests/Router/Middleware/BasePathTest.php
+++ b/tests/Router/Middleware/BasePathTest.php
@@ -1,25 +1,22 @@
<?php
+namespace Jasny\Router\Middleware;
+
use Jasny\Router\Middleware\BasePath;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
-class BasePathTest extends PHPUnit_Framework_TestCase
-{
- /**
- * Test creating middleware with invalid parameter
- *
- * @dataProvider invalidConstructProvider
- */
- public function testInvalidConstruct($basePath)
- {
- $this->expectException(\InvalidArgumentException::class);
-
- $pathHandler = new BasePath($basePath);
- }
+use Jasny\Router\TestHelpers;
+/**
+ * @covers Jasny\Router\Middleware\BasePath
+ */
+class BasePathTest extends \PHPUnit_Framework_TestCase
+{
+ use TestHelpers;
+
/**
* Provide data for testing invalid BasePath creation
*
@@ -39,17 +36,14 @@ class BasePathTest extends PHPUnit_Framework_TestCase
}
/**
- * Test creating BasePath instance
+ * Test creating middleware with invalid parameter
*
- * @dataProvider validConstructProvider
- * @param string $basePath
+ * @dataProvider invalidConstructProvider
+ * @expectedException InvalidArgumentException
*/
- public function testValidConstruct($basePath, $validBasePath)
+ public function testInvalidConstruct($basePath)
{
- $pathHandler = new BasePath($basePath);
-
- $this->assertNotEmpty($pathHandler->getBasePath(), "Empty base path");
- $this->assertEquals($validBasePath, $pathHandler->getBasePath(), "Base path was not set correctly");
+ new BasePath($basePath);
}
/**
@@ -71,41 +65,32 @@ class BasePathTest extends PHPUnit_Framework_TestCase
}
/**
- * Test invoke with invalid 'next' param
+ * Test creating BasePath instance
+ *
+ * @dataProvider validConstructProvider
+ * @param string $basePath
*/
- public function testInvokeInvalidNext()
+ public function testValidConstruct($basePath, $validBasePath)
{
- $middleware = new BasePath('/foo');
- list($request, $response) = $this->getRequests();
-
- $this->expectException(\InvalidArgumentException::class);
+ $pathHandler = new BasePath($basePath);
- $result = $middleware($request, $response, 'not_callable');
+ $this->assertNotEmpty($pathHandler->getBasePath(), "Empty base path");
+ $this->assertEquals($validBasePath, $pathHandler->getBasePath(), "Base path was not set correctly");
}
/**
- * Test case when given request path does not starts with given base path
- *
- * @dataProvider notFoundProvider
- * @param string $basePath
- * @param string $path
+ * Test invoke with invalid 'next' param
+ *
+ * @expectedException InvalidArgumentException
*/
- public function testNotFound($basePath, $path)
+ public function testInvokeInvalidNext()
{
- $middleware = new BasePath($basePath);
- list($request, $response) = $this->getRequests();
-
- $this->expectRequestGetPath($request, $path);
- $this->expectNotFound($response);
-
- $result = $middleware($request, $response, function($response, $request) {
- $response->nextCalled = true;
+ $request = $this->createMock(ServerRequestInterface::class);
+ $response = $this->createMock(ResponseInterface::class);
- return $response;
- });
+ $middleware = new BasePath('/foo');
- $this->assertEquals(get_class($response), get_class($result), "Middleware should return response object");
- $this->assertFalse(isset($response->nextCalled), "'next' was called");
+ $middleware($request, $response, 'not_callable');
}
/**
@@ -128,28 +113,27 @@ class BasePathTest extends PHPUnit_Framework_TestCase
}
/**
- * Test correct case, when path contains base path
- *
- * @dataProvider foundProvider
+ * Test case when given request path does not starts with given base path
+ * @dataProvider notFoundProvider
+ *
* @param string $basePath
* @param string $path
- * @param string $noBasePath
*/
- public function testFound($basePath, $path, $noBasePath)
+ public function testNotFound($basePath, $path)
{
- $middleware = new BasePath($basePath);
- list($request, $response) = $this->getRequests();
-
- $this->expectRequestSetBasePath($request, $basePath, $path, $noBasePath);
+ $request = $this->createMock(ServerRequestInterface::class);
+ $response = $this->createMock(ResponseInterface::class);
- $result = $middleware($request, $response, function($request, $response) {
- $response->nextCalled = true;
+ $middleware = new BasePath($basePath);
+
+ $this->expectRequestGetPath($request, $path);
+ $finalResponse = $this->expectNotFound($response);
- return $response;
- });
+ $next = $this->createCallbackMock($this->never());
+
+ $result = $middleware($request, $response, $next);
- $this->assertEquals(get_class($response), get_class($result), "Middleware should return response object");
- $this->assertTrue($response->nextCalled, "'next' was not called");
+ $this->assertSame($finalResponse, $result);
}
/**
@@ -172,19 +156,31 @@ class BasePathTest extends PHPUnit_Framework_TestCase
}
/**
- * Get requests for testing
- *
- * @param string $path
- * @return array
+ * Test correct case, when path contains base path
+ * @dataProvider foundProvider
+ *
+ * @param string $basePath
+ * @param string $path
+ * @param string $noBasePath
*/
- public function getRequests($path = null)
+ public function testFound($basePath, $path, $noBasePath)
{
$request = $this->createMock(ServerRequestInterface::class);
$response = $this->createMock(ResponseInterface::class);
+ $finalRespose = $this->createMock(ResponseInterface::class);
+
+ $middleware = new BasePath($basePath);
+
+ $this->expectRequestSetBasePath($request, $basePath, $path, $noBasePath);
- return [$request, $response];
+ $next = $this->createCallbackMock($this->once(), [$request, $response], $finalRespose);
+
+ $result = $middleware($request, $response, $next);
+
+ $this->assertSame($finalRespose, $result);
}
+
/**
* Expect that request will return a path
*
@@ -221,15 +217,19 @@ class BasePathTest extends PHPUnit_Framework_TestCase
* Expect for not found error
*
* @param ResponseInterface $response
+ * @return ResponseInterface
*/
public function expectNotFound(ResponseInterface $response)
{
+ $finalResponse = $this->createMock(ResponseInterface::class);
+
+ $response->expects($this->once())->method('withStatus')->with(404)->willReturn($finalResponse);
+
$stream = $this->createMock(StreamInterface::class);
- $stream->expects($this->once())->method('rewind');
$stream->expects($this->once())->method('write')->with($this->equalTo('Not Found'));
- $response->method('getBody')->will($this->returnValue($stream));
- $response->expects($this->once())->method('withBody')->with($this->equalTo($stream))->will($this->returnSelf());
- $response->expects($this->once())->method('withStatus')->with($this->equalTo(404), $this->equalTo('Not Found'))->will($this->returnSelf());
+ $finalResponse->expects($this->once())->method('getBody')->willReturn($stream);
+
+ return $finalResponse;
}
}