Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 6d09019

Browse files
committed
Rename MiddlewareNotCallableException to InvalidMiddlewareException
Also, adds a `fromNull()` method, allowing it to be used in cases where a `null` value is provided for middleware. This means that `null` middleware is handled exactly the same way as any other invalid middleware type, which required changes to several test expectations.
1 parent bf741df commit 6d09019

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

src/Exception/MiddlewareNotCallableException.php renamed to src/Exception/InvalidMiddlewareException.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace Zend\Mvc\Exception;
1111

12-
final class MiddlewareNotCallableException extends RuntimeException
12+
final class InvalidMiddlewareException extends RuntimeException
1313
{
1414
/**
1515
* @var string
@@ -28,6 +28,12 @@ public static function fromMiddlewareName($middlewareName)
2828
return $instance;
2929
}
3030

31+
public static function fromNull()
32+
{
33+
$instance = new self('Middleware name cannot be null');
34+
return $instance;
35+
}
36+
3137
/**
3238
* @return string
3339
*/

src/MiddlewareListener.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Psr\Http\Message\ServerRequestInterface as PsrServerRequestInterface;
1717
use Zend\EventManager\AbstractListenerAggregate;
1818
use Zend\EventManager\EventManagerInterface;
19-
use Zend\Mvc\Exception\MiddlewareNotCallableException;
19+
use Zend\Mvc\Exception\InvalidMiddlewareException;
2020
use Zend\Mvc\Exception\ReachedFinalHandlerException;
2121
use Zend\Psr7Bridge\Psr7ServerRequest as Psr7Request;
2222
use Zend\Psr7Bridge\Psr7Response;
@@ -63,12 +63,13 @@ public function onDispatch(MvcEvent $event)
6363
$psr7ResponsePrototype,
6464
is_array($middleware) ? $middleware : [$middleware]
6565
);
66-
} catch (MiddlewareNotCallableException $middlewareNotCallableException) {
67-
$return = $this->marshalMiddlewareNotCallable(
66+
} catch (InvalidMiddlewareException $invalidMiddlewareException) {
67+
$return = $this->marshalInvalidMiddleware(
6868
$application::ERROR_MIDDLEWARE_CANNOT_DISPATCH,
69-
$middlewareNotCallableException->toMiddlewareName(),
69+
$invalidMiddlewareException->toMiddlewareName(),
7070
$event,
71-
$application
71+
$application,
72+
$invalidMiddlewareException
7273
);
7374
$event->setResult($return);
7475
return $return;
@@ -122,8 +123,7 @@ function (PsrServerRequestInterface $request, PsrResponseInterface $response) {
122123
* @param ResponseInterface $responsePrototype
123124
* @param array $middlewaresToBePiped
124125
* @return MiddlewarePipe
125-
* @throws \InvalidArgumentException
126-
* @throws \Zend\Mvc\Exception\MiddlewareNotCallableException
126+
* @throws InvalidMiddlewareException
127127
*/
128128
private function createPipeFromSpec(
129129
ContainerInterface $serviceLocator,
@@ -134,7 +134,7 @@ private function createPipeFromSpec(
134134
$pipe->setResponsePrototype($responsePrototype);
135135
foreach ($middlewaresToBePiped as $middlewareToBePiped) {
136136
if (null === $middlewareToBePiped) {
137-
throw new \InvalidArgumentException('Middleware name cannot be null');
137+
throw InvalidMiddlewareException::fromNull();
138138
}
139139

140140
$middlewareName = is_string($middlewareToBePiped) ? $middlewareToBePiped : get_class($middlewareToBePiped);
@@ -143,7 +143,7 @@ private function createPipeFromSpec(
143143
$middlewareToBePiped = $serviceLocator->get($middlewareToBePiped);
144144
}
145145
if (! $middlewareToBePiped instanceof MiddlewareInterface && ! is_callable($middlewareToBePiped)) {
146-
throw MiddlewareNotCallableException::fromMiddlewareName($middlewareName);
146+
throw InvalidMiddlewareException::fromMiddlewareName($middlewareName);
147147
}
148148

149149
$pipe->pipe($middlewareToBePiped);
@@ -161,7 +161,7 @@ private function createPipeFromSpec(
161161
* @param \Exception $exception
162162
* @return mixed
163163
*/
164-
protected function marshalMiddlewareNotCallable(
164+
protected function marshalInvalidMiddleware(
165165
$type,
166166
$middlewareName,
167167
MvcEvent $event,

test/Exception/MiddlewareNotCallableExceptionTest.php renamed to test/Exception/InvalidMiddlewareExceptionTest.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,32 @@
1010
namespace ZendTest\Mvc;
1111

1212
use PHPUnit\Framework\TestCase;
13-
use Zend\Mvc\Exception\MiddlewareNotCallableException;
13+
use Zend\Mvc\Exception\InvalidMiddlewareException;
1414

15-
final class MiddlewareNotCallableExceptionTest extends TestCase
15+
final class InvalidMiddlewareExceptionTest extends TestCase
1616
{
1717
public function testFromMiddlewareName()
1818
{
1919
$middlewareName = uniqid('middlewareName', true);
20-
$exception = MiddlewareNotCallableException::fromMiddlewareName($middlewareName);
20+
$exception = InvalidMiddlewareException::fromMiddlewareName($middlewareName);
2121

22-
$this->assertInstanceOf(MiddlewareNotCallableException::class, $exception);
22+
$this->assertInstanceOf(InvalidMiddlewareException::class, $exception);
2323
$this->assertSame('Cannot dispatch middleware ' . $middlewareName, $exception->getMessage());
2424
$this->assertSame($middlewareName, $exception->toMiddlewareName());
2525
}
2626

2727
public function testToMiddlewareNameWhenNotSet()
2828
{
29-
$exception = new MiddlewareNotCallableException();
29+
$exception = new InvalidMiddlewareException();
30+
$this->assertSame('', $exception->toMiddlewareName());
31+
}
32+
33+
public function testFromNull()
34+
{
35+
$exception = InvalidMiddlewareException::fromNull();
36+
37+
$this->assertInstanceOf(InvalidMiddlewareException::class, $exception);
38+
$this->assertSame('Middleware name cannot be null', $exception->getMessage());
3039
$this->assertSame('', $exception->toMiddlewareName());
3140
}
3241
}

test/MiddlewareListenerTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Zend\Http\Request;
2121
use Zend\Http\Response;
2222
use Zend\Mvc\Application;
23+
use Zend\Mvc\Exception\InvalidMiddlewareException;
2324
use Zend\Mvc\Exception\ReachedFinalHandlerException;
2425
use Zend\Mvc\MiddlewareListener;
2526
use Zend\Mvc\MvcEvent;
@@ -305,7 +306,7 @@ public function testMiddlewareWithNothingPipedReachesFinalHandlerException()
305306
$this->assertEquals('FAILED', $return);
306307
}
307308

308-
public function testNullMiddlewareThrowsInvalidArgument()
309+
public function testNullMiddlewareThrowsInvalidMiddlewareException()
309310
{
310311
$response = new Response();
311312
$routeMatch = $this->prophesize(RouteMatch::class);
@@ -329,14 +330,14 @@ public function testNullMiddlewareThrowsInvalidArgument()
329330
$event->setRouteMatch($routeMatch->reveal());
330331

331332
$event->getApplication()->getEventManager()->attach(MvcEvent::EVENT_DISPATCH_ERROR, function ($e) {
332-
$this->assertEquals(Application::ERROR_EXCEPTION, $e->getError());
333-
$this->assertInstanceOf(ReachedFinalHandlerException::class, $e->getParam('exception'));
333+
$this->assertEquals(Application::ERROR_MIDDLEWARE_CANNOT_DISPATCH, $e->getError());
334+
$this->assertInstanceOf(InvalidMiddlewareException::class, $e->getParam('exception'));
334335
return 'FAILED';
335336
});
336337

337338
$listener = new MiddlewareListener();
338339

339-
$this->expectException(\InvalidArgumentException::class);
340-
$listener->onDispatch($event);
340+
$return = $listener->onDispatch($event);
341+
$this->assertEquals('FAILED', $return);
341342
}
342343
}

0 commit comments

Comments
 (0)