11<?php
22namespace Respect \Validation \Rules ;
33
4+ /**
5+ * @covers Respect\Validation\Rules\PostalCode
6+ */
47class PostalCodeTest extends \PHPUnit_Framework_TestCase
58{
6- public function testShouldUsePatternAccordingToLocale ()
9+ public function testShouldUsePatternAccordingToCountryCode ()
710 {
8- $ locale = 'BR ' ;
11+ $ countryCode = 'BR ' ;
912
10- $ rule = new PostalCode ($ locale );
13+ $ rule = new PostalCode ($ countryCode );
1114
1215 $ actualPattern = $ rule ->regex ;
13- $ expectedPattern = $ rule ->postalCodes [$ locale ];
16+ $ expectedPattern = $ rule ->postalCodes [$ countryCode ];
1417
1518 $ this ->assertEquals ($ expectedPattern , $ actualPattern );
1619 }
1720
21+ public function testShouldNotBeCaseSensitiveWhenChoosingPatternAccordingToCountryCode ()
22+ {
23+ $ rule1 = new PostalCode ('BR ' );
24+ $ rule2 = new PostalCode ('br ' );
25+
26+ $ this ->assertEquals ($ rule1 ->regex , $ rule2 ->regex );
27+ }
28+
29+ public function testShouldUseDefaultPatternWhenCountryCodeDoesNotHavePostalCode ()
30+ {
31+ $ rule = new PostalCode ('ZW ' );
32+
33+ $ actualPattern = $ rule ->regex ;
34+ $ expectedPattern = PostalCode::DEFAULT_PATTERN ;
35+
36+ $ this ->assertEquals ($ expectedPattern , $ actualPattern );
37+ }
38+
39+ public function testShouldValidateEmptyStringsWhenUsingDefaultPattern ()
40+ {
41+ $ rule = new PostalCode ('ZW ' );
42+
43+ $ this ->assertTrue ($ rule ->validate ('' ));
44+ }
45+
46+ public function testShouldNotValidateNonEmptyStringsWhenUsingDefaultPattern ()
47+ {
48+ $ rule = new PostalCode ('ZW ' );
49+
50+ $ this ->assertFalse ($ rule ->validate (' ' ));
51+ }
52+
1853 /**
1954 * @expectedException Respect\Validation\Exceptions\ComponentException
2055 * @expectedExceptionMessage Cannot validate postal code from "Whatever" country
2156 */
22- public function testShouldThrowsExceptionWhenCannotFindLocalePattern ()
57+ public function testShouldThrowsExceptionWhenCountryCodeIsNotValid ()
2358 {
2459 new PostalCode ('Whatever ' );
2560 }
2661
2762 /**
2863 * @dataProvider validPostalCodesProvider
2964 */
30- public function testShouldValidatePatternAccordingToTheDefinedLocale ( $ locale , $ postalCode )
65+ public function testShouldValidatePatternAccordingToTheDefinedCountryCode ( $ countryCode , $ postalCode )
3166 {
32- $ rule = new PostalCode ($ locale );
67+ $ rule = new PostalCode ($ countryCode );
3368
3469 $ this ->assertTrue ($ rule ->validate ($ postalCode ));
3570 }
3671
3772 public function validPostalCodesProvider ()
3873 {
3974 return array (
40- array ('BR ' , '02179000 ' ),
4175 array ('BR ' , '02179-000 ' ),
76+ array ('BR ' , '02179000 ' ),
4277 array ('US ' , '02179 ' ),
78+ array ('YE ' , '' ),
4379 );
4480 }
4581
4682 /**
4783 * @dataProvider invalidPostalCodesProvider
4884 */
49- public function testShouldNotValidatePatternAccordingToTheDefinedLocale ( $ locale , $ postalCode )
85+ public function testShouldNotValidatePatternAccordingToTheDefinedCountryCode ( $ countryCode , $ postalCode )
5086 {
51- $ rule = new PostalCode ($ locale );
87+ $ rule = new PostalCode ($ countryCode );
5288
5389 $ this ->assertFalse ($ rule ->validate ($ postalCode ));
5490 }
@@ -59,6 +95,7 @@ public function invalidPostalCodesProvider()
5995 array ('BR ' , '02179 ' ),
6096 array ('BR ' , '02179.000 ' ),
6197 array ('US ' , '021 79 ' ),
98+ array ('YE ' , '02179 ' ),
6299 );
63100 }
64101}
0 commit comments