From 9a5674807311d38a66422fae7b78a429a18819f5 Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Wed, 28 Aug 2024 12:47:08 +0200 Subject: [PATCH] [Tests] Improved BaseNumericValidatorTestCase and FloatValueValidatorTest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Adam Wójs --- .../BaseNumericValidatorTestCase.php | 97 ++++++++++--------- .../Validator/FloatValueValidatorTest.php | 42 +++++--- 2 files changed, 80 insertions(+), 59 deletions(-) diff --git a/tests/lib/FieldType/Validator/BaseNumericValidatorTestCase.php b/tests/lib/FieldType/Validator/BaseNumericValidatorTestCase.php index 0c499a3ff8..a664bfc87c 100644 --- a/tests/lib/FieldType/Validator/BaseNumericValidatorTestCase.php +++ b/tests/lib/FieldType/Validator/BaseNumericValidatorTestCase.php @@ -52,9 +52,9 @@ abstract public static function providerForConstraintsInitializeSetGet(): iterab abstract protected function getIncorrectNumericTypeValidationMessage(string $parameterName): string; /** - * @return list, array}> + * @return iterable, array}> */ - final public function providerForValidateConstraintsKO(): array + final public function providerForValidateConstraintsKO(): iterable { $minNumericValueName = $this->getMinNumericValueName(); $minValueNumericTypeValidationMessage = $this->getIncorrectNumericTypeValidationMessage( @@ -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], ]; } diff --git a/tests/lib/FieldType/Validator/FloatValueValidatorTest.php b/tests/lib/FieldType/Validator/FloatValueValidatorTest.php index 67c2bfbb40..92b8db737c 100644 --- a/tests/lib/FieldType/Validator/FloatValueValidatorTest.php +++ b/tests/lib/FieldType/Validator/FloatValueValidatorTest.php @@ -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(); @@ -40,12 +43,12 @@ 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 @@ -53,7 +56,7 @@ public static function providerForConstraintsInitializeSetGet(): iterable yield [ [ 'minFloatValue' => 0.5, - 'maxFloatValue' => 22 / 7, + 'maxFloatValue' => 3.1428571428571, ], ]; } @@ -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()); } @@ -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, + ], ]; } @@ -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()])], ]; }