Edit comments for Dispatch namespace and clean up tests
This commit is contained in:
parent
f542aaf3a9
commit
288705b77a
|
|
@ -15,9 +15,6 @@ class DispatchStack implements DispatchStackInterface
|
|||
/** @var DispatcherInterface */
|
||||
private $dispatcher;
|
||||
|
||||
/**
|
||||
* @param DispatcherInterface $dispatcher
|
||||
*/
|
||||
public function __construct(DispatcherInterface $dispatcher)
|
||||
{
|
||||
$this->dispatcher = $dispatcher;
|
||||
|
|
@ -42,7 +39,7 @@ class DispatchStack implements DispatchStackInterface
|
|||
* The first middleware that was added is dispatched first.
|
||||
*
|
||||
* Each middleware, when dispatched, receives a $next callable that, when
|
||||
* called, will dispatch the next middleware in the sequence.
|
||||
* called, will dispatch the following middleware in the sequence.
|
||||
*
|
||||
* When the stack is dispatched empty, or when all middleware in the stack
|
||||
* call the $next argument they were passed, this method will call the
|
||||
|
|
|
|||
|
|
@ -8,12 +8,12 @@ use Psr\Http\Server\MiddlewareInterface;
|
|||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
/**
|
||||
* Dispatches handlers and middleware
|
||||
* Runs a handler or middleware with a request and return the response.
|
||||
*/
|
||||
class Dispatcher implements DispatcherInterface
|
||||
{
|
||||
/**
|
||||
* Dispatch a handler or middleware and return the response.
|
||||
* Run a handler or middleware with a request and return the response.
|
||||
*
|
||||
* Dispatcher can dispatch any of the following:
|
||||
* - An instance implementing one of these interfaces:
|
||||
|
|
@ -63,7 +63,7 @@ class Dispatcher implements DispatcherInterface
|
|||
} elseif ($dispatchable instanceof ResponseInterface) {
|
||||
return $dispatchable;
|
||||
} else {
|
||||
throw new DispatchException('Unable to dispatch middleware.');
|
||||
throw new DispatchException('Unable to dispatch handler.');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,12 +6,12 @@ use Psr\Http\Message\ResponseInterface;
|
|||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
/**
|
||||
* Dispatches handlers and middleware
|
||||
* Runs a handler or middleware with a request and return the response.
|
||||
*/
|
||||
interface DispatcherInterface
|
||||
{
|
||||
/**
|
||||
* Dispatch a handler or middleware and return the response.
|
||||
* Run a handler or middleware with a request and return the response.
|
||||
*
|
||||
* Dispatchables (middleware and handlers) comes in a number of varieties
|
||||
* (e.g., instance, string, callable). DispatcherInterface interface unpacks
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace WellRESTed\Test\Unit\Dispatching;
|
||||
namespace WellRESTed\Dispatching;
|
||||
|
||||
use WellRESTed\Dispatching\Dispatcher;
|
||||
use WellRESTed\Dispatching\DispatchStack;
|
||||
use WellRESTed\Message\Response;
|
||||
use WellRESTed\Message\ServerRequest;
|
||||
use WellRESTed\Test\Doubles\NextMock;
|
||||
|
|
@ -23,7 +21,7 @@ class DispatchStackTest extends TestCase
|
|||
$this->next = new NextMock();
|
||||
}
|
||||
|
||||
public function testDispatchesMiddlewareInOrderAdded()
|
||||
public function testDispatchesMiddlewareInOrderAdded(): void
|
||||
{
|
||||
// Each middleware will add its "name" to this array.
|
||||
$callOrder = [];
|
||||
|
|
@ -44,14 +42,14 @@ class DispatchStackTest extends TestCase
|
|||
$this->assertEquals(['first', 'second', 'third'], $callOrder);
|
||||
}
|
||||
|
||||
public function testCallsNextAfterDispatchingEmptyStack()
|
||||
public function testCallsNextAfterDispatchingEmptyStack(): void
|
||||
{
|
||||
$stack = new DispatchStack(new Dispatcher());
|
||||
$stack($this->request, $this->response, $this->next);
|
||||
$this->assertTrue($this->next->called);
|
||||
}
|
||||
|
||||
public function testCallsNextAfterDispatchingStack()
|
||||
public function testCallsNextAfterDispatchingStack(): void
|
||||
{
|
||||
$middleware = function ($request, $response, $next) use (&$callOrder) {
|
||||
return $next($request, $response);
|
||||
|
|
@ -66,7 +64,7 @@ class DispatchStackTest extends TestCase
|
|||
$this->assertTrue($this->next->called);
|
||||
}
|
||||
|
||||
public function testDoesNotCallNextWhenStackStopsEarly()
|
||||
public function testDoesNotCallNextWhenStackStopsEarly(): void
|
||||
{
|
||||
$middlewareGo = function ($request, $response, $next) use (&$callOrder) {
|
||||
return $next($request, $response);
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace WellRESTed\Test\Unit\Dispatching;
|
||||
namespace WellRESTed\Dispatching;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use WellRESTed\Dispatching\Dispatcher;
|
||||
use WellRESTed\Dispatching\DispatchException;
|
||||
use WellRESTed\Message\Response;
|
||||
use WellRESTed\Message\ServerRequest;
|
||||
use WellRESTed\MiddlewareInterface;
|
||||
|
|
@ -35,6 +33,8 @@ class DispatcherTest extends TestCase
|
|||
/**
|
||||
* Dispatch the provided dispatchable using the class under test and the
|
||||
* ivars $request, $response, and $next. Return the response.
|
||||
* @param $dispatchable
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
private function dispatch($dispatchable): ResponseInterface
|
||||
{
|
||||
|
|
@ -50,14 +50,14 @@ class DispatcherTest extends TestCase
|
|||
// -------------------------------------------------------------------------
|
||||
// PSR-15 Handler
|
||||
|
||||
public function testDispatchesPsr15Handler()
|
||||
public function testDispatchesPsr15Handler(): void
|
||||
{
|
||||
$handler = new HandlerDouble($this->stubResponse);
|
||||
$response = $this->dispatch($handler);
|
||||
$this->assertSame($this->stubResponse, $response);
|
||||
}
|
||||
|
||||
public function testDispatchesPsr15HandlerFromFactory()
|
||||
public function testDispatchesPsr15HandlerFromFactory(): void
|
||||
{
|
||||
$factory = function () {
|
||||
return new HandlerDouble($this->stubResponse);
|
||||
|
|
@ -70,7 +70,7 @@ class DispatcherTest extends TestCase
|
|||
// -------------------------------------------------------------------------
|
||||
// PSR-15 Middleware
|
||||
|
||||
public function testDispatchesPsr15MiddlewareWithDelegate()
|
||||
public function testDispatchesPsr15MiddlewareWithDelegate(): void
|
||||
{
|
||||
$this->next->upstreamResponse = $this->stubResponse;
|
||||
$middleware = new MiddlewareDouble();
|
||||
|
|
@ -79,7 +79,7 @@ class DispatcherTest extends TestCase
|
|||
$this->assertSame($this->stubResponse, $response);
|
||||
}
|
||||
|
||||
public function testDispatchesPsr15MiddlewareFromFactoryWithDelegate()
|
||||
public function testDispatchesPsr15MiddlewareFromFactoryWithDelegate(): void
|
||||
{
|
||||
$this->next->upstreamResponse = $this->stubResponse;
|
||||
$factory = function () {
|
||||
|
|
@ -93,7 +93,7 @@ class DispatcherTest extends TestCase
|
|||
// -------------------------------------------------------------------------
|
||||
// Double-Pass Middleware Callable
|
||||
|
||||
public function testDispatchesDoublePassMiddlewareCallable()
|
||||
public function testDispatchesDoublePassMiddlewareCallable(): void
|
||||
{
|
||||
$doublePass = function ($request, $response, $next) {
|
||||
return $next($request, $this->stubResponse);
|
||||
|
|
@ -103,7 +103,7 @@ class DispatcherTest extends TestCase
|
|||
$this->assertSame($this->stubResponse, $response);
|
||||
}
|
||||
|
||||
public function testDispatchesDoublePassMiddlewareCallableFromFactory()
|
||||
public function testDispatchesDoublePassMiddlewareCallableFromFactory(): void
|
||||
{
|
||||
$factory = function () {
|
||||
return function ($request, $response, $next) {
|
||||
|
|
@ -118,14 +118,14 @@ class DispatcherTest extends TestCase
|
|||
// -------------------------------------------------------------------------
|
||||
// Double-Pass Middleware Instance
|
||||
|
||||
public function testDispatchesDoublePassMiddlewareInstance()
|
||||
public function testDispatchesDoublePassMiddlewareInstance(): void
|
||||
{
|
||||
$doublePass = new DoublePassMiddlewareDouble();
|
||||
$response = $this->dispatch($doublePass);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
public function testDispatchesDoublePassMiddlewareInstanceFromFactory()
|
||||
public function testDispatchesDoublePassMiddlewareInstanceFromFactory(): void
|
||||
{
|
||||
$factory = function () {
|
||||
return new DoublePassMiddlewareDouble();
|
||||
|
|
@ -137,7 +137,7 @@ class DispatcherTest extends TestCase
|
|||
// -------------------------------------------------------------------------
|
||||
// String
|
||||
|
||||
public function testDispatchesInstanceFromStringName()
|
||||
public function testDispatchesInstanceFromStringName(): void
|
||||
{
|
||||
$response = $this->dispatch(DoublePassMiddlewareDouble::class);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
|
|
@ -146,14 +146,14 @@ class DispatcherTest extends TestCase
|
|||
// -------------------------------------------------------------------------
|
||||
// Arrays
|
||||
|
||||
public function testDispatchesArrayAsDispatchStack()
|
||||
public function testDispatchesArrayAsDispatchStack(): void
|
||||
{
|
||||
$doublePass = new DoublePassMiddlewareDouble();
|
||||
$response = $this->dispatch([$doublePass]);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
public function testThrowsExceptionWhenUnableToDispatch()
|
||||
public function testThrowsExceptionWhenUnableToDispatch(): void
|
||||
{
|
||||
$this->expectException(DispatchException::class);
|
||||
$this->dispatch(null);
|
||||
|
|
|
|||
Loading…
Reference in New Issue