Skip to content

Commit

Permalink
Merge pull request laminas-api-tools#77 from ppaulis/1.10.x
Browse files Browse the repository at this point in the history
laminas-api-tools#75 fixed broken validation of page_size parameter
  • Loading branch information
weierophinney authored Jan 14, 2022
2 parents 98513d4 + 8999bc2 commit 853243c
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 76 deletions.
3 changes: 2 additions & 1 deletion src/InputFilter/RestService/PatchInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public function init()
'continue_if_empty' => true,
'validators' => [
new CallbackValidator(function ($value) {
if (is_numeric($value) && intval($value) !== $value) {
// Make sure that the value is a numeric int or string and that there are no decimals
if (! is_numeric($value) || ((string) $value) !== (string) (int) $value) {
return false;
}

Expand Down
177 changes: 102 additions & 75 deletions test/InputFilter/RestService/PatchInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,82 +24,14 @@ public function getInputFilter(): PatchInputFilter
]);
}

/** @psalm-return array<string, array{0: array<string, mixed>}> */
/** @psalm-return array<string, array{0: string|int}> */
public function dataProviderIsValidTrue(): array
{
return [
'all-inputs-present' => [
[
'accept_whitelist' => [
0 => 'application/vnd.foo_bar.v1+json',
1 => 'application/hal+json',
2 => 'application/json',
],
'collection_class' => Paginator::class,
'collection_http_methods' => [
0 => 'GET',
1 => 'POST',
],
'collection_name' => 'foo_bar',
'collection_query_whitelist' => [],
'content_type_whitelist' => [
0 => 'application/vnd.foo_bar.v1+json',
1 => 'application/json',
],
'entity_class' => 'StdClass',
'entity_http_methods' => [
0 => 'GET',
1 => 'PATCH',
2 => 'PUT',
3 => 'DELETE',
],
'entity_identifier_name' => 'id',
'hydrator_name' => ArraySerializableHydrator::class,
'page_size' => 25,
'page_size_param' => null,
'resource_class' => 'Foo_Bar\\V1\\Rest\\Baz_Bat\\Baz_BatResource',
'route_identifier_name' => 'foo_bar_id',
'route_match' => '/foo_bar[/:foo_bar_id]',
'selector' => 'HalJson',
'service_name' => 'Baz_Bat',
],
],
'page_size-negative' => [
[
'accept_whitelist' => [
0 => 'application/vnd.foo_bar.v1+json',
1 => 'application/hal+json',
2 => 'application/json',
],
'collection_class' => Paginator::class,
'collection_http_methods' => [
0 => 'GET',
1 => 'POST',
],
'collection_name' => 'foo_bar',
'collection_query_whitelist' => [],
'content_type_whitelist' => [
0 => 'application/vnd.foo_bar.v1+json',
1 => 'application/json',
],
'entity_class' => 'StdClass',
'entity_http_methods' => [
0 => 'GET',
1 => 'PATCH',
2 => 'PUT',
3 => 'DELETE',
],
'entity_identifier_name' => 'id',
'hydrator_name' => ArraySerializableHydrator::class,
'page_size' => -1,
'page_size_param' => null,
'resource_class' => 'Foo_Bar\\V1\\Rest\\Baz_Bat\\Baz_BatResource',
'route_identifier_name' => 'foo_bar_id',
'route_match' => '/foo_bar[/:foo_bar_id]',
'selector' => 'HalJson',
'service_name' => 'Baz_Bat',
],
],
'page_size-string' => ['25'],
'page_size-string-negative' => ['-1'],
'page_size-integer' => [25],
'page_size-integer-negative' => [-1],
];
}

Expand Down Expand Up @@ -198,10 +130,46 @@ public function dataProviderIsValidFalse(): array

/**
* @dataProvider dataProviderIsValidTrue
* @param array<string, mixed> $data
* @param string|int $pageSize
*/
public function testIsValidTrue(array $data): void
public function testIsValidTrue($pageSize): void
{
$data =
[
'accept_whitelist' => [
0 => 'application/vnd.foo_bar.v1+json',
1 => 'application/hal+json',
2 => 'application/json',
],
'collection_class' => Paginator::class,
'collection_http_methods' => [
0 => 'GET',
1 => 'POST',
],
'collection_name' => 'foo_bar',
'collection_query_whitelist' => [],
'content_type_whitelist' => [
0 => 'application/vnd.foo_bar.v1+json',
1 => 'application/json',
],
'entity_class' => 'StdClass',
'entity_http_methods' => [
0 => 'GET',
1 => 'PATCH',
2 => 'PUT',
3 => 'DELETE',
],
'entity_identifier_name' => 'id',
'hydrator_name' => ArraySerializableHydrator::class,
'page_size' => $pageSize,
'page_size_param' => null,
'resource_class' => 'Foo_Bar\\V1\\Rest\\Baz_Bat\\Baz_BatResource',
'route_identifier_name' => 'foo_bar_id',
'route_match' => '/foo_bar[/:foo_bar_id]',
'selector' => 'HalJson',
'service_name' => 'Baz_Bat',
];

$filter = $this->getInputFilter();
$filter->setData($data);
self::assertTrue($filter->isValid(), var_export($filter->getMessages(), true));
Expand All @@ -224,4 +192,63 @@ public function testIsValidFalse(array $data, array $expectedInvalidKeys): void
sort($testKeys);
self::assertEquals($expectedInvalidKeys, $testKeys);
}

/** @psalm-return array<string, array{0: string|int|float}> */
public function dataProviderInvalidPageSizes(): array
{
return [
'page_size-string-float' => ['25.5'],
'page_size-string-wrong-negative' => ['-2'],
'page_size-string-nan' => ['invalid'],
'page_size-float' => [25.5],
'page_size-integer-wrong-negative' => [-2],
];
}

/**
* @dataProvider dataProviderInvalidPageSizes
* @param string|int|float $pageSize
*/
public function testInvalidPageSizes($pageSize): void
{
$data =
[
'accept_whitelist' => [
0 => 'application/vnd.foo_bar.v1+json',
1 => 'application/hal+json',
2 => 'application/json',
],
'collection_class' => Paginator::class,
'collection_http_methods' => [
0 => 'GET',
1 => 'POST',
],
'collection_name' => 'foo_bar',
'collection_query_whitelist' => [],
'content_type_whitelist' => [
0 => 'application/vnd.foo_bar.v1+json',
1 => 'application/json',
],
'entity_class' => 'StdClass',
'entity_http_methods' => [
0 => 'GET',
1 => 'PATCH',
2 => 'PUT',
3 => 'DELETE',
],
'entity_identifier_name' => 'id',
'hydrator_name' => ArraySerializableHydrator::class,
'page_size' => $pageSize,
'page_size_param' => null,
'resource_class' => 'Foo_Bar\\V1\\Rest\\Baz_Bat\\Baz_BatResource',
'route_identifier_name' => 'foo_bar_id',
'route_match' => '/foo_bar[/:foo_bar_id]',
'selector' => 'HalJson',
'service_name' => 'Baz_Bat',
];

$filter = $this->getInputFilter();
$filter->setData($data);
self::assertFalse($filter->isValid(), var_export($filter->getMessages(), true));
}
}

0 comments on commit 853243c

Please sign in to comment.