diff --git a/src/PHPixie/Processors.php b/src/PHPixie/Processors.php index e367a3d..70a86ad 100755 --- a/src/PHPixie/Processors.php +++ b/src/PHPixie/Processors.php @@ -8,4 +8,18 @@ public function chain($processors) { return new Processors\Processor\Chain($processors); } + + public function dispatch($dispatcher) + { + return new Processors\Processor\Dispatch($dispatcher); + } + + public function checkIsDispatchable($dispatcher, $foundProcessor, $notFoundProcessor) + { + return new Processors\Processor\CheckIsDispatchable( + $dispatcher, + $foundProcessor, + $notFoundProcessor + ); + } } \ No newline at end of file diff --git a/src/PHPixie/Processors/Dispatcher.php b/src/PHPixie/Processors/Dispatcher.php new file mode 100644 index 0000000..6e7efab --- /dev/null +++ b/src/PHPixie/Processors/Dispatcher.php @@ -0,0 +1,9 @@ +dispatcher = $dispatcher; + $this->foundProcessor = $foundProcessor; + $this->notFoundProcessor = $notFoundProcessor; + } + + public function process($request) + { + if(!$this->dispatcher->hasProcessorFor($request)) { + $processor = $this->notFoundProcessor; + }else{ + $processor = $this->foundProcessor; + } + + return $processor->process($request); + } +} \ No newline at end of file diff --git a/src/PHPixie/Processors/Processor/Dispatch.php b/src/PHPixie/Processors/Processor/Dispatch.php new file mode 100644 index 0000000..0f2012e --- /dev/null +++ b/src/PHPixie/Processors/Processor/Dispatch.php @@ -0,0 +1,19 @@ +dispatcher = $dispatcher; + } + + public function process($request) + { + $processor = $this->dispatcher->getProcessorFor($request); + return $processor->process($request); + } +} \ No newline at end of file diff --git a/tests/PHPixie/Tests/Processors/Processor/CheckIsDispatchableTest.php b/tests/PHPixie/Tests/Processors/Processor/CheckIsDispatchableTest.php new file mode 100644 index 0000000..974d55f --- /dev/null +++ b/tests/PHPixie/Tests/Processors/Processor/CheckIsDispatchableTest.php @@ -0,0 +1,58 @@ +dispatcher = $this->quickMock('\PHPixie\Processors\Dispatcher'); + $this->foundProcessor = $this->quickMock('\PHPixie\Processors\Processor'); + $this->notFoundProcessor = $this->quickMock('\PHPixie\Processors\Processor'); + + $this->checkRoute = new \PHPixie\Processors\Processor\CheckIsDispatchable( + $this->dispatcher, + $this->foundProcessor, + $this->notFoundProcessor + ); + } + + /** + * @covers ::__construct + */ + public function testConstruct() + { + + } + + /** + * @covers ::process + */ + public function testProcess() + { + $this->processTest(false); + $this->processTest(true); + } + + protected function processTest($found) + { + $request = $this->quickMock('\PHPixie\HTTP\Request'); + $this->method($this->dispatcher, 'hasProcessorFor', $found, array($request), 0); + + $processor = $found ? $this->foundProcessor : $this->notFoundProcessor; + $response = $this->quickMock('\PHPixie\HTTP\Response'); + + $this->method($processor, 'process', $response, array($request), 0); + + $this->assertSame($response, $this->checkRoute->process($request)); + } +} \ No newline at end of file diff --git a/tests/PHPixie/Tests/Processors/Processor/DispatchTest.php b/tests/PHPixie/Tests/Processors/Processor/DispatchTest.php new file mode 100644 index 0000000..46b9058 --- /dev/null +++ b/tests/PHPixie/Tests/Processors/Processor/DispatchTest.php @@ -0,0 +1,45 @@ +dispatcher = $this->quickMock('\PHPixie\Processors\Dispatcher'); + + $this->dispatch = new \PHPixie\Processors\Processor\Dispatch( + $this->dispatcher + ); + } + + /** + * @covers ::__construct + */ + public function testConstruct() + { + + } + + /** + * @covers ::process + */ + public function testProcess() + { + $input = new \stdClass(); + $output = new \stdClass(); + $processor = $this->quickMock('\PHPixie\Processors\Processor'); + + $this->method($this->dispatcher, 'getProcessorFor', $processor, array($input), 0); + $this->method($processor, 'process', $output, array($input), 0); + + $this->assertSame($output, $this->dispatch->process($input)); + } +} \ No newline at end of file diff --git a/tests/PHPixie/Tests/ProcessorsTest.php b/tests/PHPixie/Tests/ProcessorsTest.php index 0bb5ebf..ab077f3 100755 --- a/tests/PHPixie/Tests/ProcessorsTest.php +++ b/tests/PHPixie/Tests/ProcessorsTest.php @@ -28,4 +28,40 @@ public function testChain() 'processors' => $processors )); } + + /** + * @covers ::dispatch + * @covers :: + */ + public function testDispatch() + { + $dispatcher = $this->quickMock('\PHPixie\Processors\Dispatcher'); + $dispatch = $this->processors->dispatch($dispatcher); + $this->assertInstance($dispatch, '\PHPixie\Processors\Processor\Dispatch', array( + 'dispatcher' => $dispatcher + )); + } + + /** + * @covers ::checkIsDispatchable + * @covers :: + */ + public function testCheckIsDispatchable() + { + $dispatcher = $this->quickMock('\PHPixie\Processors\Dispatcher'); + $foundProcessor = $this->quickMock('\PHPixie\Processors\Processor'); + $notFoundProcessor = $this->quickMock('\PHPixie\Processors\Processor'); + + $processor = $this->processors->checkIsDispatchable( + $dispatcher, + $foundProcessor, + $notFoundProcessor + ); + + $this->assertInstance($processor, '\PHPixie\Processors\Processor\CheckIsDispatchable', array( + 'dispatcher' => $dispatcher, + 'foundProcessor' => $foundProcessor, + 'notFoundProcessor' => $notFoundProcessor, + )); + } } \ No newline at end of file