From 288705b77adfa59cd7b7ea2b8dc2fb419dd08cd6 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Fri, 14 Aug 2020 06:31:09 -0400 Subject: [PATCH] Edit comments for Dispatch namespace and clean up tests --- src/Dispatching/DispatchStack.php | 5 +--- src/Dispatching/Dispatcher.php | 6 ++-- src/Dispatching/DispatcherInterface.php | 4 +-- .../unit/Dispatching/DispatchStackTest.php | 12 ++++---- .../tests/unit/Dispatching/DispatcherTest.php | 28 +++++++++---------- 5 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/Dispatching/DispatchStack.php b/src/Dispatching/DispatchStack.php index e7d9b56..67b652f 100644 --- a/src/Dispatching/DispatchStack.php +++ b/src/Dispatching/DispatchStack.php @@ -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 diff --git a/src/Dispatching/Dispatcher.php b/src/Dispatching/Dispatcher.php index fe0bcd1..c806589 100644 --- a/src/Dispatching/Dispatcher.php +++ b/src/Dispatching/Dispatcher.php @@ -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.'); } } diff --git a/src/Dispatching/DispatcherInterface.php b/src/Dispatching/DispatcherInterface.php index 7be31af..501da5e 100644 --- a/src/Dispatching/DispatcherInterface.php +++ b/src/Dispatching/DispatcherInterface.php @@ -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 diff --git a/test/tests/unit/Dispatching/DispatchStackTest.php b/test/tests/unit/Dispatching/DispatchStackTest.php index c473ae8..9264f2b 100644 --- a/test/tests/unit/Dispatching/DispatchStackTest.php +++ b/test/tests/unit/Dispatching/DispatchStackTest.php @@ -1,9 +1,7 @@ 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); diff --git a/test/tests/unit/Dispatching/DispatcherTest.php b/test/tests/unit/Dispatching/DispatcherTest.php index 8fa2824..159bc51 100644 --- a/test/tests/unit/Dispatching/DispatcherTest.php +++ b/test/tests/unit/Dispatching/DispatcherTest.php @@ -1,12 +1,10 @@ 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);