@@ -135,6 +135,41 @@ public function testGivenBadPostcodeForCountry_correctViolationsAreReturned(): v
135135 $ this ->assertSame ( 'postcode ' , $ validationResult ->getViolations ()[0 ]->getSource () );
136136 }
137137
138+ public function testGivenBadPostcodeForCountryWithoutPatterns_addressPatternIsUsedViolationsAreReturned (): void {
139+ $ addressPatterns = [
140+ 'firstName ' => "/.+$/ " ,
141+ 'lastName ' => "/.+$/ " ,
142+ // Weird pattern to make numbers fail
143+ 'postcode ' => '/^[bao]{5}$/ ' ,
144+ ];
145+ $ validator = new AddressValidator ( [], $ addressPatterns );
146+ $ validationResult = $ validator ->validatePostalAddress ( 'Test 1234 ' , '123 ' , 'Test City ' , 'US ' );
147+ $ this ->assertSame ( 'postcode ' , $ validationResult ->getViolations ()[0 ]->getSource () );
148+ }
149+
150+ public function testGivenLengthValidationFailsForPostCode_violationsContainOnlyLengthViolationsAndNoPatternViolations (): void {
151+ $ addressPatterns = [
152+ 'firstName ' => "/.+$/ " ,
153+ 'lastName ' => "/.+$/ " ,
154+ 'postcode ' => '/^[0-9]{10}$/ ' ,
155+ ];
156+ $ countryPatterns = [
157+ 'DE ' => '/^[0-9]{5}$/ ' ,
158+ ];
159+ // has to be longer than maximum field length in AddressValidator
160+ $ longPostalCode = '1234567890123456789 ' ;
161+ $ validator = new AddressValidator ( $ countryPatterns , $ addressPatterns );
162+ $ validationResultForUnknownCountry = $ validator ->validatePostalAddress ( 'Test 1234 ' , $ longPostalCode , 'Test City ' , 'US ' );
163+ $ validationResultForKnownCountry = $ validator ->validatePostalAddress ( 'Test 1234 ' , $ longPostalCode , 'Test City ' , 'DE ' );
164+
165+ $ this ->assertCount ( 1 , $ validationResultForUnknownCountry ->getViolations () );
166+ $ this ->assertSame ( 'postcode ' , $ validationResultForUnknownCountry ->getViolations ()[0 ]->getSource () );
167+ $ this ->assertSame ( 'wrong-length ' , $ validationResultForUnknownCountry ->getViolations ()[0 ]->getMessageIdentifier () );
168+ $ this ->assertCount ( 1 , $ validationResultForKnownCountry ->getViolations () );
169+ $ this ->assertSame ( 'postcode ' , $ validationResultForKnownCountry ->getViolations ()[0 ]->getSource () );
170+ $ this ->assertSame ( 'wrong-length ' , $ validationResultForKnownCountry ->getViolations ()[0 ]->getMessageIdentifier () );
171+ }
172+
138173 public function testGivenBadFirstAndLastName_correctViolationsAreReturned (): void {
139174 $ validator = new AddressValidator ( self ::COUNTRY_POSTCODE_PATTERNS , self ::ADDRESS_PATTERNS );
140175 $ validationResult = $ validator ->validatePersonName ( 'Herr ' , '' , '£$%^&*() ' , '£$%^&*() ' );
0 commit comments