24
24
25
25
class MiddlewareListenerTest extends TestCase
26
26
{
27
+ /**
28
+ * @var \Prophecy\Prophecy\ObjectProphecy
29
+ */
30
+ private $ routeMatch ;
31
+
32
+
27
33
/**
28
34
* Create an MvcEvent, populated with everything it needs.
29
35
*
@@ -34,8 +40,8 @@ class MiddlewareListenerTest extends TestCase
34
40
public function createMvcEvent ($ middlewareMatched , $ middleware = null )
35
41
{
36
42
$ 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 );
39
45
40
46
$ eventManager = new EventManager ();
41
47
@@ -54,7 +60,7 @@ public function createMvcEvent($middlewareMatched, $middleware = null)
54
60
$ event ->setRequest (new Request ());
55
61
$ event ->setResponse ($ response );
56
62
$ event ->setApplication ($ application ->reveal ());
57
- $ event ->setRouteMatch ($ routeMatch ->reveal ());
63
+ $ event ->setRouteMatch ($ this -> routeMatch ->reveal ());
58
64
59
65
return $ event ;
60
66
}
@@ -82,6 +88,28 @@ public function testSuccessfullyDispatchesMiddleware()
82
88
$ this ->assertEquals ('Test! ' , $ return ->getBody ());
83
89
}
84
90
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
+
85
113
public function testTriggersErrorForUncallableMiddleware ()
86
114
{
87
115
$ event = $ this ->createMvcEvent ('path ' );
0 commit comments