8
8
use Nyholm \Psr7 \ServerRequest ;
9
9
use PHPUnit \Framework \TestCase ;
10
10
use Psr \Container \ContainerInterface ;
11
+ use Psr \EventDispatcher \EventDispatcherInterface ;
11
12
use Psr \Http \Message \ResponseInterface ;
12
13
use Psr \Http \Message \ServerRequestInterface ;
13
14
use Psr \Http \Server \RequestHandlerInterface ;
15
+ use Yiisoft \Middleware \Dispatcher \Event \AfterMiddleware ;
16
+ use Yiisoft \Middleware \Dispatcher \Event \BeforeMiddleware ;
14
17
use Yiisoft \Middleware \Dispatcher \MiddlewareDispatcher ;
15
18
use Yiisoft \Middleware \Dispatcher \MiddlewareFactory ;
16
19
use Yiisoft \Middleware \Dispatcher \MiddlewareStack ;
17
20
use Yiisoft \Middleware \Dispatcher \Tests \Support \Container ;
21
+ use Yiisoft \Middleware \Dispatcher \Tests \Support \MockEventDispatcher ;
18
22
use Yiisoft \Middleware \Dispatcher \Tests \Support \TestController ;
19
23
20
24
final class MiddlewareDispatcherTest extends TestCase
@@ -62,11 +66,11 @@ public function testMiddlewareFullStackCalled(): void
62
66
{
63
67
$ request = new ServerRequest ('GET ' , '/ ' );
64
68
65
- $ middleware1 = function (ServerRequestInterface $ request , RequestHandlerInterface $ handler ) {
69
+ $ middleware1 = static function (ServerRequestInterface $ request , RequestHandlerInterface $ handler ) {
66
70
$ request = $ request ->withAttribute ('middleware ' , 'middleware1 ' );
67
71
return $ handler ->handle ($ request );
68
72
};
69
- $ middleware2 = function (ServerRequestInterface $ request ) {
73
+ $ middleware2 = static function (ServerRequestInterface $ request ) {
70
74
return new Response (200 , [], null , '1.1 ' , implode ($ request ->getAttributes ()));
71
75
};
72
76
@@ -81,10 +85,10 @@ public function testMiddlewareStackInterrupted(): void
81
85
{
82
86
$ request = new ServerRequest ('GET ' , '/ ' );
83
87
84
- $ middleware1 = function () {
88
+ $ middleware1 = static function () {
85
89
return new Response (403 );
86
90
};
87
- $ middleware2 = function () {
91
+ $ middleware2 = static function () {
88
92
return new Response (200 );
89
93
};
90
94
@@ -106,6 +110,33 @@ public function testArrayMiddlewareSuccessfulCall(): void
106
110
$ this ->assertSame (200 , $ response ->getStatusCode ());
107
111
}
108
112
113
+ public function testEventsAreDispatched (): void
114
+ {
115
+ $ eventDispatcher = new MockEventDispatcher ();
116
+
117
+ $ request = new ServerRequest ('GET ' , '/ ' );
118
+
119
+ $ middleware1 = static function (ServerRequestInterface $ request , RequestHandlerInterface $ handler ) {
120
+ return $ handler ->handle ($ request );
121
+ };
122
+ $ middleware2 = static function () {
123
+ return new Response ();
124
+ };
125
+
126
+ $ dispatcher = $ this ->getDispatcher (null , $ eventDispatcher )->withMiddlewares ([$ middleware2 , $ middleware1 ]);
127
+ $ dispatcher ->dispatch ($ request , $ this ->getRequestHandler ());
128
+
129
+ $ this ->assertEquals (
130
+ [
131
+ BeforeMiddleware::class,
132
+ BeforeMiddleware::class,
133
+ AfterMiddleware::class,
134
+ AfterMiddleware::class,
135
+ ],
136
+ $ eventDispatcher ->getClassesEvents ()
137
+ );
138
+ }
139
+
109
140
private function getRequestHandler (): RequestHandlerInterface
110
141
{
111
142
return new class () implements RequestHandlerInterface {
@@ -116,13 +147,23 @@ public function handle(ServerRequestInterface $request): ResponseInterface
116
147
};
117
148
}
118
149
119
- private function getDispatcher (ContainerInterface $ container = null ): MiddlewareDispatcher
150
+ private function getDispatcher (ContainerInterface $ container = null , ? EventDispatcherInterface $ eventDispatcher = null ): MiddlewareDispatcher
120
151
{
152
+ if ($ eventDispatcher === null ) {
153
+ $ eventDispatcher = $ this ->createMock (EventDispatcherInterface::class);
154
+ }
155
+
121
156
if ($ container === null ) {
122
- return new MiddlewareDispatcher (new MiddlewareFactory ($ this ->getContainer ()), new MiddlewareStack ());
157
+ return new MiddlewareDispatcher (
158
+ new MiddlewareFactory ($ this ->getContainer ()),
159
+ new MiddlewareStack ($ eventDispatcher )
160
+ );
123
161
}
124
162
125
- return new MiddlewareDispatcher (new MiddlewareFactory ($ container ), new MiddlewareStack ());
163
+ return new MiddlewareDispatcher (
164
+ new MiddlewareFactory ($ container ),
165
+ new MiddlewareStack ($ eventDispatcher )
166
+ );
126
167
}
127
168
128
169
private function getContainer (array $ instances = []): ContainerInterface
0 commit comments