summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorArnold Daniels <arnold@jasny.net>2016-11-29 14:40:21 +0100
committerArnold Daniels <arnold@jasny.net>2016-11-29 14:40:21 +0100
commit058a9c68acbc2bcbff21749df4207ac2f550586d (patch)
treef0d3ac09c76ec38fa7420587db3d1706c72cb777 /tests
parent2ab3d5087944baf87980c234d7e73958f65261a1 (diff)
downloadrouter-origin/middleware-path.zip
router-origin/middleware-path.tar.gz
router-origin/middleware-path.tar.bz2
Added tests for adding middleware with a pathorigin/middleware-path
Diffstat (limited to 'tests')
-rw-r--r--tests/RouterTest.php67
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/RouterTest.php b/tests/RouterTest.php
index e1c080c..3e5accf 100644
--- a/tests/RouterTest.php
+++ b/tests/RouterTest.php
@@ -10,6 +10,7 @@ use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
+use PHPUnit_Framework_MockObject_Matcher_InvokedCount as InvokedCount;
use Jasny\Router\TestHelpers;
@@ -170,6 +171,50 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$this->assertSame([$middlewareOne, $middlewareTwo], $router->getMiddlewares());
}
+
+ public function middlewareWithPathProvider()
+ {
+ return [
+ ['/', $this->never(), $this->once(), 'zoo'],
+ ['/foo', $this->once(), $this->never(), 'foo'],
+ ['/foo/bar', $this->once(), $this->never(), 'foo'],
+ ['/zoo', $this->never(), $this->once(), 'zoo']
+ ];
+ }
+
+ /**
+ * Test adding middleware action specifying a path
+ * @dataProvider middlewareWithPathProvider
+ *
+ * @param string $path
+ * @param InvokedCount $invokeMiddleware
+ * @param InvokedCount $invokeNext
+ * @param string $expect
+ */
+ public function testAddMiddlewareWithPath($path, $invokeMiddleware, $invokeNext, $expect)
+ {
+ $uri = $this->createMock(UriInterface::class);
+ $uri->method('getPath')->willReturn($path);
+
+ $request = $this->createMock(ServerRequestInterface::class);
+ $request->method('getUri')->willReturn($uri);
+
+ $response = $this->createMock(ResponseInterface::class);
+
+ $middleware = $this->createCallbackMock($invokeMiddleware, [$request, $response], 'foo');
+ $next = $this->createCallbackMock($invokeNext, [$request, $response], 'zoo');
+
+ $router = new Router($this->createMock(Routes::class));
+ $router->add('/foo', $middleware);
+
+ list($fn) = $router->getMiddlewares() + [null];
+
+ $this->assertNotSame($middleware, $fn);
+
+ $result = $fn($request, $response, $next);
+
+ $this->assertEquals($expect, $result);
+ }
/**
* @expectedException \InvalidArgumentException
@@ -179,6 +224,28 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$router = new Router($this->createMock(Routes::class));
$router->add('foo bar zoo');
}
+
+ /**
+ * @expectedException PHPUnit_Framework_Error
+ * @expectedException Middleware path 'foobar' doesn't start with a '/'
+ */
+ public function testAddNoticeWeirdPath()
+ {
+ $middleware = $this->createCallbackMock($this->never());
+
+ $router = new Router($this->createMock(Routes::class));
+ $router->add('foobar', $middleware);
+ }
+
+ public function testAddNoticeWeirdPathSkip()
+ {
+ $middleware = $this->createCallbackMock($this->never());
+
+ $router = new Router($this->createMock(Routes::class));
+ @$router->add('foobar', $middleware);
+
+ $this->assertCount(1, $router->getMiddlewares());
+ }
/**
* Test executing router with middlewares chain