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