Add Dispatching namesapce

This commit is contained in:
PJ Dietz 2015-05-10 11:02:59 -04:00
parent 560b1e8ff0
commit bbb138996a
10 changed files with 88 additions and 53 deletions

View File

@ -0,0 +1,7 @@
<?php
namespace WellRESTed\Dispatching;
class DispatchException extends \InvalidArgumentException
{
}

View File

@ -1,6 +1,6 @@
<?php
namespace WellRESTed\Routing;
namespace WellRESTed\Dispatching;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
@ -10,9 +10,12 @@ class DispatchStack implements DispatchStackInterface
private $stack;
private $dispatcher;
public function __construct()
/**
* @param DispatcherInterface $dispatcher
*/
public function __construct(DispatcherInterface $dispatcher)
{
$this->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;

View File

@ -1,9 +1,10 @@
<?php
namespace WellRESTed\Routing;
namespace WellRESTed\Dispatching;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use WellRESTed\Routing\MiddlewareInterface;
interface DispatchStackInterface extends MiddlewareInterface
{

View File

@ -1,18 +1,20 @@
<?php
namespace WellRESTed\Routing;
namespace WellRESTed\Dispatching;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use WellRESTed\Routing\MiddlewareInterface;
class Dispatcher implements DispatcherInterface
{
/**
* @param $middleware
* @param mixed $middleware
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
* @return ResponseInterface
* @throws \InvalidArgumentException $middleware is not a valid type.
* @throws DispatchException Unable to dispatch $middleware
*/
public function dispatch($middleware, ServerRequestInterface $request, ResponseInterface $response, $next)
{
@ -26,7 +28,7 @@ class Dispatcher implements DispatcherInterface
} elseif ($middleware instanceof ResponseInterface) {
return $middleware;
} else {
throw new \InvalidArgumentException("Unable to dispatch middleware.");
throw new DispatchException("Unable to dispatch middleware.");
}
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace WellRESTed\Dispatching;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
interface DispatcherInterface
{
/**
* Dispatch middleware and return the response.
*
* This method MUST pass $request, $response, and $next to the middleware
* to be dispatched.
*
* $middleware comes in a number of varieties (e.g., instance, string,
* callable). DispatcherInterface interface exist to unpack the middleware
* and dispatch it.
*
* Implementations MUST be able to dispatch the following:
* - An instance implementing MiddlewareInterface
* - A string containing the fully qualified class name of a class
* implementing MiddlewareInterface
* - A callable that returns an instance implementing MiddlewareInterface
* - A callable with a signature matching MiddlewareInterface::dispatch
*
* Implementation MAY dispatch other types of middleware.
*
* When an implementation recieves a $middware that is not of a type it can
* dispatch, it MUST throw a DispatchException.
*
* @param mixed $middleware
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
* @return ResponseInterface
* @throws DispatchException Unable to dispatch $middleware
*/
public function dispatch($middleware, ServerRequestInterface $request, ResponseInterface $response, $next);
}

View File

@ -1,23 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: pjdietz
* Date: 4/6/15
* Time: 8:29 PM
*/
namespace WellRESTed\Routing;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
interface DispatcherInterface
{
/**
* @param $middleware
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
* @return ResponseInterface
*/
public function dispatch($middleware, ServerRequestInterface $request, ResponseInterface $response, $next);
}

View File

@ -4,6 +4,8 @@ namespace WellRESTed\Routing;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use WellRESTed\Dispatching\Dispatcher;
use WellRESTed\Dispatching\DispatcherInterface;
class MethodMap implements MiddlewareInterface, MethodMapInterface
{

View File

@ -3,18 +3,19 @@
namespace WellRESTed\Test\Unit\Routing;
use Prophecy\Argument;
use WellRESTed\Routing\DispatchStack;
use WellRESTed\Dispatching\DispatchStack;
/**
* @coversDefaultClass WellRESTed\Routing\DispatchStack
* @uses WellRESTed\Routing\DispatchStack
* @uses WellRESTed\Routing\Dispatcher
* @coversDefaultClass WellRESTed\Dispatching\DispatchStack
* @uses WellRESTed\Dispatching\DispatchStack
* @group dispatching
*/
class DispatchStackTest extends \PHPUnit_Framework_TestCase
{
private $request;
private $response;
private $next;
private $dispatcher;
public function setUp()
{
@ -24,15 +25,19 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase
$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);
});
}
/**
* @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);
}

View File

@ -1,15 +1,16 @@
<?php
namespace WellRESTed\Test\Unit\Routing;
namespace WellRESTed\Test\Unit\Dispatching;
use Prophecy\Argument;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use WellRESTed\Routing\Dispatcher;
use WellRESTed\Dispatching\Dispatcher;
use WellRESTed\Routing\MiddlewareInterface;
/**
* @covers WellRESTed\Routing\Dispatcher
* @covers WellRESTed\Dispatching\Dispatcher
* @group dispatching
*/
class DispatcherTest extends \PHPUnit_Framework_TestCase
{
@ -75,7 +76,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase
/**
* @expectedException \InvalidArgumentException
*/
public function testThrowExceptionWhenUnableToDispatch()
public function testThrowsExceptionWhenUnableToDispatch()
{
$middleware = null;

View File

@ -8,7 +8,7 @@ use WellRESTed\Routing\MethodMap;
/**
* @coversDefaultClass WellRESTed\Routing\MethodMap
* @uses WellRESTed\Routing\MethodMap
* @uses WellRESTed\Routing\Dispatcher
* @uses WellRESTed\Dispatching\Dispatcher
*/
class MethodMapTest extends \PHPUnit_Framework_TestCase
{