@@ -144,13 +144,95 @@ public function testMiddleware(string $method, string $uri): void
144
144
->middleware ('foo ' )
145
145
->resources (function ($ server ) {
146
146
$ server ->resource ('posts ' )->middleware ('bar ' )->relationships (function ($ relations ) {
147
- $ relations ->hasMany ('tags ' )->middleware ('baz ' );
147
+ $ relations ->hasMany ('tags ' )->middleware ('baz1 ' , ' baz2 ' );
148
148
});
149
149
});
150
150
});
151
151
152
152
$ route = $ this ->assertMatch ($ method , $ uri );
153
- $ this ->assertSame (['api ' , 'jsonapi:v1 ' , 'foo ' , 'bar ' , 'baz ' ], $ route ->action ['middleware ' ]);
153
+ $ this ->assertSame (['api ' , 'jsonapi:v1 ' , 'foo ' , 'bar ' , 'baz1 ' , 'baz2 ' ], $ route ->action ['middleware ' ]);
154
+ }
155
+
156
+ /**
157
+ * @param string $method
158
+ * @param string $uri
159
+ * @dataProvider genericProvider
160
+ */
161
+ public function testMiddlewareAsArrayList (string $ method , string $ uri ): void
162
+ {
163
+ $ server = $ this ->createServer ('v1 ' );
164
+ $ schema = $ this ->createSchema ($ server , 'posts ' , '\d+ ' );
165
+ $ this ->createRelation ($ schema , 'tags ' );
166
+
167
+ $ this ->defaultApiRoutesWithNamespace (function () {
168
+ JsonApiRoute::server ('v1 ' )
169
+ ->prefix ('v1 ' )
170
+ ->namespace ('Api \\V1 ' )
171
+ ->middleware ('foo ' )
172
+ ->resources (function ($ server ) {
173
+ $ server ->resource ('posts ' )->middleware ('bar ' )->relationships (function ($ relations ) {
174
+ $ relations ->hasMany ('tags ' )->middleware (['baz1 ' , 'baz2 ' ]);
175
+ });
176
+ });
177
+ });
178
+
179
+ $ route = $ this ->assertMatch ($ method , $ uri );
180
+ $ this ->assertSame (['api ' , 'jsonapi:v1 ' , 'foo ' , 'bar ' , 'baz1 ' , 'baz2 ' ], $ route ->action ['middleware ' ]);
181
+ }
182
+
183
+ /**
184
+ * @param string $method
185
+ * @param string $uri
186
+ * @param string $action
187
+ * @dataProvider genericProvider
188
+ */
189
+ public function testActionMiddleware (string $ method , string $ uri , string $ action ): void
190
+ {
191
+ $ actions = [
192
+ '* ' => ['baz1 ' , 'baz2 ' ],
193
+ 'showRelated ' => 'showRelated1 ' ,
194
+ 'showRelationship ' => ['showRelationship1 ' , 'showRelationship2 ' ],
195
+ 'updateRelationship ' => 'updateRelationship1 ' ,
196
+ 'attachRelationship ' => ['attachRelationship1 ' , 'attachRelationship2 ' ],
197
+ 'detachRelationship ' => 'detachRelationship1 ' ,
198
+ ];
199
+
200
+ $ expected = [
201
+ 'api ' ,
202
+ 'jsonapi:v1 ' ,
203
+ 'foo ' ,
204
+ 'bar ' ,
205
+ ...$ actions ['* ' ],
206
+ ...Arr::wrap ($ actions [$ action ]),
207
+ ];
208
+
209
+ $ server = $ this ->createServer ('v1 ' );
210
+ $ schema = $ this ->createSchema ($ server , 'posts ' , '\d+ ' );
211
+ $ this ->createRelation ($ schema , 'tags ' );
212
+
213
+ $ this ->defaultApiRoutesWithNamespace (function () use ($ actions ) {
214
+ JsonApiRoute::server ('v1 ' )
215
+ ->prefix ('v1 ' )
216
+ ->namespace ('Api \\V1 ' )
217
+ ->middleware ('foo ' )
218
+ ->resources (function ($ server ) use ($ actions ) {
219
+ $ server ->resource ('posts ' )->middleware ('bar ' )->relationships (
220
+ function ($ relations ) use ($ actions ) {
221
+ $ relations ->hasMany ('tags ' )->middleware ([
222
+ '* ' => $ actions ['* ' ],
223
+ 'related ' => $ actions ['showRelated ' ],
224
+ 'show ' => $ actions ['showRelationship ' ],
225
+ 'update ' => $ actions ['updateRelationship ' ],
226
+ 'attach ' => $ actions ['attachRelationship ' ],
227
+ 'detach ' => $ actions ['detachRelationship ' ],
228
+ ]);
229
+ },
230
+ );
231
+ });
232
+ });
233
+
234
+ $ route = $ this ->assertMatch ($ method , $ uri );
235
+ $ this ->assertSame ($ expected , $ route ->action ['middleware ' ]);
154
236
}
155
237
156
238
/**
0 commit comments