From bbb138996ad3fa3b0d1d3f603a302869830b7ba7 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Sun, 10 May 2015 11:02:59 -0400 Subject: [PATCH] Add Dispatching namesapce --- src/Dispatching/DispatchException.php | 7 ++++ .../DispatchStack.php | 16 +++----- .../DispatchStackInterface.php | 3 +- src/{Routing => Dispatching}/Dispatcher.php | 10 +++-- src/Dispatching/DispatcherInterface.php | 40 +++++++++++++++++++ src/Routing/DispatcherInterface.php | 23 ----------- src/Routing/MethodMap.php | 2 + .../{ => Dispatching}/DispatchStackTest.php | 29 +++++++++----- .../{ => Dispatching}/DispatcherTest.php | 9 +++-- test/tests/unit/Routing/MethodMapTest.php | 2 +- 10 files changed, 88 insertions(+), 53 deletions(-) create mode 100644 src/Dispatching/DispatchException.php rename src/{Routing => Dispatching}/DispatchStack.php (89%) rename src/{Routing => Dispatching}/DispatchStackInterface.php (94%) rename src/{Routing => Dispatching}/Dispatcher.php (75%) create mode 100644 src/Dispatching/DispatcherInterface.php delete mode 100644 src/Routing/DispatcherInterface.php rename test/tests/unit/Routing/{ => Dispatching}/DispatchStackTest.php (75%) rename test/tests/unit/Routing/{ => Dispatching}/DispatcherTest.php (93%) 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 @@