Skip to content

Commit

Permalink
add unit tests for each Validators
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Jun 21, 2018
1 parent 0de3f9c commit c7fffe6
Show file tree
Hide file tree
Showing 17 changed files with 929 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Filter/QueryParameterValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function validateFilters(string $resourceClass, array $resourceFilters, R

foreach ($filter->getDescription($resourceClass) as $name => $data) {
foreach ($this->validators as $validator) {
$errorList = array_merge($errorList, $validator->validate($name, $data, $request));
$errorList = \array_merge($errorList, $validator->validate($name, $data, $request));
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/Filter/Validator/ArrayItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ public function validate(string $name, array $filterDescription, Request $reques
$errorList = [];

$value = $this->getValue($name, $filterDescription, $request);
$nbItems = count($value);
$nbItems = \count($value);

if (null !== $maxItems && $nbItems > $maxItems) {
$errorList[] = sprintf('Query parameter "%s" must contain less than %d values', $name, $maxItems);
$errorList[] = \sprintf('Query parameter "%s" must contain less than %d values', $name, $maxItems);
}

if (null !== $minItems && $nbItems < $minItems) {
$errorList[] = sprintf('Query parameter "%s" must contain more than %d values', $name, $minItems);
$errorList[] = \sprintf('Query parameter "%s" must contain more than %d values', $name, $minItems);
}

if (true === $uniqueItems && $nbItems > count(array_unique($value))) {
$errorList[] = sprintf('Query parameter "%s" must contain unique values', $name);
if (true === $uniqueItems && $nbItems > \count(array_unique($value))) {
$errorList[] = \sprintf('Query parameter "%s" must contain unique values', $name);
}

return $errorList;
Expand All @@ -55,7 +55,7 @@ private function getValue(string $name, array $filterDescription, Request $reque
return [];
}

if (is_array($value)) {
if (\is_array($value)) {
return $value;
}

Expand All @@ -76,7 +76,7 @@ private static function getSeparator(string $collectionFormat): string
case 'pipes':
return '|';
default:
throw new \InvalidArgumentException(sprintf('Unkwown collection format %s', $collectionFormat));
throw new \InvalidArgumentException(sprintf('Unknown collection format %s', $collectionFormat));
}
}
}
8 changes: 4 additions & 4 deletions src/Filter/Validator/Bounds.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ public function validate(string $name, array $filterDescription, Request $reques

if (null !== $maximum) {
if (($filterDescription['swagger']['exclusiveMaximum'] ?? false) && $value >= $maximum) {
$errorList[] = sprintf('Query parameter "%s" must be less than %s', $name, $maximum);
$errorList[] = \sprintf('Query parameter "%s" must be less than %s', $name, $maximum);
} elseif ($value > $maximum) {
$errorList[] = sprintf('Query parameter "%s" must be less than or equal to %s', $name, $maximum);
$errorList[] = \sprintf('Query parameter "%s" must be less than or equal to %s', $name, $maximum);
}
}

if (null !== $minimum) {
if (($filterDescription['swagger']['exclusiveMinimum'] ?? false) && $value <= $minimum) {
$errorList[] = sprintf('Query parameter "%s" must be greater than %s', $name, $minimum);
$errorList[] = \sprintf('Query parameter "%s" must be greater than %s', $name, $minimum);
} elseif ($value < $minimum) {
$errorList[] = sprintf('Query parameter "%s" must be greater than or equal to %s', $name, $minimum);
$errorList[] = \sprintf('Query parameter "%s" must be greater than or equal to %s', $name, $minimum);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Filter/Validator/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ class Enum implements ValidatorInterface
public function validate(string $name, array $filterDescription, Request $request): array
{
$value = $request->query->get($name);
if (empty($value) && '0' !== $value || !is_string($value)) {
if (empty($value) && '0' !== $value || !\is_string($value)) {
return [];
}

$enum = $filterDescription['swagger']['enum'] ?? null;

if (null !== $enum && !in_array($value, $enum, true)) {
if (null !== $enum && !\in_array($value, $enum, true)) {
return [
sprintf('Query parameter "%s" must be one of "%s"', $name, implode(', ', $enum)),
\sprintf('Query parameter "%s" must be one of "%s"', $name, implode(', ', $enum)),
];
}

Expand Down
6 changes: 3 additions & 3 deletions src/Filter/Validator/Length.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ public function validate(string $name, array $filterDescription, Request $reques
$minLength = $filterDescription['swagger']['minLength'] ?? null;

$value = $request->query->get($name);
if (empty($value) && '0' !== $value || !is_string($value)) {
if (empty($value) && '0' !== $value || !\is_string($value)) {
return [];
}

$errorList = [];

if (null !== $maxLength && mb_strlen($value) > $maxLength) {
$errorList[] = sprintf('Query parameter "%s" length must be lower than or equal to %s', $name, $maxLength);
$errorList[] = \sprintf('Query parameter "%s" length must be lower than or equal to %s', $name, $maxLength);
}

if (null !== $minLength && mb_strlen($value) < $minLength) {
$errorList[] = sprintf('Query parameter "%s" length must be greater than or equal to %s', $name, $minLength);
$errorList[] = \sprintf('Query parameter "%s" length must be greater than or equal to %s', $name, $minLength);
}

return $errorList;
Expand Down
4 changes: 2 additions & 2 deletions src/Filter/Validator/MultipleOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ class MultipleOf implements ValidatorInterface
public function validate(string $name, array $filterDescription, Request $request): array
{
$value = $request->query->get($name);
if (empty($value) && '0' !== $value || !is_string($value)) {
if (empty($value) && '0' !== $value || !\is_string($value)) {
return [];
}

$multipleOf = $filterDescription['swagger']['multipleOf'] ?? null;

if (null !== $multipleOf && 0 !== ($value % $multipleOf)) {
return [
sprintf('Query parameter "%s" must multiple of %s', $name, $multipleOf),
\sprintf('Query parameter "%s" must multiple of %s', $name, $multipleOf),
];
}

Expand Down
4 changes: 2 additions & 2 deletions src/Filter/Validator/Pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ class Pattern implements ValidatorInterface
public function validate(string $name, array $filterDescription, Request $request): array
{
$value = $request->query->get($name);
if (empty($value) && '0' !== $value || !is_string($value)) {
if (empty($value) && '0' !== $value || !\is_string($value)) {
return [];
}

$pattern = $filterDescription['swagger']['pattern'] ?? null;

if (null !== $pattern && !preg_match($pattern, $value)) {
return [
sprintf('Query parameter "%s" must match pattern %s', $name, $pattern),
\sprintf('Query parameter "%s" must match pattern %s', $name, $pattern),
];
}

Expand Down
18 changes: 9 additions & 9 deletions src/Filter/Validator/Required.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public function validate(string $name, array $filterDescription, Request $reques
// if query param is not given, then break
if (!$this->requestHasQueryParameter($request, $name)) {
return [
sprintf('Query parameter "%s" is required', $name),
\sprintf('Query parameter "%s" is required', $name),
];
}

// if query param is empty and the configuration does not allow it
if (!($filterDescription['swagger']['allowEmptyValue'] ?? false) && empty($this->requestGetQueryParameter($request, $name))) {
return [
sprintf('Query parameter "%s" does not allow empty value', $name),
\sprintf('Query parameter "%s" does not allow empty value', $name),
];
}

Expand All @@ -52,13 +52,13 @@ private function requestHasQueryParameter(Request $request, string $name): bool
return false;
}

$rootName = array_keys($matches)[0] ?? '';
$rootName = \array_keys($matches)[0] ?? '';
if (!$rootName) {
return false;
}

if (\is_array($matches[$rootName])) {
$keyName = array_keys($matches[$rootName])[0];
$keyName = \array_keys($matches[$rootName])[0];

$queryParameter = $request->query->get($rootName);

Expand All @@ -74,22 +74,22 @@ private function requestHasQueryParameter(Request $request, string $name): bool
private function requestGetQueryParameter(Request $request, string $name)
{
$matches = [];
parse_str($name, $matches);
\parse_str($name, $matches);
if (empty($matches)) {
return null;
}

$rootName = array_keys($matches)[0] ?? '';
$rootName = \array_keys($matches)[0] ?? '';
if (!$rootName) {
return null;
}

if (is_array($matches[$rootName])) {
$keyName = array_keys($matches[$rootName])[0];
if (\is_array($matches[$rootName])) {
$keyName = \array_keys($matches[$rootName])[0];

$queryParameter = $request->query->get($rootName);

if (is_array($queryParameter) && isset($queryParameter[$keyName])) {
if (\is_array($queryParameter) && isset($queryParameter[$keyName])) {
return $queryParameter[$keyName];
}

Expand Down
4 changes: 1 addition & 3 deletions tests/EventListener/QueryParameterValidateListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Filter;
namespace ApiPlatform\Core\Tests\EventListener;

use ApiPlatform\Core\Api\FilterInterface;
use ApiPlatform\Core\EventListener\QueryParameterValidateListener;
use ApiPlatform\Core\Exception\FilterValidationException;
use ApiPlatform\Core\Filter\QueryParameterValidator;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

Expand Down
18 changes: 15 additions & 3 deletions tests/Filter/QueryParameterValidatorTest.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
<?php

namespace ApiPlatform\Core\Test\Filter;
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Filter;

use ApiPlatform\Core\Api\FilterInterface;
use ApiPlatform\Core\Exception\FilterValidationException;
use ApiPlatform\Core\Filter\QueryParameterValidator;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

/**
* Class QueryParameterValidatorTest
* Class QueryParameterValidatorTest.
*
* @author Julien Deniau <julien.deniau@mapado.com>
*/
class QueryParameterValidatorTest extends TestCase
Expand Down

0 comments on commit c7fffe6

Please sign in to comment.