4
4
5
5
namespace F9Web \ApiResponseHelpers \Tests ;
6
6
7
- use F9Web \ApiResponseHelpers ;
8
- use Illuminate \Http \JsonResponse ;
9
- use Symfony \Component \HttpFoundation \Response ;
10
- use JsonException ;
11
7
use DomainException ;
12
- use Illuminate \ Support \ Collection ;
8
+ use F9Web \ ApiResponseHelpers ;
13
9
use F9Web \ApiResponseHelpers \Tests \Models \User ;
14
10
use F9Web \ApiResponseHelpers \Tests \Resources \UserResource ;
15
- use Illuminate \Database \Eloquent \Collection As EloquentCollection ;
11
+ use Illuminate \Contracts \Support \Arrayable ;
12
+ use Illuminate \Database \Eloquent \Collection as EloquentCollection ;
13
+ use Illuminate \Http \JsonResponse ;
14
+ use Illuminate \Support \Collection ;
15
+ use JsonException ;
16
+ use Symfony \Component \HttpFoundation \Response ;
16
17
17
18
use function json_encode ;
18
19
@@ -35,8 +36,21 @@ public function testResponses(string $method, array $args, int $code, array $dat
35
36
/** @var \Illuminate\Http\JsonResponse $response */
36
37
$ response = call_user_func_array ([$ this ->service , $ method ], $ args );
37
38
self ::assertInstanceOf (JsonResponse::class, $ response );
38
- self ::assertEquals ($ code , $ response ->getStatusCode ());
39
- self ::assertEquals ($ data , $ response ->getData (true ));
39
+ self ::assertSame ($ code , $ response ->getStatusCode ());
40
+ self ::assertSame ($ data , $ response ->getData (true ));
41
+ self ::assertJsonStringEqualsJsonString (json_encode ($ data , JSON_THROW_ON_ERROR ), $ response ->getContent ());
42
+ }
43
+
44
+ /**
45
+ * @dataProvider successDefaultsDataProvider
46
+ * @throws JsonException
47
+ */
48
+ public function testSuccessResponseDefaults (?array $ default , $ args , int $ code , array $ data ): void
49
+ {
50
+ $ response = $ this ->service ->setDefaultSuccessResponse ($ default )->respondWithSuccess ($ args );
51
+ self ::assertInstanceOf (JsonResponse::class, $ response );
52
+ self ::assertSame ($ code , $ response ->getStatusCode ());
53
+ self ::assertSame ($ data , $ response ->getData (true ));
40
54
self ::assertJsonStringEqualsJsonString (json_encode ($ data , JSON_THROW_ON_ERROR ), $ response ->getContent ());
41
55
}
42
56
@@ -74,6 +88,13 @@ public function basicResponsesDataProvider(): array
74
88
['success ' => true ],
75
89
],
76
90
91
+ 'respondWithSuccess(), null response data ' => [
92
+ 'respondWithSuccess ' ,
93
+ [null ],
94
+ Response::HTTP_OK ,
95
+ ['success ' => true ],
96
+ ],
97
+
77
98
'respondWithSuccess(), custom response data ' => [
78
99
'respondWithSuccess ' ,
79
100
[['order ' => 237 ]],
@@ -95,6 +116,69 @@ public function basicResponsesDataProvider(): array
95
116
['invoice ' => 23 , 'status ' => 'pending ' ],
96
117
],
97
118
119
+ 'respondWithSuccess(), empty collection ' => [
120
+ 'respondWithSuccess ' ,
121
+ [new Collection ()],
122
+ Response::HTTP_OK ,
123
+ ['success ' => true ],
124
+ ],
125
+
126
+ 'respondWithSuccess(), Arrayable ' => [
127
+ 'respondWithSuccess ' ,
128
+ [
129
+ new class implements Arrayable {
130
+ public function toArray ()
131
+ {
132
+ return ['id ' => 1 , 'name ' => 'John ' ];
133
+ }
134
+ },
135
+ ],
136
+ Response::HTTP_OK ,
137
+ ['id ' => 1 , 'name ' => 'John ' ]
138
+ ],
139
+
140
+ 'respondWithSuccess(), empty Arrayable ' => [
141
+ 'respondWithSuccess ' ,
142
+ [
143
+ new class implements Arrayable {
144
+ public function toArray ()
145
+ {
146
+ return [];
147
+ }
148
+ },
149
+ ],
150
+ Response::HTTP_OK ,
151
+ ['success ' => true ]
152
+ ],
153
+
154
+ 'respondWithSuccess(), JsonSerializable ' => [
155
+ 'respondWithSuccess ' ,
156
+ [
157
+ new class implements \JsonSerializable {
158
+ public function jsonSerialize ()
159
+ {
160
+ return ['id ' => 1 , 'name ' => 'John ' ];
161
+ }
162
+ },
163
+ ],
164
+ Response::HTTP_OK ,
165
+ ['id ' => 1 , 'name ' => 'John ' ]
166
+ ],
167
+
168
+ 'respondWithSuccess(), empty JsonSerializable ' => [
169
+ 'respondWithSuccess ' ,
170
+ [
171
+ new class implements \JsonSerializable {
172
+ public function jsonSerialize ()
173
+ {
174
+ return [];
175
+ }
176
+ },
177
+ ],
178
+ Response::HTTP_OK ,
179
+ ['success ' => true ]
180
+ ],
181
+
98
182
'respondOk() ' => [
99
183
'respondOk ' ,
100
184
['Order accepted ' ],
@@ -151,6 +235,13 @@ public function basicResponsesDataProvider(): array
151
235
[],
152
236
],
153
237
238
+ 'respondCreated() with null ' => [
239
+ 'respondCreated ' ,
240
+ [null ],
241
+ Response::HTTP_CREATED ,
242
+ [],
243
+ ],
244
+
154
245
'respondCreated() with response data ' => [
155
246
'respondCreated ' ,
156
247
[['user ' => ['name ' => 'Jet Li ' ]]],
@@ -233,6 +324,13 @@ public function basicResponsesDataProvider(): array
233
324
[],
234
325
],
235
326
327
+ 'respondNoContent() with null ' => [
328
+ 'respondNoContent ' ,
329
+ [null ],
330
+ Response::HTTP_NO_CONTENT ,
331
+ [],
332
+ ],
333
+
236
334
// @see https://github.com/f9webltd/laravel-api-response-helpers/issues/5#issuecomment-917418285
237
335
'respondNoContent() with data ' => [
238
336
'respondNoContent ' ,
@@ -250,4 +348,40 @@ public function basicResponsesDataProvider(): array
250
348
],
251
349
];
252
350
}
351
+
352
+ public function successDefaultsDataProvider (): array
353
+ {
354
+ return [
355
+ 'respondWithSuccess(), default empty array ' => [
356
+ 'default ' => [],
357
+ 'args ' => [],
358
+ 'code ' => Response::HTTP_OK ,
359
+ 'data ' => [],
360
+ ],
361
+ 'respondWithSuccess(), default null ' => [
362
+ 'default ' => null ,
363
+ 'args ' => [],
364
+ 'code ' => Response::HTTP_OK ,
365
+ 'data ' => [],
366
+ ],
367
+ 'respondWithSuccess(), default null, null response ' => [
368
+ 'default ' => null ,
369
+ 'args ' => null ,
370
+ 'code ' => Response::HTTP_OK ,
371
+ 'data ' => [],
372
+ ],
373
+ 'respondWithSuccess(), default non-empty array ' => [
374
+ 'default ' => ['message ' => 'Task successful! ' ],
375
+ 'args ' => [],
376
+ 'code ' => Response::HTTP_OK ,
377
+ 'data ' => ['message ' => 'Task successful! ' ],
378
+ ],
379
+ 'respondWithSuccess(), default non-empty array, custom response data ' => [
380
+ 'default ' => ['message ' => 'Task successful! ' ],
381
+ 'args ' => ['numbers ' => [1 , 2 , 3 ]],
382
+ 'code ' => Response::HTTP_OK ,
383
+ 'data ' => ['numbers ' => [1 , 2 , 3 ]],
384
+ ]
385
+ ];
386
+ }
253
387
}
0 commit comments