24
24
25
25
class MiddlewareListenerTest extends TestCase
26
26
{
27
+ /**
28
+ * @var \Prophecy\Prophecy\ObjectProphecy
29
+ */
30
+ private $ routeMatch ;
31
+
27
32
/**
28
33
* Create an MvcEvent, populated with everything it needs.
29
34
*
@@ -34,8 +39,9 @@ class MiddlewareListenerTest extends TestCase
34
39
public function createMvcEvent ($ middlewareMatched , $ middleware = null )
35
40
{
36
41
$ response = new Response ();
37
- $ routeMatch = $ this ->prophesize (RouteMatch::class);
38
- $ routeMatch ->getParam ('middleware ' , false )->willReturn ($ middlewareMatched );
42
+ $ this ->routeMatch = $ this ->prophesize (RouteMatch::class);
43
+ $ this ->routeMatch ->getParam ('middleware ' , false )->willReturn ($ middlewareMatched );
44
+ $ this ->routeMatch ->getParams ()->willReturn ([]);
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,31 @@ 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
+ $ routeAttribute = null ;
95
+
96
+ $ event = $ this ->createMvcEvent (
97
+ 'foo ' ,
98
+ function (ServerRequestInterface $ request , ResponseInterface $ response ) use (&$ routeAttribute ) {
99
+ $ routeAttribute = $ request ->getAttribute (RouteMatch::class);
100
+ $ response ->getBody ()->write ($ request ->getAttribute ('myParam ' , 'param did not exist ' ));
101
+ return $ response ;
102
+ }
103
+ );
104
+
105
+ $ this ->routeMatch ->getParams ()->willReturn ([
106
+ 'myParam ' => $ matchedRouteParam ,
107
+ ]);
108
+
109
+ $ listener = new MiddlewareListener ();
110
+ $ return = $ listener ->onDispatch ($ event );
111
+ $ this ->assertInstanceOf (Response::class, $ return );
112
+ $ this ->assertSame ($ matchedRouteParam , $ return ->getBody ());
113
+ $ this ->assertSame ($ this ->routeMatch ->reveal (), $ routeAttribute );
114
+ }
115
+
85
116
public function testTriggersErrorForUncallableMiddleware ()
86
117
{
87
118
$ event = $ this ->createMvcEvent ('path ' );
@@ -125,6 +156,7 @@ public function testCanLoadFromAbstractFactory()
125
156
$ response = new Response ();
126
157
$ routeMatch = $ this ->prophesize (RouteMatch::class);
127
158
$ routeMatch ->getParam ('middleware ' , false )->willReturn ('test ' );
159
+ $ routeMatch ->getParams ()->willReturn ([]);
128
160
129
161
$ eventManager = new EventManager ();
130
162
0 commit comments