From 090f1eac739d5ba17ee82450d2ca68da534c996c Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Tue, 2 Jul 2024 15:03:18 +0200 Subject: [PATCH 01/10] Refactored Float and Integer field types to external validators --- src/lib/FieldType/BaseNumericType.php | 83 ++++++++++ src/lib/FieldType/Float/Type.php | 130 ++-------------- src/lib/FieldType/Integer/Type.php | 143 ++---------------- src/lib/FieldType/Validator.php | 2 +- .../Validator/BaseNumericValidator.php | 39 +++++ .../Validator/FloatValueValidator.php | 48 ++---- .../Validator/IntegerValueValidator.php | 52 ++----- 7 files changed, 175 insertions(+), 322 deletions(-) create mode 100644 src/lib/FieldType/BaseNumericType.php create mode 100644 src/lib/FieldType/Validator/BaseNumericValidator.php diff --git a/src/lib/FieldType/BaseNumericType.php b/src/lib/FieldType/BaseNumericType.php new file mode 100644 index 0000000000..18f997eed2 --- /dev/null +++ b/src/lib/FieldType/BaseNumericType.php @@ -0,0 +1,83 @@ + + */ + abstract protected function getValidators(): array; + + public function getValidator(string $validatorIdentifier): ?Validator + { + return $this->getValidators()[$validatorIdentifier] ?? null; + } + + /** + * Validates the validatorConfiguration of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct. + * + * @param array $validatorConfiguration + * + * @return \Ibexa\Contracts\Core\FieldType\ValidationError[] + */ + public function validateValidatorConfiguration($validatorConfiguration): array + { + $validationErrors = []; + $validatorValidationErrors = []; + foreach ($validatorConfiguration as $validatorIdentifier => $constraints) { + $validator = $this->getValidator($validatorIdentifier); + if (null === $validator) { + $validationErrors[] = new ValidationError( + "Validator '%validator%' is unknown", + null, + [ + '%validator%' => $validatorIdentifier, + ], + "[$validatorIdentifier]" + ); + + continue; + } + + $validatorValidationErrors[] = $validator->validateConstraints($constraints); + } + + return array_merge($validationErrors, ...$validatorValidationErrors); + } + + /** + * Validates a field based on the validators in the field definition. + * + * @return \Ibexa\Contracts\Core\FieldType\ValidationError[] + * + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\PropertyNotFoundException + */ + public function validate(FieldDefinition $fieldDefinition, SPIValue $value): array + { + if ($this->isEmptyValue($value)) { + return []; + } + + $errors = []; + $validatorConfiguration = $fieldDefinition->getValidatorConfiguration(); + foreach ($this->getValidators() as $validatorIdentifier => $validator) { + $validator->initializeWithConstraints($validatorConfiguration[$validatorIdentifier] ?? []); + if (!$validator->validate($value, $fieldDefinition)) { + $errors[] = $validator->getMessage(); + } + } + + return array_merge(...$errors); + } +} diff --git a/src/lib/FieldType/Float/Type.php b/src/lib/FieldType/Float/Type.php index 9551224c57..06e4198698 100644 --- a/src/lib/FieldType/Float/Type.php +++ b/src/lib/FieldType/Float/Type.php @@ -10,18 +10,17 @@ use Ibexa\Contracts\Core\FieldType\Value as SPIValue; use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition; use Ibexa\Core\Base\Exceptions\InvalidArgumentType; -use Ibexa\Core\FieldType\FieldType; -use Ibexa\Core\FieldType\ValidationError; +use Ibexa\Core\FieldType\BaseNumericType; +use Ibexa\Core\FieldType\Validator; use Ibexa\Core\FieldType\Value as BaseValue; use JMS\TranslationBundle\Model\Message; -use JMS\TranslationBundle\Translation\TranslationContainerInterface; /** * Float field types. * * Represents floats. */ -class Type extends FieldType implements TranslationContainerInterface +class Type extends BaseNumericType { protected $validatorConfigurationSchema = [ 'FloatValueValidator' => [ @@ -36,112 +35,9 @@ class Type extends FieldType implements TranslationContainerInterface ], ]; - /** - * Validates the validatorConfiguration of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct. - * - * @param mixed $validatorConfiguration - * - * @return \Ibexa\Contracts\Core\FieldType\ValidationError[] - */ - public function validateValidatorConfiguration($validatorConfiguration) - { - $validationErrors = []; - - foreach ($validatorConfiguration as $validatorIdentifier => $constraints) { - if ($validatorIdentifier !== 'FloatValueValidator') { - $validationErrors[] = new ValidationError( - "Validator '%validator%' is unknown", - null, - [ - '%validator%' => $validatorIdentifier, - ], - "[$validatorIdentifier]" - ); - - continue; - } - - foreach ($constraints as $name => $value) { - switch ($name) { - case 'minFloatValue': - case 'maxFloatValue': - if ($value !== null && !is_numeric($value)) { - $validationErrors[] = new ValidationError( - "Validator parameter '%parameter%' value must be of numeric type", - null, - [ - '%parameter%' => $name, - ], - "[$validatorIdentifier][$name]" - ); - } - break; - default: - $validationErrors[] = new ValidationError( - "Validator parameter '%parameter%' is unknown", - null, - [ - '%parameter%' => $name, - ], - "[$validatorIdentifier][$name]" - ); - } - } - } - - return $validationErrors; - } - - /** - * Validates a field based on the validators in the field definition. - * - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException - * - * @param \Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition $fieldDefinition The field definition of the field - * @param \Ibexa\Core\FieldType\Float\Value $fieldValue The field value for which an action is performed - * - * @return \Ibexa\Contracts\Core\FieldType\ValidationError[] - */ - public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue) + protected function getValidators(): array { - $validationErrors = []; - - if ($this->isEmptyValue($fieldValue)) { - return $validationErrors; - } - - $validatorConfiguration = $fieldDefinition->getValidatorConfiguration(); - $constraints = isset($validatorConfiguration['FloatValueValidator']) ? - $validatorConfiguration['FloatValueValidator'] : - []; - - $validationErrors = []; - - if (isset($constraints['maxFloatValue']) && - $constraints['maxFloatValue'] !== null && $fieldValue->value > $constraints['maxFloatValue']) { - $validationErrors[] = new ValidationError( - 'The value can not be higher than %size%.', - null, - [ - '%size%' => $constraints['maxFloatValue'], - ], - 'value' - ); - } - - if (isset($constraints['minFloatValue']) && - $constraints['minFloatValue'] !== null && $fieldValue->value < $constraints['minFloatValue']) { - $validationErrors[] = new ValidationError( - 'The value can not be lower than %size%.', - null, - [ - '%size%' => $constraints['minFloatValue'], - ], - 'value' - ); - } - - return $validationErrors; + return ['FloatValueValidator' => new Validator\FloatValueValidator()]; } /** @@ -165,20 +61,14 @@ public function getName(SPIValue $value, FieldDefinition $fieldDefinition, strin /** * Returns the fallback default value of field type when no such default * value is provided in the field definition in content types. - * - * @return \Ibexa\Core\FieldType\Float\Value */ - public function getEmptyValue() + public function getEmptyValue(): Value { return new Value(); } /** - * Implements the core of {@see isEmptyValue()}. - * - * @param mixed $value - * - * @return bool + * @param \Ibexa\Core\FieldType\Float\Value $value */ public function isEmptyValue(SPIValue $value) { @@ -209,7 +99,7 @@ protected function createValueFromInput($inputValue) * * @param \Ibexa\Core\FieldType\Float\Value $value */ - protected function checkValueStructure(BaseValue $value) + protected function checkValueStructure(BaseValue $value): void { if (!is_float($value->value)) { throw new InvalidArgumentType( @@ -237,7 +127,7 @@ protected function getSortInfo(BaseValue $value) * * @return \Ibexa\Core\FieldType\Float\Value $value */ - public function fromHash($hash) + public function fromHash($hash): Value { if ($hash === null) { return $this->getEmptyValue(); @@ -253,7 +143,7 @@ public function fromHash($hash) * * @return mixed */ - public function toHash(SPIValue $value) + public function toHash(SPIValue $value): mixed { if ($this->isEmptyValue($value)) { return null; diff --git a/src/lib/FieldType/Integer/Type.php b/src/lib/FieldType/Integer/Type.php index 959a410c9a..735cf69db4 100644 --- a/src/lib/FieldType/Integer/Type.php +++ b/src/lib/FieldType/Integer/Type.php @@ -10,18 +10,17 @@ use Ibexa\Contracts\Core\FieldType\Value as SPIValue; use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition; use Ibexa\Core\Base\Exceptions\InvalidArgumentType; -use Ibexa\Core\FieldType\FieldType; -use Ibexa\Core\FieldType\ValidationError; +use Ibexa\Core\FieldType\BaseNumericType; +use Ibexa\Core\FieldType\Validator; use Ibexa\Core\FieldType\Value as BaseValue; use JMS\TranslationBundle\Model\Message; -use JMS\TranslationBundle\Translation\TranslationContainerInterface; /** * Integer field types. * * Represents integers. */ -class Type extends FieldType implements TranslationContainerInterface +class Type extends BaseNumericType { protected $validatorConfigurationSchema = [ 'IntegerValueValidator' => [ @@ -36,115 +35,9 @@ class Type extends FieldType implements TranslationContainerInterface ], ]; - /** - * Validates the validatorConfiguration of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct. - * - * @param mixed $validatorConfiguration - * - * @return \Ibexa\Contracts\Core\FieldType\ValidationError[] - */ - public function validateValidatorConfiguration($validatorConfiguration) + protected function getValidators(): array { - $validationErrors = []; - - foreach ($validatorConfiguration as $validatorIdentifier => $constraints) { - if ($validatorIdentifier !== 'IntegerValueValidator') { - $validationErrors[] = new ValidationError( - "Validator '%validator%' is unknown", - null, - [ - '%validator%' => $validatorIdentifier, - ], - "[$validatorIdentifier]" - ); - - continue; - } - foreach ($constraints as $name => $value) { - switch ($name) { - case 'minIntegerValue': - case 'maxIntegerValue': - if ($value !== null && !is_int($value)) { - $validationErrors[] = new ValidationError( - "Validator parameter '%parameter%' value must be of integer type", - null, - [ - '%parameter%' => $name, - ], - "[$validatorIdentifier][$name]" - ); - } - break; - default: - $validationErrors[] = new ValidationError( - "Validator parameter '%parameter%' is unknown", - null, - [ - '%parameter%' => $name, - ], - "[$validatorIdentifier][$name]" - ); - } - } - } - - return $validationErrors; - } - - /** - * Validates a field based on the validators in the field definition. - * - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException - * - * @param \Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition $fieldDefinition The field definition of the field - * @param \Ibexa\Core\FieldType\Integer\Value $fieldValue The field value for which an action is performed - * - * @return \Ibexa\Contracts\Core\FieldType\ValidationError[] - */ - public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue) - { - $validationErrors = []; - - if ($this->isEmptyValue($fieldValue)) { - return $validationErrors; - } - - $validatorConfiguration = $fieldDefinition->getValidatorConfiguration(); - $constraints = isset($validatorConfiguration['IntegerValueValidator']) ? - $validatorConfiguration['IntegerValueValidator'] : - []; - - $validationErrors = []; - - // 0 and False are unlimited value for maxIntegerValue - if (isset($constraints['maxIntegerValue']) && - $constraints['maxIntegerValue'] !== 0 && - $constraints['maxIntegerValue'] !== false && - $fieldValue->value > $constraints['maxIntegerValue'] - ) { - $validationErrors[] = new ValidationError( - 'The value can not be higher than %size%.', - null, - [ - '%size%' => $constraints['maxIntegerValue'], - ], - 'value' - ); - } - - if (isset($constraints['minIntegerValue']) && - $constraints['minIntegerValue'] !== false && $fieldValue->value < $constraints['minIntegerValue']) { - $validationErrors[] = new ValidationError( - 'The value can not be lower than %size%.', - null, - [ - '%size%' => $constraints['minIntegerValue'], - ], - 'value' - ); - } - - return $validationErrors; + return ['IntegerValueValidator' => new Validator\IntegerValueValidator()]; } /** @@ -158,7 +51,7 @@ public function getFieldTypeIdentifier() } /** - * @param \Ibexa\Core\FieldType\Integer\Value|\Ibexa\Contracts\Core\FieldType\Value $value + * @param \Ibexa\Core\FieldType\Integer\Value $value */ public function getName(SPIValue $value, FieldDefinition $fieldDefinition, string $languageCode): string { @@ -168,10 +61,8 @@ public function getName(SPIValue $value, FieldDefinition $fieldDefinition, strin /** * Returns the fallback default value of field type when no such default * value is provided in the field definition in content types. - * - * @return \Ibexa\Core\FieldType\Integer\Value */ - public function getEmptyValue() + public function getEmptyValue(): Value { return new Value(); } @@ -179,9 +70,7 @@ public function getEmptyValue() /** * Returns if the given $value is considered empty by the field type. * - * @param mixed $value - * - * @return bool + * @param \Ibexa\Core\FieldType\Integer\Value $value */ public function isEmptyValue(SPIValue $value) { @@ -211,7 +100,7 @@ protected function createValueFromInput($inputValue) * * @param \Ibexa\Core\FieldType\Integer\Value $value */ - protected function checkValueStructure(BaseValue $value) + protected function checkValueStructure(BaseValue $value): void { if (!is_int($value->value)) { throw new InvalidArgumentType( @@ -223,7 +112,7 @@ protected function checkValueStructure(BaseValue $value) } /** - * {@inheritdoc} + * @param \Ibexa\Core\FieldType\Integer\Value $value */ protected function getSortInfo(BaseValue $value) { @@ -231,29 +120,27 @@ protected function getSortInfo(BaseValue $value) } /** - * Converts an $hash to the Value defined by the field type. + * Converts a $hash to the Value defined by the field type. * - * @param mixed $hash + * @param int|string|null $hash * * @return \Ibexa\Core\FieldType\Integer\Value $value */ - public function fromHash($hash) + public function fromHash($hash): Value { if ($hash === null) { return $this->getEmptyValue(); } - return new Value($hash); + return new Value((int)$hash); } /** * Converts a $Value to a hash. * * @param \Ibexa\Core\FieldType\Integer\Value $value - * - * @return mixed */ - public function toHash(SPIValue $value) + public function toHash(SPIValue $value): ?int { if ($this->isEmptyValue($value)) { return null; diff --git a/src/lib/FieldType/Validator.php b/src/lib/FieldType/Validator.php index 9d514a9655..225ec981b2 100644 --- a/src/lib/FieldType/Validator.php +++ b/src/lib/FieldType/Validator.php @@ -83,7 +83,7 @@ public function getConstraintsSchema() /** * @param mixed $constraints * - * @return mixed + * @return array<\Ibexa\Contracts\Core\FieldType\ValidationError> */ abstract public function validateConstraints($constraints); diff --git a/src/lib/FieldType/Validator/BaseNumericValidator.php b/src/lib/FieldType/Validator/BaseNumericValidator.php new file mode 100644 index 0000000000..d0741c91b3 --- /dev/null +++ b/src/lib/FieldType/Validator/BaseNumericValidator.php @@ -0,0 +1,39 @@ + $constraints + */ + public function validateConstraints($constraints): array + { + $validationErrors = []; + foreach ($constraints as $name => $value) { + $validationErrorMessage = $this->getConstraintsValidationErrorMessage($name, $value); + if (null !== $validationErrorMessage) { + $validationErrors[] = new ValidationError( + $validationErrorMessage, + null, + [ + '%parameter%' => $name, + ] + ); + } + } + + return $validationErrors; + } +} diff --git a/src/lib/FieldType/Validator/FloatValueValidator.php b/src/lib/FieldType/Validator/FloatValueValidator.php index 579d5e3e9f..98dd2024a9 100644 --- a/src/lib/FieldType/Validator/FloatValueValidator.php +++ b/src/lib/FieldType/Validator/FloatValueValidator.php @@ -21,7 +21,7 @@ * @property float $minFloatValue Minimum value for float. * @property float $maxFloatValue Maximum value for float. */ -class FloatValueValidator extends Validator +class FloatValueValidator extends BaseNumericValidator { protected $constraints = [ 'minFloatValue' => null, @@ -39,36 +39,14 @@ class FloatValueValidator extends Validator ], ]; - public function validateConstraints($constraints) + protected function getConstraintsValidationErrorMessage(string $name, mixed $value): ?string { - $validationErrors = []; - - foreach ($constraints as $name => $value) { - switch ($name) { - case 'minFloatValue': - case 'maxFloatValue': - if ($value !== null && !is_numeric($value)) { - $validationErrors[] = new ValidationError( - "Validator parameter '%parameter%' value must be of numeric type", - null, - [ - '%parameter%' => $name, - ] - ); - } - break; - default: - $validationErrors[] = new ValidationError( - "Validator parameter '%parameter%' is unknown", - null, - [ - '%parameter%' => $name, - ] - ); - } - } - - return $validationErrors; + return match ($name) { + 'minFloatValue', 'maxFloatValue' => $value !== null && !is_numeric($value) + ? "Validator parameter '%parameter%' value must be of numeric type" + : null, + default => "Validator parameter '%parameter%' is unknown", + }; } /** @@ -88,24 +66,26 @@ public function validate(BaseValue $value, ?FieldDefinition $fieldDefinition = n { $isValid = true; - if ($this->constraints['maxFloatValue'] !== null && $value->value > $this->constraints['maxFloatValue']) { + if (isset($this->constraints['maxFloatValue']) && $value->value > $this->constraints['maxFloatValue']) { $this->errors[] = new ValidationError( 'The value can not be higher than %size%.', null, [ '%size%' => $this->constraints['maxFloatValue'], - ] + ], + 'value' ); $isValid = false; } - if ($this->constraints['minFloatValue'] !== null && $value->value < $this->constraints['minFloatValue']) { + if (isset($this->constraints['minFloatValue']) && $value->value < $this->constraints['minFloatValue']) { $this->errors[] = new ValidationError( 'The value can not be lower than %size%.', null, [ '%size%' => $this->constraints['minFloatValue'], - ] + ], + 'value' ); $isValid = false; } diff --git a/src/lib/FieldType/Validator/IntegerValueValidator.php b/src/lib/FieldType/Validator/IntegerValueValidator.php index 2b4edfa944..ad00d42a51 100644 --- a/src/lib/FieldType/Validator/IntegerValueValidator.php +++ b/src/lib/FieldType/Validator/IntegerValueValidator.php @@ -9,8 +9,8 @@ use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition; use Ibexa\Core\FieldType\ValidationError; -use Ibexa\Core\FieldType\Validator; use Ibexa\Core\FieldType\Value as BaseValue; +use function is_int; /** * Validate ranges of integer value. @@ -18,7 +18,7 @@ * @property int $minIntegerValue The minimum allowed integer value. * @property int $maxIntegerValue The maximum allowed integer value. */ -class IntegerValueValidator extends Validator +class IntegerValueValidator extends BaseNumericValidator { protected $constraints = [ 'minIntegerValue' => null, @@ -36,42 +36,14 @@ class IntegerValueValidator extends Validator ], ]; - public function validateConstraints($constraints) + protected function getConstraintsValidationErrorMessage(string $name, mixed $value): ?string { - $validationErrors = []; - foreach ($constraints as $name => $value) { - switch ($name) { - case 'minIntegerValue': - case 'maxIntegerValue': - if ($value === false) { - @trigger_error( - "The IntegerValueValidator constraint value 'false' is deprecated, and will be removed in 7.0. Use 'null' instead.", - E_USER_DEPRECATED - ); - $value = null; - } - if ($value !== null && !is_int($value)) { - $validationErrors[] = new ValidationError( - "Validator parameter '%parameter%' value must be of integer type", - null, - [ - '%parameter%' => $name, - ] - ); - } - break; - default: - $validationErrors[] = new ValidationError( - "Validator parameter '%parameter%' is unknown", - null, - [ - '%parameter%' => $name, - ] - ); - } - } - - return $validationErrors; + return match ($name) { + 'minIntegerValue', 'maxIntegerValue' => $value !== null && !is_int($value) + ? "Validator parameter '%parameter%' value must be of integer type" + : null, + default => "Validator parameter '%parameter%' is unknown", + }; } /** @@ -97,7 +69,8 @@ public function validate(BaseValue $value, ?FieldDefinition $fieldDefinition = n null, [ '%size%' => $this->constraints['maxIntegerValue'], - ] + ], + 'value' ); $isValid = false; } @@ -108,7 +81,8 @@ public function validate(BaseValue $value, ?FieldDefinition $fieldDefinition = n null, [ '%size%' => $this->constraints['minIntegerValue'], - ] + ], + 'value' ); $isValid = false; } From e382bcdd25fafe98343e598c2d7890fbc24ab634 Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Wed, 3 Jul 2024 15:06:39 +0200 Subject: [PATCH 02/10] [Tests] Refactored Float, Integer, and String validator tests --- .../lib/FieldType/FloatValueValidatorTest.php | 190 +++++------- .../FieldType/IntegerValueValidatorTest.php | 196 +++++-------- .../FieldType/StringLengthValidatorTest.php | 274 ++++++++---------- 3 files changed, 254 insertions(+), 406 deletions(-) diff --git a/tests/lib/FieldType/FloatValueValidatorTest.php b/tests/lib/FieldType/FloatValueValidatorTest.php index 4daa6123fe..bd9b66b781 100644 --- a/tests/lib/FieldType/FloatValueValidatorTest.php +++ b/tests/lib/FieldType/FloatValueValidatorTest.php @@ -4,6 +4,7 @@ * @copyright Copyright (C) Ibexa AS. All rights reserved. * @license For full copyright and license information view LICENSE file distributed with this source code. */ +declare(strict_types=1); namespace Ibexa\Tests\Core\FieldType; @@ -16,23 +17,28 @@ use PHPUnit\Framework\TestCase; /** + * @covers \Ibexa\Core\FieldType\Validator\FloatValueValidator + * * @group fieldType * @group validator */ -class FloatValueValidatorTest extends TestCase +final class FloatValueValidatorTest extends TestCase { - /** - * @return float - */ - protected function getMinFloatValue() + private const string VALUE_TOO_LOW_VALIDATION_MESSAGE = 'The value can not be lower than %size%.'; + private const string VALUE_TOO_HIGH_VALIDATION_MESSAGE = 'The value can not be higher than %size%.'; + private const string SIZE_PARAM = '%size%'; + private const string MIN_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE = "Validator parameter 'minFloatValue' value must be of numeric type"; + private const string MAX_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE = "Validator parameter 'maxFloatValue' value must be of numeric type"; + private const string WRONG_MIN_FLOAT_VALUE = 'five thousand bytes'; + private const string WRONG_MAX_FLOAT_VALUE = 'ten billion bytes'; + private const string UNKNOWN_PARAM_VALIDATION_MESSAGE = "Validator parameter 'brljix' is unknown"; + + protected function getMinFloatValue(): float { return 10 / 7; } - /** - * @return float - */ - protected function getMaxFloatValue() + protected function getMaxFloatValue(): float { return 11 / 7; } @@ -40,7 +46,7 @@ protected function getMaxFloatValue() /** * This test ensure an FloatValueValidator can be created. */ - public function testConstructor() + public function testConstructor(): void { self::assertInstanceOf( Validator::class, @@ -49,12 +55,9 @@ public function testConstructor() } /** - * Tests setting and getting constraints. - * - * @covers \Ibexa\Core\FieldType\Validator::initializeWithConstraints - * @covers \Ibexa\Core\FieldType\Validator::__get + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\PropertyNotFoundException */ - public function testConstraintsInitializeGet() + public function testConstraintsInitializeGet(): void { $constraints = [ 'minFloatValue' => 0.5, @@ -70,10 +73,8 @@ public function testConstraintsInitializeGet() /** * Test getting constraints schema. - * - * @covers \Ibexa\Core\FieldType\Validator::getConstraintsSchema */ - public function testGetConstraintsSchema() + public function testGetConstraintsSchema(): void { $constraintsSchema = [ 'minFloatValue' => [ @@ -89,13 +90,7 @@ public function testGetConstraintsSchema() self::assertSame($constraintsSchema, $validator->getConstraintsSchema()); } - /** - * Tests setting and getting constraints. - * - * @covers \Ibexa\Core\FieldType\Validator::__set - * @covers \Ibexa\Core\FieldType\Validator::__get - */ - public function testConstraintsSetGet() + public function testConstraintsSetGet(): void { $constraints = [ 'minFloatValue' => 0.5, @@ -108,59 +103,39 @@ public function testConstraintsSetGet() self::assertSame($constraints['maxFloatValue'], $validator->maxFloatValue); } - /** - * Tests initializing with a wrong constraint. - * - * @covers \Ibexa\Core\FieldType\Validator::initializeWithConstraints - */ - public function testInitializeBadConstraint() + public function testInitializeBadConstraint(): void { - $this->expectException(PropertyNotFoundException::class); - $constraints = [ 'unexisting' => 0, ]; $validator = new FloatValueValidator(); + + $this->expectException(PropertyNotFoundException::class); $validator->initializeWithConstraints( $constraints ); } - /** - * Tests setting a wrong constraint. - * - * @covers \Ibexa\Core\FieldType\Validator::__set - */ - public function testSetBadConstraint() + public function testSetBadConstraint(): void { - $this->expectException(PropertyNotFoundException::class); - $validator = new FloatValueValidator(); + + $this->expectException(PropertyNotFoundException::class); $validator->unexisting = 0; } - /** - * Tests getting a wrong constraint. - * - * @covers \Ibexa\Core\FieldType\Validator::__get - */ - public function testGetBadConstraint() + public function testGetBadConstraint(): void { - $this->expectException(PropertyNotFoundException::class); - $validator = new FloatValueValidator(); + + $this->expectException(PropertyNotFoundException::class); $null = $validator->unexisting; } /** - * Tests validating a correct value. - * * @dataProvider providerForValidateOK - * - * @covers \Ibexa\Core\FieldType\Validator\FloatValueValidator::validate - * @covers \Ibexa\Core\FieldType\Validator::getMessage */ - public function testValidateCorrectValues($value) + public function testValidateCorrectValues(float $value): void { $validator = new FloatValueValidator(); $validator->minFloatValue = 10 / 7; @@ -169,7 +144,10 @@ public function testValidateCorrectValues($value) self::assertSame([], $validator->getMessage()); } - public function providerForValidateOK() + /** + * @return list + */ + public function providerForValidateOK(): array { return [ [100 / 70], @@ -181,13 +159,9 @@ public function providerForValidateOK() } /** - * Tests validating a wrong value. - * * @dataProvider providerForValidateKO - * - * @covers \Ibexa\Core\FieldType\Validator\FloatValueValidator::validate */ - public function testValidateWrongValues($value, $message, $values) + public function testValidateWrongValues(float $value, string $message): void { $validator = new FloatValueValidator(); $validator->minFloatValue = $this->getMinFloatValue(); @@ -205,21 +179,20 @@ public function testValidateWrongValues($value, $message, $values) ); self::assertEquals( $message, - $messages[0]->getTranslatableMessage()->message - ); - self::assertEquals( - $values, - $messages[0]->getTranslatableMessage()->values + (string)$messages[0]->getTranslatableMessage() ); } - public function providerForValidateKO() + /** + * @return list + */ + public function providerForValidateKO(): array { return [ - [-10 / 7, 'The value can not be lower than %size%.', ['%size%' => $this->getMinFloatValue()]], - [0, 'The value can not be lower than %size%.', ['%size%' => $this->getMinFloatValue()]], - [99 / 70, 'The value can not be lower than %size%.', ['%size%' => $this->getMinFloatValue()]], - [111 / 70, 'The value can not be higher than %size%.', ['%size%' => $this->getMaxFloatValue()]], + [-10 / 7, 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()])], ]; } @@ -228,9 +201,9 @@ public function providerForValidateKO() * * @dataProvider providerForValidateConstraintsOK * - * @covers \Ibexa\Core\FieldType\Validator\FileSizeValidator::validateConstraints + * @param array $constraints */ - public function testValidateConstraintsCorrectValues($constraints) + public function testValidateConstraintsCorrectValues(array $constraints): void { $validator = new FloatValueValidator(); @@ -239,7 +212,10 @@ public function testValidateConstraintsCorrectValues($constraints) ); } - public function providerForValidateConstraintsOK() + /** + * @return list}> + */ + public function providerForValidateConstraintsOK(): array { return [ [ @@ -271,13 +247,12 @@ public function providerForValidateConstraintsOK() } /** - * Tests validation of constraints. + * @param array $constraints + * @param array $expectedMessages * * @dataProvider providerForValidateConstraintsKO - * - * @covers \Ibexa\Core\FieldType\Validator\FileSizeValidator::validateConstraints */ - public function testValidateConstraintsWrongValues($constraints, $expectedMessages, $values) + public function testValidateConstraintsWrongValues(array $constraints, array $expectedMessages): void { $validator = new FloatValueValidator(); $messages = $validator->validateConstraints($constraints); @@ -289,98 +264,75 @@ public function testValidateConstraintsWrongValues($constraints, $expectedMessag ); self::assertEquals( $expectedMessage, - $messages[$index]->getTranslatableMessage()->message - ); - self::assertEquals( - $values[$index], - $messages[$index]->getTranslatableMessage()->values + (string)$messages[$index]->getTranslatableMessage() ); } } - public function providerForValidateConstraintsKO() + /** + * @return list, array}> + */ + public function providerForValidateConstraintsKO(): array { return [ [ [ 'minFloatValue' => true, ], - ["Validator parameter '%parameter%' value must be of numeric type"], - [ - ['%parameter%' => 'minFloatValue'], - ], + [self::MIN_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE], ], [ [ - 'minFloatValue' => 'five thousand bytes', - ], - ["Validator parameter '%parameter%' value must be of numeric type"], - [ - ['%parameter%' => 'minFloatValue'], + 'minFloatValue' => self::WRONG_MIN_FLOAT_VALUE, ], + [self::MIN_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE], ], [ [ - 'minFloatValue' => 'five thousand bytes', + 'minFloatValue' => self::WRONG_MIN_FLOAT_VALUE, 'maxFloatValue' => 1234, ], - ["Validator parameter '%parameter%' value must be of numeric type"], - [ - ['%parameter%' => 'minFloatValue'], - ], + [self::MIN_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE], ], [ [ 'maxFloatValue' => new \DateTime(), 'minFloatValue' => 1234, ], - ["Validator parameter '%parameter%' value must be of numeric type"], - [ - ['%parameter%' => 'maxFloatValue'], - ], + [self::MAX_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE], ], [ [ 'minFloatValue' => true, 'maxFloatValue' => 1234, ], - ["Validator parameter '%parameter%' value must be of numeric type"], + [self::MIN_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE], [ ['%parameter%' => 'minFloatValue'], ], ], [ [ - 'minFloatValue' => 'five thousand bytes', - 'maxFloatValue' => 'ten billion bytes', - ], - [ - "Validator parameter '%parameter%' value must be of numeric type", - "Validator parameter '%parameter%' value must be of numeric type", + 'minFloatValue' => self::WRONG_MIN_FLOAT_VALUE, + 'maxFloatValue' => self::WRONG_MAX_FLOAT_VALUE, ], [ - ['%parameter%' => 'minFloatValue'], - ['%parameter%' => 'maxFloatValue'], + self::MIN_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE, + self::MAX_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE, ], ], [ [ 'brljix' => 12345, ], - ["Validator parameter '%parameter%' is unknown"], - [ - ['%parameter%' => 'brljix'], - ], + [self::UNKNOWN_PARAM_VALIDATION_MESSAGE], ], [ [ 'minFloatValue' => 12345, 'brljix' => 12345, ], - ["Validator parameter '%parameter%' is unknown"], - [ - ['%parameter%' => 'brljix'], - ], + [self::UNKNOWN_PARAM_VALIDATION_MESSAGE], ], ]; } diff --git a/tests/lib/FieldType/IntegerValueValidatorTest.php b/tests/lib/FieldType/IntegerValueValidatorTest.php index a567420109..4fc327f010 100644 --- a/tests/lib/FieldType/IntegerValueValidatorTest.php +++ b/tests/lib/FieldType/IntegerValueValidatorTest.php @@ -4,6 +4,7 @@ * @copyright Copyright (C) Ibexa AS. All rights reserved. * @license For full copyright and license information view LICENSE file distributed with this source code. */ +declare(strict_types=1); namespace Ibexa\Tests\Core\FieldType; @@ -16,31 +17,33 @@ use PHPUnit\Framework\TestCase; /** + * @covers \Ibexa\Core\FieldType\Validator\IntegerValueValidator + * * @group fieldType * @group validator */ -class IntegerValueValidatorTest extends TestCase +final class IntegerValueValidatorTest extends TestCase { - /** - * @return int - */ - protected function getMinIntegerValue() + private const string VALUE_TOO_LOW_VALIDATION_MESSAGE = 'The value can not be lower than %size%.'; + private const string VALUE_TOO_HIGH_VALIDATION_MESSAGE = 'The value can not be higher than %size%.'; + private const string MIN_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE = "Validator parameter 'minIntegerValue' value must be of integer type"; + private const string MAX_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE = "Validator parameter 'maxIntegerValue' value must be of integer type"; + private const string WRONG_MIN_INT_VALUE = 'five thousand bytes'; + private const string WRONG_MAX_INT_VALUE = 'ten billion bytes'; + private const string UNKNOWN_PARAM_VALIDATION_MESSAGE = "Validator parameter 'brljix' is unknown"; + public const string SIZE_PARAM = '%size%'; + + protected function getMinIntegerValue(): int { return 10; } - /** - * @return int - */ - protected function getMaxIntegerValue() + protected function getMaxIntegerValue(): int { return 15; } - /** - * This test ensure an IntegerValueValidator can be created. - */ - public function testConstructor() + public function testConstructor(): void { self::assertInstanceOf( Validator::class, @@ -49,12 +52,9 @@ public function testConstructor() } /** - * Tests setting and getting constraints. - * - * @covers \Ibexa\Core\FieldType\Validator::initializeWithConstraints - * @covers \Ibexa\Core\FieldType\Validator::__get + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\PropertyNotFoundException */ - public function testConstraintsInitializeGet() + public function testConstraintsInitializeGet(): void { $constraints = [ 'minIntegerValue' => 0, @@ -68,12 +68,7 @@ public function testConstraintsInitializeGet() self::assertSame($constraints['maxIntegerValue'], $validator->maxIntegerValue); } - /** - * Test getting constraints schema. - * - * @covers \Ibexa\Core\FieldType\Validator::getConstraintsSchema - */ - public function testGetConstraintsSchema() + public function testGetConstraintsSchema(): void { $constraintsSchema = [ 'minIntegerValue' => [ @@ -89,13 +84,7 @@ public function testGetConstraintsSchema() self::assertSame($constraintsSchema, $validator->getConstraintsSchema()); } - /** - * Tests setting and getting constraints. - * - * @covers \Ibexa\Core\FieldType\Validator::__set - * @covers \Ibexa\Core\FieldType\Validator::__get - */ - public function testConstraintsSetGet() + public function testConstraintsSetGet(): void { $constraints = [ 'minIntegerValue' => 0, @@ -108,12 +97,7 @@ public function testConstraintsSetGet() self::assertSame($constraints['maxIntegerValue'], $validator->maxIntegerValue); } - /** - * Tests initializing with a wrong constraint. - * - * @covers \Ibexa\Core\FieldType\Validator::initializeWithConstraints - */ - public function testInitializeBadConstraint() + public function testInitializeBadConstraint(): void { $this->expectException(PropertyNotFoundException::class); @@ -126,41 +110,26 @@ public function testInitializeBadConstraint() ); } - /** - * Tests setting a wrong constraint. - * - * @covers \Ibexa\Core\FieldType\Validator::__set - */ - public function testSetBadConstraint() + public function testSetBadConstraint(): void { - $this->expectException(PropertyNotFoundException::class); - $validator = new IntegerValueValidator(); + + $this->expectException(PropertyNotFoundException::class); $validator->unexisting = 0; } - /** - * Tests getting a wrong constraint. - * - * @covers \Ibexa\Core\FieldType\Validator::__get - */ - public function testGetBadConstraint() + public function testGetBadConstraint(): void { - $this->expectException(PropertyNotFoundException::class); - $validator = new IntegerValueValidator(); + + $this->expectException(PropertyNotFoundException::class); $null = $validator->unexisting; } /** - * Tests validating a correct value. - * * @dataProvider providerForValidateOK - * - * @covers \Ibexa\Core\FieldType\Validator\IntegerValueValidator::validate - * @covers \Ibexa\Core\FieldType\Validator::getMessage */ - public function testValidateCorrectValues($value) + public function testValidateCorrectValues(int $value): void { $validator = new IntegerValueValidator(); $validator->minIntegerValue = 10; @@ -169,13 +138,15 @@ public function testValidateCorrectValues($value) self::assertSame([], $validator->getMessage()); } - public function providerForValidateOK() + /** + * @return list + */ + public function providerForValidateOK(): array { return [ [10], [11], [12], - [12.5], [13], [14], [15], @@ -186,10 +157,8 @@ public function providerForValidateOK() * Tests validating a wrong value. * * @dataProvider providerForValidateKO - * - * @covers \Ibexa\Core\FieldType\Validator\IntegerValueValidator::validate */ - public function testValidateWrongValues($value, $message, $values) + public function testValidateWrongValues($value, $message): void { $validator = new IntegerValueValidator(); $validator->minIntegerValue = $this->getMinIntegerValue(); @@ -207,32 +176,29 @@ public function testValidateWrongValues($value, $message, $values) ); self::assertEquals( $message, - $messages[0]->getTranslatableMessage()->message - ); - self::assertEquals( - $values, - $messages[0]->getTranslatableMessage()->values + $messages[0]->getTranslatableMessage() ); } - public function providerForValidateKO() + /** + * @return list + */ + public function providerForValidateKO(): array { return [ - [-12, 'The value can not be lower than %size%.', ['%size%' => $this->getMinIntegerValue()]], - [0, 'The value can not be lower than %size%.', ['%size%' => $this->getMinIntegerValue()]], - [9, 'The value can not be lower than %size%.', ['%size%' => $this->getMinIntegerValue()]], - [16, 'The value can not be higher than %size%.', ['%size%' => $this->getMaxIntegerValue()]], + [-12, strtr(self::VALUE_TOO_LOW_VALIDATION_MESSAGE, [self::SIZE_PARAM => $this->getMinIntegerValue()])], + [0, strtr(self::VALUE_TOO_LOW_VALIDATION_MESSAGE, [self::SIZE_PARAM => $this->getMinIntegerValue()])], + [9, strtr(self::VALUE_TOO_LOW_VALIDATION_MESSAGE, [self::SIZE_PARAM => $this->getMinIntegerValue()])], + [16, strtr(self::VALUE_TOO_HIGH_VALIDATION_MESSAGE, [self::SIZE_PARAM => $this->getMaxIntegerValue()])], ]; } /** - * Tests validation of constraints. - * * @dataProvider providerForValidateConstraintsOK * - * @covers \Ibexa\Core\FieldType\Validator\FileSizeValidator::validateConstraints + * @param array $constraints */ - public function testValidateConstraintsCorrectValues($constraints) + public function testValidateConstraintsCorrectValues(array $constraints): void { $validator = new IntegerValueValidator(); @@ -241,7 +207,10 @@ public function testValidateConstraintsCorrectValues($constraints) ); } - public function providerForValidateConstraintsOK() + /** + * @return list>> + */ + public function providerForValidateConstraintsOK(): array { return [ [ @@ -273,13 +242,12 @@ public function providerForValidateConstraintsOK() } /** - * Tests validation of constraints. - * * @dataProvider providerForValidateConstraintsKO * - * @covers \Ibexa\Core\FieldType\Validator\FileSizeValidator::validateConstraints + * @param array $constraints + * @param array $expectedMessages */ - public function testValidateConstraintsWrongValues($constraints, $expectedMessages, $values) + public function testValidateConstraintsWrongValues(array $constraints, array $expectedMessages): void { $validator = new IntegerValueValidator(); $messages = $validator->validateConstraints($constraints); @@ -291,98 +259,72 @@ public function testValidateConstraintsWrongValues($constraints, $expectedMessag ); self::assertEquals( $expectedMessage, - $messages[$index]->getTranslatableMessage()->message - ); - self::assertEquals( - $values[$index], - $messages[$index]->getTranslatableMessage()->values + (string)$messages[$index]->getTranslatableMessage() ); } } - public function providerForValidateConstraintsKO() + /** + * @return list, string[]}> + */ + public function providerForValidateConstraintsKO(): array { return [ [ [ 'minIntegerValue' => true, ], - ["Validator parameter '%parameter%' value must be of integer type"], - [ - ['%parameter%' => 'minIntegerValue'], - ], + [self::MIN_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE], ], [ [ - 'minIntegerValue' => 'five thousand bytes', - ], - ["Validator parameter '%parameter%' value must be of integer type"], - [ - ['%parameter%' => 'minIntegerValue'], + 'minIntegerValue' => self::WRONG_MIN_INT_VALUE, ], + [self::MIN_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE], ], [ [ - 'minIntegerValue' => 'five thousand bytes', + 'minIntegerValue' => self::WRONG_MIN_INT_VALUE, 'maxIntegerValue' => 1234, ], - ["Validator parameter '%parameter%' value must be of integer type"], - [ - ['%parameter%' => 'minIntegerValue'], - ], + [self::MIN_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE], ], [ [ 'maxIntegerValue' => new \DateTime(), 'minIntegerValue' => 1234, ], - ["Validator parameter '%parameter%' value must be of integer type"], - [ - ['%parameter%' => 'maxIntegerValue'], - ], + [self::MAX_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE], ], [ [ 'minIntegerValue' => true, 'maxIntegerValue' => 1234, ], - ["Validator parameter '%parameter%' value must be of integer type"], - [ - ['%parameter%' => 'minIntegerValue'], - ], + [self::MIN_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE], ], [ [ - 'minIntegerValue' => 'five thousand bytes', - 'maxIntegerValue' => 'ten billion bytes', - ], - [ - "Validator parameter '%parameter%' value must be of integer type", - "Validator parameter '%parameter%' value must be of integer type", + 'minIntegerValue' => self::WRONG_MIN_INT_VALUE, + 'maxIntegerValue' => self::WRONG_MAX_INT_VALUE, ], [ - ['%parameter%' => 'minIntegerValue'], - ['%parameter%' => 'maxIntegerValue'], + self::MIN_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE, + self::MAX_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE, ], ], [ [ 'brljix' => 12345, ], - ["Validator parameter '%parameter%' is unknown"], - [ - ['%parameter%' => 'brljix'], - ], + [self::UNKNOWN_PARAM_VALIDATION_MESSAGE], ], [ [ 'minIntegerValue' => 12345, 'brljix' => 12345, ], - ["Validator parameter '%parameter%' is unknown"], - [ - ['%parameter%' => 'brljix'], - ], + [self::UNKNOWN_PARAM_VALIDATION_MESSAGE], ], ]; } diff --git a/tests/lib/FieldType/StringLengthValidatorTest.php b/tests/lib/FieldType/StringLengthValidatorTest.php index 64469531b4..c3b4e48660 100644 --- a/tests/lib/FieldType/StringLengthValidatorTest.php +++ b/tests/lib/FieldType/StringLengthValidatorTest.php @@ -4,6 +4,7 @@ * @copyright Copyright (C) Ibexa AS. All rights reserved. * @license For full copyright and license information view LICENSE file distributed with this source code. */ +declare(strict_types=1); namespace Ibexa\Tests\Core\FieldType; @@ -17,31 +18,31 @@ use PHPUnit\Framework\TestCase; /** + * @covers \Ibexa\Core\FieldType\Validator\StringLengthValidator + * * @group fieldType * @group validator */ -class StringLengthValidatorTest extends TestCase +final class StringLengthValidatorTest extends TestCase { - /** - * @return int - */ - protected function getMinStringLength() + private const string STRING_TOO_SHORT_VALIDATION_MESSAGE = 'The string cannot be shorter than 5 characters.'; + private const string STRING_TOO_LONG_VALIDATION_MESSAGE = 'The string can not exceed 10 characters.'; + private const string MIN_STR_LEN_INT_TYPE_VALIDATION_MESSAGE = "Validator parameter 'minStringLength' value must be of integer type"; + private const string WRONG_MIN_STR_LEN_VALUE = 'five thousand characters'; + private const string MAX_STR_LEN_INT_TYPE_VALIDATION_MESSAGE = "Validator parameter 'maxStringLength' value must be of integer type"; + private const string WRONG_MAX_STR_LEN_VALUE = 'ten billion characters'; + + protected function getMinStringLength(): int { return 5; } - /** - * @return int - */ - protected function getMaxStringLength() + protected function getMaxStringLength(): int { return 10; } - /** - * This test ensure an StringLengthValidator can be created. - */ - public function testConstructor() + public function testConstructor(): void { self::assertInstanceOf( Validator::class, @@ -50,12 +51,9 @@ public function testConstructor() } /** - * Tests setting and getting constraints. - * - * @covers \Ibexa\Core\FieldType\Validator::initializeWithConstraints - * @covers \Ibexa\Core\FieldType\Validator::__get + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\PropertyNotFoundException */ - public function testConstraintsInitializeGet() + public function testConstraintsInitializeGet(): void { $constraints = [ 'minStringLength' => 5, @@ -69,12 +67,7 @@ public function testConstraintsInitializeGet() self::assertSame($constraints['maxStringLength'], $validator->maxStringLength); } - /** - * Test getting constraints schema. - * - * @covers \Ibexa\Core\FieldType\Validator::getConstraintsSchema - */ - public function testGetConstraintsSchema() + public function testGetConstraintsSchema(): void { $constraintsSchema = [ 'minStringLength' => [ @@ -90,13 +83,7 @@ public function testGetConstraintsSchema() self::assertSame($constraintsSchema, $validator->getConstraintsSchema()); } - /** - * Tests setting and getting constraints. - * - * @covers \Ibexa\Core\FieldType\Validator::__set - * @covers \Ibexa\Core\FieldType\Validator::__get - */ - public function testConstraintsSetGet() + public function testConstraintsSetGet(): void { $constraints = [ 'minStringLength' => 5, @@ -109,59 +96,39 @@ public function testConstraintsSetGet() self::assertSame($constraints['maxStringLength'], $validator->maxStringLength); } - /** - * Tests initializing with a wrong constraint. - * - * @covers \Ibexa\Core\FieldType\Validator::initializeWithConstraints - */ - public function testInitializeBadConstraint() + public function testInitializeBadConstraint(): void { - $this->expectException(PropertyNotFoundException::class); - $constraints = [ 'unexisting' => 0, ]; $validator = new StringLengthValidator(); + + $this->expectException(PropertyNotFoundException::class); $validator->initializeWithConstraints( $constraints ); } - /** - * Tests setting a wrong constraint. - * - * @covers \Ibexa\Core\FieldType\Validator::__set - */ - public function testSetBadConstraint() + public function testSetBadConstraint(): void { - $this->expectException(PropertyNotFoundException::class); - $validator = new StringLengthValidator(); + + $this->expectException(PropertyNotFoundException::class); $validator->unexisting = 0; } - /** - * Tests getting a wrong constraint. - * - * @covers \Ibexa\Core\FieldType\Validator::__get - */ - public function testGetBadConstraint() + public function testGetBadConstraint(): void { - $this->expectException(PropertyNotFoundException::class); - $validator = new StringLengthValidator(); + + $this->expectException(PropertyNotFoundException::class); $null = $validator->unexisting; } /** - * Tests validating a correct value. - * * @dataProvider providerForValidateOK - * - * @covers \Ibexa\Core\FieldType\Validator\StringLengthValidator::validate - * @covers \Ibexa\Core\FieldType\Validator::getMessage */ - public function testValidateCorrectValues($value) + public function testValidateCorrectValues(string $value): void { $validator = new StringLengthValidator(); $validator->minStringLength = 5; @@ -170,7 +137,10 @@ public function testValidateCorrectValues($value) self::assertSame([], $validator->getMessage()); } - public function providerForValidateOK() + /** + * @return list + */ + public function providerForValidateOK(): array { return [ ['hello'], @@ -181,17 +151,17 @@ public function providerForValidateOK() } /** - * Tests validating a wrong value. - * * @dataProvider providerForValidateKO - * - * @covers \Ibexa\Core\FieldType\Validator\StringLengthValidator::validate */ - public function testValidateWrongValues($value, $messageSingular, $messagePlural, $values) - { + public function testValidateWrongValues( + string $value, + string $expectedMessage, + int $minStringLength, + int $maxStringLength + ): void { $validator = new StringLengthValidator(); - $validator->minStringLength = $this->getMinStringLength(); - $validator->maxStringLength = $this->getMaxStringLength(); + $validator->minStringLength = $minStringLength; + $validator->maxStringLength = $maxStringLength; self::assertFalse($validator->validate(new TextLineValue($value))); $messages = $validator->getMessage(); self::assertCount(1, $messages); @@ -204,57 +174,65 @@ public function testValidateWrongValues($value, $messageSingular, $messagePlural $messages[0]->getTranslatableMessage() ); self::assertEquals( - $messageSingular, - $messages[0]->getTranslatableMessage()->singular - ); - self::assertEquals( - $messagePlural, - $messages[0]->getTranslatableMessage()->plural - ); - self::assertEquals( - $values, - $messages[0]->getTranslatableMessage()->values + $expectedMessage, + (string)$messages[0]->getTranslatableMessage() ); } - public function providerForValidateKO() + /** + * @return iterable + */ + public function providerForValidateKO(): iterable { - return [ - [ - '', - 'The string cannot be shorter than %size% character.', - 'The string cannot be shorter than %size% characters.', - ['%size%' => $this->getMinStringLength()], - ], - [ - 'Hi!', - 'The string cannot be shorter than %size% character.', - 'The string cannot be shorter than %size% characters.', - ['%size%' => $this->getMinStringLength()], - ], - [ - '0123456789!', - 'The string can not exceed %size% character.', - 'The string can not exceed %size% characters.', - ['%size%' => $this->getMaxStringLength()], - ], - [ - 'ABC♔', - 'The string cannot be shorter than %size% character.', - 'The string cannot be shorter than %size% characters.', - ['%size%' => $this->getMinStringLength()], - ], + yield 'empty string' => [ + '', + self::STRING_TOO_SHORT_VALIDATION_MESSAGE, + $this->getMinStringLength(), + $this->getMaxStringLength(), + ]; + + yield 'too short string' => [ + 'Hi!', + self::STRING_TOO_SHORT_VALIDATION_MESSAGE, + $this->getMinStringLength(), + $this->getMaxStringLength(), + ]; + + yield 'too long string' => [ + '0123456789!', + self::STRING_TOO_LONG_VALIDATION_MESSAGE, + $this->getMinStringLength(), + $this->getMaxStringLength(), + ]; + + yield 'too short string with special characters' => [ + 'ABC♔', + self::STRING_TOO_SHORT_VALIDATION_MESSAGE, + $this->getMinStringLength(), + $this->getMaxStringLength(), + ]; + + yield 'too short string, singular form validation message' => [ + '', + 'The string cannot be shorter than 1 character.', + 1, + $this->getMaxStringLength(), + ]; + + yield 'too long string, singular form validation message' => [ + 'foo', + 'The string can not exceed 1 character.', + 1, + 1, ]; } /** - * Tests validation of constraints. + * @param array $constraints * * @dataProvider providerForValidateConstraintsOK - * - * @covers \Ibexa\Core\FieldType\Validator\FileSizeValidator::validateConstraints */ - public function testValidateConstraintsCorrectValues($constraints) + public function testValidateConstraintsCorrectValues(array $constraints): void { $validator = new StringLengthValidator(); @@ -263,7 +241,10 @@ public function testValidateConstraintsCorrectValues($constraints) ); } - public function providerForValidateConstraintsOK() + /** + * @return list>> + */ + public function providerForValidateConstraintsOK(): array { return [ [ @@ -295,13 +276,12 @@ public function providerForValidateConstraintsOK() } /** - * Tests validation of constraints. - * * @dataProvider providerForValidateConstraintsKO * - * @covers \Ibexa\Core\FieldType\Validator\FileSizeValidator::validateConstraints + * @param array $constraints + * @param string[] $expectedMessages */ - public function testValidateConstraintsWrongValues($constraints, $expectedMessages, $values) + public function testValidateConstraintsWrongValues(array $constraints, array $expectedMessages): void { $validator = new StringLengthValidator(); $messages = $validator->validateConstraints($constraints); @@ -309,102 +289,76 @@ public function testValidateConstraintsWrongValues($constraints, $expectedMessag foreach ($expectedMessages as $index => $expectedMessage) { self::assertInstanceOf( Message::class, - $messages[0]->getTranslatableMessage() + $messages[$index]->getTranslatableMessage() ); self::assertEquals( $expectedMessage, - $messages[$index]->getTranslatableMessage()->message - ); - self::assertEquals( - $values[$index], - $messages[$index]->getTranslatableMessage()->values + (string)$messages[$index]->getTranslatableMessage() ); } } - public function providerForValidateConstraintsKO() + /** + * @return list, string[]}> + */ + public function providerForValidateConstraintsKO(): array { return [ [ [ 'minStringLength' => true, ], - ["Validator parameter '%parameter%' value must be of integer type"], - [ - ['%parameter%' => 'minStringLength'], - ], + [self::MIN_STR_LEN_INT_TYPE_VALIDATION_MESSAGE], ], [ [ - 'minStringLength' => 'five thousand characters', - ], - ["Validator parameter '%parameter%' value must be of integer type"], - [ - ['%parameter%' => 'minStringLength'], + 'minStringLength' => self::WRONG_MIN_STR_LEN_VALUE, ], + [self::MIN_STR_LEN_INT_TYPE_VALIDATION_MESSAGE], ], [ [ - 'minStringLength' => 'five thousand characters', + 'minStringLength' => self::WRONG_MIN_STR_LEN_VALUE, 'maxStringLength' => 1234, ], - ["Validator parameter '%parameter%' value must be of integer type"], - [ - ['%parameter%' => 'minStringLength'], - ], + [self::MIN_STR_LEN_INT_TYPE_VALIDATION_MESSAGE], ], [ [ 'maxStringLength' => new \DateTime(), 'minStringLength' => 1234, ], - ["Validator parameter '%parameter%' value must be of integer type"], - [ - ['%parameter%' => 'maxStringLength'], - ], + [self::MAX_STR_LEN_INT_TYPE_VALIDATION_MESSAGE], ], [ [ 'minStringLength' => true, 'maxStringLength' => 1234, ], - ["Validator parameter '%parameter%' value must be of integer type"], - [ - ['%parameter%' => 'minStringLength'], - ], + [self::MIN_STR_LEN_INT_TYPE_VALIDATION_MESSAGE], ], [ [ - 'minStringLength' => 'five thousand characters', - 'maxStringLength' => 'ten billion characters', + 'minStringLength' => self::WRONG_MIN_STR_LEN_VALUE, + 'maxStringLength' => self::WRONG_MAX_STR_LEN_VALUE, ], [ - "Validator parameter '%parameter%' value must be of integer type", - "Validator parameter '%parameter%' value must be of integer type", - ], - [ - ['%parameter%' => 'minStringLength'], - ['%parameter%' => 'maxStringLength'], + self::MIN_STR_LEN_INT_TYPE_VALIDATION_MESSAGE, + self::MAX_STR_LEN_INT_TYPE_VALIDATION_MESSAGE, ], ], [ [ 'brljix' => 12345, ], - ["Validator parameter '%parameter%' is unknown"], - [ - ['%parameter%' => 'brljix'], - ], + ["Validator parameter 'brljix' is unknown"], ], [ [ 'minStringLength' => 12345, 'brljix' => 12345, ], - ["Validator parameter '%parameter%' is unknown"], - [ - ['%parameter%' => 'brljix'], - ], + ["Validator parameter 'brljix' is unknown"], ], ]; } From 690317d8a4cfdaf75846c1ae333bc8a7276ede7f Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Tue, 27 Aug 2024 12:47:35 +0200 Subject: [PATCH 03/10] [PHPStan] Aligned baseline with the changes --- phpstan-baseline.neon | 410 ------------------------------------------ 1 file changed, 410 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 94e053d031..7dbaaa083d 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -9200,21 +9200,6 @@ parameters: count: 1 path: src/lib/FieldType/FieldType.php - - - message: "#^Access to an undefined property Ibexa\\\\Contracts\\\\Core\\\\FieldType\\\\Value\\:\\:\\$value\\.$#" - count: 1 - path: src/lib/FieldType/Float/Type.php - - - - message: "#^Method Ibexa\\\\Core\\\\FieldType\\\\Float\\\\Type\\:\\:checkValueStructure\\(\\) has no return type specified\\.$#" - count: 1 - path: src/lib/FieldType/Float/Type.php - - - - message: "#^PHPDoc tag @param for parameter \\$value with type mixed is not subtype of native type Ibexa\\\\Contracts\\\\Core\\\\FieldType\\\\Value\\.$#" - count: 1 - path: src/lib/FieldType/Float/Type.php - - message: "#^Method Ibexa\\\\Core\\\\FieldType\\\\GatewayBasedStorage\\:\\:addGateway\\(\\) has no return type specified\\.$#" count: 1 @@ -9600,26 +9585,6 @@ parameters: count: 1 path: src/lib/FieldType/ImageAsset/Type.php - - - message: "#^Access to an undefined property Ibexa\\\\Contracts\\\\Core\\\\FieldType\\\\Value\\:\\:\\$value\\.$#" - count: 1 - path: src/lib/FieldType/Integer/Type.php - - - - message: "#^Access to an undefined property Ibexa\\\\Core\\\\FieldType\\\\Value\\:\\:\\$value\\.$#" - count: 1 - path: src/lib/FieldType/Integer/Type.php - - - - message: "#^Method Ibexa\\\\Core\\\\FieldType\\\\Integer\\\\Type\\:\\:checkValueStructure\\(\\) has no return type specified\\.$#" - count: 1 - path: src/lib/FieldType/Integer/Type.php - - - - message: "#^PHPDoc tag @param for parameter \\$value with type mixed is not subtype of native type Ibexa\\\\Contracts\\\\Core\\\\FieldType\\\\Value\\.$#" - count: 1 - path: src/lib/FieldType/Integer/Type.php - - message: "#^Class Ibexa\\\\Core\\\\FieldType\\\\Keyword\\\\KeywordStorage extends generic class Ibexa\\\\Contracts\\\\Core\\\\FieldType\\\\GatewayBasedStorage but does not specify its types\\: T$#" count: 1 @@ -42300,131 +42265,6 @@ parameters: count: 2 path: tests/lib/FieldType/FloatValueValidatorTest.php - - - message: "#^Access to protected property Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Translation\\\\Message\\:\\:\\$message\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Access to protected property Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Translation\\\\Message\\:\\:\\$values\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:providerForValidateConstraintsKO\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:providerForValidateConstraintsOK\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:providerForValidateKO\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:providerForValidateOK\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testConstraintsInitializeGet\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testConstraintsSetGet\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testConstructor\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testGetBadConstraint\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testGetConstraintsSchema\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testInitializeBadConstraint\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testSetBadConstraint\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testValidateConstraintsCorrectValues\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testValidateConstraintsCorrectValues\\(\\) has parameter \\$constraints with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testValidateConstraintsWrongValues\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testValidateConstraintsWrongValues\\(\\) has parameter \\$constraints with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testValidateConstraintsWrongValues\\(\\) has parameter \\$expectedMessages with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testValidateConstraintsWrongValues\\(\\) has parameter \\$values with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testValidateCorrectValues\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testValidateCorrectValues\\(\\) has parameter \\$value with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testValidateWrongValues\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testValidateWrongValues\\(\\) has parameter \\$message with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testValidateWrongValues\\(\\) has parameter \\$value with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\FloatValueValidatorTest\\:\\:testValidateWrongValues\\(\\) has parameter \\$values with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - message: "#^Class Symfony\\\\Component\\\\Validator\\\\ConstraintViolation constructor invoked with 1 parameter, 6\\-10 required\\.$#" count: 1 @@ -42910,116 +42750,6 @@ parameters: count: 2 path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - message: "#^Access to protected property Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Translation\\\\Message\\:\\:\\$message\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Access to protected property Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Translation\\\\Message\\:\\:\\$values\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:providerForValidateConstraintsKO\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:providerForValidateConstraintsOK\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:providerForValidateKO\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:providerForValidateOK\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testConstraintsInitializeGet\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testConstraintsSetGet\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testConstructor\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testGetBadConstraint\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testGetConstraintsSchema\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testInitializeBadConstraint\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testSetBadConstraint\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testValidateConstraintsCorrectValues\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testValidateConstraintsCorrectValues\\(\\) has parameter \\$constraints with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testValidateConstraintsWrongValues\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testValidateConstraintsWrongValues\\(\\) has parameter \\$constraints with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testValidateConstraintsWrongValues\\(\\) has parameter \\$expectedMessages with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testValidateConstraintsWrongValues\\(\\) has parameter \\$values with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testValidateCorrectValues\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testValidateCorrectValues\\(\\) has parameter \\$value with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testValidateWrongValues\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testValidateWrongValues\\(\\) has parameter \\$message with no type specified\\.$#" count: 1 @@ -43030,11 +42760,6 @@ parameters: count: 1 path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testValidateWrongValues\\(\\) has parameter \\$values with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\KeywordTest\\:\\:getSettingsSchemaExpectation\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 @@ -43385,141 +43110,6 @@ parameters: count: 2 path: tests/lib/FieldType/StringLengthValidatorTest.php - - - message: "#^Access to protected property Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Translation\\\\Plural\\:\\:\\$plural\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Access to protected property Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Translation\\\\Plural\\:\\:\\$singular\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Access to protected property Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Translation\\\\Plural\\:\\:\\$values\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:providerForValidateConstraintsKO\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:providerForValidateConstraintsOK\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:providerForValidateKO\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:providerForValidateOK\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testConstraintsInitializeGet\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testConstraintsSetGet\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testConstructor\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testGetBadConstraint\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testGetConstraintsSchema\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testInitializeBadConstraint\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testSetBadConstraint\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testValidateConstraintsCorrectValues\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testValidateConstraintsCorrectValues\\(\\) has parameter \\$constraints with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testValidateConstraintsWrongValues\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testValidateConstraintsWrongValues\\(\\) has parameter \\$constraints with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testValidateConstraintsWrongValues\\(\\) has parameter \\$expectedMessages with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testValidateConstraintsWrongValues\\(\\) has parameter \\$values with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testValidateCorrectValues\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testValidateCorrectValues\\(\\) has parameter \\$value with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testValidateWrongValues\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testValidateWrongValues\\(\\) has parameter \\$messagePlural with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testValidateWrongValues\\(\\) has parameter \\$messageSingular with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testValidateWrongValues\\(\\) has parameter \\$value with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\StringLengthValidatorTest\\:\\:testValidateWrongValues\\(\\) has parameter \\$values with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\TimeTest\\:\\:getSettingsSchemaExpectation\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 From 202d3215bb4d038a3d0db9645c6abd2a0c89e343 Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Tue, 27 Aug 2024 12:55:42 +0200 Subject: [PATCH 04/10] [Tests] Moved Float, Integer, String Validator tests to proper namespace --- tests/lib/FieldType/{ => Validator}/FloatValueValidatorTest.php | 2 +- .../lib/FieldType/{ => Validator}/IntegerValueValidatorTest.php | 2 +- .../lib/FieldType/{ => Validator}/StringLengthValidatorTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename tests/lib/FieldType/{ => Validator}/FloatValueValidatorTest.php (99%) rename tests/lib/FieldType/{ => Validator}/IntegerValueValidatorTest.php (99%) rename tests/lib/FieldType/{ => Validator}/StringLengthValidatorTest.php (99%) diff --git a/tests/lib/FieldType/FloatValueValidatorTest.php b/tests/lib/FieldType/Validator/FloatValueValidatorTest.php similarity index 99% rename from tests/lib/FieldType/FloatValueValidatorTest.php rename to tests/lib/FieldType/Validator/FloatValueValidatorTest.php index bd9b66b781..554d21b56f 100644 --- a/tests/lib/FieldType/FloatValueValidatorTest.php +++ b/tests/lib/FieldType/Validator/FloatValueValidatorTest.php @@ -6,7 +6,7 @@ */ declare(strict_types=1); -namespace Ibexa\Tests\Core\FieldType; +namespace Ibexa\Tests\Core\FieldType\Validator; use Ibexa\Contracts\Core\FieldType\ValidationError; use Ibexa\Contracts\Core\Repository\Exceptions\PropertyNotFoundException; diff --git a/tests/lib/FieldType/IntegerValueValidatorTest.php b/tests/lib/FieldType/Validator/IntegerValueValidatorTest.php similarity index 99% rename from tests/lib/FieldType/IntegerValueValidatorTest.php rename to tests/lib/FieldType/Validator/IntegerValueValidatorTest.php index 4fc327f010..6325662a82 100644 --- a/tests/lib/FieldType/IntegerValueValidatorTest.php +++ b/tests/lib/FieldType/Validator/IntegerValueValidatorTest.php @@ -6,7 +6,7 @@ */ declare(strict_types=1); -namespace Ibexa\Tests\Core\FieldType; +namespace Ibexa\Tests\Core\FieldType\Validator; use Ibexa\Contracts\Core\FieldType\ValidationError; use Ibexa\Contracts\Core\Repository\Exceptions\PropertyNotFoundException; diff --git a/tests/lib/FieldType/StringLengthValidatorTest.php b/tests/lib/FieldType/Validator/StringLengthValidatorTest.php similarity index 99% rename from tests/lib/FieldType/StringLengthValidatorTest.php rename to tests/lib/FieldType/Validator/StringLengthValidatorTest.php index c3b4e48660..8b5298a7d2 100644 --- a/tests/lib/FieldType/StringLengthValidatorTest.php +++ b/tests/lib/FieldType/Validator/StringLengthValidatorTest.php @@ -6,7 +6,7 @@ */ declare(strict_types=1); -namespace Ibexa\Tests\Core\FieldType; +namespace Ibexa\Tests\Core\FieldType\Validator; use Ibexa\Contracts\Core\FieldType\ValidationError; use Ibexa\Contracts\Core\Repository\Exceptions\PropertyNotFoundException; From 116b4d126cc8bcfce7fb61c0e0e46ff225c8352f Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Tue, 27 Aug 2024 13:55:55 +0200 Subject: [PATCH 05/10] [Tests] Extracted common base for Float and Integer validator tests --- .../BaseNumericValidatorTestCase.php | 248 +++++++++++++++++ .../Validator/FloatValueValidatorTest.php | 263 +++--------------- .../Validator/IntegerValueValidatorTest.php | 257 +++-------------- .../Validator/StringLengthValidatorTest.php | 4 +- 4 files changed, 338 insertions(+), 434 deletions(-) create mode 100644 tests/lib/FieldType/Validator/BaseNumericValidatorTestCase.php diff --git a/tests/lib/FieldType/Validator/BaseNumericValidatorTestCase.php b/tests/lib/FieldType/Validator/BaseNumericValidatorTestCase.php new file mode 100644 index 0000000000..0c499a3ff8 --- /dev/null +++ b/tests/lib/FieldType/Validator/BaseNumericValidatorTestCase.php @@ -0,0 +1,248 @@ + + */ + abstract public function providerForValidateConstraintsOK(): iterable; + + /** + * @return iterable>> + */ + abstract public static function providerForConstraintsInitializeSetGet(): iterable; + + abstract protected function getIncorrectNumericTypeValidationMessage(string $parameterName): string; + + /** + * @return list, array}> + */ + final public function providerForValidateConstraintsKO(): array + { + $minNumericValueName = $this->getMinNumericValueName(); + $minValueNumericTypeValidationMessage = $this->getIncorrectNumericTypeValidationMessage( + $minNumericValueName + ); + $maxNumericValueName = $this->getMaxNumericValueName(); + $maxValueNumericTypeValidationMessage = $this->getIncorrectNumericTypeValidationMessage( + $maxNumericValueName + ); + + return [ + [ + [ + $minNumericValueName => true, + ], + [$minValueNumericTypeValidationMessage], + ], + [ + [ + $minNumericValueName => self::WRONG_NUMERIC_MIN_VALUE, + ], + [$minValueNumericTypeValidationMessage], + ], + [ + [ + $minNumericValueName => self::WRONG_NUMERIC_MIN_VALUE, + $maxNumericValueName => 1234, + ], + [$minValueNumericTypeValidationMessage], + ], + [ + [ + $maxNumericValueName => new \DateTime(), + $minNumericValueName => 1234, + ], + [$maxValueNumericTypeValidationMessage], + ], + [ + [ + $minNumericValueName => true, + $maxNumericValueName => 1234, + ], + [$minValueNumericTypeValidationMessage], + [ + ['%parameter%' => $minNumericValueName], + ], + ], + [ + [ + $minNumericValueName => self::WRONG_NUMERIC_MIN_VALUE, + $maxNumericValueName => self::WRONG_NUMERIC_MAX_VALUE, + ], + [ + $minValueNumericTypeValidationMessage, + $maxValueNumericTypeValidationMessage, + ], + ], + [ + [ + 'brljix' => 12345, + ], + [self::UNKNOWN_PARAM_VALIDATION_MESSAGE], + ], + [ + [ + $minNumericValueName => 12345, + 'brljix' => 12345, + ], + [self::UNKNOWN_PARAM_VALIDATION_MESSAGE], + ], + ]; + } + + /** + * @param array $constraints + * @param array $expectedMessages + * + * @dataProvider providerForValidateConstraintsKO + */ + final public function testValidateConstraintsWrongValues(array $constraints, array $expectedMessages): void + { + $validator = $this->getValidatorInstance(); + $messages = $validator->validateConstraints($constraints); + + foreach ($expectedMessages as $index => $expectedMessage) { + self::assertInstanceOf( + Message::class, + $messages[0]->getTranslatableMessage() + ); + self::assertEquals( + $expectedMessage, + (string)$messages[$index]->getTranslatableMessage() + ); + } + } + + final public function testSetBadConstraint(): void + { + $validator = $this->getValidatorInstance(); + + $this->expectException(PropertyNotFoundException::class); + /** @phpstan-ignore-next-line */ + $validator->unexisting = 0; + } + + final public function testGetBadConstraint(): void + { + $validator = $this->getValidatorInstance(); + + $this->expectException(PropertyNotFoundException::class); + /** @phpstan-ignore-next-line */ + $validator->unexisting; + } + + /** + * @dataProvider providerForValidateConstraintsOK + * + * @param array{min?: ?scalar, max?: ?scalar} $data + */ + public function testValidateConstraintsCorrectValues(array $data): void + { + $validator = $this->getValidatorInstance(); + + $constraints = []; + if (array_key_exists('min', $data)) { + $constraints[$this->getMinNumericValueName()] = $data['min']; + } + if (array_key_exists('max', $data)) { + $constraints[$this->getMinNumericValueName()] = $data['max']; + } + + self::assertEmpty( + $validator->validateConstraints($constraints) + ); + } + + /** + * @dataProvider providerForConstraintsInitializeSetGet + * + * @param array $constraints + * + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\PropertyNotFoundException + */ + final public function testConstraintsInitializeGet(array $constraints): void + { + $minNumericValueName = $this->getMinNumericValueName(); + $maxNumericValueName = $this->getMaxNumericValueName(); + $validator = $this->getValidatorInstance(); + $validator->initializeWithConstraints( + $constraints + ); + self::assertSame($constraints[$minNumericValueName], $validator->{$minNumericValueName}); + self::assertSame($constraints[$maxNumericValueName], $validator->{$maxNumericValueName}); + } + + /** + * @dataProvider providerForConstraintsInitializeSetGet + * + * @param array $constraints + */ + final public function testConstraintsSetGet(array $constraints): void + { + $minNumericValueName = $this->getMinNumericValueName(); + $maxNumericValueName = $this->getMaxNumericValueName(); + $validator = $this->getValidatorInstance(); + $validator->{$minNumericValueName} = $constraints[$minNumericValueName]; + $validator->{$maxNumericValueName} = $constraints[$maxNumericValueName]; + self::assertSame($constraints[$minNumericValueName], $validator->{$minNumericValueName}); + self::assertSame($constraints[$maxNumericValueName], $validator->{$maxNumericValueName}); + } + + /** + * @param \Ibexa\Contracts\Core\FieldType\ValidationError[] $actualMessages + */ + protected static function assertWrongValueValidationMessage(array $actualMessages, string $expectedMessage): void + { + self::assertCount(1, $actualMessages); + self::assertInstanceOf( + ValidationError::class, + $actualMessages[0] + ); + self::assertInstanceOf( + Message::class, + $actualMessages[0]->getTranslatableMessage() + ); + self::assertEquals( + $expectedMessage, + (string)$actualMessages[0]->getTranslatableMessage() + ); + } +} diff --git a/tests/lib/FieldType/Validator/FloatValueValidatorTest.php b/tests/lib/FieldType/Validator/FloatValueValidatorTest.php index 554d21b56f..67c2bfbb40 100644 --- a/tests/lib/FieldType/Validator/FloatValueValidatorTest.php +++ b/tests/lib/FieldType/Validator/FloatValueValidatorTest.php @@ -8,30 +8,35 @@ namespace Ibexa\Tests\Core\FieldType\Validator; -use Ibexa\Contracts\Core\FieldType\ValidationError; use Ibexa\Contracts\Core\Repository\Exceptions\PropertyNotFoundException; -use Ibexa\Contracts\Core\Repository\Values\Translation\Message; use Ibexa\Core\FieldType\Float\Value as FloatValue; use Ibexa\Core\FieldType\Validator; use Ibexa\Core\FieldType\Validator\FloatValueValidator; -use PHPUnit\Framework\TestCase; /** * @covers \Ibexa\Core\FieldType\Validator\FloatValueValidator * + * @extends BaseNumericValidatorTestCase + * * @group fieldType * @group validator */ -final class FloatValueValidatorTest extends TestCase +final class FloatValueValidatorTest extends BaseNumericValidatorTestCase { - private const string VALUE_TOO_LOW_VALIDATION_MESSAGE = 'The value can not be lower than %size%.'; - private const string VALUE_TOO_HIGH_VALIDATION_MESSAGE = 'The value can not be higher than %size%.'; - private const string SIZE_PARAM = '%size%'; - private const string MIN_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE = "Validator parameter 'minFloatValue' value must be of numeric type"; - private const string MAX_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE = "Validator parameter 'maxFloatValue' value must be of numeric type"; - private const string WRONG_MIN_FLOAT_VALUE = 'five thousand bytes'; - private const string WRONG_MAX_FLOAT_VALUE = 'ten billion bytes'; - private const string UNKNOWN_PARAM_VALIDATION_MESSAGE = "Validator parameter 'brljix' is unknown"; + protected function getValidatorInstance(): Validator + { + return new FloatValueValidator(); + } + + protected function getMinNumericValueName(): string + { + return 'minFloatValue'; + } + + protected function getMaxNumericValueName(): string + { + return 'maxFloatValue'; + } protected function getMinFloatValue(): float { @@ -43,32 +48,14 @@ protected function getMaxFloatValue(): float return 11 / 7; } - /** - * This test ensure an FloatValueValidator can be created. - */ - public function testConstructor(): void + public static function providerForConstraintsInitializeSetGet(): iterable { - self::assertInstanceOf( - Validator::class, - new FloatValueValidator() - ); - } - - /** - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\PropertyNotFoundException - */ - public function testConstraintsInitializeGet(): void - { - $constraints = [ - 'minFloatValue' => 0.5, - 'maxFloatValue' => 22 / 7, + yield [ + [ + 'minFloatValue' => 0.5, + 'maxFloatValue' => 22 / 7, + ], ]; - $validator = new FloatValueValidator(); - $validator->initializeWithConstraints( - $constraints - ); - self::assertSame($constraints['minFloatValue'], $validator->minFloatValue); - self::assertSame($constraints['maxFloatValue'], $validator->maxFloatValue); } /** @@ -77,38 +64,25 @@ public function testConstraintsInitializeGet(): void public function testGetConstraintsSchema(): void { $constraintsSchema = [ - 'minFloatValue' => [ + $this->getMinNumericValueName() => [ 'type' => 'float', 'default' => null, ], - 'maxFloatValue' => [ + $this->getMaxNumericValueName() => [ 'type' => 'float', 'default' => null, ], ]; - $validator = new FloatValueValidator(); + $validator = $this->getValidatorInstance(); self::assertSame($constraintsSchema, $validator->getConstraintsSchema()); } - public function testConstraintsSetGet(): void - { - $constraints = [ - 'minFloatValue' => 0.5, - 'maxFloatValue' => 22 / 7, - ]; - $validator = new FloatValueValidator(); - $validator->minFloatValue = $constraints['minFloatValue']; - $validator->maxFloatValue = $constraints['maxFloatValue']; - self::assertSame($constraints['minFloatValue'], $validator->minFloatValue); - self::assertSame($constraints['maxFloatValue'], $validator->maxFloatValue); - } - public function testInitializeBadConstraint(): void { $constraints = [ 'unexisting' => 0, ]; - $validator = new FloatValueValidator(); + $validator = $this->getValidatorInstance(); $this->expectException(PropertyNotFoundException::class); $validator->initializeWithConstraints( @@ -116,28 +90,12 @@ public function testInitializeBadConstraint(): void ); } - public function testSetBadConstraint(): void - { - $validator = new FloatValueValidator(); - - $this->expectException(PropertyNotFoundException::class); - $validator->unexisting = 0; - } - - public function testGetBadConstraint(): void - { - $validator = new FloatValueValidator(); - - $this->expectException(PropertyNotFoundException::class); - $null = $validator->unexisting; - } - /** * @dataProvider providerForValidateOK */ public function testValidateCorrectValues(float $value): void { - $validator = new FloatValueValidator(); + $validator = $this->getValidatorInstance(); $validator->minFloatValue = 10 / 7; $validator->maxFloatValue = 11 / 7; self::assertTrue($validator->validate(new FloatValue($value))); @@ -163,24 +121,11 @@ public function providerForValidateOK(): array */ public function testValidateWrongValues(float $value, string $message): void { - $validator = new FloatValueValidator(); + $validator = $this->getValidatorInstance(); $validator->minFloatValue = $this->getMinFloatValue(); $validator->maxFloatValue = $this->getMaxFloatValue(); self::assertFalse($validator->validate(new FloatValue($value))); - $messages = $validator->getMessage(); - self::assertCount(1, $messages); - self::assertInstanceOf( - ValidationError::class, - $messages[0] - ); - self::assertInstanceOf( - Message::class, - $messages[0]->getTranslatableMessage() - ); - self::assertEquals( - $message, - (string)$messages[0]->getTranslatableMessage() - ); + self::assertWrongValueValidationMessage($validator->getMessage(), $message); } /** @@ -196,144 +141,22 @@ public function providerForValidateKO(): array ]; } - /** - * Tests validation of constraints. - * - * @dataProvider providerForValidateConstraintsOK - * - * @param array $constraints - */ - public function testValidateConstraintsCorrectValues(array $constraints): void - { - $validator = new FloatValueValidator(); - - self::assertEmpty( - $validator->validateConstraints($constraints) - ); - } - - /** - * @return list}> - */ - public function providerForValidateConstraintsOK(): array - { - return [ - [ - [], - [ - 'minFloatValue' => 5, - ], - [ - 'maxFloatValue' => 2.2, - ], - [ - 'minFloatValue' => null, - 'maxFloatValue' => null, - ], - [ - 'minFloatValue' => -5, - 'maxFloatValue' => null, - ], - [ - 'minFloatValue' => null, - 'maxFloatValue' => 12.7, - ], - [ - 'minFloatValue' => 6, - 'maxFloatValue' => 8.3, - ], - ], - ]; - } - - /** - * @param array $constraints - * @param array $expectedMessages - * - * @dataProvider providerForValidateConstraintsKO - */ - public function testValidateConstraintsWrongValues(array $constraints, array $expectedMessages): void + public function providerForValidateConstraintsOK(): iterable { - $validator = new FloatValueValidator(); - $messages = $validator->validateConstraints($constraints); - - foreach ($expectedMessages as $index => $expectedMessage) { - self::assertInstanceOf( - Message::class, - $messages[0]->getTranslatableMessage() - ); - self::assertEquals( - $expectedMessage, - (string)$messages[$index]->getTranslatableMessage() - ); - } + yield [[]]; + yield [[self::MAX => 3.2]]; + yield [[self::MIN => 7]]; + yield [[self::MIN => -7, self::MAX => null]]; + yield [[self::MIN => 4, self::MAX => 4.3]]; + yield [[self::MIN => null, self::MAX => 12.7]]; + yield [[self::MIN => null, self::MAX => null]]; } - /** - * @return list, array}> - */ - public function providerForValidateConstraintsKO(): array + protected function getIncorrectNumericTypeValidationMessage(string $parameterName): string { - return [ - [ - [ - 'minFloatValue' => true, - ], - [self::MIN_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE], - ], - [ - [ - 'minFloatValue' => self::WRONG_MIN_FLOAT_VALUE, - ], - [self::MIN_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE], - ], - [ - [ - 'minFloatValue' => self::WRONG_MIN_FLOAT_VALUE, - 'maxFloatValue' => 1234, - ], - [self::MIN_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE], - ], - [ - [ - 'maxFloatValue' => new \DateTime(), - 'minFloatValue' => 1234, - ], - [self::MAX_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE], - ], - [ - [ - 'minFloatValue' => true, - 'maxFloatValue' => 1234, - ], - [self::MIN_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE], - [ - ['%parameter%' => 'minFloatValue'], - ], - ], - [ - [ - 'minFloatValue' => self::WRONG_MIN_FLOAT_VALUE, - 'maxFloatValue' => self::WRONG_MAX_FLOAT_VALUE, - ], - [ - self::MIN_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE, - self::MAX_FLOAT_VALUE_NUMERIC_TYPE_VALIDATION_MESSAGE, - ], - ], - [ - [ - 'brljix' => 12345, - ], - [self::UNKNOWN_PARAM_VALIDATION_MESSAGE], - ], - [ - [ - 'minFloatValue' => 12345, - 'brljix' => 12345, - ], - [self::UNKNOWN_PARAM_VALIDATION_MESSAGE], - ], - ]; + return sprintf( + "Validator parameter '%s' value must be of numeric type", + $parameterName + ); } } diff --git a/tests/lib/FieldType/Validator/IntegerValueValidatorTest.php b/tests/lib/FieldType/Validator/IntegerValueValidatorTest.php index 6325662a82..0caa83823b 100644 --- a/tests/lib/FieldType/Validator/IntegerValueValidatorTest.php +++ b/tests/lib/FieldType/Validator/IntegerValueValidatorTest.php @@ -8,30 +8,35 @@ namespace Ibexa\Tests\Core\FieldType\Validator; -use Ibexa\Contracts\Core\FieldType\ValidationError; use Ibexa\Contracts\Core\Repository\Exceptions\PropertyNotFoundException; -use Ibexa\Contracts\Core\Repository\Values\Translation\Message; use Ibexa\Core\FieldType\Integer\Value as IntegerValue; use Ibexa\Core\FieldType\Validator; use Ibexa\Core\FieldType\Validator\IntegerValueValidator; -use PHPUnit\Framework\TestCase; /** * @covers \Ibexa\Core\FieldType\Validator\IntegerValueValidator * + * @extends BaseNumericValidatorTestCase + * * @group fieldType * @group validator */ -final class IntegerValueValidatorTest extends TestCase +final class IntegerValueValidatorTest extends BaseNumericValidatorTestCase { - private const string VALUE_TOO_LOW_VALIDATION_MESSAGE = 'The value can not be lower than %size%.'; - private const string VALUE_TOO_HIGH_VALIDATION_MESSAGE = 'The value can not be higher than %size%.'; - private const string MIN_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE = "Validator parameter 'minIntegerValue' value must be of integer type"; - private const string MAX_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE = "Validator parameter 'maxIntegerValue' value must be of integer type"; - private const string WRONG_MIN_INT_VALUE = 'five thousand bytes'; - private const string WRONG_MAX_INT_VALUE = 'ten billion bytes'; - private const string UNKNOWN_PARAM_VALIDATION_MESSAGE = "Validator parameter 'brljix' is unknown"; - public const string SIZE_PARAM = '%size%'; + protected function getValidatorInstance(): Validator + { + return new IntegerValueValidator(); + } + + protected function getMinNumericValueName(): string + { + return 'minIntegerValue'; + } + + protected function getMaxNumericValueName(): string + { + return 'maxIntegerValue'; + } protected function getMinIntegerValue(): int { @@ -43,60 +48,32 @@ protected function getMaxIntegerValue(): int return 15; } - public function testConstructor(): void + public static function providerForConstraintsInitializeSetGet(): iterable { - self::assertInstanceOf( - Validator::class, - new IntegerValueValidator() - ); - } - - /** - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\PropertyNotFoundException - */ - public function testConstraintsInitializeGet(): void - { - $constraints = [ - 'minIntegerValue' => 0, - 'maxIntegerValue' => 100, + yield [ + [ + 'minIntegerValue' => 0, + 'maxIntegerValue' => 100, + ], ]; - $validator = new IntegerValueValidator(); - $validator->initializeWithConstraints( - $constraints - ); - self::assertSame($constraints['minIntegerValue'], $validator->minIntegerValue); - self::assertSame($constraints['maxIntegerValue'], $validator->maxIntegerValue); } public function testGetConstraintsSchema(): void { $constraintsSchema = [ - 'minIntegerValue' => [ + $this->getMinNumericValueName() => [ 'type' => 'int', 'default' => 0, ], - 'maxIntegerValue' => [ + $this->getMaxNumericValueName() => [ 'type' => 'int', 'default' => null, ], ]; - $validator = new IntegerValueValidator(); + $validator = $this->getValidatorInstance(); self::assertSame($constraintsSchema, $validator->getConstraintsSchema()); } - public function testConstraintsSetGet(): void - { - $constraints = [ - 'minIntegerValue' => 0, - 'maxIntegerValue' => 100, - ]; - $validator = new IntegerValueValidator(); - $validator->minIntegerValue = $constraints['minIntegerValue']; - $validator->maxIntegerValue = $constraints['maxIntegerValue']; - self::assertSame($constraints['minIntegerValue'], $validator->minIntegerValue); - self::assertSame($constraints['maxIntegerValue'], $validator->maxIntegerValue); - } - public function testInitializeBadConstraint(): void { $this->expectException(PropertyNotFoundException::class); @@ -104,34 +81,18 @@ public function testInitializeBadConstraint(): void $constraints = [ 'unexisting' => 0, ]; - $validator = new IntegerValueValidator(); + $validator = $this->getValidatorInstance(); $validator->initializeWithConstraints( $constraints ); } - public function testSetBadConstraint(): void - { - $validator = new IntegerValueValidator(); - - $this->expectException(PropertyNotFoundException::class); - $validator->unexisting = 0; - } - - public function testGetBadConstraint(): void - { - $validator = new IntegerValueValidator(); - - $this->expectException(PropertyNotFoundException::class); - $null = $validator->unexisting; - } - /** * @dataProvider providerForValidateOK */ public function testValidateCorrectValues(int $value): void { - $validator = new IntegerValueValidator(); + $validator = $this->getValidatorInstance(); $validator->minIntegerValue = 10; $validator->maxIntegerValue = 15; self::assertTrue($validator->validate(new IntegerValue($value))); @@ -158,26 +119,13 @@ public function providerForValidateOK(): array * * @dataProvider providerForValidateKO */ - public function testValidateWrongValues($value, $message): void + public function testValidateWrongValues(int $value, string $message): void { - $validator = new IntegerValueValidator(); + $validator = $this->getValidatorInstance(); $validator->minIntegerValue = $this->getMinIntegerValue(); $validator->maxIntegerValue = $this->getMaxIntegerValue(); self::assertFalse($validator->validate(new IntegerValue($value))); - $messages = $validator->getMessage(); - self::assertCount(1, $messages); - self::assertInstanceOf( - ValidationError::class, - $messages[0] - ); - self::assertInstanceOf( - Message::class, - $messages[0]->getTranslatableMessage() - ); - self::assertEquals( - $message, - $messages[0]->getTranslatableMessage() - ); + self::assertWrongValueValidationMessage($validator->getMessage(), $message); } /** @@ -193,139 +141,22 @@ public function providerForValidateKO(): array ]; } - /** - * @dataProvider providerForValidateConstraintsOK - * - * @param array $constraints - */ - public function testValidateConstraintsCorrectValues(array $constraints): void - { - $validator = new IntegerValueValidator(); - - self::assertEmpty( - $validator->validateConstraints($constraints) - ); - } - - /** - * @return list>> - */ - public function providerForValidateConstraintsOK(): array - { - return [ - [ - [], - [ - 'minIntegerValue' => 5, - ], - [ - 'maxIntegerValue' => 2, - ], - [ - 'minIntegerValue' => null, - 'maxIntegerValue' => null, - ], - [ - 'minIntegerValue' => -5, - 'maxIntegerValue' => null, - ], - [ - 'minIntegerValue' => null, - 'maxIntegerValue' => 12, - ], - [ - 'minIntegerValue' => 6, - 'maxIntegerValue' => 8, - ], - ], - ]; - } - - /** - * @dataProvider providerForValidateConstraintsKO - * - * @param array $constraints - * @param array $expectedMessages - */ - public function testValidateConstraintsWrongValues(array $constraints, array $expectedMessages): void + public function providerForValidateConstraintsOK(): iterable { - $validator = new IntegerValueValidator(); - $messages = $validator->validateConstraints($constraints); - - foreach ($expectedMessages as $index => $expectedMessage) { - self::assertInstanceOf( - Message::class, - $messages[0]->getTranslatableMessage() - ); - self::assertEquals( - $expectedMessage, - (string)$messages[$index]->getTranslatableMessage() - ); - } + yield [[]]; + yield [[self::MIN => 5]]; + yield [[self::MAX => 2]]; + yield [[self::MIN => null, self::MAX => null]]; + yield [[self::MIN => -5, self::MAX => null]]; + yield [[self::MIN => null, self::MAX => 12]]; + yield [[self::MIN => 6, self::MAX => 8]]; } - /** - * @return list, string[]}> - */ - public function providerForValidateConstraintsKO(): array + protected function getIncorrectNumericTypeValidationMessage(string $parameterName): string { - return [ - [ - [ - 'minIntegerValue' => true, - ], - [self::MIN_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE], - ], - [ - [ - 'minIntegerValue' => self::WRONG_MIN_INT_VALUE, - ], - [self::MIN_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE], - ], - [ - [ - 'minIntegerValue' => self::WRONG_MIN_INT_VALUE, - 'maxIntegerValue' => 1234, - ], - [self::MIN_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE], - ], - [ - [ - 'maxIntegerValue' => new \DateTime(), - 'minIntegerValue' => 1234, - ], - [self::MAX_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE], - ], - [ - [ - 'minIntegerValue' => true, - 'maxIntegerValue' => 1234, - ], - [self::MIN_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE], - ], - [ - [ - 'minIntegerValue' => self::WRONG_MIN_INT_VALUE, - 'maxIntegerValue' => self::WRONG_MAX_INT_VALUE, - ], - [ - self::MIN_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE, - self::MAX_VALUE_OF_INT_TYPE_VALIDATION_MESSAGE, - ], - ], - [ - [ - 'brljix' => 12345, - ], - [self::UNKNOWN_PARAM_VALIDATION_MESSAGE], - ], - [ - [ - 'minIntegerValue' => 12345, - 'brljix' => 12345, - ], - [self::UNKNOWN_PARAM_VALIDATION_MESSAGE], - ], - ]; + return sprintf( + "Validator parameter '%s' value must be of integer type", + $parameterName + ); } } diff --git a/tests/lib/FieldType/Validator/StringLengthValidatorTest.php b/tests/lib/FieldType/Validator/StringLengthValidatorTest.php index 8b5298a7d2..e34506e7ef 100644 --- a/tests/lib/FieldType/Validator/StringLengthValidatorTest.php +++ b/tests/lib/FieldType/Validator/StringLengthValidatorTest.php @@ -114,6 +114,7 @@ public function testSetBadConstraint(): void $validator = new StringLengthValidator(); $this->expectException(PropertyNotFoundException::class); + /** @phpstan-ignore-next-line */ $validator->unexisting = 0; } @@ -122,7 +123,8 @@ public function testGetBadConstraint(): void $validator = new StringLengthValidator(); $this->expectException(PropertyNotFoundException::class); - $null = $validator->unexisting; + /** @phpstan-ignore-next-line */ + $validator->unexisting; } /** From aa81992de8877832dc9f07215d165cb38e0b5bd4 Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Tue, 27 Aug 2024 14:44:32 +0200 Subject: [PATCH 06/10] [PHPStan] Aligned baseline with the changes --- phpstan-baseline.neon | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 7dbaaa083d..3946b38411 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -42260,11 +42260,6 @@ parameters: count: 1 path: tests/lib/FieldType/FloatTest.php - - - message: "#^Access to an undefined property Ibexa\\\\Core\\\\FieldType\\\\Validator\\\\FloatValueValidator\\:\\:\\$unexisting\\.$#" - count: 2 - path: tests/lib/FieldType/FloatValueValidatorTest.php - - message: "#^Class Symfony\\\\Component\\\\Validator\\\\ConstraintViolation constructor invoked with 1 parameter, 6\\-10 required\\.$#" count: 1 @@ -42745,21 +42740,6 @@ parameters: count: 1 path: tests/lib/FieldType/IntegerTest.php - - - message: "#^Access to an undefined property Ibexa\\\\Core\\\\FieldType\\\\Validator\\\\IntegerValueValidator\\:\\:\\$unexisting\\.$#" - count: 2 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testValidateWrongValues\\(\\) has parameter \\$message with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\IntegerValueValidatorTest\\:\\:testValidateWrongValues\\(\\) has parameter \\$value with no type specified\\.$#" - count: 1 - path: tests/lib/FieldType/IntegerValueValidatorTest.php - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\KeywordTest\\:\\:getSettingsSchemaExpectation\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 @@ -43105,11 +43085,6 @@ parameters: count: 4 path: tests/lib/FieldType/SelectionTest.php - - - message: "#^Access to an undefined property Ibexa\\\\Core\\\\FieldType\\\\Validator\\\\StringLengthValidator\\:\\:\\$unexisting\\.$#" - count: 2 - path: tests/lib/FieldType/StringLengthValidatorTest.php - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\FieldType\\\\TimeTest\\:\\:getSettingsSchemaExpectation\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 From 0521244ebaa3545f9af9052f3b9637ae712baa1b Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Wed, 28 Aug 2024 12:47:08 +0200 Subject: [PATCH 07/10] [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()])], ]; } From a887e22f53ee954528e2b6315fec9409f96e1a66 Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Wed, 28 Aug 2024 14:13:23 +0200 Subject: [PATCH 08/10] Dropped unnecessary `is_int` function import MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Paweł Niedzielski --- src/lib/FieldType/Validator/IntegerValueValidator.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/FieldType/Validator/IntegerValueValidator.php b/src/lib/FieldType/Validator/IntegerValueValidator.php index ad00d42a51..6d327e21de 100644 --- a/src/lib/FieldType/Validator/IntegerValueValidator.php +++ b/src/lib/FieldType/Validator/IntegerValueValidator.php @@ -10,7 +10,6 @@ use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition; use Ibexa\Core\FieldType\ValidationError; use Ibexa\Core\FieldType\Value as BaseValue; -use function is_int; /** * Validate ranges of integer value. From 608d4e791aca8b3e1661da7cf6d084a7cad7049b Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Wed, 28 Aug 2024 14:15:26 +0200 Subject: [PATCH 09/10] Added strict return type to `validate` methods --- src/lib/FieldType/Validator/FloatValueValidator.php | 2 +- src/lib/FieldType/Validator/IntegerValueValidator.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/FieldType/Validator/FloatValueValidator.php b/src/lib/FieldType/Validator/FloatValueValidator.php index 98dd2024a9..5feb84c222 100644 --- a/src/lib/FieldType/Validator/FloatValueValidator.php +++ b/src/lib/FieldType/Validator/FloatValueValidator.php @@ -62,7 +62,7 @@ protected function getConstraintsValidationErrorMessage(string $name, mixed $val * * @return bool */ - public function validate(BaseValue $value, ?FieldDefinition $fieldDefinition = null) + public function validate(BaseValue $value, ?FieldDefinition $fieldDefinition = null): bool { $isValid = true; diff --git a/src/lib/FieldType/Validator/IntegerValueValidator.php b/src/lib/FieldType/Validator/IntegerValueValidator.php index 6d327e21de..a9c84679ce 100644 --- a/src/lib/FieldType/Validator/IntegerValueValidator.php +++ b/src/lib/FieldType/Validator/IntegerValueValidator.php @@ -58,7 +58,7 @@ protected function getConstraintsValidationErrorMessage(string $name, mixed $val * * @return bool */ - public function validate(BaseValue $value, ?FieldDefinition $fieldDefinition = null) + public function validate(BaseValue $value, ?FieldDefinition $fieldDefinition = null): bool { $isValid = true; From 8493436ef6b6c79685e29b14644b6eef9bf6e2ef Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Wed, 28 Aug 2024 14:19:28 +0200 Subject: [PATCH 10/10] [PHPDoc] Simplified Validator classes `validate` method doc --- src/lib/FieldType/Validator.php | 2 -- src/lib/FieldType/Validator/FloatValueValidator.php | 11 ----------- src/lib/FieldType/Validator/IntegerValueValidator.php | 10 ---------- 3 files changed, 23 deletions(-) diff --git a/src/lib/FieldType/Validator.php b/src/lib/FieldType/Validator.php index 225ec981b2..870e9fb8bd 100644 --- a/src/lib/FieldType/Validator.php +++ b/src/lib/FieldType/Validator.php @@ -96,8 +96,6 @@ abstract public function validateConstraints($constraints); * When a check against a constraint has failed, an entry will be added to the * $errors array. * - * @param \Ibexa\Core\FieldType\Value $value - * * @return bool */ abstract public function validate(Value $value, ?FieldDefinition $fieldDefinition = null); diff --git a/src/lib/FieldType/Validator/FloatValueValidator.php b/src/lib/FieldType/Validator/FloatValueValidator.php index 5feb84c222..69da8f811c 100644 --- a/src/lib/FieldType/Validator/FloatValueValidator.php +++ b/src/lib/FieldType/Validator/FloatValueValidator.php @@ -9,7 +9,6 @@ use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition; use Ibexa\Core\FieldType\ValidationError; -use Ibexa\Core\FieldType\Validator; use Ibexa\Core\FieldType\Value as BaseValue; /** @@ -50,17 +49,7 @@ protected function getConstraintsValidationErrorMessage(string $name, mixed $val } /** - * Perform validation on $value. - * - * Will return true when all constraints are matched. If one or more - * constraints fail, the method will return false. - * - * When a check against a constant has failed, an entry will be added to the - * $errors array. - * * @param \Ibexa\Core\FieldType\Float\Value $value - * - * @return bool */ public function validate(BaseValue $value, ?FieldDefinition $fieldDefinition = null): bool { diff --git a/src/lib/FieldType/Validator/IntegerValueValidator.php b/src/lib/FieldType/Validator/IntegerValueValidator.php index a9c84679ce..53fdb66e08 100644 --- a/src/lib/FieldType/Validator/IntegerValueValidator.php +++ b/src/lib/FieldType/Validator/IntegerValueValidator.php @@ -46,17 +46,7 @@ protected function getConstraintsValidationErrorMessage(string $name, mixed $val } /** - * Perform validation on $value. - * - * Will return true when all constraints are matched. If one or more - * constraints fail, the method will return false. - * - * When a check against a constraint has failed, an entry will be added to the - * $errors array. - * * @param \Ibexa\Core\FieldType\Integer\Value $value - * - * @return bool */ public function validate(BaseValue $value, ?FieldDefinition $fieldDefinition = null): bool {