From bf59b80d806db9f899b0321a7cdf5365d4e238f5 Mon Sep 17 00:00:00 2001 From: tcoch Date: Wed, 14 May 2025 16:12:06 +0200 Subject: [PATCH 1/6] Add tests for MAC address known as not valid --- .../Test/ConstraintValidatorTestCase.php | 5 ++++ .../Constraints/MacAddressValidatorTest.php | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php b/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php index 2fe70eaef4569..c1291b6b21b67 100644 --- a/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php +++ b/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php @@ -282,6 +282,11 @@ protected function assertNoViolation() $this->assertSame(0, $violationsCount = \count($this->context->getViolations()), \sprintf('0 violation expected. Got %u.', $violationsCount)); } + protected function assertViolation() + { + $this->assertGreaterThan(0, $violationsCount = \count($this->context->getViolations()), \sprintf('Some violation(s) expected. Got %u.', $violationsCount)); + } + protected function buildViolation(string|\Stringable $message): ConstraintViolationAssertion { return new ConstraintViolationAssertion($this->context, $message, $this->constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php index d755df486e140..203444f0cbab6 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php @@ -63,6 +63,16 @@ public function testValidMac($mac) $this->assertNoViolation(); } + /** + * @dataProvider getNotValidMacs + */ + public function testNotValidMac($mac) + { + $this->validator->validate($mac, new MacAddress()); + + $this->assertViolation(); + } + public static function getValidMacs(): array { return [ @@ -73,6 +83,19 @@ public static function getValidMacs(): array ['FF:FF:FF:FF:FF:FF'], ['FF-FF-FF-FF-FF-FF'], ['FFFF.FFFF.FFFF'], + [''], + [null] + ]; + } + + public static function getNotValidMacs(): array + { + return [ + ['00:00:00:00:00'], + ['00:00:00:00:00:0G'], + ['GG:GG:GG:GG:GG:GG'], + ['GG-GG-GG-GG-GG-GG'], + ['GGGG.GGGG.GGGG'], ]; } From 86a78322c812601d4c6801501b794f94d4eb4cab Mon Sep 17 00:00:00 2001 From: tcoch Date: Wed, 14 May 2025 17:11:50 +0200 Subject: [PATCH 2/6] Remove empty and null, handled in other methods --- .../Validator/Tests/Constraints/MacAddressValidatorTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php index 203444f0cbab6..586b233df027c 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php @@ -82,9 +82,7 @@ public static function getValidMacs(): array ['ff-ff-ff-ff-ff-ff'], ['FF:FF:FF:FF:FF:FF'], ['FF-FF-FF-FF-FF-FF'], - ['FFFF.FFFF.FFFF'], - [''], - [null] + ['FFFF.FFFF.FFFF'] ]; } From 7efbd02f1dc648727a76ec1d76cfb85a11c7f389 Mon Sep 17 00:00:00 2001 From: tcoch Date: Tue, 20 May 2025 14:11:29 +0200 Subject: [PATCH 3/6] Update changelog - add method assertViolation in ConstraintValidatorTestCase --- src/Symfony/Component/Validator/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index ae1ae20da804d..3d60fc68cafcc 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -13,6 +13,7 @@ CHANGELOG * Add support for multiple fields containing nested constraints in `Composite` constraints * Add the `stopOnFirstError` option to the `Unique` constraint to validate all elements * Add support for closures in the `When` constraint + * Add method `assertViolation` to `ConstraintValidatorTestCase` 7.2 --- From 576df8d0a28ff5d798beba1bf8ddc34b431382cd Mon Sep 17 00:00:00 2001 From: tcoch Date: Tue, 20 May 2025 14:13:56 +0200 Subject: [PATCH 4/6] Add PHPdoc --- .../Component/Validator/Test/ConstraintValidatorTestCase.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php b/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php index c1291b6b21b67..4c4acfb20f52c 100644 --- a/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php +++ b/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php @@ -282,7 +282,10 @@ protected function assertNoViolation() $this->assertSame(0, $violationsCount = \count($this->context->getViolations()), \sprintf('0 violation expected. Got %u.', $violationsCount)); } - protected function assertViolation() + /** + * Assert if at least one violation is raised + */ + protected function assertViolation(): void { $this->assertGreaterThan(0, $violationsCount = \count($this->context->getViolations()), \sprintf('Some violation(s) expected. Got %u.', $violationsCount)); } From 73295a37877d98d5204275b6eb991370df1007f4 Mon Sep 17 00:00:00 2001 From: tcoch Date: Tue, 20 May 2025 14:26:55 +0200 Subject: [PATCH 5/6] Fix return type - using annotation --- .../Component/Validator/Test/ConstraintValidatorTestCase.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php b/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php index 4c4acfb20f52c..68bc72ee44915 100644 --- a/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php +++ b/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php @@ -284,8 +284,9 @@ protected function assertNoViolation() /** * Assert if at least one violation is raised + * @return void */ - protected function assertViolation(): void + protected function assertViolation() { $this->assertGreaterThan(0, $violationsCount = \count($this->context->getViolations()), \sprintf('Some violation(s) expected. Got %u.', $violationsCount)); } From 34b03723ba8635338194eedc365af90751cdef3c Mon Sep 17 00:00:00 2001 From: tcoch Date: Tue, 20 May 2025 14:38:01 +0200 Subject: [PATCH 6/6] use buildViolation existing method --- src/Symfony/Component/Validator/CHANGELOG.md | 1 - .../Validator/Test/ConstraintValidatorTestCase.php | 9 --------- .../Tests/Constraints/MacAddressValidatorTest.php | 5 ++++- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 3d60fc68cafcc..ae1ae20da804d 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -13,7 +13,6 @@ CHANGELOG * Add support for multiple fields containing nested constraints in `Composite` constraints * Add the `stopOnFirstError` option to the `Unique` constraint to validate all elements * Add support for closures in the `When` constraint - * Add method `assertViolation` to `ConstraintValidatorTestCase` 7.2 --- diff --git a/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php b/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php index 68bc72ee44915..2fe70eaef4569 100644 --- a/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php +++ b/src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php @@ -282,15 +282,6 @@ protected function assertNoViolation() $this->assertSame(0, $violationsCount = \count($this->context->getViolations()), \sprintf('0 violation expected. Got %u.', $violationsCount)); } - /** - * Assert if at least one violation is raised - * @return void - */ - protected function assertViolation() - { - $this->assertGreaterThan(0, $violationsCount = \count($this->context->getViolations()), \sprintf('Some violation(s) expected. Got %u.', $violationsCount)); - } - protected function buildViolation(string|\Stringable $message): ConstraintViolationAssertion { return new ConstraintViolationAssertion($this->context, $message, $this->constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php index 586b233df027c..228d3d6078789 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php @@ -70,7 +70,10 @@ public function testNotValidMac($mac) { $this->validator->validate($mac, new MacAddress()); - $this->assertViolation(); + $this->buildViolation('This value is not a valid MAC address.') + ->setParameter('{{ value }}', '"'.$mac.'"') + ->setCode(MacAddress::INVALID_MAC_ERROR) + ->assertRaised(); } public static function getValidMacs(): array