diff --git a/src/ZipCodeValidator/Constraints/ZipCodeValidator.php b/src/ZipCodeValidator/Constraints/ZipCodeValidator.php index a1a141c..8f4cc01 100644 --- a/src/ZipCodeValidator/Constraints/ZipCodeValidator.php +++ b/src/ZipCodeValidator/Constraints/ZipCodeValidator.php @@ -126,7 +126,7 @@ class ZipCodeValidator extends ConstraintValidator 'LR' => '\\d{4}', 'LS' => '\\d{3}', 'LT' => '(LT-)?\\d{5}', - 'LU' => '\\d{4}', + 'LU' => '(L-)?\\d{4}', 'LV' => '(LV-)?\\d{4}', 'MA' => '\\d{5}', 'MC' => '980\\d{2}', diff --git a/tests/Constraints/LuZipCodeValidatorTest.php b/tests/Constraints/LuZipCodeValidatorTest.php new file mode 100644 index 0000000..78ecc2e --- /dev/null +++ b/tests/Constraints/LuZipCodeValidatorTest.php @@ -0,0 +1,91 @@ +validator = new ZipCodeValidator; + } + + /** + * @dataProvider getValidLuxembourgZipCodes + */ + public function testValidZipcodes(string $zipCode): void + { + $constraint = new ZipCode('LU'); + + /** @var ExecutionContext|MockObject $contextMock */ + $contextMock = $this->getMockBuilder(ExecutionContext::class) + ->disableOriginalConstructor() + ->getMock(); + + # be sure that buildViolation never gets called + $contextMock->expects($this->never())->method('buildViolation'); + $contextMock->setConstraint($constraint); + + $this->validator->initialize($contextMock); + $this->validator->validate($zipCode, $constraint); + } + + /** + * Valid Luxembourg postal codes are four-digit numbers, optionally prefixed with "L-". + * @see https://www.post.lu/fr/particuliers/colis-courrier/bien-rediger-une-adresse + */ + public static function getValidLuxembourgZipCodes(): array + { + return [ + ['8211'], + ['L-8211'], + ['1234'], + ['L-1234'], + ]; + } + + /** + * @dataProvider getInvalidLuxembourgZipCodes + */ + public function testInvalidZipcodes(string $zipCode): void + { + $constraint = new ZipCode('LU'); + + $violation = $this->createMock(ConstraintViolationBuilderInterface::class); + $violation->expects($this->once())->method('setParameter')->willReturnSelf(); + + /** @var ExecutionContext|MockObject $contextMock */ + $contextMock = $this->getMockBuilder(ExecutionContext::class) + ->disableOriginalConstructor() + ->getMock(); + + $contextMock->expects($this->once())->method('buildViolation')->willReturn($violation); + $contextMock->setConstraint($constraint); + + $this->validator->initialize($contextMock); + $this->validator->validate($zipCode, $constraint); + } + + /** + * Valid Luxembourg postal codes are four-digit numbers, optionally prefixed with "L-". + * @see https://www.post.lu/fr/particuliers/colis-courrier/bien-rediger-une-adresse + */ + public static function getInvalidLuxembourgZipCodes(): array + { + return [ + ['123'], + ['12345'], + ['L1234'], + ]; + } +}