Skip to content

Commit

Permalink
fix(parametervalidator): deduplicate messages
Browse files Browse the repository at this point in the history
  • Loading branch information
aegypius committed Jan 4, 2024
1 parent d6ff9ed commit 6d1e03e
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/ParameterValidator/ParameterValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,24 @@ public function validateFilters(string $resourceClass, array $resourceFilters, a

foreach ($filter->getDescription($resourceClass) as $name => $data) {
$collectionFormat = $this->getCollectionFormat($data);
$validatorErrors = [];

// validate simple values
if ($errors = $this->validate($name, $data, $queryParameters)) {
$errorList[] = $errors;
$validatorErrors[] = $errors;

Check warning on line 67 in src/ParameterValidator/ParameterValidator.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/ParameterValidator.php#L67

Added line #L67 was not covered by tests
}

// manipulate query data to validate each value
foreach ($this->iterateValue($name, $queryParameters, $collectionFormat) as $scalarQueryParameters) {
if ($errors = $this->validate($name, $data, $scalarQueryParameters)) {
$errorList[] = $errors;
$validatorErrors[] = $errors;

Check warning on line 73 in src/ParameterValidator/ParameterValidator.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/ParameterValidator.php#L72-L73

Added lines #L72 - L73 were not covered by tests
}
}

if (\count($validatorErrors)) {
// Remove duplicate messages
$errorList[] = array_unique(array_merge(...$validatorErrors));

Check warning on line 79 in src/ParameterValidator/ParameterValidator.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/ParameterValidator.php#L79

Added line #L79 was not covered by tests
}
}
}

Expand Down
107 changes: 107 additions & 0 deletions src/ParameterValidator/Tests/ParameterValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,111 @@ public function testOnKernelRequestWithRequiredFilter(): void

$this->testedInstance->validateFilters(Dummy::class, ['some_filter'], $request);
}

/**
* @dataProvider provideValidateNonScalarsCases
*/
public function testValidateNonScalars(array $request, array $description, string|null $exceptionMessage): void

Check warning on line 134 in src/ParameterValidator/Tests/ParameterValidatorTest.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/Tests/ParameterValidatorTest.php#L134

Added line #L134 was not covered by tests
{
$this->filterLocatorProphecy
->has('some_filter')
->shouldBeCalled()
->willReturn(true);

Check warning on line 139 in src/ParameterValidator/Tests/ParameterValidatorTest.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/Tests/ParameterValidatorTest.php#L136-L139

Added lines #L136 - L139 were not covered by tests

$filterProphecy = $this->prophesize(FilterInterface::class);
$filterProphecy
->getDescription(Dummy::class)
->shouldBeCalled()
->willReturn($description);

Check warning on line 145 in src/ParameterValidator/Tests/ParameterValidatorTest.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/Tests/ParameterValidatorTest.php#L141-L145

Added lines #L141 - L145 were not covered by tests

$this->filterLocatorProphecy
->get('some_filter')
->shouldBeCalled()
->willReturn($filterProphecy->reveal());

Check warning on line 150 in src/ParameterValidator/Tests/ParameterValidatorTest.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/Tests/ParameterValidatorTest.php#L147-L150

Added lines #L147 - L150 were not covered by tests

if (null !== $exceptionMessage) {
$this->expectException(ValidationException::class);
$this->expectExceptionMessageMatches('#^'.preg_quote($exceptionMessage).'$#');

Check warning on line 154 in src/ParameterValidator/Tests/ParameterValidatorTest.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/Tests/ParameterValidatorTest.php#L152-L154

Added lines #L152 - L154 were not covered by tests
}

$this->testedInstance->validateFilters(Dummy::class, ['some_filter'], $request);

Check warning on line 157 in src/ParameterValidator/Tests/ParameterValidatorTest.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/Tests/ParameterValidatorTest.php#L157

Added line #L157 was not covered by tests
}

public function provideValidateNonScalarsCases(): iterable

Check warning on line 160 in src/ParameterValidator/Tests/ParameterValidatorTest.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/Tests/ParameterValidatorTest.php#L160

