Skip to content

Commit

Permalink
dispatchers
Browse files Browse the repository at this point in the history
  • Loading branch information
dracony committed Jun 18, 2015
1 parent bf8f2a7 commit 672ee8c
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/PHPixie/Processors.php
Expand Up @@ -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
);
}
}
9 changes: 9 additions & 0 deletions src/PHPixie/Processors/Dispatcher.php
@@ -0,0 +1,9 @@
<?php

namespace PHPixie\Processors;

interface Dispatcher
{
public function hasProcessorFor($value);
public function getProcessorFor($value);
}
28 changes: 28 additions & 0 deletions src/PHPixie/Processors/Processor/CheckIsDispatchable.php
@@ -0,0 +1,28 @@
<?php

namespace PHPixie\Processors\Processor;

class CheckIsDispatchable
{
protected $dispatcher;
protected $foundProcessor;
protected $notFoundProcessor;

public function __construct($dispatcher, $foundProcessor, $notFoundProcessor)
{
$this->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);
}
}
19 changes: 19 additions & 0 deletions src/PHPixie/Processors/Processor/Dispatch.php
@@ -0,0 +1,19 @@
<?php

namespace PHPixie\Processors\Processor;

class Dispatch
{
protected $dispatcher;

public function __construct($dispatcher)
{
$this->dispatcher = $dispatcher;
}

public function process($request)
{
$processor = $this->dispatcher->getProcessorFor($request);
return $processor->process($request);
}
}
@@ -0,0 +1,58 @@
<?php

namespace PHPixie\Tests\Processors\Processor;

/**
* @coversDefaultClass \PHPixie\Processors\Processor\CheckIsDispatchable
*/
class CheckIsDispatchableTest extends \PHPixie\Test\Testcase
{
protected $dispatcher;
protected $foundProcessor;
protected $notFoundProcessor;

protected $checkRoute;

public function setUp()
{
$this->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));
}
}
45 changes: 45 additions & 0 deletions tests/PHPixie/Tests/Processors/Processor/DispatchTest.php
@@ -0,0 +1,45 @@
<?php

namespace PHPixie\Tests\Processors\Processor;

/**
* @coversDefaultClass \PHPixie\Processors\Processor\Dispatch
*/
class DispatchTest extends \PHPixie\Test\Testcase
{
protected $dispatcher;

protected $dispatch;

public function setUp()
{
$this->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));
}
}
36 changes: 36 additions & 0 deletions tests/PHPixie/Tests/ProcessorsTest.php
Expand Up @@ -28,4 +28,40 @@ public function testChain()
'processors' => $processors
));
}

/**
* @covers ::dispatch
* @covers ::<protected>
*/
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 ::<protected>
*/
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,
));
}
}

0 comments on commit 672ee8c

Please sign in to comment.