Skip to content

Commit

Permalink
[Tests] Improved BaseNumericValidatorTestCase and FloatValueValidator…
Browse files Browse the repository at this point in the history
…Test

Co-Authored-By: Adam Wójs <[email protected]>
  • Loading branch information
alongosz and adamwojs committed Aug 28, 2024
1 parent f7b774e commit 9a56748
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 59 deletions.
97 changes: 51 additions & 46 deletions tests/lib/FieldType/Validator/BaseNumericValidatorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ abstract public static function providerForConstraintsInitializeSetGet(): iterab
abstract protected function getIncorrectNumericTypeValidationMessage(string $parameterName): string;

/**
* @return list<array{array<string, mixed>, array<string>}>
* @return iterable<string, array{array<string, mixed>, array<string>}>
*/
final public function providerForValidateConstraintsKO(): array
final public function providerForValidateConstraintsKO(): iterable
{
$minNumericValueName = $this->getMinNumericValueName();
$minValueNumericTypeValidationMessage = $this->getIncorrectNumericTypeValidationMessage(
Expand All @@ -65,66 +65,71 @@ final public function providerForValidateConstraintsKO(): array
$maxNumericValueName
);

return [
yield 'invalid min type (bool), max not set' => [
[
[
$minNumericValueName => true,
],
[$minValueNumericTypeValidationMessage],
$minNumericValueName => true,
],
[$minValueNumericTypeValidationMessage],
];

yield 'invalid min type (string), max not set' => [
[
$minNumericValueName => self::WRONG_NUMERIC_MIN_VALUE,
],
[$minValueNumericTypeValidationMessage],
];

yield 'invalid min type (string), valid max' => [
[
$minNumericValueName => self::WRONG_NUMERIC_MIN_VALUE,
$maxNumericValueName => 1234,
],
[$minValueNumericTypeValidationMessage],
];

yield 'valid min, invalid max type (DateTime object)' => [
[
[
$minNumericValueName => self::WRONG_NUMERIC_MIN_VALUE,
],
[$minValueNumericTypeValidationMessage],
$maxNumericValueName => new \DateTime(),
$minNumericValueName => 1234,
],
[$maxValueNumericTypeValidationMessage],
];

yield 'invalid min type (bool), valid max, with a parameter' => [
[
[
$minNumericValueName => self::WRONG_NUMERIC_MIN_VALUE,
$maxNumericValueName => 1234,
],
[$minValueNumericTypeValidationMessage],
$minNumericValueName => true,
$maxNumericValueName => 1234,
],
[$minValueNumericTypeValidationMessage],
[
[
$maxNumericValueName => new \DateTime(),
$minNumericValueName => 1234,
],
[$maxValueNumericTypeValidationMessage],
['%parameter%' => $minNumericValueName],
],
];

yield 'invalid min and max types (strings)' => [
[
[
$minNumericValueName => true,
$maxNumericValueName => 1234,
],
[$minValueNumericTypeValidationMessage],
[
['%parameter%' => $minNumericValueName],
],
$minNumericValueName => self::WRONG_NUMERIC_MIN_VALUE,
$maxNumericValueName => self::WRONG_NUMERIC_MAX_VALUE,
],
[
[
$minNumericValueName => self::WRONG_NUMERIC_MIN_VALUE,
$maxNumericValueName => self::WRONG_NUMERIC_MAX_VALUE,
],
[
$minValueNumericTypeValidationMessage,
$maxValueNumericTypeValidationMessage,
],
$minValueNumericTypeValidationMessage,
$maxValueNumericTypeValidationMessage,
],
];

yield 'unknown parameter' => [
[
[
'brljix' => 12345,
],
[self::UNKNOWN_PARAM_VALIDATION_MESSAGE],
'brljix' => 12345,
],
[self::UNKNOWN_PARAM_VALIDATION_MESSAGE],
];

yield 'unknown parameter, valid min' => [
[
[
$minNumericValueName => 12345,
'brljix' => 12345,
],
[self::UNKNOWN_PARAM_VALIDATION_MESSAGE],
$minNumericValueName => 12345,
'brljix' => 12345,
],
[self::UNKNOWN_PARAM_VALIDATION_MESSAGE],
];
}

Expand Down
42 changes: 29 additions & 13 deletions tests/lib/FieldType/Validator/FloatValueValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
final class FloatValueValidatorTest extends BaseNumericValidatorTestCase
{
private const float MIN_FLOAT_VALUE = 1.4285714285714;
private const float MAX_FLOAT_VALUE = 1.5714285714286;

protected function getValidatorInstance(): Validator
{
return new FloatValueValidator();
Expand All @@ -40,20 +43,20 @@ protected function getMaxNumericValueName(): string

protected function getMinFloatValue(): float
{
return 10 / 7;
return self::MIN_FLOAT_VALUE;
}

protected function getMaxFloatValue(): float
{
return 11 / 7;
return self::MAX_FLOAT_VALUE;
}

public static function providerForConstraintsInitializeSetGet(): iterable
{
yield [
[
'minFloatValue' => 0.5,
'maxFloatValue' => 22 / 7,
'maxFloatValue' => 3.1428571428571,
],
];
}
Expand Down Expand Up @@ -96,8 +99,8 @@ public function testInitializeBadConstraint(): void
public function testValidateCorrectValues(float $value): void
{
$validator = $this->getValidatorInstance();
$validator->minFloatValue = 10 / 7;
$validator->maxFloatValue = 11 / 7;
$validator->minFloatValue = self::MIN_FLOAT_VALUE;
$validator->maxFloatValue = self::MAX_FLOAT_VALUE;
self::assertTrue($validator->validate(new FloatValue($value)));
self::assertSame([], $validator->getMessage());
}
Expand All @@ -108,11 +111,21 @@ public function testValidateCorrectValues(float $value): void
public function providerForValidateOK(): array
{
return [
[100 / 70],
[101 / 70],
[105 / 70],
[109 / 70],
[110 / 70],
[
1.4285714285714286,
],
[
1.4428571428571428,
],
[
1.5,
],
[
1.5571428571428572,
],
[
1.5714285714285714,
],
];
}

Expand All @@ -134,10 +147,13 @@ public function testValidateWrongValues(float $value, string $message): void
public function providerForValidateKO(): array
{
return [
[-10 / 7, strtr(self::VALUE_TOO_LOW_VALIDATION_MESSAGE, [self::SIZE_PARAM => $this->getMinFloatValue()])],
[-self::MIN_FLOAT_VALUE, strtr(
self::VALUE_TOO_LOW_VALIDATION_MESSAGE,
[self::SIZE_PARAM => $this->getMinFloatValue()]
)],
[0, strtr(self::VALUE_TOO_LOW_VALIDATION_MESSAGE, [self::SIZE_PARAM => $this->getMinFloatValue()])],
[99 / 70, strtr(self::VALUE_TOO_LOW_VALIDATION_MESSAGE, [self::SIZE_PARAM => $this->getMinFloatValue()])],
[111 / 70, strtr(self::VALUE_TOO_HIGH_VALIDATION_MESSAGE, [self::SIZE_PARAM => $this->getMaxFloatValue()])],
[1.4142857142857, strtr(self::VALUE_TOO_LOW_VALIDATION_MESSAGE, [self::SIZE_PARAM => $this->getMinFloatValue()])],
[1.5857142857143, strtr(self::VALUE_TOO_HIGH_VALIDATION_MESSAGE, [self::SIZE_PARAM => $this->getMaxFloatValue()])],
];
}

Expand Down

0 comments on commit 9a56748

Please sign in to comment.