@@ -28,20 +28,26 @@ final class MiddlewareFactoryTest extends TestCase
28
28
public function testCreateFromString (): void
29
29
{
30
30
$ container = $ this ->getContainer ([TestMiddleware::class => new TestMiddleware ()]);
31
- $ middleware = $ this ->getMiddlewareFactory ($ container )->create (TestMiddleware::class);
31
+ $ middleware = $ this
32
+ ->getMiddlewareFactory ($ container )
33
+ ->create (TestMiddleware::class);
32
34
self ::assertInstanceOf (TestMiddleware::class, $ middleware );
33
35
}
34
36
35
37
public function testCreateFromArray (): void
36
38
{
37
39
$ container = $ this ->getContainer ([TestController::class => new TestController ()]);
38
- $ middleware = $ this ->getMiddlewareFactory ($ container )->create ([TestController::class, 'index ' ]);
40
+ $ middleware = $ this
41
+ ->getMiddlewareFactory ($ container )
42
+ ->create ([TestController::class, 'index ' ]);
39
43
self ::assertSame (
40
44
'yii ' ,
41
- $ middleware ->process (
42
- $ this ->createMock (ServerRequestInterface::class),
43
- $ this ->createMock (RequestHandlerInterface::class)
44
- )->getHeaderLine ('test ' )
45
+ $ middleware
46
+ ->process (
47
+ $ this ->createMock (ServerRequestInterface::class),
48
+ $ this ->createMock (RequestHandlerInterface::class)
49
+ )
50
+ ->getHeaderLine ('test ' )
45
51
);
46
52
self ::assertSame (
47
53
[TestController::class, 'index ' ],
@@ -52,73 +58,91 @@ public function testCreateFromArray(): void
52
58
public function testCreateFromClosureResponse (): void
53
59
{
54
60
$ container = $ this ->getContainer ([TestController::class => new TestController ()]);
55
- $ middleware = $ this ->getMiddlewareFactory ($ container )->create (
56
- static function (): ResponseInterface {
57
- return (new Response ())->withStatus (418 );
58
- }
59
- );
61
+ $ middleware = $ this
62
+ ->getMiddlewareFactory ($ container )
63
+ ->create (
64
+ static function (): ResponseInterface {
65
+ return (new Response ())->withStatus (418 );
66
+ }
67
+ );
60
68
self ::assertSame (
61
69
418 ,
62
- $ middleware ->process (
63
- $ this ->createMock (ServerRequestInterface::class),
64
- $ this ->createMock (RequestHandlerInterface::class)
65
- )->getStatusCode ()
70
+ $ middleware
71
+ ->process (
72
+ $ this ->createMock (ServerRequestInterface::class),
73
+ $ this ->createMock (RequestHandlerInterface::class)
74
+ )
75
+ ->getStatusCode ()
66
76
);
67
77
}
68
78
69
79
public function testCreateFromClosureMiddleware (): void
70
80
{
71
81
$ container = $ this ->getContainer ([TestController::class => new TestController ()]);
72
- $ middleware = $ this ->getMiddlewareFactory ($ container )->create (
73
- static function (): MiddlewareInterface {
74
- return new TestMiddleware ();
75
- }
76
- );
82
+ $ middleware = $ this
83
+ ->getMiddlewareFactory ($ container )
84
+ ->create (
85
+ static function (): MiddlewareInterface {
86
+ return new TestMiddleware ();
87
+ }
88
+ );
77
89
self ::assertSame (
78
90
'42 ' ,
79
- $ middleware ->process (
80
- $ this ->createMock (ServerRequestInterface::class),
81
- $ this ->createMock (RequestHandlerInterface::class)
82
- )->getHeaderLine ('test ' )
91
+ $ middleware
92
+ ->process (
93
+ $ this ->createMock (ServerRequestInterface::class),
94
+ $ this ->createMock (RequestHandlerInterface::class)
95
+ )
96
+ ->getHeaderLine ('test ' )
83
97
);
84
98
}
85
99
86
100
public function testCreateWithUseParamsMiddleware (): void
87
101
{
88
102
$ container = $ this ->getContainer ([UseParamsMiddleware::class => new UseParamsMiddleware ()]);
89
- $ middleware = $ this ->getMiddlewareFactory ($ container )->create (UseParamsMiddleware::class);
103
+ $ middleware = $ this
104
+ ->getMiddlewareFactory ($ container )
105
+ ->create (UseParamsMiddleware::class);
90
106
91
107
self ::assertSame (
92
108
'GET ' ,
93
- $ middleware ->process (
94
- new ServerRequest ('GET ' , '/ ' ),
95
- $ this ->getRequestHandler ()
96
- )->getHeaderLine ('method ' )
109
+ $ middleware
110
+ ->process (
111
+ new ServerRequest ('GET ' , '/ ' ),
112
+ $ this ->getRequestHandler ()
113
+ )
114
+ ->getHeaderLine ('method ' )
97
115
);
98
116
}
99
117
100
118
public function testCreateWithUseParamsController (): void
101
119
{
102
120
$ container = $ this ->getContainer ([UseParamsController::class => new UseParamsController ()]);
103
- $ middleware = $ this ->getMiddlewareFactory ($ container )->create ([UseParamsController::class, 'index ' ]);
121
+ $ middleware = $ this
122
+ ->getMiddlewareFactory ($ container )
123
+ ->create ([UseParamsController::class, 'index ' ]);
104
124
105
125
self ::assertSame (
106
126
'GET ' ,
107
- $ middleware ->process (
108
- new ServerRequest ('GET ' , '/ ' ),
109
- $ this ->getRequestHandler ()
110
- )->getHeaderLine ('method ' )
127
+ $ middleware
128
+ ->process (
129
+ new ServerRequest ('GET ' , '/ ' ),
130
+ $ this ->getRequestHandler ()
131
+ )
132
+ ->getHeaderLine ('method ' )
111
133
);
112
134
}
113
135
114
136
public function testInvalidMiddlewareWithWrongCallable (): void
115
137
{
116
138
$ container = $ this ->getContainer ([TestController::class => new TestController ()]);
117
- $ middleware = $ this ->getMiddlewareFactory ($ container )->create (
118
- static function () {
119
- return 42 ;
120
- }
121
- );
139
+ $ middleware = $ this
140
+ ->getMiddlewareFactory ($ container )
141
+ ->create (
142
+ static function () {
143
+ return 42 ;
144
+ }
145
+ );
122
146
123
147
$ this ->expectException (InvalidMiddlewareDefinitionException::class);
124
148
$ middleware ->process (
@@ -130,26 +154,34 @@ static function () {
130
154
public function testInvalidMiddlewareWithWrongInstance (): void
131
155
{
132
156
$ this ->expectException (InvalidMiddlewareDefinitionException::class);
133
- $ this ->getMiddlewareFactory ()->create (new stdClass ());
157
+ $ this
158
+ ->getMiddlewareFactory ()
159
+ ->create (new stdClass ());
134
160
}
135
161
136
162
public function testInvalidMiddlewareWithWrongString (): void
137
163
{
138
164
$ this ->expectException (InvalidMiddlewareDefinitionException::class);
139
- $ this ->getMiddlewareFactory ()->create ('test ' );
165
+ $ this
166
+ ->getMiddlewareFactory ()
167
+ ->create ('test ' );
140
168
}
141
169
142
170
public function testInvalidMiddlewareWithWrongClass (): void
143
171
{
144
172
$ this ->expectException (InvalidMiddlewareDefinitionException::class);
145
173
$ this ->expectExceptionMessage ('Parameter should be either PSR middleware class name or a callable. ' );
146
- $ this ->getMiddlewareFactory ()->create (TestController::class);
174
+ $ this
175
+ ->getMiddlewareFactory ()
176
+ ->create (TestController::class);
147
177
}
148
178
149
179
public function testInvalidMiddlewareWithWrongController (): void
150
180
{
151
181
$ container = $ this ->getContainer ([InvalidController::class => new InvalidController ()]);
152
- $ middleware = $ this ->getMiddlewareFactory ($ container )->create ([InvalidController::class, 'index ' ]);
182
+ $ middleware = $ this
183
+ ->getMiddlewareFactory ($ container )
184
+ ->create ([InvalidController::class, 'index ' ]);
153
185
154
186
$ this ->expectException (InvalidMiddlewareDefinitionException::class);
155
187
$ middleware ->process (
@@ -161,31 +193,41 @@ public function testInvalidMiddlewareWithWrongController(): void
161
193
public function testInvalidMiddlewareWithWrongArraySize (): void
162
194
{
163
195
$ this ->expectException (InvalidMiddlewareDefinitionException::class);
164
- $ this ->getMiddlewareFactory ()->create (['test ' ]);
196
+ $ this
197
+ ->getMiddlewareFactory ()
198
+ ->create (['test ' ]);
165
199
}
166
200
167
201
public function testInvalidMiddlewareWithWrongArrayClass (): void
168
202
{
169
203
$ this ->expectException (InvalidMiddlewareDefinitionException::class);
170
- $ this ->getMiddlewareFactory ()->create (['class ' , 'test ' ]);
204
+ $ this
205
+ ->getMiddlewareFactory ()
206
+ ->create (['class ' , 'test ' ]);
171
207
}
172
208
173
209
public function testInvalidMiddlewareWithWrongArrayType (): void
174
210
{
175
211
$ this ->expectException (InvalidMiddlewareDefinitionException::class);
176
- $ this ->getMiddlewareFactory ()->create (['class ' => TestController::class, 'index ' ]);
212
+ $ this
213
+ ->getMiddlewareFactory ()
214
+ ->create (['class ' => TestController::class, 'index ' ]);
177
215
}
178
216
179
217
public function testInvalidMiddlewareWithWrongArrayWithInstance (): void
180
218
{
181
219
$ this ->expectException (InvalidMiddlewareDefinitionException::class);
182
- $ this ->getMiddlewareFactory ()->create ([new TestController (), 'index ' ]);
220
+ $ this
221
+ ->getMiddlewareFactory ()
222
+ ->create ([new TestController (), 'index ' ]);
183
223
}
184
224
185
225
public function testInvalidMiddlewareWithWrongArrayWithIntItems (): void
186
226
{
187
227
$ this ->expectException (InvalidMiddlewareDefinitionException::class);
188
- $ this ->getMiddlewareFactory ()->create ([7 , 42 ]);
228
+ $ this
229
+ ->getMiddlewareFactory ()
230
+ ->create ([7 , 42 ]);
189
231
}
190
232
191
233
private function getMiddlewareFactory (ContainerInterface $ container = null ): MiddlewareFactoryInterface
0 commit comments