Skip to content

Commit

Permalink
Merge pull request #287 from henriquemoody/postalcode
Browse files Browse the repository at this point in the history
Required improvements on "PostalCode" rule
  • Loading branch information
henriquemoody committed Feb 5, 2015
2 parents cbd0695 + 29d3fcc commit d1af494
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
17 changes: 13 additions & 4 deletions library/Rules/PostalCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class PostalCode extends Regex
{
const DEFAULT_PATTERN = '/^$/';

/**
* @link http://download.geonames.org/export/dump/countryInfo.txt
*/
Expand All @@ -32,6 +34,7 @@ class PostalCode extends Regex
"CL" => "/^(\d{7})$/",
"CN" => "/^(\d{6})$/",
"CR" => "/^(\d{4})$/",
"CS" => "/^(\d{5})$/",
"CU" => "/^(?:CP)*(\d{5})$/",
"CV" => "/^(\d{4})$/",
"CX" => "/^(\d{4})$/",
Expand Down Expand Up @@ -160,15 +163,21 @@ class PostalCode extends Regex
"YT" => "/^(\d{5})$/",
"ZA" => "/^(\d{4})$/",
"ZM" => "/^(\d{5})$/",
"CS" => "/^(\d{5})$/",
);

public function __construct($countryCode)
public function __construct($countryCode, CountryCode $countryCodeRule = null)
{
if (!isset($this->postalCodes[$countryCode])) {
$countryCodeRule = $countryCodeRule ?: new CountryCode();
if (! $countryCodeRule->validate($countryCode)) {
throw new ComponentException(sprintf('Cannot validate postal code from "%s" country', $countryCode));
}

parent::__construct($this->postalCodes[$countryCode]);
$regex = self::DEFAULT_PATTERN;
$upperCountryCode = strtoupper($countryCode);
if (isset($this->postalCodes[$upperCountryCode])) {
$regex = $this->postalCodes[$upperCountryCode];
}

parent::__construct($regex);
}
}
57 changes: 47 additions & 10 deletions tests/library/Respect/Validation/Rules/PostalCodeTest.php
Original file line number Diff line number Diff line change
@@ -1,54 +1,90 @@
<?php
namespace Respect\Validation\Rules;

/**
* @covers Respect\Validation\Rules\PostalCode
*/
class PostalCodeTest extends \PHPUnit_Framework_TestCase
{
public function testShouldUsePatternAccordingToLocale()
public function testShouldUsePatternAccordingToCountryCode()
{
$locale = 'BR';
$countryCode = 'BR';

$rule = new PostalCode($locale);
$rule = new PostalCode($countryCode);

$actualPattern = $rule->regex;
$expectedPattern = $rule->postalCodes[$locale];
$expectedPattern = $rule->postalCodes[$countryCode];

$this->assertEquals($expectedPattern, $actualPattern);
}

public function testShouldNotBeCaseSensitiveWhenChoosingPatternAccordingToCountryCode()
{
$rule1 = new PostalCode('BR');
$rule2 = new PostalCode('br');

$this->assertEquals($rule1->regex, $rule2->regex);
}

public function testShouldUseDefaultPatternWhenCountryCodeDoesNotHavePostalCode()
{
$rule = new PostalCode('ZW');

$actualPattern = $rule->regex;
$expectedPattern = PostalCode::DEFAULT_PATTERN;

$this->assertEquals($expectedPattern, $actualPattern);
}

public function testShouldValidateEmptyStringsWhenUsingDefaultPattern()
{
$rule = new PostalCode('ZW');

$this->assertTrue($rule->validate(''));
}

public function testShouldNotValidateNonEmptyStringsWhenUsingDefaultPattern()
{
$rule = new PostalCode('ZW');

$this->assertFalse($rule->validate(' '));
}

/**
* @expectedException Respect\Validation\Exceptions\ComponentException
* @expectedExceptionMessage Cannot validate postal code from "Whatever" country
*/
public function testShouldThrowsExceptionWhenCannotFindLocalePattern()
public function testShouldThrowsExceptionWhenCountryCodeIsNotValid()
{
new PostalCode('Whatever');
}

/**
* @dataProvider validPostalCodesProvider
*/
public function testShouldValidatePatternAccordingToTheDefinedLocale($locale, $postalCode)
public function testShouldValidatePatternAccordingToTheDefinedCountryCode($countryCode, $postalCode)
{
$rule = new PostalCode($locale);
$rule = new PostalCode($countryCode);

$this->assertTrue($rule->validate($postalCode));
}

public function validPostalCodesProvider()
{
return array(
array('BR', '02179000'),
array('BR', '02179-000'),
array('BR', '02179000'),
array('US', '02179'),
array('YE', ''),
);
}

/**
* @dataProvider invalidPostalCodesProvider
*/
public function testShouldNotValidatePatternAccordingToTheDefinedLocale($locale, $postalCode)
public function testShouldNotValidatePatternAccordingToTheDefinedCountryCode($countryCode, $postalCode)
{
$rule = new PostalCode($locale);
$rule = new PostalCode($countryCode);

$this->assertFalse($rule->validate($postalCode));
}
Expand All @@ -59,6 +95,7 @@ public function invalidPostalCodesProvider()
array('BR', '02179'),
array('BR', '02179.000'),
array('US', '021 79'),
array('YE', '02179'),
);
}
}

0 comments on commit d1af494

Please sign in to comment.