5
5
use Illuminate \Foundation \Testing \Concerns \MakesHttpRequests ;
6
6
use Illuminate \Support \Arr ;
7
7
use Illuminate \Support \Collection ;
8
+ use PHPUnit \Framework \Assert ;
9
+ use PHPUnit \Framework \TestCase ;
10
+ use function array_walk_recursive ;
8
11
use function collect ;
9
12
use function http_build_query ;
10
13
use function implode ;
14
+ use function is_bool ;
11
15
use function is_null ;
16
+ use function is_scalar ;
12
17
13
- class TestBuilder
18
+ final class TestBuilder
14
19
{
15
20
16
21
/**
17
- * @var MakesHttpRequests
22
+ * @var TestCase| MakesHttpRequests
18
23
*/
19
24
private $ test ;
20
25
@@ -62,7 +67,7 @@ public function __construct($test)
62
67
* @param string $resourceType
63
68
* @return $this
64
69
*/
65
- public function expects (string $ resourceType )
70
+ public function expects (string $ resourceType ): self
66
71
{
67
72
$ this ->expectedResourceType = $ resourceType ;
68
73
@@ -75,7 +80,7 @@ public function expects(string $resourceType)
75
80
* @param string|null $mediaType
76
81
* @return $this
77
82
*/
78
- public function accept (?string $ mediaType )
83
+ public function accept (?string $ mediaType ): self
79
84
{
80
85
$ this ->accept = $ mediaType ;
81
86
@@ -88,7 +93,7 @@ public function accept(?string $mediaType)
88
93
* @param string|null $mediaType
89
94
* @return $this
90
95
*/
91
- public function contentType (?string $ mediaType )
96
+ public function contentType (?string $ mediaType ): self
92
97
{
93
98
$ this ->contentType = $ mediaType ;
94
99
@@ -101,7 +106,7 @@ public function contentType(?string $mediaType)
101
106
* @param iterable $query
102
107
* @return $this
103
108
*/
104
- public function query (iterable $ query )
109
+ public function query (iterable $ query ): self
105
110
{
106
111
$ this ->query = collect ($ query )->merge ($ query );
107
112
@@ -114,7 +119,7 @@ public function query(iterable $query)
114
119
* @param string ...$paths
115
120
* @return $this
116
121
*/
117
- public function includePaths (string ...$ paths )
122
+ public function includePaths (string ...$ paths ): self
118
123
{
119
124
$ this ->query ['include ' ] = implode (', ' , $ paths );
120
125
@@ -128,7 +133,7 @@ public function includePaths(string ...$paths)
128
133
* @param string|string[] $fieldNames
129
134
* @return $this
130
135
*/
131
- public function sparseFields (string $ resourceType , $ fieldNames )
136
+ public function sparseFields (string $ resourceType , $ fieldNames ): self
132
137
{
133
138
$ this ->query ['fields ' ] = collect ($ this ->query ->get ('fields ' ))
134
139
->put ($ resourceType , implode (', ' , Arr::wrap ($ fieldNames )));
@@ -142,7 +147,7 @@ public function sparseFields(string $resourceType, $fieldNames)
142
147
* @param iterable $filter
143
148
* @return $this
144
149
*/
145
- public function filter (iterable $ filter )
150
+ public function filter (iterable $ filter ): self
146
151
{
147
152
$ this ->query ['filter ' ] = collect ($ filter );
148
153
@@ -155,7 +160,7 @@ public function filter(iterable $filter)
155
160
* @param string ...$sort
156
161
* @return $this
157
162
*/
158
- public function sort (string ...$ sort )
163
+ public function sort (string ...$ sort ): self
159
164
{
160
165
$ thi
A93C
s ->query ['sort ' ] = implode (', ' , $ sort );
161
166
@@ -168,7 +173,7 @@ public function sort(string ...$sort)
168
173
* @param iterable $page
169
174
* @return $this
170
175
*/
171
- public function page (iterable $ page )
176
+ public function page (iterable $ page ): self
172
177
{
173
178
$ this ->query ['page ' ] = collect ($ page );
174
179
@@ -181,7 +186,7 @@ public function page(iterable $page)
181
186
* @param mixed|null $data
182
187
* @return $this
183
188
*/
184
- public function data ($ data )
189
+ public function data ($ data ): self
185
190
{
186
191
if (is_null ($ data )) {
187
192
$ this ->document ->put ('data ' , null );
@@ -199,7 +204,7 @@ public function data($data)
199
204
* @param string|null $contentType
200
205
* @return $this
201
206
*/
202
- public function content ($ document , string $ contentType = null )
207
+ public function content ($ document , string $ contentType = null ): self
203
208
{
204
209
$ this ->document = collect ($ document );
205
210
@@ -217,7 +222,7 @@ public function content($document, string $contentType = null)
217
222
* @param iterable $headers
218
223
* @return TestResponse
219
224
*/
220
- public function get (string $ uri , iterable $ headers = [])
225
+ public function get (string $ uri , iterable $ headers = []): TestResponse
221
226
{
222
227
return $ this ->call ('GET ' , $ uri , $ headers );
223
228
}
@@ -229,7 +234,7 @@ public function get(string $uri, iterable $headers = [])
229
234
* @param iterable $headers
230
235
* @return TestResponse
231
236
*/
232
- public function post (string $ uri , iterable $ headers = [])
237
+ public function post (string $ uri , iterable $ headers = []): TestResponse
233
238
{
234
239
return $ this ->call ('POST ' , $ uri , $ headers );
235
240
}
@@ -241,7 +246,7 @@ public function post(string $uri, iterable $headers = [])
241
246
* @param iterable $headers
242
247
* @return TestResponse
243
248
*/
244
- public function patch (string $ uri , iterable $ headers = [])
249
+ public function patch (string $ uri , iterable $ headers = []): TestResponse
245
250
{
246
251
return $ this ->call ('PATCH ' , $ uri , $ headers );
247
252
}
@@ -253,7 +258,7 @@ public function patch(string $uri, iterable $headers = [])
253
258
* @param iterable $headers
254
259
* @return TestResponse
255
260
*/
256
- public function delete (string $ uri , iterable $ headers = [])
261
+ public function delete (string $ uri , iterable $ headers = []): TestResponse
257
262
{
258
263
return $ this ->call ('DELETE ' , $ uri , $ headers );
259
264
}
@@ -267,7 +272,7 @@ public function delete(string $uri, iterable $headers = [])
267
272
public function call (string $ method , string $ uri , iterable $ headers = []): TestResponse
268
273
{
269
274
if ($ this ->query ->isNotEmpty ()) {
270
- $ uri .= '? ' . http_build_query ( $ this ->query -> toArray () );
275
+ $ uri .= '? ' . $ this ->buildQuery ( );
271
276
}
272
277
273
278
$ headers = collect ([
@@ -288,4 +293,28 @@ public function call(string $method, string $uri, iterable $headers = []): TestR
288
293
289
294
return $ response ;
290
295
}
296
+
297
+ /**
298
+ * Convert query params to a string.
299
+ *
300
+ * We check all values are strings, integers or floats as these are the only
301
+ * valid values that can be sent in the query params. E.g. if the developer
302
+ * uses a `boolean`, they actually need to test where the strings `'true'`
303
+ * or `'false'` (or the string/integer equivalents) work.
304
+ *
305
+ * @return string
306
+ * @see https://github.com/cloudcreativity/laravel-json-api/issues/427
307
+ */
308
+ private function buildQuery (): string
309
+ {
310
+ $ query = $ this ->query ->toArray ();
311
+
312
+ array_walk_recursive ($ query , function ($ value , $ key ) {
313
+ if (!is_scalar ($ value ) || is_bool ($ value )) {
314
+ Assert::fail ("Test query parameter at {$ key } is not a string, integer or float. " );
315
+ }
316
+ });
317
+
318
+ return http_build_query ($ query );
319
+ }
291
320
}
0 commit comments