diff --git a/src/Validator/Constraints/BelgianEnterpriseNumber.php b/src/Validator/Constraints/BelgianEnterpriseNumber.php new file mode 100644 index 0000000..3902969 --- /dev/null +++ b/src/Validator/Constraints/BelgianEnterpriseNumber.php @@ -0,0 +1,14 @@ +context->buildViolation($constraint::MESSAGE) + ->setParameter('{{ value }}', $this->formatValue($value)) + ->setCode($constraint::WRONG_FORMAT_ERROR) + ->addViolation(); + } + } +} diff --git a/tests/Validator/Constraints/BelgianEnterpriseNumberValidatorTest.php b/tests/Validator/Constraints/BelgianEnterpriseNumberValidatorTest.php new file mode 100644 index 0000000..6192587 --- /dev/null +++ b/tests/Validator/Constraints/BelgianEnterpriseNumberValidatorTest.php @@ -0,0 +1,52 @@ + + */ +class BelgianEnterpriseNumberValidatorTest extends ConstraintValidatorTestCase +{ + protected function getConstraint(): Constraint + { + return new BelgianEnterpriseNumber(); + } + + public function createValidator(): ConstraintValidator + { + return new BelgianEnterpriseNumberValidator(); + } + + public static function providerValidValues(): iterable + { + yield 'null value' => [null]; + yield 'empty string' => ['']; + yield 'valid number starting with 0' => ['0123456789']; + yield 'valid number starting with 1' => ['1234567890']; + yield 'valid number starting with 0 all zeros' => ['0000000000']; + yield 'valid number starting with 1 all digits' => ['1999999999']; + } + + public static function providerInvalidValues(): iterable + { + $expectedMessage = BelgianEnterpriseNumber::MESSAGE; + $code = BelgianEnterpriseNumber::WRONG_FORMAT_ERROR; + + yield 'too short' => ['012345678', $code, $expectedMessage]; + yield 'too long' => ['01234567890', $code, $expectedMessage]; + yield 'starts with 2' => ['2123456789', $code, $expectedMessage]; + yield 'starts with 9' => ['9123456789', $code, $expectedMessage]; + yield 'contains letters' => ['012345678A', $code, $expectedMessage]; + yield 'contains special characters' => ['01234-6789', $code, $expectedMessage]; + yield 'contains spaces' => ['0123 56789', $code, $expectedMessage]; + yield 'single digit' => ['0', $code, $expectedMessage]; + } +}