8000 [Feature] Ensure test query parameters are not booleans · CodingSeo/laravel-json-api@93cfc88 · GitHub
[go: up one dir, main page]

Skip to content

Commit 93cfc88

Browse files
committed
[Feature] Ensure test query parameters are not booleans
Closes cloudcreativity#427
1 parent fd6ce60 commit 93cfc88

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ All notable changes to this project will be documented in this file. This projec
88
- [#348](https://github.com/cloudcreativity/laravel-json-api/issues/348)
99
Can now use route parameters in the API's URL configuration value.
1010
- New test builder class allows tests to fluently construct a test JSON API request.
11+
- [#427](https://github.com/cloudcreativity/laravel-json-api/issues/427)
12+
Test requests will now ensure that query parameter values are strings, integers or
13+
floats. This is to ensure that the developer is correctly testing boolean filters.
1114

1215
### Changed
1316
- Minimum PHP version is now `7.2`.

src/Testing/TestBuilder.php

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@
55
use Illuminate\Foundation\Testing\Concerns\MakesHttpRequests;
66
use Illuminate\Support\Arr;
77
use Illuminate\Support\Collection;
8+
use PHPUnit\Framework\Assert;
9+
use PHPUnit\Framework\TestCase;
10+
use function array_walk_recursive;
811
use function collect;
912
use function http_build_query;
1013
use function implode;
14+
use function is_bool;
1115
use function is_null;
16+
use function is_scalar;
1217

13-
class TestBuilder
18+
final class TestBuilder
1419
{
1520

1621
/**
17-
* @var MakesHttpRequests
22+
* @var TestCase|MakesHttpRequests
1823
*/
1924
private $test;
2025

@@ -62,7 +67,7 @@ public function __construct($test)
6267
* @param string $resourceType
6368
* @return $this
6469
*/
65-
public function expects(string $resourceType)
70+
public function expects(string $resourceType): self
6671
{
6772
$this->expectedResourceType = $resourceType;
6873

@@ -75,7 +80,7 @@ public function expects(string $resourceType)
7580
* @param string|null $mediaType
7681
* @return $this
7782
*/
78-
public function accept(?string $mediaType)
83+
public function accept(?string $mediaType): self
7984
{
8085
$this->accept = $mediaType;
8186

@@ -88,7 +93,7 @@ public function accept(?string $mediaType)
8893
* @param string|null $mediaType
8994
* @return $this
9095
*/
91-
public function contentType(?string $mediaType)
96+
public function contentType(?string $mediaType): self
9297
{
9398
$this->contentType = $mediaType;
9499

@@ -101,7 +106,7 @@ public function contentType(?string $mediaType)
101106
* @param iterable $query
102107
* @return $this
103108
*/
104-
public function query(iterable $query)
109+
public function query(iterable $query): self
105110
{
106111
$this->query = collect($query)->merge($query);
107112

@@ -114,7 +119,7 @@ public function query(iterable $query)
114119
* @param string ...$paths
115120
* @return $this
116121
*/
117-
public function includePaths(string ...$paths)
122+
public function includePaths(string ...$paths): self
118123
{
119124
$this->query['include'] = implode(',', $paths);
120125

@@ -128,7 +133,7 @@ public function includePaths(string ...$paths)
128133
* @param string|string[] $fieldNames
129134
* @return $this
130135
*/
131-
public function sparseFields(string $resourceType, $fieldNames)
136+
public function sparseFields(string $resourceType, $fieldNames): self
132137
{
133138
$this->query['fields'] = collect($this->query->get('fields'))
134139
->put($resourceType, implode(',', Arr::wrap($fieldNames)));
@@ -142,7 +147,7 @@ public function sparseFields(string $resourceType, $fieldNames)
142147
* @param iterable $filter
143148
* @return $this
144149
*/
145-
public function filter(iterable $filter)
150+
public function filter(iterable $filter): self
146151
{
147152
$this->query['filter'] = collect($filter);
148153

@@ -155,7 +160,7 @@ public function filter(iterable $filter)
155160
* @param string ...$sort
156161
* @return $this
157162
*/
158-
public function sort(string ...$sort)
163+
public function sort(string ...$sort): self
159164
{
160165
$thi A93C s->query['sort'] = implode(',', $sort);
161166

@@ -168,7 +173,7 @@ public function sort(string ...$sort)
168173
* @param iterable $page
169174
* @return $this
170175
*/
171-
public function page(iterable $page)
176+
public function page(iterable $page): self
172177
{
173178
$this->query['page'] = collect($page);
174179

@@ -181,7 +186,7 @@ public function page(iterable $page)
181186
* @param mixed|null $data
182187
* @return $this
183188
*/
184-
public function data($data)
189+
public function data($data): self
185190
{
186191
if (is_null($data)) {
187192
$this->document->put('data', null);
@@ -199,7 +204,7 @@ public function data($data)
199204
* @param string|null $contentType
200205
* @return $this
201206
*/
202-
public function content($document, string $contentType = null)
207+
public function content($document, string $contentType = null): self
203208
{
204209
$this->document = collect($document);
205210

@@ -217,7 +222,7 @@ public function content($document, string $contentType = null)
217222
* @param iterable $headers
218223
* @return TestResponse
219224
*/
220-
public function get(string $uri, iterable $headers = [])
225+
public function get(string $uri, iterable $headers = []): TestResponse
221226
{
222227
return $this->call('GET', $uri, $headers);
223228
}
@@ -229,7 +234,7 @@ public function get(string $uri, iterable $headers = [])
229234
* @param iterable $headers
230235
* @return TestResponse
231236
*/
232-
public function post(string $uri, iterable $headers = [])
237+
public function post(string $uri, iterable $headers = []): TestResponse
233238
{
234239
return $this->call('POST', $uri, $headers);
235240
}
@@ -241,7 +246,7 @@ public function post(string $uri, iterable $headers = [])
241246
* @param iterable $headers
242247
* @return TestResponse
243248
*/
244-
public function patch(string $uri, iterable $headers = [])
249+
public function patch(string $uri, iterable $headers = []): TestResponse
245250
{
246251
return $this->call('PATCH', $uri, $headers);
247252
}
@@ -253,7 +258,7 @@ public function patch(string $uri, iterable $headers = [])
253258
* @param iterable $headers
254259
* @return TestResponse
255260
*/
256-
public function delete(string $uri, iterable $headers = [])
261+
public function delete(string $uri, iterable $headers = []): TestResponse
257262
{
258263
return $this->call('DELETE', $uri, $headers);
259264
}
@@ -267,7 +272,7 @@ public function delete(string $uri, iterable $headers = [])
267272
public function call(string $method, string $uri, iterable $headers = []): TestResponse
268273
{
269274
if ($this->query->isNotEmpty()) {
270-
$uri .= '?' . http_build_query($this->query->toArray());
275+
$uri .= '?' . $this->buildQuery();
271276
}
272277

273278
$headers = collect([
@@ -288,4 +293,28 @@ public function call(string $method, string $uri, iterable $headers = []): TestR
288293

289294
return $response;
290295
}
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+
}
291320
}

0 commit comments

Comments
 (0)
0