From 91249d885fa8cd1f05bf144deea0dce6bb24d3c4 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Sat, 21 May 2016 10:16:22 -0400 Subject: [PATCH] MethodMap tests use Dispatcher and MiddlewareMock; rename NextMock --- test/src/MiddlewareMock.php | 32 ++++++ test/src/{NextSpy.php => NextMock.php} | 8 +- .../unit/Dispatching/DispatchStackTest.php | 4 +- .../tests/unit/Dispatching/DispatcherTest.php | 4 +- test/tests/unit/Routing/MethodMapTest.php | 108 +++++++----------- 5 files changed, 82 insertions(+), 74 deletions(-) create mode 100644 test/src/MiddlewareMock.php rename test/src/{NextSpy.php => NextMock.php} (76%) diff --git a/test/src/MiddlewareMock.php b/test/src/MiddlewareMock.php new file mode 100644 index 0000000..628ceee --- /dev/null +++ b/test/src/MiddlewareMock.php @@ -0,0 +1,32 @@ +called = true; + $this->callCount++; + $this->request = $request; + $this->response = $response; + if ($this->propagate) { + return $next($request, $response); + } else { + return $response; + } + } +} diff --git a/test/src/NextSpy.php b/test/src/NextMock.php similarity index 76% rename from test/src/NextSpy.php rename to test/src/NextMock.php index bc05f16..93e1d8a 100644 --- a/test/src/NextSpy.php +++ b/test/src/NextMock.php @@ -5,7 +5,7 @@ namespace WellRESTed\Test; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -class NextSpy +class NextMock { public $called = false; public $request = null; @@ -13,11 +13,11 @@ class NextSpy public function __invoke( ServerRequestInterface $request, - ResponseInterface $respone + ResponseInterface $response ) { $this->called = true; $this->request = $request; - $this->response = $respone; - return $respone; + $this->response = $response; + return $response; } } diff --git a/test/tests/unit/Dispatching/DispatchStackTest.php b/test/tests/unit/Dispatching/DispatchStackTest.php index 50c47d9..37542da 100644 --- a/test/tests/unit/Dispatching/DispatchStackTest.php +++ b/test/tests/unit/Dispatching/DispatchStackTest.php @@ -6,7 +6,7 @@ use WellRESTed\Dispatching\Dispatcher; use WellRESTed\Dispatching\DispatchStack; use WellRESTed\Message\Response; use WellRESTed\Message\ServerRequest; -use WellRESTed\Test\NextSpy; +use WellRESTed\Test\NextMock; /** * @covers WellRESTed\Dispatching\DispatchStack @@ -23,7 +23,7 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase parent::setUp(); $this->request = new ServerRequest(); $this->response = new Response(); - $this->next = new NextSpy(); + $this->next = new NextMock(); } public function testDispatchesMiddlewareInOrderAdded() diff --git a/test/tests/unit/Dispatching/DispatcherTest.php b/test/tests/unit/Dispatching/DispatcherTest.php index 9cc3921..e386de1 100644 --- a/test/tests/unit/Dispatching/DispatcherTest.php +++ b/test/tests/unit/Dispatching/DispatcherTest.php @@ -9,7 +9,7 @@ use WellRESTed\Dispatching\Dispatcher; use WellRESTed\Message\Response; use WellRESTed\Message\ServerRequest; use WellRESTed\MiddlewareInterface; -use WellRESTed\Test\NextSpy; +use WellRESTed\Test\NextMock; /** * @covers WellRESTed\Dispatching\Dispatcher @@ -25,7 +25,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase { $this->request = new ServerRequest(); $this->response = new Response(); - $this->next = new NextSpy(); + $this->next = new NextMock(); } public function testDispatchesCallableThatReturnsResponse() diff --git a/test/tests/unit/Routing/MethodMapTest.php b/test/tests/unit/Routing/MethodMapTest.php index 8f43069..29f46d9 100644 --- a/test/tests/unit/Routing/MethodMapTest.php +++ b/test/tests/unit/Routing/MethodMapTest.php @@ -3,10 +3,12 @@ namespace WellRESTed\Test\Unit\Routing; use Prophecy\Argument; +use WellRESTed\Dispatching\Dispatcher; use WellRESTed\Message\Response; use WellRESTed\Message\ServerRequest; use WellRESTed\Routing\MethodMap; -use WellRESTed\Test\NextSpy; +use WellRESTed\Test\MiddlewareMock; +use WellRESTed\Test\NextMock; /** * @covers WellRESTed\Routing\MethodMap @@ -24,95 +26,69 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase { $this->request = new ServerRequest(); $this->response = new Response(); - $this->next = new NextSpy(); - $this->middleware = $this->prophesize('WellRESTed\MiddlewareInterface'); - $this->middleware->__invoke(Argument::cetera())->willReturn(); - $this->dispatcher = $this->prophesize('WellRESTed\Dispatching\DispatcherInterface'); - $this->dispatcher->dispatch(Argument::cetera())->will( - function ($args) { - list($middleware, $request, $response, $next) = $args; - return $middleware($request, $response, $next); - } - ); + $this->next = new NextMock(); + $this->middleware = new MiddlewareMock(); + $this->dispatcher = new Dispatcher(); } - public function testCreatesInstance() - { - $methodMap = new MethodMap($this->dispatcher->reveal()); - $this->assertNotNull($methodMap); + private function getMethodMap() { + return new MethodMap($this->dispatcher); } + // ------------------------------------------------------------------------- + public function testDispatchesMiddlewareWithMatchingMethod() { $this->request = $this->request->withMethod("GET"); - $map = new MethodMap($this->dispatcher->reveal()); - $map->register("GET", $this->middleware->reveal()); + $map = $this->getMethodMap(); + $map->register("GET", $this->middleware); $map($this->request, $this->response, $this->next); - $this->middleware->__invoke( - $this->request, - $this->response, - $this->next - )->shouldHaveBeenCalled(); + $this->assertTrue($this->middleware->called); } public function testTreatsMethodNamesCaseSensitively() { $this->request = $this->request->withMethod("get"); - $middlewareUpper = $this->prophesize('WellRESTed\MiddlewareInterface'); - $middlewareUpper->__invoke(Argument::cetera())->willReturn(); + $middlewareUpper = new MiddlewareMock(); + $middlewareLower = new MiddlewareMock(); - $middlewareLower = $this->prophesize('WellRESTed\MiddlewareInterface'); - $middlewareLower->__invoke(Argument::cetera())->willReturn(); - - $map = new MethodMap($this->dispatcher->reveal()); - $map->register("GET", $middlewareUpper->reveal()); - $map->register("get", $middlewareLower->reveal()); + $map = $this->getMethodMap(); + $map->register("GET", $middlewareUpper); + $map->register("get", $middlewareLower); $map($this->request, $this->response, $this->next); - $middlewareLower->__invoke( - $this->request, - $this->response, - $this->next - )->shouldHaveBeenCalled(); + $this->assertTrue($middlewareLower->called); } public function testDispatchesWildcardMiddlewareWithNonMatchingMethod() { $this->request = $this->request->withMethod("GET"); - $map = new MethodMap($this->dispatcher->reveal()); - $map->register("*", $this->middleware->reveal()); + $map = $this->getMethodMap(); + $map->register("*", $this->middleware); $map($this->request, $this->response, $this->next); - $this->middleware->__invoke( - $this->request, - $this->response, - $this->next - )->shouldHaveBeenCalled(); + $this->assertTrue($this->middleware->called); } public function testDispatchesGetMiddlewareForHeadByDefault() { $this->request = $this->request->withMethod("HEAD"); - $map = new MethodMap($this->dispatcher->reveal()); - $map->register("GET", $this->middleware->reveal()); + $map = $this->getMethodMap(); + $map->register("GET", $this->middleware); $map($this->request, $this->response, $this->next); - $this->middleware->__invoke( - $this->request, - $this->response, - $this->next - )->shouldHaveBeenCalled(); + $this->assertTrue($this->middleware->called); } public function testRegistersMiddlewareForMultipleMethods() { - $map = new MethodMap($this->dispatcher->reveal()); - $map->register("GET,POST", $this->middleware->reveal()); + $map = $this->getMethodMap(); + $map->register("GET,POST", $this->middleware); $this->request = $this->request->withMethod("GET"); $map($this->request, $this->response, $this->next); @@ -120,15 +96,15 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase $this->request = $this->request->withMethod("POST"); $map($this->request, $this->response, $this->next); - $this->middleware->__invoke(Argument::cetera())->shouldHaveBeenCalledTimes(2); + $this->assertEquals(2, $this->middleware->callCount); } public function testSettingNullUnregistersMiddleware() { $this->request = $this->request->withMethod("POST"); - $map = new MethodMap($this->dispatcher->reveal()); - $map->register("POST", $this->middleware->reveal()); + $map = $this->getMethodMap(); + $map->register("POST", $this->middleware); $map->register("POST", null); $response = $map($this->request, $this->response, $this->next); @@ -139,8 +115,8 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase { $this->request = $this->request->withMethod("OPTIONS"); - $map = new MethodMap($this->dispatcher->reveal()); - $map->register("GET", $this->middleware->reveal()); + $map = $this->getMethodMap(); + $map->register("GET", $this->middleware); $response = $map($this->request, $this->response, $this->next); $this->assertEquals(200, $response->getStatusCode()); @@ -150,8 +126,8 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase { $this->request = $this->request->withMethod("OPTIONS"); - $map = new MethodMap($this->dispatcher->reveal()); - $map->register("GET", $this->middleware->reveal()); + $map = $this->getMethodMap(); + $map->register("GET", $this->middleware); $map($this->request, $this->response, $this->next); $this->assertFalse($this->next->called); @@ -162,9 +138,9 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase { $this->request = $this->request->withMethod("OPTIONS"); - $map = new MethodMap($this->dispatcher->reveal()); + $map = $this->getMethodMap(); foreach ($methodsDeclared as $method) { - $map->register($method, $this->middleware->reveal()); + $map->register($method, $this->middleware); } $response = $map($this->request, $this->response, $this->next); @@ -176,8 +152,8 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase { $this->request = $this->request->withMethod("POST"); - $map = new MethodMap($this->dispatcher->reveal()); - $map->register("GET", $this->middleware->reveal()); + $map = $this->getMethodMap(); + $map->register("GET", $this->middleware); $response = $map($this->request, $this->response, $this->next); $this->assertEquals(405, $response->getStatusCode()); @@ -191,8 +167,8 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase { $this->request = $this->request->withMethod("POST"); - $map = new MethodMap($this->dispatcher->reveal()); - $map->register("GET", $this->middleware->reveal()); + $map = $this->getMethodMap(); + $map->register("GET", $this->middleware); $map($this->request, $this->response, $this->next); $this->assertFalse($this->next->called); } @@ -202,9 +178,9 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase { $this->request = $this->request->withMethod("BAD"); - $map = new MethodMap($this->dispatcher->reveal()); + $map = $this->getMethodMap(); foreach ($methodsDeclared as $method) { - $map->register($method, $this->middleware->reveal()); + $map->register($method, $this->middleware); } $response = $map($this->request, $this->response, $this->next);