Skip to content

Commit 853243c

Browse files
Merge pull request #77 from ppaulis/1.10.x
#75 fixed broken validation of page_size parameter
2 parents 98513d4 + 8999bc2 commit 853243c

File tree

2 files changed

+104
-76
lines changed

2 files changed

+104
-76
lines changed

src/InputFilter/RestService/PatchInputFilter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ public function init()
7575
'continue_if_empty' => true,
7676
'validators' => [
7777
new CallbackValidator(function ($value) {
78-
if (is_numeric($value) && intval($value) !== $value) {
78+
// Make sure that the value is a numeric int or string and that there are no decimals
79+
if (! is_numeric($value) || ((string) $value) !== (string) (int) $value) {
7980
return false;
8081
}
8182

test/InputFilter/RestService/PatchInputFilterTest.php

Lines changed: 102 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -24,82 +24,14 @@ public function getInputFilter(): PatchInputFilter
2424
]);
2525
}
2626

27-
/** @psalm-return array<string, array{0: array<string, mixed>}> */
27+
/** @psalm-return array<string, array{0: string|int}> */
2828
public function dataProviderIsValidTrue(): array
2929
{
3030
return [
31-
'all-inputs-present' => [
32-
[
33-
'accept_whitelist' => [
34-
0 => 'application/vnd.foo_bar.v1+json',
35-
1 => 'application/hal+json',
36-
2 => 'application/json',
37-
],
38-
'collection_class' => Paginator::class,
39-
'collection_http_methods' => [
40-
0 => 'GET',
41-
1 => 'POST',
42-
],
43-
'collection_name' => 'foo_bar',
44-
'collection_query_whitelist' => [],
45-
'content_type_whitelist' => [
46-
0 => 'application/vnd.foo_bar.v1+json',
47-
1 => 'application/json',
48-
],
49-
'entity_class' => 'StdClass',
50-
'entity_http_methods' => [
51-
0 => 'GET',
52-
1 => 'PATCH',
53-
2 => 'PUT',
54-
3 => 'DELETE',
55-
],
56-
'entity_identifier_name' => 'id',
57-
'hydrator_name' => ArraySerializableHydrator::class,
58-
'page_size' => 25,
59-
'page_size_param' => null,
60-
'resource_class' => 'Foo_Bar\\V1\\Rest\\Baz_Bat\\Baz_BatResource',
61-
'route_identifier_name' => 'foo_bar_id',
62-
'route_match' => '/foo_bar[/:foo_bar_id]',
63-
'selector' => 'HalJson',
64-
'service_name' => 'Baz_Bat',
65-
],
66-
],
67-
'page_size-negative' => [
68-
[
69-
'accept_whitelist' => [
70-
0 => 'application/vnd.foo_bar.v1+json',
71-
1 => 'application/hal+json',
72-
2 => 'application/json',
73-
],
74-
'collection_class' => Paginator::class,
75-
'collection_http_methods' => [
76-
0 => 'GET',
77-
1 => 'POST',
78-
],
79-
'collection_name' => 'foo_bar',
80-
'collection_query_whitelist' => [],
81-
'content_type_whitelist' => [
82-
0 => 'application/vnd.foo_bar.v1+json',
83-
1 => 'application/json',
84-
],
85-
'entity_class' => 'StdClass',
86-
'entity_http_methods' => [
87-
0 => 'GET',
88-
1 => 'PATCH',
89-
2 => 'PUT',
90-
3 => 'DELETE',
91-
],
92-
'entity_identifier_name' => 'id',
93-
'hydrator_name' => ArraySerializableHydrator::class,
94-
'page_size' => -1,
95-
'page_size_param' => null,
96-
'resource_class' => 'Foo_Bar\\V1\\Rest\\Baz_Bat\\Baz_BatResource',
97-
'route_identifier_name' => 'foo_bar_id',
98-
'route_match' => '/foo_bar[/:foo_bar_id]',
99-
'selector' => 'HalJson',
100-
'service_name' => 'Baz_Bat',
101-
],
102-
],
31+
'page_size-string' => ['25'],
32+
'page_size-string-negative' => ['-1'],
33+
'page_size-integer' => [25],
34+
'page_size-integer-negative' => [-1],
10335
];
10436
}
10537

@@ -198,10 +130,46 @@ public function dataProviderIsValidFalse(): array
198130

199131
/**
200132
* @dataProvider dataProviderIsValidTrue
201-
* @param array<string, mixed> $data
133+
* @param string|int $pageSize
202134
*/
203-
public function testIsValidTrue(array $data): void
135+
public function testIsValidTrue($pageSize): void
204136
{
137+
$data =
138+
[
139+
'accept_whitelist' => [
140+
0 => 'application/vnd.foo_bar.v1+json',
141+
1 => 'application/hal+json',
142+
2 => 'application/json',
143+
],
144+
'collection_class' => Paginator::class,
145+
'collection_http_methods' => [
146+
0 => 'GET',
147+
1 => 'POST',
148+
],
149+
'collection_name' => 'foo_bar',
150+
'collection_query_whitelist' => [],
151+
'content_type_whitelist' => [
152+
0 => 'application/vnd.foo_bar.v1+json',
153+
1 => 'application/json',
154+
],
155+
'entity_class' => 'StdClass',
156+
'entity_http_methods' => [
157+
0 => 'GET',
158+
1 => 'PATCH',
159+
2 => 'PUT',
160+
3 => 'DELETE',
161+
],
162+
'entity_identifier_name' => 'id',
163+
'hydrator_name' => ArraySerializableHydrator::class,
164+
'page_size' => $pageSize,
165+
'page_size_param' => null,
166+
'resource_class' => 'Foo_Bar\\V1\\Rest\\Baz_Bat\\Baz_BatResource',
167+
'route_identifier_name' => 'foo_bar_id',
168+
'route_match' => '/foo_bar[/:foo_bar_id]',
169+
'selector' => 'HalJson',
170+
'service_name' => 'Baz_Bat',
171+
];
172+
205173
$filter = $this->getInputFilter();
206174
$filter->setData($data);
207175
self::assertTrue($filter->isValid(), var_export($filter->getMessages(), true));
@@ -224,4 +192,63 @@ public function testIsValidFalse(array $data, array $expectedInvalidKeys): void
224192
sort($testKeys);
225193
self::assertEquals($expectedInvalidKeys, $testKeys);
226194
}
195+
196+
/** @psalm-return array<string, array{0: string|int|float}> */
197+
public function dataProviderInvalidPageSizes(): array
198+
{
199+
return [
200+
'page_size-string-float' => ['25.5'],
201+
'page_size-string-wrong-negative' => ['-2'],
202+
'page_size-string-nan' => ['invalid'],
203+
'page_size-float' => [25.5],
204+
'page_size-integer-wrong-negative' => [-2],
205+
];
206+
}
207+
208+
/**
209+
* @dataProvider dataProviderInvalidPageSizes
210+
* @param string|int|float $pageSize
211+
*/
212+
public function testInvalidPageSizes($pageSize): void
213+
{
214+
$data =
215+
[
216+
'accept_whitelist' => [
217+
0 => 'application/vnd.foo_bar.v1+json',
218+
1 => 'application/hal+json',
219+
2 => 'application/json',
220+
],
221+
'collection_class' => Paginator::class,
222+
'collection_http_methods' => [
223+
0 => 'GET',
224+
1 => 'POST',
225+
],
226+
'collection_name' => 'foo_bar',
227+
'collection_query_whitelist' => [],
228+
'content_type_whitelist' => [
229+
0 => 'application/vnd.foo_bar.v1+json',
230+
1 => 'application/json',
231+
],
232+
'entity_class' => 'StdClass',
233+
'entity_http_methods' => [
234+
0 => 'GET',
235+
1 => 'PATCH',
236+
2 => 'PUT',
237+
3 => 'DELETE',
238+
],
239+
'entity_identifier_name' => 'id',
240+
'hydrator_name' => ArraySerializableHydrator::class,
241+
'page_size' => $pageSize,
242+
'page_size_param' => null,
243+
'resource_class' => 'Foo_Bar\\V1\\Rest\\Baz_Bat\\Baz_BatResource',
244+
'route_identifier_name' => 'foo_bar_id',
245+
'route_match' => '/foo_bar[/:foo_bar_id]',
246+
'selector' => 'HalJson',
247+
'service_name' => 'Baz_Bat',
248+
];
249+
250+
$filter = $this->getInputFilter();
251+
$filter->setData($data);
252+
self::assertFalse($filter->isValid(), var_export($filter->getMessages(), true));
253+
}
227254
}

0 commit comments

Comments
 (0)