mirror of
https://github.com/Respect/Validation.git
synced 2026-03-18 08:09:51 +01:00
Due to the current status of the development of the library, it seems like we will be supporting version 1.1 for a long time. Even when we release version 2.0 we will still give support for version 1.1 for a while. This commit will make sure that version 1.1 is fully supported for PHP 7.2 and 7.3. Also, it will remove the support for HHVM since it will not keep the compatibility with PHP anymore [1]. In order to make that happen, this commit will create a TestCase from Validation so we can use the same API to create mocks in both PHPUnit versions 4.0 and 5.0. During the development of this commit, I noticed that PHPUnit 4.0 had issues to mock "SplFileInfo" and for that reason, this commit will also replace those mocks by "SplFileInfo" instances. [1]: https://hhvm.com/blog/2018/09/12/end-of-php-support-future-of-hack.html Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
142 lines
3.8 KiB
PHP
142 lines
3.8 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Respect/Validation.
|
|
*
|
|
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
*
|
|
* For the full copyright and license information, please view the "LICENSE.md"
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
use Respect\Validation\TestCase;
|
|
|
|
/**
|
|
* @group rule
|
|
* @covers Respect\Validation\Rules\PostalCode
|
|
* @covers Respect\Validation\Exceptions\PostalCodeException
|
|
*/
|
|
class PostalCodeTest extends TestCase
|
|
{
|
|
public function testShouldUsePatternAccordingToCountryCode()
|
|
{
|
|
$countryCode = 'BR';
|
|
|
|
$rule = new PostalCode($countryCode);
|
|
|
|
$actualPattern = $rule->regex;
|
|
$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 testShouldThrowsExceptionWhenCountryCodeIsNotValid()
|
|
{
|
|
new PostalCode('Whatever');
|
|
}
|
|
|
|
/**
|
|
* @dataProvider validPostalCodesProvider
|
|
*/
|
|
public function testShouldValidatePatternAccordingToTheDefinedCountryCode($countryCode, $postalCode)
|
|
{
|
|
$rule = new PostalCode($countryCode);
|
|
|
|
$this->assertTrue($rule->validate($postalCode));
|
|
}
|
|
|
|
public function validPostalCodesProvider()
|
|
{
|
|
return [
|
|
['BR', '02179-000'],
|
|
['BR', '02179000'],
|
|
['CA', 'A1A 2B2'],
|
|
['GB', 'GIR 0AA'],
|
|
['GB', 'PR1 9LY'],
|
|
['US', '02179'],
|
|
['YE', ''],
|
|
['PL', '99-300'],
|
|
['NL', '1012 GX'],
|
|
['NL', '1012GX'],
|
|
['PT', '3660-606'],
|
|
['PT', '3660606'],
|
|
['CO', '110231'],
|
|
['KR', '03187'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider invalidPostalCodesProvider
|
|
*/
|
|
public function testShouldNotValidatePatternAccordingToTheDefinedCountryCode($countryCode, $postalCode)
|
|
{
|
|
$rule = new PostalCode($countryCode);
|
|
|
|
$this->assertFalse($rule->validate($postalCode));
|
|
}
|
|
|
|
/**
|
|
* @expectedException Respect\Validation\Exceptions\PostalCodeException
|
|
* @expectedExceptionMessage "02179-000" must be a valid postal code on "US"
|
|
*/
|
|
public function testShouldThrowsPostalCodeExceptionWhenValidationFails()
|
|
{
|
|
$rule = new PostalCode('US');
|
|
$rule->check('02179-000');
|
|
}
|
|
|
|
public function invalidPostalCodesProvider()
|
|
{
|
|
return [
|
|
['BR', '02179'],
|
|
['BR', '02179.000'],
|
|
['CA', '1A1B2B'],
|
|
['GB', 'GIR 00A'],
|
|
['GB', 'GIR0AA'],
|
|
['GB', 'PR19LY'],
|
|
['US', '021 79'],
|
|
['YE', '02179'],
|
|
['PL', '99300'],
|
|
['KR', '548940'],
|
|
['KR', '548-940'],
|
|
];
|
|
}
|
|
}
|