4
4
5
5
namespace F9Web \ApiResponseHelpers \Tests ;
6
6
7
- use DomainException ;
8
7
use F9Web \ApiResponseHelpers ;
9
8
use Illuminate \Http \JsonResponse ;
10
- use Illuminate \Support \Collection ;
11
9
use Symfony \Component \HttpFoundation \Response ;
10
+ use JsonException ;
11
+ use DomainException ;
12
+ use Illuminate \Support \Collection ;
13
+
14
+ use function json_encode ;
12
15
13
16
class ResponseTest extends TestCase
14
17
{
@@ -20,141 +23,181 @@ public function setUp(): void
20
23
parent ::setUp ();
21
24
}
22
25
23
- public function testRespondNotFound (): void
26
+ /**
27
+ * @dataProvider basicResponsesDataProvider
28
+ * @throws JsonException
29
+ */
30
+ public function testResponses (string $ method , array $ args , int $ code , array $ data ): void
24
31
{
25
32
/** @var \Illuminate\Http\JsonResponse $response */
26
- $ response = $ this ->service ->respondNotFound ('Ouch! ' );
27
- self ::assertInstanceOf (JsonResponse::class, $ response );
28
- self ::assertEquals (Response::HTTP_NOT_FOUND , $ response ->getStatusCode ());
29
- self ::assertJsonStringEqualsJsonString ('{"error":"Ouch!"} ' , $ response ->getContent ());
30
- self ::assertEquals (['error ' => 'Ouch! ' ], $ response ->getData (true ));
31
-
32
- $ response = $ this ->service ->respondNotFound (new DomainException ('Unknown model ' ));
33
- self ::assertJsonStringEqualsJsonString ('{"error":"Unknown model"} ' , $ response ->getContent ());
34
- self ::assertEquals (['error ' => 'Unknown model ' ], $ response ->getData (true ));
35
-
36
- $ response = $ this ->service ->respondNotFound ('Ouch! ' , 'nope ' );
33
+ $ response = call_user_func_array ([$ this ->service , $ method ], $ args );
37
34
self ::assertInstanceOf (JsonResponse::class, $ response );
38
- self ::assertJsonStringEqualsJsonString ('{"nope":"Ouch!"} ' , $ response ->getContent ());
39
- self ::assertEquals (['nope ' => 'Ouch! ' ], $ response ->getData (true ));
40
- }
41
-
42
- public function testRespondWithSuccess (): void
43
- {
44
- /** @var \Illuminate\Http\JsonResponse $response */
45
- $ response = $ this ->service ->respondWithSuccess ();
46
- self ::assertInstanceOf (JsonResponse::class, $ response );
47
- self ::assertEquals (Response::HTTP_OK , $ response ->getStatusCode ());
48
- self ::assertJsonStringEqualsJsonString ('{"success":true} ' , $ response ->getContent ());
49
- self ::assertEquals (['success ' => true ], $ response ->getData (true ));
50
-
51
- $ response = $ this ->service ->respondWithSuccess (['super ' => 'response ' , 'yes ' => 123 ]);
52
- self ::assertJsonStringEqualsJsonString ('{"super":"response","yes":123} ' , $ response ->getContent ());
53
- self ::assertEquals (['super ' => 'response ' , 'yes ' => 123 ], $ response ->getData (true ));
54
-
55
- $ response = $ this ->service ->respondWithSuccess (new Collection (['super ' => 'response ' , 'yes ' => 123 ]));
56
- self ::assertJsonStringEqualsJsonString ('{"super":"response","yes":123} ' , $ response ->getContent ());
57
- self ::assertEquals (['super ' => 'response ' , 'yes ' => 123 ], $ response ->getData (true ));
35
+ self ::assertEquals ($ code , $ response ->getStatusCode ());
36
+ self ::assertEquals ($ data , $ response ->getData (true ));
37
+ self ::assertJsonStringEqualsJsonString (json_encode ($ data , JSON_THROW_ON_ERROR ), $ response ->getContent ());
58
38
}
59
39
60
- public function testRespondOk (): void
40
+ public function basicResponsesDataProvider (): array
61
41
{
62
- /** @var \Illuminate\Http\JsonResponse $response */
63
- $ response = $ this ->service ->respondOk ('Record updated ' );
64
- self ::assertInstanceOf (JsonResponse::class, $ response );
65
- self ::assertEquals (Response::HTTP_OK , $ response ->getStatusCode ());
66
- self ::assertJsonStringEqualsJsonString ('{"success":"Record updated"} ' , $ response ->getContent ());
67
- self ::assertEquals (['success ' => 'Record updated ' ], $ response ->getData (true ));
68
- }
69
-
70
- public function testRespondUnAuthenticated (): void
71
- {
72
- /** @var \Illuminate\Http\JsonResponse $response */
73
- $ response = $ this ->service ->respondUnAuthenticated ();
74
- self ::assertInstanceOf (JsonResponse::class, $ response );
75
- self ::assertEquals (Response::HTTP_UNAUTHORIZED , $ response ->getStatusCode ());
76
- self ::assertJsonStringEqualsJsonString ('{"error":"Unauthenticated"} ' , $ response ->getContent ());
77
- self ::assertEquals (['error ' => 'Unauthenticated ' ], $ response ->getData (true ));
78
-
79
- /** @var \Illuminate\Http\JsonResponse $response */
80
- $ response = $ this ->service ->respondUnAuthenticated ('Not allowed ' );
81
- self ::assertEquals (['error ' => 'Not allowed ' ], $ response ->getData (true ));
82
- }
83
-
84
- public function testRespondForbidden (): void
85
- {
86
- /** @var \Illuminate\Http\JsonResponse $response */
87
- $ response = $ this ->service ->respondForbidden ();
88
- self ::assertInstanceOf (JsonResponse::class, $ response );
89
- self ::assertEquals (Response::HTTP_FORBIDDEN , $ response ->getStatusCode ());
90
- self ::assertJsonStringEqualsJsonString ('{"error":"Forbidden"} ' , $ response ->getContent ());
91
- self ::assertEquals (['error ' => 'Forbidden ' ], $ response ->getData (true ));
92
-
93
- /** @var \Illuminate\Http\JsonResponse $response */
94
- $ response = $ this ->service ->respondForbidden ('No way ' );
95
- self ::assertEquals (['error ' => 'No way ' ], $ response ->getData (true ));
96
- }
97
-
98
- public function testRespondError (): void
99
- {
100
- /** @var \Illuminate\Http\JsonResponse $response */
101
- $ response = $ this ->service ->respondError ();
102
- self ::assertInstanceOf (JsonResponse::class, $ response );
103
- self ::assertEquals (Response::HTTP_BAD_REQUEST , $ response ->getStatusCode ());
104
- self ::assertJsonStringEqualsJsonString ('{"error":"Error"} ' , $ response ->getContent ());
105
- self ::assertEquals (['error ' => 'Error ' ], $ response ->getData (true ));
106
-
107
- /** @var \Illuminate\Http\JsonResponse $response */
108
- $ response = $ this ->service ->respondError ('Error ... ' );
109
- self ::assertEquals (['error ' => 'Error ... ' ], $ response ->getData (true ));
110
- }
111
-
112
- public function testRespondCreated (): void
113
- {
114
- /** @var \Illuminate\Http\JsonResponse $response */
115
- $ response = $ this ->service ->respondCreated ();
116
- self ::assertInstanceOf (JsonResponse::class, $ response );
117
- self ::assertEquals (Response::HTTP_CREATED , $ response ->getStatusCode ());
118
- self ::assertJsonStringEqualsJsonString ('[] ' , $ response ->getContent ());
119
- self ::assertEquals ([], $ response ->getData (true ));
120
-
121
- $ response = $ this ->service ->respondCreated ([ 'id ' => 123 , 'title ' => 'ABC ' ]);
122
- self ::assertJsonStringEqualsJsonString ('{"id":123,"title":"ABC"} ' , $ response ->getContent ());
123
- self ::assertEquals ([ 'id ' => 123 , 'title ' => 'ABC ' ], $ response ->getData (true ));
124
-
125
- $ response = $ this ->service ->respondCreated (new Collection ([ 'id ' => 123 , 'title ' => 'ABC ' ]));
126
- self ::assertJsonStringEqualsJsonString ('{"id":123,"title":"ABC"} ' , $ response ->getContent ());
127
- self ::assertEquals ([ 'id ' => 123 , 'title ' => 'ABC ' ], $ response ->getData (true ));
128
- self ::assertEquals (Response::HTTP_CREATED , $ response ->getStatusCode ());
129
- }
130
-
131
- public function testRespondFailedValidation (): void
132
- {
133
- /** @var \Illuminate\Http\JsonResponse $response */
134
- $ response = $ this ->service ->respondFailedValidation ('Password is required ' );
135
- self ::assertInstanceOf (JsonResponse::class, $ response );
136
- self ::assertEquals (Response::HTTP_UNPROCESSABLE_ENTITY , $ response ->getStatusCode ());
137
- self ::assertJsonStringEqualsJsonString ('{"message":"Password is required"} ' , $ response ->getContent ());
138
- self ::assertEquals (['message ' => 'Password is required ' ], $ response ->getData (true ));
139
-
140
- /** @var \Illuminate\Http\JsonResponse $response */
141
- $ response = $ this ->service ->respondFailedValidation (new DomainException ('Bad things happening ... ' ));
142
- self ::assertJsonStringEqualsJsonString ('{"message":"Bad things happening ..."} ' , $ response ->getContent ());
143
- self ::assertEquals (['message ' => 'Bad things happening ... ' ], $ response ->getData (true ));
144
-
145
- /** @var \Illuminate\Http\JsonResponse $response */
146
- $ response = $ this ->service ->respondFailedValidation ('Password is required ' , 'erm ' );
147
- self ::assertJsonStringEqualsJsonString ('{"erm":"Password is required"} ' , $ response ->getContent ());
148
- self ::assertEquals (['erm ' => 'Password is required ' ], $ response ->getData (true ));
149
- }
150
-
151
- public function testRespondTeapot (): void
152
- {
153
- /** @var \Illuminate\Http\JsonResponse $response */
154
- $ response = $ this ->service ->respondTeapot ();
155
- self ::assertInstanceOf (JsonResponse::class, $ response );
156
- self ::assertEquals (Response::HTTP_I_AM_A_TEAPOT , $ response ->getStatusCode ());
157
- self ::assertJsonStringEqualsJsonString ('{"message":"I \'m a teapot"} ' , $ response ->getContent ());
158
- self ::assertEquals (['message ' => 'I \'m a teapot ' ], $ response ->getData (true ));
42
+ return [
43
+ 'respondNotFound() ' => [
44
+ 'respondNotFound ' ,
45
+ ['Ouch ' ],
46
+ Response::HTTP_NOT_FOUND ,
47
+ ['error ' => 'Ouch ' ],
48
+ ],
49
+
50
+ 'respondNotFound() with custom key ' => [
51
+ 'respondNotFound ' ,
52
+ ['Ouch ' , 'message ' ],
53
+ Response::HTTP_NOT_FOUND ,
54
+ ['message ' => 'Ouch ' ],
55
+ ],
56
+
57
+ 'respondNotFound() with exception and custom key ' => [
58
+ 'respondNotFound ' ,
59
+ [
60
+ new DomainException ('Unknown model ' ),
61
+ 'message '
62
+ ],
63
+ Response::HTTP_NOT_FOUND ,
64
+ ['message ' => 'Unknown model ' ],
65
+ ],
66
+
67
+ 'respondWithSuccess(), default response data ' => [
68
+ 'respondWithSuccess ' ,
69
+ [],
70
+ Response::HTTP_OK ,
71
+ ['success ' => true ],
72
+ ],
73
+
74
+ 'respondWithSuccess(), custom response data ' => [
75
+ 'respondWithSuccess ' ,
76
+ [['order ' => 237 ]],
77
+ Response::HTTP_OK ,
78
+ ['order ' => 237 ],
79
+ ],
80
+
81
+ 'respondWithSuccess(), nested custom response data ' => [
82
+ 'respondWithSuccess ' ,
83
+ [['order ' => 237 , 'customer ' => ['name ' => 'Jason Bourne ' ]]],
84
+ Response::HTTP_OK ,
85
+ ['order ' => 237 , 'customer ' => ['name ' => 'Jason Bourne ' ]],
86
+ ],
87
+
88
+ 'respondWithSuccess(), collection ' => [
89
+ 'respondWithSuccess ' ,
90
+ [new Collection (['invoice ' => 23 , 'status ' => 'pending ' ])],
91
+ Response::HTTP_OK ,
92
+ ['invoice ' => 23 , 'status ' => 'pending ' ],
93
+ ],
94
+
95
+ 'respondOk() ' => [
96
+ 'respondOk ' ,
97
+ ['Order accepted ' ],
98
+ Response::HTTP_OK ,
99
+ ['success ' => 'Order accepted ' ],
100
+ ],
101
+
102
+ 'respondUnAuthenticated(), default message ' => [
103
+ 'respondUnAuthenticated ' ,
104
+ [],
105
+ Response::HTTP_UNAUTHORIZED ,
106
+ ['error ' => 'Unauthenticated ' ],
107
+ ],
108
+
109
+ 'respondUnAuthenticated(), custom message ' => [
110
+ 'respondUnAuthenticated ' ,
111
+ ['Denied ' ],
112
+ Response::HTTP_UNAUTHORIZED ,
113
+ ['error ' => 'Denied ' ],
114
+ ],
115
+
116
+ 'respondForbidden(), default message ' => [
117
+ 'respondForbidden ' ,
118
+ [],
119
+ Response::HTTP_FORBIDDEN ,
120
+ ['error ' => 'Forbidden ' ],
121
+ ],
122
+
123
+ 'respondForbidden(), custom message ' => [
124
+ 'respondForbidden ' ,
125
+ ['Disavowed ' ],
126
+ Response::HTTP_FORBIDDEN ,
127
+ ['error ' => 'Disavowed ' ],
128
+ ],
129
+
130
+ 'respondError(), default message ' => [
131
+ 'respondError ' ,
132
+ [],
133
+ Response::HTTP_BAD_REQUEST ,
134
+ ['error ' => 'Error ' ],
135
+ ],
136
+
137
+ 'respondError(), custom message ' => [
138
+ 'respondError ' ,
139
+ ['Order tampering detected ' ],
140
+ Response::HTTP_BAD_REQUEST ,
141
+ ['error ' => 'Order tampering detected ' ],
142
+ ],
143
+
144
+ 'respondCreated() ' => [
145
+ 'respondCreated ' ,
146
+ [],
147
+ Response::HTTP_CREATED ,
148
+ [],
149
+ ],
150
+
151
+ 'respondCreated() with response data ' => [
152
+ 'respondCreated ' ,
153
+ [['user ' => ['name ' => 'Jet Li ' ]]],
154
+ Response::HTTP_CREATED ,
155
+ ['user ' => ['name ' => 'Jet Li ' ]],
156
+ ],
157
+
158
+ 'respondCreated(), with collection as response ' => [
159
+ 'respondCreated ' ,
160
+ [new Collection (['invoice ' => 23 , 'status ' => 'pending ' ])],
161
+ Response::HTTP_CREATED ,
162
+ ['invoice ' => 23 , 'status ' => 'pending ' ],
163
+ ],
164
+
165
+ 'respondFailedValidation() ' => [
166
+ 'respondFailedValidation ' ,
167
+ ['An invoice is required ' ],
168
+ Response::HTTP_UNPROCESSABLE_ENTITY ,
169
+ ['message ' => 'An invoice is required ' ],
170
+ ],
171
+
172
+ 'respondTeapot() ' => [
173
+ 'respondTeapot ' ,
174
+ [],
175
+ Response::HTTP_I_AM_A_TEAPOT ,
176
+ ['message ' => 'I \'m a teapot ' ],
177
+ ],
178
+
179
+ 'respondNoContent() ' => [
180
+ 'respondNoContent ' ,
181
+ [],
182
+ Response::HTTP_NO_CONTENT ,
183
+ [],
184
+ ],
185
+
186
+ // @see https://github.com/f9webltd/laravel-api-response-helpers/issues/5#issuecomment-917418285
187
+ 'respondNoContent() with data ' => [
188
+ 'respondNoContent ' ,
189
+ [['role ' => 'manager ' ]],
190
+ Response::HTTP_NO_CONTENT ,
191
+ ['role ' => 'manager ' ],
192
+ ],
193
+
194
+ // @see https://github.com/f9webltd/laravel-api-response-helpers/issues/5#issuecomment-917418285
195
+ 'respondNoContent(), with collection as response ' => [
196
+ 'respondNoContent ' ,
197
+ [new Collection (['invoice ' => 23 , 'status ' => 'pending ' ])],
198
+ Response::HTTP_NO_CONTENT ,
199
+ ['invoice ' => 23 , 'status ' => 'pending ' ],
200
+ ],
201
+ ];
159
202
}
160
- }
203
+ }
0 commit comments