diff --git a/src/Dispatching/DispatchException.php b/src/Dispatching/DispatchException.php new file mode 100644 index 0000000..9a7d9da --- /dev/null +++ b/src/Dispatching/DispatchException.php @@ -0,0 +1,7 @@ +dispatcher = $this->getDispatcher(); + $this->dispatcher = $dispatcher; $this->stack = []; } @@ -59,13 +62,6 @@ class DispatchStack implements DispatchStackInterface // ------------------------------------------------------------------------ - protected function getDispatcher() - { - return new Dispatcher(); - } - - // ------------------------------------------------------------------------ - private function getCallableChain() { $dispatcher = $this->dispatcher; diff --git a/src/Routing/DispatchStackInterface.php b/src/Dispatching/DispatchStackInterface.php similarity index 94% rename from src/Routing/DispatchStackInterface.php rename to src/Dispatching/DispatchStackInterface.php index f516f16..936972a 100644 --- a/src/Routing/DispatchStackInterface.php +++ b/src/Dispatching/DispatchStackInterface.php @@ -1,9 +1,10 @@ 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); + }); } /** * @covers ::__construct - * @covers ::getDispatcher */ public function testCreatesInstance() { - $stack = new DispatchStack(); + $stack = new DispatchStack($this->dispatcher->reveal()); $this->assertNotNull($stack); } @@ -41,19 +46,20 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase */ public function testAddIsFluid() { - $stack = new DispatchStack(); + $stack = new DispatchStack($this->dispatcher->reveal()); $this->assertSame($stack, $stack->add("middleware1")); } /** * @covers ::dispatch + * @covers ::getCallableChain */ public function testDispachesMiddlewareInOrderAdded() { // Each middelware will add its "name" to this array. $callOrder = []; - $stack = new DispatchStack(); + $stack = new DispatchStack($this->dispatcher->reveal()); $stack->add(function ($request, $response, $next) use (&$callOrder) { $callOrder[] = "first"; return $next($request, $response); @@ -70,6 +76,9 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase $this->assertEquals(["first", "second", "third"], $callOrder); } + /** + * @covers ::dispatch + */ public function testCallsNextAfterDispatchingStack() { $nextCalled = false; @@ -82,7 +91,7 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase return $next($request, $response); }; - $stack = new DispatchStack(); + $stack = new DispatchStack($this->dispatcher->reveal()); $stack->add($middleware); $stack->add($middleware); $stack->add($middleware); @@ -102,7 +111,7 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase return $response; }; - $stack = new DispatchStack(); + $stack = new DispatchStack($this->dispatcher->reveal()); $stack->dispatch($this->request->reveal(), $this->response->reveal(), $next); $this->assertTrue($nextCalled); } diff --git a/test/tests/unit/Routing/DispatcherTest.php b/test/tests/unit/Routing/Dispatching/DispatcherTest.php similarity index 93% rename from test/tests/unit/Routing/DispatcherTest.php rename to test/tests/unit/Routing/Dispatching/DispatcherTest.php index db32f90..501de7d 100644 --- a/test/tests/unit/Routing/DispatcherTest.php +++ b/test/tests/unit/Routing/Dispatching/DispatcherTest.php @@ -1,15 +1,16 @@