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

Commit e84fbfa

Browse files
committed
Added failing test case testMatchedRouteParamsAreInjectedToRequestAsAttributes
1 parent c6d6392 commit e84fbfa

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

test/MiddlewareListenerTest.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424

2525
class MiddlewareListenerTest extends TestCase
2626
{
27+
/**
28+
* @var \Prophecy\Prophecy\ObjectProphecy
29+
*/
30+
private $routeMatch;
31+
32+
2733
/**
2834
* Create an MvcEvent, populated with everything it needs.
2935
*
@@ -34,8 +40,8 @@ class MiddlewareListenerTest extends TestCase
3440
public function createMvcEvent($middlewareMatched, $middleware = null)
3541
{
3642
$response = new Response();
37-
$routeMatch = $this->prophesize(RouteMatch::class);
38-
$routeMatch->getParam('middleware', false)->willReturn($middlewareMatched);
43+
$this->routeMatch = $this->prophesize(RouteMatch::class);
44+
$this->routeMatch->getParam('middleware', false)->willReturn($middlewareMatched);
3945

4046
$eventManager = new EventManager();
4147

@@ -54,7 +60,7 @@ public function createMvcEvent($middlewareMatched, $middleware = null)
5460
$event->setRequest(new Request());
5561
$event->setResponse($response);
5662
$event->setApplication($application->reveal());
57-
$event->setRouteMatch($routeMatch->reveal());
63+
$event->setRouteMatch($this->routeMatch->reveal());
5864

5965
return $event;
6066
}
@@ -82,6 +88,28 @@ public function testSuccessfullyDispatchesMiddleware()
8288
$this->assertEquals('Test!', $return->getBody());
8389
}
8490

91+
public function testMatchedRouteParamsAreInjectedToRequestAsAttributes()
92+
{
93+
$matchedRouteParam = uniqid('matched param', true);
94+
95+
$event = $this->createMvcEvent(
96+
'foo',
97+
function (ServerRequestInterface $request, ResponseInterface $response) {
98+
$response->getBody()->write($request->getAttribute('myParam', 'param did not exist'));
99+
return $response;
100+
}
101+
);
102+
103+
$this->routeMatch->getParams()->willReturn([
104+
'myParam' => $matchedRouteParam,
105+
]);
106+
107+
$listener = new MiddlewareListener();
108+
$return = $listener->onDispatch($event);
109+
self::assertInstanceOf(Response::class, $return);
110+
self::assertSame($matchedRouteParam, $return->getBody());
111+
}
112+
85113
public function testTriggersErrorForUncallableMiddleware()
86114
{
87115
$event = $this->createMvcEvent('path');

0 commit comments

Comments
 (0)