Skip to content

Commit 6b4fced

Browse files
minor symfony#60422 [Validator] Add tests for MacAddress (tcoch)
This PR was submitted for the 7.4 branch but it was squashed and merged into the 7.2 branch instead. Discussion ---------- [Validator] Add tests for `MacAddress` | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | - | License | MIT This PR aims to add some tests to the MacAddress Validator. ~[x] Add testing for empty string and null values~ Already handled with `testNullIsValid` and `testEmptyStringIsValid` methods [x] Add testing of unvalid mac addresses ~[x] Create generic method `assertViolation()` in `ConstraintValidatorTestCase.php` to test that unvalid values do throw a violation.~ See conversation below If this PR is accepted, I'll try to continue this work on other validators. I believe testing empty string and null values is important to ensure no BC occurs, for example. Commits ------- 7ce3bb5 [Validator] Add tests for `MacAddress`
2 parents ed7b88c + 7ce3bb5 commit 6b4fced

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ public function testValidMac($mac)
6363
$this->assertNoViolation();
6464
}
6565

66+
/**
67+
* @dataProvider getNotValidMacs
68+
*/
69+
public function testNotValidMac($mac)
70+
{
71+
$this->validator->validate($mac, new MacAddress());
72+
73+
$this->buildViolation('This value is not a valid MAC address.')
74+
->setParameter('{{ value }}', '"'.$mac.'"')
75+
->setCode(MacAddress::INVALID_MAC_ERROR)
76+
->assertRaised();
77+
}
78+
6679
public static function getValidMacs(): array
6780
{
6881
return [
@@ -76,6 +89,17 @@ public static function getValidMacs(): array
7689
];
7790
}
7891

92+
public static function getNotValidMacs(): array
93+
{
94+
return [
95+
['00:00:00:00:00'],
96+
['00:00:00:00:00:0G'],
97+
['GG:GG:GG:GG:GG:GG'],
98+
['GG-GG-GG-GG-GG-GG'],
99+
['GGGG.GGGG.GGGG'],
100+
];
101+
}
102+
79103
public static function getValidLocalUnicastMacs(): array
80104
{
81105
return [

0 commit comments

Comments
 (0)