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

Fixed tests for latest dependencies #298

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions test/MiddlewareListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Zend\Router\RouteMatch;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\DispatchableInterface;
use Zend\Stratigility\Middleware\CallableMiddlewareWrapper;
use Zend\View\Model\ModelInterface;

class MiddlewareListenerTest extends TestCase
Expand Down Expand Up @@ -77,12 +78,12 @@ public function createMvcEvent($middlewareMatched, $middleware = null)

public function testSuccessfullyDispatchesMiddleware()
{
$event = $this->createMvcEvent('path', function ($request, $response) {
$event = $this->createMvcEvent('path', new CallableMiddlewareWrapper(function ($request, $response) {
$this->assertInstanceOf(ServerRequestInterface::class, $request);
$this->assertInstanceOf(ResponseInterface::class, $response);
$response->getBody()->write('Test!');
return $response;
});
}, new DiactorosResponse()));
$application = $event->getApplication();

$application->getEventManager()->attach(MvcEvent::EVENT_DISPATCH_ERROR, function ($e) {
Expand Down Expand Up @@ -128,11 +129,11 @@ public function testMatchedRouteParamsAreInjectedToRequestAsAttributes()

$event = $this->createMvcEvent(
'foo',
function (ServerRequestInterface $request, ResponseInterface $response) use (&$routeAttribute) {
new CallableMiddlewareWrapper(function (ServerRequestInterface $request, ResponseInterface $response) use (&$routeAttribute) {
$routeAttribute = $request->getAttribute(RouteMatch::class);
$response->getBody()->write($request->getAttribute('myParam', 'param did not exist'));
return $response;
}
}, new DiactorosResponse())
);

$this->routeMatch->getParams()->willReturn([
Expand Down Expand Up @@ -161,12 +162,12 @@ public function testSuccessfullyDispatchesPipeOfCallableAndHttpInteropStyleMiddl
$serviceManager = $this->prophesize(ContainerInterface::class);
$serviceManager->get('EventManager')->willReturn($eventManager);
$serviceManager->has('firstMiddleware')->willReturn(true);
$serviceManager->get('firstMiddleware')->willReturn(function ($request, $response, $next) {
$serviceManager->get('firstMiddleware')->willReturn(new CallableMiddlewareWrapper(function ($request, $response, $next) {
$this->assertInstanceOf(ServerRequestInterface::class, $request);
$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertTrue(is_callable($next));
return $next($request->withAttribute('firstMiddlewareAttribute', 'firstMiddlewareValue'), $response);
});
}, $this->prophesize(ResponseInterface::class)->reveal()));

$secondMiddleware = $this->createMock(MiddlewareInterface::class);
$secondMiddleware->expects($this->once())
Expand Down Expand Up @@ -223,9 +224,9 @@ public function testTriggersErrorForUncallableMiddleware()
public function testTriggersErrorForExceptionRaisedInMiddleware()
{
$exception = new \Exception();
$event = $this->createMvcEvent('path', function ($request, $response) use ($exception) {
$event = $this->createMvcEvent('path', new CallableMiddlewareWrapper(function ($request, $response) use ($exception) {
throw $exception;
});
}, $this->prophesize(ResponseInterface::class)->reveal()));

$application = $event->getApplication();
$application->getEventManager()->attach(MvcEvent::EVENT_DISPATCH_ERROR, function ($e) use ($exception) {
Expand Down Expand Up @@ -362,7 +363,7 @@ public function testValidMiddlewareDispatchCancelsPreviousDispatchFailures()
/* @var $application Application|\PHPUnit_Framework_MockObject_MockObject */
$application = $this->createMock(Application::class);
$eventManager = new EventManager();
$middleware = $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock();
$middleware = $this->getMockBuilder(MiddlewareInterface::class)->setMethods(['process'])->getMock();
$serviceManager = new ServiceManager([
'factories' => [
'EventManager' => function () {
Expand All @@ -378,7 +379,7 @@ public function testValidMiddlewareDispatchCancelsPreviousDispatchFailures()
$application->expects(self::any())->method('getEventManager')->willReturn($eventManager);
$application->expects(self::any())->method('getServiceManager')->willReturn($serviceManager);
$application->expects(self::any())->method('getResponse')->willReturn(new Response());
$middleware->expects(self::once())->method('__invoke')->willReturn($response);
$middleware->expects(self::once())->method('process')->willReturn($response);

$event = new MvcEvent();

Expand Down Expand Up @@ -406,7 +407,7 @@ public function testValidMiddlewareFiresDispatchableInterfaceEventListeners()
/* @var $sharedListener callable|\PHPUnit_Framework_MockObject_MockObject */
$sharedListener = $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock();
$eventManager = new EventManager();
$middleware = $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock();
$middleware = $this->getMockBuilder(MiddlewareInterface::class)->setMethods(['process'])->getMock();
$serviceManager = new ServiceManager([
'factories' => [
'EventManager' => function () use ($sharedManager) {
Expand All @@ -422,7 +423,7 @@ public function testValidMiddlewareFiresDispatchableInterfaceEventListeners()
$application->expects(self::any())->method('getEventManager')->willReturn($eventManager);
$application->expects(self::any())->method('getServiceManager')->willReturn($serviceManager);
$application->expects(self::any())->method('getResponse')->willReturn(new Response());
$middleware->expects(self::once())->method('__invoke')->willReturn($response);
$middleware->expects(self::once())->method('process')->willReturn($response);

$event = new MvcEvent();

Expand Down
10 changes: 7 additions & 3 deletions test/TestAsset/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@

namespace ZendTest\Mvc\TestAsset;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Response;

class Middleware
class Middleware implements MiddlewareInterface
{
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next = null)
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$response = new Response();
$response->getBody()->write(__CLASS__);

return $response;
}
}