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

Commit 4dbdb4b

Browse files
committed
Use mocks instead of anon classes for middlewares
1 parent 03ffda0 commit 4dbdb4b

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

test/MiddlewareListenerTest.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,10 @@ public function testSuccessfullyDispatchesHttpInteropMiddleware()
9696
{
9797
$expectedOutput = uniqid('expectedOutput', true);
9898

99-
$event = $this->createMvcEvent('path', new class($expectedOutput) implements MiddlewareInterface {
100-
private $expectedOutput;
99+
$middleware = $this->createMock(MiddlewareInterface::class);
100+
$middleware->expects($this->once())->method('process')->willReturn(new HtmlResponse($expectedOutput));
101101

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-
});
102+
$event = $this->createMvcEvent('path', $middleware);
112103
$application = $event->getApplication();
113104

114105
$application->getEventManager()->attach(MvcEvent::EVENT_DISPATCH_ERROR, function ($e) {
@@ -169,13 +160,16 @@ public function testSuccessfullyDispatchesPipeOfCallableAndHttpInteropStyleMiddl
169160
$this->assertTrue(is_callable($next));
170161
return $next($request->withAttribute('firstMiddlewareAttribute', 'firstMiddlewareValue'), $response);
171162
});
172-
$serviceManager->has('secondMiddleware')->willReturn(true);
173-
$serviceManager->get('secondMiddleware')->willReturn(new class implements MiddlewareInterface {
174-
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
175-
{
163+
164+
$secondMiddleware = $this->createMock(MiddlewareInterface::class);
165+
$secondMiddleware->expects($this->once())
166+
->method('process')
167+
->willReturnCallback(function (ServerRequestInterface $request) {
176168
return new HtmlResponse($request->getAttribute('firstMiddlewareAttribute'));
177-
}
178-
});
169+
});
170+
171+
$serviceManager->has('secondMiddleware')->willReturn(true);
172+
$serviceManager->get('secondMiddleware')->willReturn($secondMiddleware);
179173

180174
$application = $this->prophesize(Application::class);
181175
$application->getEventManager()->willReturn($eventManager);

0 commit comments

Comments
 (0)