From d3e924485cc8303fa425d14198c0f7fa1bec71eb Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Fri, 20 May 2016 19:17:20 -0400 Subject: [PATCH] Refactor dispatch tests --- test/bootstrap.php | 3 +- test/src/NextSpy.php | 23 +++++ .../unit/Dispatching/DispatchStackTest.php | 97 ++++--------------- .../tests/unit/Dispatching/DispatcherTest.php | 29 +++--- 4 files changed, 58 insertions(+), 94 deletions(-) create mode 100644 test/src/NextSpy.php diff --git a/test/bootstrap.php b/test/bootstrap.php index 1a9b7ca..d4132bc 100644 --- a/test/bootstrap.php +++ b/test/bootstrap.php @@ -2,4 +2,5 @@ error_reporting(E_ALL); -require_once __DIR__ . "/../vendor/autoload.php"; +$loader = require __DIR__ . '/../vendor/autoload.php'; +$loader->addPsr4('WellRESTed\\Test\\', __DIR__ . '/src'); diff --git a/test/src/NextSpy.php b/test/src/NextSpy.php new file mode 100644 index 0000000..bc05f16 --- /dev/null +++ b/test/src/NextSpy.php @@ -0,0 +1,23 @@ +called = true; + $this->request = $request; + $this->response = $respone; + return $respone; + } +} diff --git a/test/tests/unit/Dispatching/DispatchStackTest.php b/test/tests/unit/Dispatching/DispatchStackTest.php index d732f4d..50c47d9 100644 --- a/test/tests/unit/Dispatching/DispatchStackTest.php +++ b/test/tests/unit/Dispatching/DispatchStackTest.php @@ -2,12 +2,14 @@ namespace WellRESTed\Test\Unit\Dispatching; -use Prophecy\Argument; +use WellRESTed\Dispatching\Dispatcher; use WellRESTed\Dispatching\DispatchStack; +use WellRESTed\Message\Response; +use WellRESTed\Message\ServerRequest; +use WellRESTed\Test\NextSpy; /** - * @coversDefaultClass WellRESTed\Dispatching\DispatchStack - * @uses WellRESTed\Dispatching\DispatchStack + * @covers WellRESTed\Dispatching\DispatchStack * @group dispatching */ class DispatchStackTest extends \PHPUnit_Framework_TestCase @@ -15,50 +17,20 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase private $request; private $response; private $next; - private $dispatcher; public function setUp() { parent::setUp(); - $this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface'); - $this->response = $this->prophesize('Psr\Http\Message\ResponseInterface'); - $this->next = function ($request, $response) { - return $response; - }; - $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->request = new ServerRequest(); + $this->response = new Response(); + $this->next = new NextSpy(); } - /** - * @covers ::__construct - */ - public function testCreatesInstance() + public function testDispatchesMiddlewareInOrderAdded() { - $stack = new DispatchStack($this->dispatcher->reveal()); - $this->assertNotNull($stack); - } - - /** - * @covers ::add - */ - public function testAddIsFluid() - { - $stack = new DispatchStack($this->dispatcher->reveal()); - $this->assertSame($stack, $stack->add("middleware1")); - } - - /** - * @covers ::__invoke - */ - public function testDispachesMiddlewareInOrderAdded() - { - // Each middelware will add its "name" to this array. + // Each middleware will add its "name" to this array. $callOrder = []; - - $stack = new DispatchStack($this->dispatcher->reveal()); + $stack = new DispatchStack(new Dispatcher()); $stack->add(function ($request, $response, $next) use (&$callOrder) { $callOrder[] = "first"; return $next($request, $response); @@ -71,61 +43,34 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase $callOrder[] = "third"; return $next($request, $response); }); - $stack($this->request->reveal(), $this->response->reveal(), $this->next); + $stack($this->request, $this->response, $this->next); $this->assertEquals(["first", "second", "third"], $callOrder); } - /** - * @covers ::__invoke - */ public function testCallsNextAfterDispatchingEmptyStack() { - $nextCalled = false; - $next = function ($request, $response) use (&$nextCalled) { - $nextCalled = true; - return $response; - }; - - $stack = new DispatchStack($this->dispatcher->reveal()); - $stack($this->request->reveal(), $this->response->reveal(), $next); - $this->assertTrue($nextCalled); + $stack = new DispatchStack(new Dispatcher()); + $stack($this->request, $this->response, $this->next); + $this->assertTrue($this->next->called); } - /** - * @covers ::__invoke - */ public function testCallsNextAfterDispatchingStack() { - $nextCalled = false; - $next = function ($request, $response) use (&$nextCalled) { - $nextCalled = true; - return $response; - }; - $middleware = function ($request, $response, $next) use (&$callOrder) { return $next($request, $response); }; - $stack = new DispatchStack($this->dispatcher->reveal()); + $stack = new DispatchStack(new Dispatcher()); $stack->add($middleware); $stack->add($middleware); $stack->add($middleware); - $stack($this->request->reveal(), $this->response->reveal(), $next); - $this->assertTrue($nextCalled); + $stack($this->request, $this->response, $this->next); + $this->assertTrue($this->next->called); } - /** - * @covers ::__invoke - */ public function testDoesNotCallNextWhenStackStopsEarly() { - $nextCalled = false; - $next = function ($request, $response) use (&$nextCalled) { - $nextCalled = true; - return $response; - }; - $middlewareGo = function ($request, $response, $next) use (&$callOrder) { return $next($request, $response); }; @@ -133,12 +78,12 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase return $response; }; - $stack = new DispatchStack($this->dispatcher->reveal()); + $stack = new DispatchStack(new Dispatcher()); $stack->add($middlewareGo); $stack->add($middlewareStop); $stack->add($middlewareStop); - $stack($this->request->reveal(), $this->response->reveal(), $next); - $this->assertFalse($nextCalled); + $stack($this->request, $this->response, $this->next); + $this->assertFalse($this->next->called); } } diff --git a/test/tests/unit/Dispatching/DispatcherTest.php b/test/tests/unit/Dispatching/DispatcherTest.php index 1a21b7f..9cc3921 100644 --- a/test/tests/unit/Dispatching/DispatcherTest.php +++ b/test/tests/unit/Dispatching/DispatcherTest.php @@ -6,7 +6,10 @@ use Prophecy\Argument; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use WellRESTed\Dispatching\Dispatcher; +use WellRESTed\Message\Response; +use WellRESTed\Message\ServerRequest; use WellRESTed\MiddlewareInterface; +use WellRESTed\Test\NextSpy; /** * @covers WellRESTed\Dispatching\Dispatcher @@ -20,17 +23,9 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase public function setUp() { - $this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface'); - $this->response = $this->prophesize('Psr\Http\Message\ResponseInterface'); - $this->response->withStatus(Argument::any())->will( - function ($args) { - $this->getStatusCode()->willReturn($args[0]); - return $this; - } - ); - $this->next = function ($request, $response) { - return $response; - }; + $this->request = new ServerRequest(); + $this->response = new Response(); + $this->next = new NextSpy(); } public function testDispatchesCallableThatReturnsResponse() @@ -40,7 +35,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase }; $dispatcher = new Dispatcher(); - $response = $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next); + $response = $dispatcher->dispatch($middleware, $this->request, $this->response, $this->next); $this->assertEquals(200, $response->getStatusCode()); } @@ -51,7 +46,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase }; $dispatcher = new Dispatcher(); - $response = $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next); + $response = $dispatcher->dispatch($middleware, $this->request, $this->response, $this->next); $this->assertEquals(200, $response->getStatusCode()); } @@ -60,7 +55,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase $middleware = __NAMESPACE__ . '\DispatcherTest_Middleware'; $dispatcher = new Dispatcher(); - $response = $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next); + $response = $dispatcher->dispatch($middleware, $this->request, $this->response, $this->next); $this->assertEquals(200, $response->getStatusCode()); } @@ -69,7 +64,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase $middleware = new DispatcherTest_Middleware(); $dispatcher = new Dispatcher(); - $response = $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next); + $response = $dispatcher->dispatch($middleware, $this->request, $this->response, $this->next); $this->assertEquals(200, $response->getStatusCode()); } @@ -81,7 +76,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase $middleware = new DispatcherTest_Middleware(); $dispatcher = new Dispatcher(); - $response = $dispatcher->dispatch([$middleware], $this->request->reveal(), $this->response->reveal(), $this->next); + $response = $dispatcher->dispatch([$middleware], $this->request, $this->response, $this->next); $this->assertEquals(200, $response->getStatusCode()); } @@ -93,7 +88,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase $middleware = null; $dispatcher = new Dispatcher(); - $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next); + $dispatcher->dispatch($middleware, $this->request, $this->response, $this->next); } }