diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 94e053d031..3946b38411 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 @@ -42295,136 +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: "#^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 @@ -42905,136 +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: "#^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 - 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\\\\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 @@ -43380,146 +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: "#^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 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..870e9fb8bd 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); @@ -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/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..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; /** @@ -21,7 +20,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,73 +38,43 @@ 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", + }; } /** - * 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) + public function validate(BaseValue $value, ?FieldDefinition $fieldDefinition = null): bool { $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..53fdb66e08 100644 --- a/src/lib/FieldType/Validator/IntegerValueValidator.php +++ b/src/lib/FieldType/Validator/IntegerValueValidator.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; /** @@ -18,7 +17,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,58 +35,20 @@ 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", + }; } /** - * 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) + public function validate(BaseValue $value, ?FieldDefinition $fieldDefinition = null): bool { $isValid = true; @@ -97,7 +58,8 @@ public function validate(BaseValue $value, ?FieldDefinition $fieldDefinition = n null, [ '%size%' => $this->constraints['maxIntegerValue'], - ] + ], + 'value' ); $isValid = false; } @@ -108,7 +70,8 @@ public function validate(BaseValue $value, ?FieldDefinition $fieldDefinition = n null, [ '%size%' => $this->constraints['minIntegerValue'], - ] + ], + 'value' ); $isValid = false; } diff --git a/tests/lib/FieldType/FloatValueValidatorTest.php b/tests/lib/FieldType/FloatValueValidatorTest.php deleted file mode 100644 index 4daa6123fe..0000000000 --- a/tests/lib/FieldType/FloatValueValidatorTest.php +++ /dev/null @@ -1,387 +0,0 @@ - 0.5, - 'maxFloatValue' => 22 / 7, - ]; - $validator = new FloatValueValidator(); - $validator->initializeWithConstraints( - $constraints - ); - self::assertSame($constraints['minFloatValue'], $validator->minFloatValue); - self::assertSame($constraints['maxFloatValue'], $validator->maxFloatValue); - } - - /** - * Test getting constraints schema. - * - * @covers \Ibexa\Core\FieldType\Validator::getConstraintsSchema - */ - public function testGetConstraintsSchema() - { - $constraintsSchema = [ - 'minFloatValue' => [ - 'type' => 'float', - 'default' => null, - ], - 'maxFloatValue' => [ - 'type' => 'float', - 'default' => null, - ], - ]; - $validator = new FloatValueValidator(); - 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() - { - $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); - } - - /** - * Tests initializing with a wrong constraint. - * - * @covers \Ibexa\Core\FieldType\Validator::initializeWithConstraints - */ - public function testInitializeBadConstraint() - { - $this->expectException(PropertyNotFoundException::class); - - $constraints = [ - 'unexisting' => 0, - ]; - $validator = new FloatValueValidator(); - $validator->initializeWithConstraints( - $constraints - ); - } - - /** - * Tests setting a wrong constraint. - * - * @covers \Ibexa\Core\FieldType\Validator::__set - */ - public function testSetBadConstraint() - { - $this->expectException(PropertyNotFoundException::class); - - $validator = new FloatValueValidator(); - $validator->unexisting = 0; - } - - /** - * Tests getting a wrong constraint. - * - * @covers \Ibexa\Core\FieldType\Validator::__get - */ - public function testGetBadConstraint() - { - $this->expectException(PropertyNotFoundException::class); - - $validator = new FloatValueValidator(); - $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) - { - $validator = new FloatValueValidator(); - $validator->minFloatValue = 10 / 7; - $validator->maxFloatValue = 11 / 7; - self::assertTrue($validator->validate(new FloatValue($value))); - self::assertSame([], $validator->getMessage()); - } - - public function providerForValidateOK() - { - return [ - [100 / 70], - [101 / 70], - [105 / 70], - [109 / 70], - [110 / 70], - ]; - } - - /** - * Tests validating a wrong value. - * - * @dataProvider providerForValidateKO - * - * @covers \Ibexa\Core\FieldType\Validator\FloatValueValidator::validate - */ - public function testValidateWrongValues($value, $message, $values) - { - $validator = new FloatValueValidator(); - $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, - $messages[0]->getTranslatableMessage()->message - ); - self::assertEquals( - $values, - $messages[0]->getTranslatableMessage()->values - ); - } - - public function providerForValidateKO() - { - 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()]], - ]; - } - - /** - * Tests validation of constraints. - * - * @dataProvider providerForValidateConstraintsOK - * - * @covers \Ibexa\Core\FieldType\Validator\FileSizeValidator::validateConstraints - */ - public function testValidateConstraintsCorrectValues($constraints) - { - $validator = new FloatValueValidator(); - - self::assertEmpty( - $validator->validateConstraints($constraints) - ); - } - - public function providerForValidateConstraintsOK() - { - return [ - [ - [], - [ - 'minFloatValue' => 5, - ], - [ - 'maxFloatValue' => 2.2, - ], - [ - 'minFloatValue' => null, - 'maxFloatValue' => null, - ], - [ - 'minFloatValue' => -5, - 'maxFloatValue' => null, - ], - [ - 'minFloatValue' => null, - 'maxFloatValue' => 12.7, - ], - [ - 'minFloatValue' => 6, - 'maxFloatValue' => 8.3, - ], - ], - ]; - } - - /** - * Tests validation of constraints. - * - * @dataProvider providerForValidateConstraintsKO - * - * @covers \Ibexa\Core\FieldType\Validator\FileSizeValidator::validateConstraints - */ - public function testValidateConstraintsWrongValues($constraints, $expectedMessages, $values) - { - $validator = new FloatValueValidator(); - $messages = $validator->validateConstraints($constraints); - - foreach ($expectedMessages as $index => $expectedMessage) { - self::assertInstanceOf( - Message::class, - $messages[0]->getTranslatableMessage() - ); - self::assertEquals( - $expectedMessage, - $messages[$index]->getTranslatableMessage()->message - ); - self::assertEquals( - $values[$index], - $messages[$index]->getTranslatableMessage()->values - ); - } - } - - public function providerForValidateConstraintsKO() - { - return [ - [ - [ - 'minFloatValue' => true, - ], - ["Validator parameter '%parameter%' value must be of numeric type"], - [ - ['%parameter%' => 'minFloatValue'], - ], - ], - [ - [ - 'minFloatValue' => 'five thousand bytes', - ], - ["Validator parameter '%parameter%' value must be of numeric type"], - [ - ['%parameter%' => 'minFloatValue'], - ], - ], - [ - [ - 'minFloatValue' => 'five thousand bytes', - 'maxFloatValue' => 1234, - ], - ["Validator parameter '%parameter%' value must be of numeric type"], - [ - ['%parameter%' => 'minFloatValue'], - ], - ], - [ - [ - 'maxFloatValue' => new \DateTime(), - 'minFloatValue' => 1234, - ], - ["Validator parameter '%parameter%' value must be of numeric type"], - [ - ['%parameter%' => 'maxFloatValue'], - ], - ], - [ - [ - 'minFloatValue' => true, - 'maxFloatValue' => 1234, - ], - ["Validator parameter '%parameter%' value must be of numeric type"], - [ - ['%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", - ], - [ - ['%parameter%' => 'minFloatValue'], - ['%parameter%' => 'maxFloatValue'], - ], - ], - [ - [ - 'brljix' => 12345, - ], - ["Validator parameter '%parameter%' is unknown"], - [ - ['%parameter%' => 'brljix'], - ], - ], - [ - [ - 'minFloatValue' => 12345, - 'brljix' => 12345, - ], - ["Validator parameter '%parameter%' is unknown"], - [ - ['%parameter%' => 'brljix'], - ], - ], - ]; - } -} diff --git a/tests/lib/FieldType/IntegerValueValidatorTest.php b/tests/lib/FieldType/IntegerValueValidatorTest.php deleted file mode 100644 index a567420109..0000000000 --- a/tests/lib/FieldType/IntegerValueValidatorTest.php +++ /dev/null @@ -1,389 +0,0 @@ - 0, - 'maxIntegerValue' => 100, - ]; - $validator = new IntegerValueValidator(); - $validator->initializeWithConstraints( - $constraints - ); - self::assertSame($constraints['minIntegerValue'], $validator->minIntegerValue); - self::assertSame($constraints['maxIntegerValue'], $validator->maxIntegerValue); - } - - /** - * Test getting constraints schema. - * - * @covers \Ibexa\Core\FieldType\Validator::getConstraintsSchema - */ - public function testGetConstraintsSchema() - { - $constraintsSchema = [ - 'minIntegerValue' => [ - 'type' => 'int', - 'default' => 0, - ], - 'maxIntegerValue' => [ - 'type' => 'int', - 'default' => null, - ], - ]; - $validator = new IntegerValueValidator(); - 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() - { - $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); - } - - /** - * Tests initializing with a wrong constraint. - * - * @covers \Ibexa\Core\FieldType\Validator::initializeWithConstraints - */ - public function testInitializeBadConstraint() - { - $this->expectException(PropertyNotFoundException::class); - - $constraints = [ - 'unexisting' => 0, - ]; - $validator = new IntegerValueValidator(); - $validator->initializeWithConstraints( - $constraints - ); - } - - /** - * Tests setting a wrong constraint. - * - * @covers \Ibexa\Core\FieldType\Validator::__set - */ - public function testSetBadConstraint() - { - $this->expectException(PropertyNotFoundException::class); - - $validator = new IntegerValueValidator(); - $validator->unexisting = 0; - } - - /** - * Tests getting a wrong constraint. - * - * @covers \Ibexa\Core\FieldType\Validator::__get - */ - public function testGetBadConstraint() - { - $this->expectException(PropertyNotFoundException::class); - - $validator = new IntegerValueValidator(); - $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) - { - $validator = new IntegerValueValidator(); - $validator->minIntegerValue = 10; - $validator->maxIntegerValue = 15; - self::assertTrue($validator->validate(new IntegerValue($value))); - self::assertSame([], $validator->getMessage()); - } - - public function providerForValidateOK() - { - return [ - [10], - [11], - [12], - [12.5], - [13], - [14], - [15], - ]; - } - - /** - * Tests validating a wrong value. - * - * @dataProvider providerForValidateKO - * - * @covers \Ibexa\Core\FieldType\Validator\IntegerValueValidator::validate - */ - public function testValidateWrongValues($value, $message, $values) - { - $validator = new IntegerValueValidator(); - $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()->message - ); - self::assertEquals( - $values, - $messages[0]->getTranslatableMessage()->values - ); - } - - public function providerForValidateKO() - { - 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()]], - ]; - } - - /** - * Tests validation of constraints. - * - * @dataProvider providerForValidateConstraintsOK - * - * @covers \Ibexa\Core\FieldType\Validator\FileSizeValidator::validateConstraints - */ - public function testValidateConstraintsCorrectValues($constraints) - { - $validator = new IntegerValueValidator(); - - self::assertEmpty( - $validator->validateConstraints($constraints) - ); - } - - public function providerForValidateConstraintsOK() - { - return [ - [ - [], - [ - 'minIntegerValue' => 5, - ], - [ - 'maxIntegerValue' => 2, - ], - [ - 'minIntegerValue' => null, - 'maxIntegerValue' => null, - ], - [ - 'minIntegerValue' => -5, - 'maxIntegerValue' => null, - ], - [ - 'minIntegerValue' => null, - 'maxIntegerValue' => 12, - ], - [ - 'minIntegerValue' => 6, - 'maxIntegerValue' => 8, - ], - ], - ]; - } - - /** - * Tests validation of constraints. - * - * @dataProvider providerForValidateConstraintsKO - * - * @covers \Ibexa\Core\FieldType\Validator\FileSizeValidator::validateConstraints - */ - public function testValidateConstraintsWrongValues($constraints, $expectedMessages, $values) - { - $validator = new IntegerValueValidator(); - $messages = $validator->validateConstraints($constraints); - - foreach ($expectedMessages as $index => $expectedMessage) { - self::assertInstanceOf( - Message::class, - $messages[0]->getTranslatableMessage() - ); - self::assertEquals( - $expectedMessage, - $messages[$index]->getTranslatableMessage()->message - ); - self::assertEquals( - $values[$index], - $messages[$index]->getTranslatableMessage()->values - ); - } - } - - public function providerForValidateConstraintsKO() - { - return [ - [ - [ - 'minIntegerValue' => true, - ], - ["Validator parameter '%parameter%' value must be of integer type"], - [ - ['%parameter%' => 'minIntegerValue'], - ], - ], - [ - [ - 'minIntegerValue' => 'five thousand bytes', - ], - ["Validator parameter '%parameter%' value must be of integer type"], - [ - ['%parameter%' => 'minIntegerValue'], - ], - ], - [ - [ - 'minIntegerValue' => 'five thousand bytes', - 'maxIntegerValue' => 1234, - ], - ["Validator parameter '%parameter%' value must be of integer type"], - [ - ['%parameter%' => 'minIntegerValue'], - ], - ], - [ - [ - 'maxIntegerValue' => new \DateTime(), - 'minIntegerValue' => 1234, - ], - ["Validator parameter '%parameter%' value must be of integer type"], - [ - ['%parameter%' => 'maxIntegerValue'], - ], - ], - [ - [ - 'minIntegerValue' => true, - 'maxIntegerValue' => 1234, - ], - ["Validator parameter '%parameter%' value must be of integer type"], - [ - ['%parameter%' => 'minIntegerValue'], - ], - ], - [ - [ - '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", - ], - [ - ['%parameter%' => 'minIntegerValue'], - ['%parameter%' => 'maxIntegerValue'], - ], - ], - [ - [ - 'brljix' => 12345, - ], - ["Validator parameter '%parameter%' is unknown"], - [ - ['%parameter%' => 'brljix'], - ], - ], - [ - [ - 'minIntegerValue' => 12345, - 'brljix' => 12345, - ], - ["Validator parameter '%parameter%' is unknown"], - [ - ['%parameter%' => 'brljix'], - ], - ], - ]; - } -} diff --git a/tests/lib/FieldType/Validator/BaseNumericValidatorTestCase.php b/tests/lib/FieldType/Validator/BaseNumericValidatorTestCase.php new file mode 100644 index 0000000000..a664bfc87c --- /dev/null +++ b/tests/lib/FieldType/Validator/BaseNumericValidatorTestCase.php @@ -0,0 +1,253 @@ + + */ + abstract public function providerForValidateConstraintsOK(): iterable; + + /** + * @return iterable>> + */ + abstract public static function providerForConstraintsInitializeSetGet(): iterable; + + abstract protected function getIncorrectNumericTypeValidationMessage(string $parameterName): string; + + /** + * @return iterable, array}> + */ + final public function providerForValidateConstraintsKO(): iterable + { + $minNumericValueName = $this->getMinNumericValueName(); + $minValueNumericTypeValidationMessage = $this->getIncorrectNumericTypeValidationMessage( + $minNumericValueName + ); + $maxNumericValueName = $this->getMaxNumericValueName(); + $maxValueNumericTypeValidationMessage = $this->getIncorrectNumericTypeValidationMessage( + $maxNumericValueName + ); + + yield 'invalid min type (bool), max not set' => [ + [ + $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)' => [ + [ + $maxNumericValueName => new \DateTime(), + $minNumericValueName => 1234, + ], + [$maxValueNumericTypeValidationMessage], + ]; + + yield 'invalid min type (bool), valid max, with a parameter' => [ + [ + $minNumericValueName => true, + $maxNumericValueName => 1234, + ], + [$minValueNumericTypeValidationMessage], + [ + ['%parameter%' => $minNumericValueName], + ], + ]; + + yield 'invalid min and max types (strings)' => [ + [ + $minNumericValueName => self::WRONG_NUMERIC_MIN_VALUE, + $maxNumericValueName => self::WRONG_NUMERIC_MAX_VALUE, + ], + [ + $minValueNumericTypeValidationMessage, + $maxValueNumericTypeValidationMessage, + ], + ]; + + yield 'unknown parameter' => [ + [ + 'brljix' => 12345, + ], + [self::UNKNOWN_PARAM_VALIDATION_MESSAGE], + ]; + + yield 'unknown parameter, valid min' => [ + [ + $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 new file mode 100644 index 0000000000..92b8db737c --- /dev/null +++ b/tests/lib/FieldType/Validator/FloatValueValidatorTest.php @@ -0,0 +1,178 @@ + + * + * @group fieldType + * @group validator + */ +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(); + } + + protected function getMinNumericValueName(): string + { + return 'minFloatValue'; + } + + protected function getMaxNumericValueName(): string + { + return 'maxFloatValue'; + } + + protected function getMinFloatValue(): float + { + return self::MIN_FLOAT_VALUE; + } + + protected function getMaxFloatValue(): float + { + return self::MAX_FLOAT_VALUE; + } + + public static function providerForConstraintsInitializeSetGet(): iterable + { + yield [ + [ + 'minFloatValue' => 0.5, + 'maxFloatValue' => 3.1428571428571, + ], + ]; + } + + /** + * Test getting constraints schema. + */ + public function testGetConstraintsSchema(): void + { + $constraintsSchema = [ + $this->getMinNumericValueName() => [ + 'type' => 'float', + 'default' => null, + ], + $this->getMaxNumericValueName() => [ + 'type' => 'float', + 'default' => null, + ], + ]; + $validator = $this->getValidatorInstance(); + self::assertSame($constraintsSchema, $validator->getConstraintsSchema()); + } + + public function testInitializeBadConstraint(): void + { + $constraints = [ + 'unexisting' => 0, + ]; + $validator = $this->getValidatorInstance(); + + $this->expectException(PropertyNotFoundException::class); + $validator->initializeWithConstraints( + $constraints + ); + } + + /** + * @dataProvider providerForValidateOK + */ + public function testValidateCorrectValues(float $value): void + { + $validator = $this->getValidatorInstance(); + $validator->minFloatValue = self::MIN_FLOAT_VALUE; + $validator->maxFloatValue = self::MAX_FLOAT_VALUE; + self::assertTrue($validator->validate(new FloatValue($value))); + self::assertSame([], $validator->getMessage()); + } + + /** + * @return list + */ + public function providerForValidateOK(): array + { + return [ + [ + 1.4285714285714286, + ], + [ + 1.4428571428571428, + ], + [ + 1.5, + ], + [ + 1.5571428571428572, + ], + [ + 1.5714285714285714, + ], + ]; + } + + /** + * @dataProvider providerForValidateKO + */ + public function testValidateWrongValues(float $value, string $message): void + { + $validator = $this->getValidatorInstance(); + $validator->minFloatValue = $this->getMinFloatValue(); + $validator->maxFloatValue = $this->getMaxFloatValue(); + self::assertFalse($validator->validate(new FloatValue($value))); + self::assertWrongValueValidationMessage($validator->getMessage(), $message); + } + + /** + * @return list + */ + public function providerForValidateKO(): array + { + return [ + [-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()])], + [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()])], + ]; + } + + public function providerForValidateConstraintsOK(): iterable + { + 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]]; + } + + protected function getIncorrectNumericTypeValidationMessage(string $parameterName): string + { + 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 new file mode 100644 index 0000000000..0caa83823b --- /dev/null +++ b/tests/lib/FieldType/Validator/IntegerValueValidatorTest.php @@ -0,0 +1,162 @@ + + * + * @group fieldType + * @group validator + */ +final class IntegerValueValidatorTest extends BaseNumericValidatorTestCase +{ + protected function getValidatorInstance(): Validator + { + return new IntegerValueValidator(); + } + + protected function getMinNumericValueName(): string + { + return 'minIntegerValue'; + } + + protected function getMaxNumericValueName(): string + { + return 'maxIntegerValue'; + } + + protected function getMinIntegerValue(): int + { + return 10; + } + + protected function getMaxIntegerValue(): int + { + return 15; + } + + public static function providerForConstraintsInitializeSetGet(): iterable + { + yield [ + [ + 'minIntegerValue' => 0, + 'maxIntegerValue' => 100, + ], + ]; + } + + public function testGetConstraintsSchema(): void + { + $constraintsSchema = [ + $this->getMinNumericValueName() => [ + 'type' => 'int', + 'default' => 0, + ], + $this->getMaxNumericValueName() => [ + 'type' => 'int', + 'default' => null, + ], + ]; + $validator = $this->getValidatorInstance(); + self::assertSame($constraintsSchema, $validator->getConstraintsSchema()); + } + + public function testInitializeBadConstraint(): void + { + $this->expectException(PropertyNotFoundException::class); + + $constraints = [ + 'unexisting' => 0, + ]; + $validator = $this->getValidatorInstance(); + $validator->initializeWithConstraints( + $constraints + ); + } + + /** + * @dataProvider providerForValidateOK + */ + public function testValidateCorrectValues(int $value): void + { + $validator = $this->getValidatorInstance(); + $validator->minIntegerValue = 10; + $validator->maxIntegerValue = 15; + self::assertTrue($validator->validate(new IntegerValue($value))); + self::assertSame([], $validator->getMessage()); + } + + /** + * @return list + */ + public function providerForValidateOK(): array + { + return [ + [10], + [11], + [12], + [13], + [14], + [15], + ]; + } + + /** + * Tests validating a wrong value. + * + * @dataProvider providerForValidateKO + */ + public function testValidateWrongValues(int $value, string $message): void + { + $validator = $this->getValidatorInstance(); + $validator->minIntegerValue = $this->getMinIntegerValue(); + $validator->maxIntegerValue = $this->getMaxIntegerValue(); + self::assertFalse($validator->validate(new IntegerValue($value))); + self::assertWrongValueValidationMessage($validator->getMessage(), $message); + } + + /** + * @return list + */ + public function providerForValidateKO(): array + { + return [ + [-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()])], + ]; + } + + public function providerForValidateConstraintsOK(): iterable + { + 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]]; + } + + protected function getIncorrectNumericTypeValidationMessage(string $parameterName): string + { + return sprintf( + "Validator parameter '%s' value must be of integer type", + $parameterName + ); + } +} diff --git a/tests/lib/FieldType/StringLengthValidatorTest.php b/tests/lib/FieldType/Validator/StringLengthValidatorTest.php similarity index 50% rename from tests/lib/FieldType/StringLengthValidatorTest.php rename to tests/lib/FieldType/Validator/StringLengthValidatorTest.php index 64469531b4..e34506e7ef 100644 --- a/tests/lib/FieldType/StringLengthValidatorTest.php +++ b/tests/lib/FieldType/Validator/StringLengthValidatorTest.php @@ -4,8 +4,9 @@ * @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; +namespace Ibexa\Tests\Core\FieldType\Validator; use Ibexa\Contracts\Core\FieldType\ValidationError; use Ibexa\Contracts\Core\Repository\Exceptions\PropertyNotFoundException; @@ -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,41 @@ 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); + /** @phpstan-ignore-next-line */ $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(); - $null = $validator->unexisting; + + $this->expectException(PropertyNotFoundException::class); + /** @phpstan-ignore-next-line */ + $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 +139,10 @@ public function testValidateCorrectValues($value) self::assertSame([], $validator->getMessage()); } - public function providerForValidateOK() + /** + * @return list + */ + public function providerForValidateOK(): array { return [ ['hello'], @@ -181,17 +153,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 +176,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 +243,10 @@ public function testValidateConstraintsCorrectValues($constraints) ); } - public function providerForValidateConstraintsOK() + /** + * @return list>> + */ + public function providerForValidateConstraintsOK(): array { return [ [ @@ -295,13 +278,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 +291,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"], ], ]; }