Added line #L160 was not covered by tests
{
$enum = ['parameter' => [
'openapi' => [
'enum' => ['foo', 'bar'],
],
]];

Check warning on line 166 in src/ParameterValidator/Tests/ParameterValidatorTest.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/Tests/ParameterValidatorTest.php#L162-L166

Added lines #L162 - L166 were not covered by tests

yield 'valid values should not throw' => [
['parameter' => 'bar'], $enum, null,
];

Check warning on line 170 in src/ParameterValidator/Tests/ParameterValidatorTest.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/Tests/ParameterValidatorTest.php#L168-L170

Added lines #L168 - L170 were not covered by tests

yield 'invalid single scalar should still throw' => [
['parameter' => 'baz'], $enum, 'Query parameter "parameter" must be one of "foo, bar"',
];

Check warning on line 174 in src/ParameterValidator/Tests/ParameterValidatorTest.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/Tests/ParameterValidatorTest.php#L172-L174

Added lines #L172 - L174 were not covered by tests

yield 'invalid single value in a non scalar should throw' => [
['parameter' => ['baz']], $enum, 'Query parameter "parameter" must be one of "foo, bar"',
];

Check warning on line 178 in src/ParameterValidator/Tests/ParameterValidatorTest.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/Tests/ParameterValidatorTest.php#L176-L178

Added lines #L176 - L178 were not covered by tests

yield 'multiple invalid values in a non scalar should throw' => [
['parameter' => ['baz', 'boo']], $enum, 'Query parameter "parameter" must be one of "foo, bar"',
];

Check warning on line 182 in src/ParameterValidator/Tests/ParameterValidatorTest.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/Tests/ParameterValidatorTest.php#L180-L182

Added lines #L180 - L182 were not covered by tests

yield 'combination of valid and invalid values should throw' => [
['parameter' => ['foo', 'boo']], $enum, 'Query parameter "parameter" must be one of "foo, bar"',
];

Check warning on line 186 in src/ParameterValidator/Tests/ParameterValidatorTest.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/Tests/ParameterValidatorTest.php#L184-L186

Added lines #L184 - L186 were not covered by tests

yield 'duplicate valid values should throw' => [
['parameter' => ['foo', 'foo']],
['parameter' => [
'openapi' => [
'enum' => ['foo', 'bar'],
'uniqueItems' => true,
],
]],
'Query parameter "parameter" must contain unique values',
];

Check warning on line 197 in src/ParameterValidator/Tests/ParameterValidatorTest.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/Tests/ParameterValidatorTest.php#L188-L197

Added lines #L188 - L197 were not covered by tests

yield 'if less values than allowed is provided it should throw' => [
['parameter' => ['foo']],
['parameter' => [
'openapi' => [
'enum' => ['foo', 'bar'],
'minItems' => 2,
],
]],
'Query parameter "parameter" must contain more than 2 values', // todo: this message does seem accurate
];

Check warning on line 208 in src/ParameterValidator/Tests/ParameterValidatorTest.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/Tests/ParameterValidatorTest.php#L199-L208

Added lines #L199 - L208 were not covered by tests

yield 'if more values than allowed is provided it should throw' => [
['parameter' => ['foo', 'bar', 'baz']],
['parameter' => [
'openapi' => [
'enum' => ['foo', 'bar', 'baz'],
'maxItems' => 2,
],
]],
'Query parameter "parameter" must contain less than 2 values', // todo: this message does seem accurate
];

Check warning on line 219 in src/ParameterValidator/Tests/ParameterValidatorTest.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/Tests/ParameterValidatorTest.php#L210-L219

Added lines #L210 - L219 were not covered by tests

yield 'for array constraints all violation should be reported' => [
['parameter' => ['foo', 'foo', 'bar']],
['parameter' => [
'openapi' => [
'enum' => ['foo', 'bar'],
'uniqueItems' => true,
'minItems' => 1,
'maxItems' => 2,
],
]],
implode(\PHP_EOL, [
'Query parameter "parameter" must contain less than 2 values',
'Query parameter "parameter" must contain unique values',
]),
];

Check warning on line 235 in src/ParameterValidator/Tests/ParameterValidatorTest.php

View check run for this annotation

Codecov / codecov/patch

src/ParameterValidator/Tests/ParameterValidatorTest.php#L221-L235

Added lines #L221 - L235 were not covered by tests
}
}

0 comments on commit 6d1e03e

Please sign in to comment.