1111 */
1212class AddressValidatorTest extends \PHPUnit \Framework \TestCase {
1313
14+ private const COUNTRY_POSTCODE_PATTERNS = [
15+ 'DE ' => '/^[0-9]{5}$/ ' ,
16+ 'AT ' => '/^[0-9]{4}$/ ' ,
17+ 'CH ' => '/^[0-9]{4}$/ ' ,
18+ 'BE ' => '/^[0-9]{4}$/ ' ,
19+ 'IT ' => '/^[0-9]{5}$/ ' ,
20+ 'LI ' => '/^[0-9]{4}$/ ' ,
21+ 'LU ' => '/^[0-9]{4}$/ ' ,
22+ ];
23+
1424 public function testGivenValidPostalAddress_noViolationsAreReturned (): void {
15- $ validator = new AddressValidator ();
25+ $ validator = new AddressValidator ( self :: COUNTRY_POSTCODE_PATTERNS );
1626 $ validationResult = $ validator ->validatePostalAddress ( 'Test 1234 ' , '12345 ' , 'Test City ' , 'Germany ' );
1727 $ this ->assertTrue ( $ validationResult ->isSuccessful () );
1828 }
1929
2030 public function testGivenValidPersonName_noViolationsAreReturned (): void {
21- $ validator = new AddressValidator ();
31+ $ validator = new AddressValidator ( self :: COUNTRY_POSTCODE_PATTERNS );
2232 $ validationResult = $ validator ->validatePersonName ( 'Herr ' , 'Prof. Dr. ' , 'Tester ' , 'Testing ' );
2333 $ this ->assertTrue ( $ validationResult ->isSuccessful () );
2434 }
2535
2636 public function testGivenValidCompany_noViolationsAreReturned (): void {
27- $ validator = new AddressValidator ();
37+ $ validator = new AddressValidator ( self :: COUNTRY_POSTCODE_PATTERNS );
2838 $ validationResult = $ validator ->validateCompanyName ( 'Test Company GmbH & Co. KG ' );
2939 $ this ->assertTrue ( $ validationResult ->isSuccessful () );
3040 }
3141
3242 public function testGivenTooLongPostalValues_correctViolationsAreReturned (): void {
33- $ validator = new AddressValidator ();
43+ $ validator = new AddressValidator ( self :: COUNTRY_POSTCODE_PATTERNS );
3444 $ validationResult = $ validator ->validatePostalAddress (
3545 str_repeat ( 'a ' , 101 ),
36- str_repeat ( '1 ' , 6 ),
46+ str_repeat ( '1 ' , 17 ),
3747 str_repeat ( 'a ' , 101 ),
3848 str_repeat ( 'a ' , 9 )
3949 );
@@ -46,7 +56,7 @@ public function testGivenTooLongPostalValues_correctViolationsAreReturned(): voi
4656 }
4757
4858 public function testGivenTooLongNameValues_correctViolationsAreReturned (): void {
49- $ validator = new AddressValidator ();
59+ $ validator = new AddressValidator ( self :: COUNTRY_POSTCODE_PATTERNS );
5060 $ validationResult = $ validator ->validatePersonName (
5161 str_repeat ( 'a ' , 17 ),
5262 str_repeat ( 'a ' , 17 ),
@@ -62,7 +72,7 @@ public function testGivenTooLongNameValues_correctViolationsAreReturned(): void
6272 }
6373
6474 public function testGivenTooLongCompanyValues_correctViolationsAreReturned (): void {
65- $ validator = new AddressValidator ();
75+ $ validator = new AddressValidator ( self :: COUNTRY_POSTCODE_PATTERNS );
6676 $ validationResult = $ validator ->validateCompanyName (
6777 str_repeat ( 'a ' , 101 )
6878 );
@@ -72,7 +82,7 @@ public function testGivenTooLongCompanyValues_correctViolationsAreReturned(): vo
7282 }
7383
7484 public function testGivenEmptyPostalValues_correctViolationsAreReturned (): void {
75- $ validator = new AddressValidator ();
85+ $ validator = new AddressValidator ( self :: COUNTRY_POSTCODE_PATTERNS );
7686 $ validationResult = $ validator ->validatePostalAddress (
7787 '' ,
7888 '' ,
@@ -89,7 +99,7 @@ public function testGivenEmptyPostalValues_correctViolationsAreReturned(): void
8999 }
90100
91101 public function testGivenEmptyNameValues_correctViolationsAreReturned (): void {
92- $ validator = new AddressValidator ();
102+ $ validator = new AddressValidator ( self :: COUNTRY_POSTCODE_PATTERNS );
93103 $ validationResult = $ validator ->validatePersonName (
94104 '' ,
95105 '' ,
@@ -105,10 +115,16 @@ public function testGivenEmptyNameValues_correctViolationsAreReturned(): void {
105115 }
106116
107117 public function testGivenEmptyCompanyValues_correctViolationsAreReturned (): void {
108- $ validator = new AddressValidator ();
118+ $ validator = new AddressValidator ( self :: COUNTRY_POSTCODE_PATTERNS );
109119 $ validationResult = $ validator ->validateCompanyName ( '' );
110120 $ this ->assertFalse ( $ validationResult ->isSuccessful () );
111121 $ this ->assertCount ( 1 , $ validationResult ->getViolations () );
112122 $ this ->assertSame ( 'companyName ' , $ validationResult ->getViolations ()[0 ]->getSource () );
113123 }
124+
125+ public function testGivenBadPostcodeForCountry_correctViolationsAreReturned (): void {
126+ $ validator = new AddressValidator ( self ::COUNTRY_POSTCODE_PATTERNS );
127+ $ validationResult = $ validator ->validatePostalAddress ( 'Test 1234 ' , '123 ' , 'Test City ' , 'DE ' );
128+ $ this ->assertSame ( 'postcode ' , $ validationResult ->getViolations ()[0 ]->getSource () );
129+ }
114130}
0 commit comments