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

Commit cebe041

Browse files
committed
Added support for HttpInterop style middlewares
1 parent 7e89d6a commit cebe041

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

src/MiddlewareListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Zend\Mvc;
1111

1212
use Interop\Container\ContainerInterface;
13+
use Interop\Http\ServerMiddleware\MiddlewareInterface;
1314
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
1415
use Psr\Http\Message\ResponseInterface;
1516
use Psr\Http\Message\ServerRequestInterface as PsrServerRequestInterface;
@@ -143,7 +144,7 @@ private function createPipeFromSpec(
143144
if (is_string($middlewareToBePiped) && $serviceLocator->has($middlewareToBePiped)) {
144145
$middlewareToBePiped = $serviceLocator->get($middlewareToBePiped);
145146
}
146-
if (! is_callable($middlewareToBePiped)) {
147+
if (! $middlewareToBePiped instanceof MiddlewareInterface && ! is_callable($middlewareToBePiped)) {
147148
throw MiddlewareNotCallableException::fromMiddlewareName($middlewareName);
148149
}
149150

test/MiddlewareListenerTest.php

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
namespace ZendTest\Mvc;
1111

1212
use Interop\Container\ContainerInterface;
13+
use Interop\Http\ServerMiddleware\DelegateInterface;
14+
use Interop\Http\ServerMiddleware\MiddlewareInterface;
1315
use PHPUnit\Framework\TestCase;
1416
use Psr\Http\Message\ResponseInterface;
1517
use Psr\Http\Message\ServerRequestInterface;
18+
use Zend\Diactoros\Response\HtmlResponse;
1619
use Zend\EventManager\EventManager;
1720
use Zend\Http\Request;
1821
use Zend\Http\Response;
@@ -89,6 +92,38 @@ public function testSuccessfullyDispatchesMiddleware()
8992
$this->assertEquals('Test!', $return->getBody());
9093
}
9194

95+
public function testSuccessfullyDispatchesHttpInteropMiddleware()
96+
{
97+
$expectedOutput = uniqid('expectedOutput', true);
98+
99+
$event = $this->createMvcEvent('path', new class($expectedOutput) implements MiddlewareInterface {
100+
private $expectedOutput;
101+
102+
public function __construct($expectedOutput)
103+
{
104+
$this->expectedOutput = $expectedOutput;
105+
}
106+
107+
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
108+
{
109+
return new HtmlResponse($this->expectedOutput);
110+
}
111+
});
112+
$application = $event->getApplication();
113+
114+
$application->getEventManager()->attach(MvcEvent::EVENT_DISPATCH_ERROR, function ($e) {
115+
$this->fail(sprintf('dispatch.error triggered when it should not be: %s', var_export($e->getError(), 1)));
116+
});
117+
118+
$listener = new MiddlewareListener();
119+
$return = $listener->onDispatch($event);
120+
$this->assertInstanceOf(Response::class, $return);
121+
122+
$this->assertInstanceOf(Response::class, $return);
123+
$this->assertSame(200, $return->getStatusCode());
124+
$this->assertEquals($expectedOutput, $return->getBody());
125+
}
126+
92127
public function testMatchedRouteParamsAreInjectedToRequestAsAttributes()
93128
{
94129
$matchedRouteParam = uniqid('matched param', true);
@@ -114,7 +149,7 @@ function (ServerRequestInterface $request, ResponseInterface $response) use (&$r
114149
$this->assertSame($this->routeMatch->reveal(), $routeAttribute);
115150
}
116151

117-
public function testSuccessfullyDispatchesPipeOfMiddleware()
152+
public function testSuccessfullyDispatchesPipeOfCallableAndHttpInteropStyleMiddlewares()
118153
{
119154
$response = new Response();
120155
$routeMatch = $this->prophesize(RouteMatch::class);
@@ -135,11 +170,11 @@ public function testSuccessfullyDispatchesPipeOfMiddleware()
135170
return $next($request->withAttribute('firstMiddlewareAttribute', 'firstMiddlewareValue'), $response);
136171
});
137172
$serviceManager->has('secondMiddleware')->willReturn(true);
138-
$serviceManager->get('secondMiddleware')->willReturn(function ($request, $response) {
139-
$this->assertInstanceOf(ServerRequestInterface::class, $request);
140-
$this->assertInstanceOf(ResponseInterface::class, $response);
141-
$response->getBody()->write($request->getAttribute('firstMiddlewareAttribute'));
142-
return $response;
173+
$serviceManager->get('secondMiddleware')->willReturn(new class implements MiddlewareInterface {
174+
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
175+
{
176+
return new HtmlResponse($request->getAttribute('firstMiddlewareAttribute'));
177+
}
143178
});
144179

145180
$application = $this->prophesize(Application::class);

0 commit comments

Comments
 (0)