Merge branch '1.0' into 1.1

This commit is contained in:
Henrique Moody 2018-01-02 22:47:22 +01:00
commit ee9e69776a
No known key found for this signature in database
GPG key ID: 221E9281655813A6
13 changed files with 29 additions and 543 deletions

View file

@ -17,7 +17,6 @@
},
"require-dev": {
"egulias/email-validator": "~1.2",
"malkusch/bav": "~1.0",
"mikey179/vfsStream": "^1.5",
"phpunit/phpunit": "~4.0",
"symfony/validator": "~2.6.9",
@ -27,7 +26,6 @@
"ext-bcmath": "Arbitrary Precision Mathematics",
"ext-mbstring": "Multibyte String Functions",
"egulias/email-validator": "Strict (RFC compliant) email validation",
"malkusch/bav": "German bank account validation",
"symfony/validator": "Use Symfony validator through Respect\\Validation",
"zendframework/zend-validator": "Use Zend Framework validator through Respect\\Validation",
"fabpot/php-cs-fixer": "Fix PSR2 and other coding style issues"

View file

@ -305,6 +305,9 @@ class ValidationException extends InvalidArgumentException implements ExceptionI
public function setTemplate($template)
{
$this->customTemplate = true;
if (isset(static::$defaultTemplates[$this->mode][$template])) {
$template = static::$defaultTemplates[$this->mode][$template];
}
$this->template = $template;
$this->buildMessage();

View file

@ -29,16 +29,12 @@ class Domain extends AbstractComposite
new Alnum('-'),
new Not(new StartsWith('-')),
new OneOf(
new Not(
new Contains('--')
),
new AllOf(
new StartsWith('xn--'),
new Callback(function ($str) {
return substr_count($str, '--') == 1;
})
)
)
new Not(new Contains('--')),
new Callback(function ($str) {
return substr_count($str, '--') == 1;
})
),
new Not(new EndsWith('-'))
);
}

View file

@ -0,0 +1,16 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Validator as v;
try {
v::when(v::alwaysInvalid(), v::alwaysValid())->check('foo');
} catch (ValidationException $exception) {
echo $exception->getMainMessage();
}
?>
--EXPECT--
"foo" is not valid

View file

@ -1,39 +0,0 @@
<?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 malkusch\bav\ConfigurationRegistry;
class LocaleTestCase extends \PHPUnit_Framework_TestCase
{
protected function getBavMock()
{
$bavMock = $this->getMockBuilder('malkusch\bav\BAV')
->disableOriginalConstructor()
->getMock();
return $bavMock;
}
protected function setUp()
{
$dataBackend = $this->getMockForAbstractClass('malkusch\bav\DataBackend');
$dataBackendContainer = $this->getMockForAbstractClass('malkusch\bav\DataBackendContainer');
$dataBackendContainer
->expects($this->any())
->method('makeDataBackend')
->will($this->returnValue($dataBackend));
ConfigurationRegistry::getConfiguration()->setDataBackendContainer($dataBackendContainer);
ConfigurationRegistry::getConfiguration()->setUpdatePlan(null);
}
}

View file

@ -1,47 +0,0 @@
<?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;
/**
* @group rule
* @covers Respect\Validation\Rules\BankAccount
* @covers Respect\Validation\Exceptions\BankAccountException
*/
class BankAccountTest extends LocaleTestCase
{
public function testShouldUseDefinedFactoryToCreateInternalRuleBasedOnGivenCountryCode()
{
$countryCode = 'XX';
$bank = '123456';
$validatable = $this->getMock('Respect\Validation\Validatable');
$factory = $this->getMock('Respect\Validation\Rules\Locale\Factory');
$factory
->expects($this->once())
->method('bankAccount')
->with($countryCode, $bank)
->will($this->returnValue($validatable));
$rule = new BankAccount($countryCode, $bank, $factory);
$this->assertSame($validatable, $rule->getValidatable());
}
public function testShouldUseDefaultFactoryToCreateInternalRuleBasedOnGivenCountryCodeWhenFactoryIsNotDefined()
{
$countryCode = 'DE';
$bank = '123456';
$rule = new BankAccount($countryCode, $bank);
$this->assertInstanceOf('Respect\Validation\Rules\Locale\GermanBankAccount', $rule->getValidatable());
}
}

View file

@ -1,45 +0,0 @@
<?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;
/**
* @group rule
* @covers Respect\Validation\Rules\Bank
* @covers Respect\Validation\Exceptions\BankException
*/
class BankTest extends LocaleTestCase
{
public function testShouldUseDefinedFactoryToCreateInternalRuleBasedOnGivenCountryCode()
{
$countryCode = 'XX';
$validatable = $this->getMock('Respect\Validation\Validatable');
$factory = $this->getMock('Respect\Validation\Rules\Locale\Factory');
$factory
->expects($this->once())
->method('bank')
->with($countryCode)
->will($this->returnValue($validatable));
$rule = new Bank($countryCode, $factory);
$this->assertSame($validatable, $rule->getValidatable());
}
public function testShouldUseDefaultFactoryToCreateInternalRuleBasedOnGivenCountryCodeWhenFactoryIsNotDefined()
{
$countryCode = 'DE';
$rule = new Bank($countryCode);
$this->assertInstanceOf('Respect\Validation\Rules\Locale\GermanBank', $rule->getValidatable());
}
}

View file

@ -1,45 +0,0 @@
<?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;
/**
* @group rule
* @covers Respect\Validation\Rules\Bic
* @covers Respect\Validation\Exceptions\BicException
*/
class BicTest extends LocaleTestCase
{
public function testShouldUseDefinedFactoryToCreateInternalRuleBasedOnGivenCountryCode()
{
$countryCode = 'XX';
$validatable = $this->getMock('Respect\Validation\Validatable');
$factory = $this->getMock('Respect\Validation\Rules\Locale\Factory');
$factory
->expects($this->once())
->method('bic')
->with($countryCode)
->will($this->returnValue($validatable));
$rule = new Bic($countryCode, $factory);
$this->assertSame($validatable, $rule->getValidatable());
}
public function testShouldUseDefaultFactoryToCreateInternalRuleBasedOnGivenCountryCodeWhenFactoryIsNotDefined()
{
$countryCode = 'DE';
$rule = new Bic($countryCode);
$this->assertInstanceOf('Respect\Validation\Rules\Locale\GermanBic', $rule->getValidatable());
}
}

View file

@ -67,6 +67,9 @@ class DomainTest extends \PHPUnit_Framework_TestCase
['xn--bcher-kva.ch'],
['mail.xn--bcher-kva.ch'],
['example-hyphen.com'],
['example--valid.com'],
['std--a.com'],
['r--w.com'],
];
}
@ -76,10 +79,10 @@ class DomainTest extends \PHPUnit_Framework_TestCase
[null],
[''],
['2222222domain.local'],
['example--invalid.com'],
['-example-invalid.com'],
['example.invalid.-com'],
['xn--bcher--kva.ch'],
['example.invalid-.com'],
['1.2.3.256'],
['1.2.3.4'],
];

View file

@ -1,92 +0,0 @@
<?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\Locale;
use Respect\Validation\Rules\LocaleTestCase;
/**
* @covers Respect\Validation\Rules\Locale\Factory
*/
class FactoryTest extends LocaleTestCase
{
/**
* @expectedException Respect\Validation\Exceptions\ComponentException
* @expectedExceptionMessage Cannot provide BIC validation for country "XX"
*/
public function testShouldThrowExceptionWhenFailedToGetBICRule()
{
$factory = new Factory();
$factory->bic('XX');
}
public function testShouldReturnBICRuleAccordingToCountry()
{
$factory = new Factory();
$this->assertInstanceOf('Respect\Validation\Validatable', $factory->bic('DE'));
}
public function testShouldNotBeCaseSensitiveToReturnBICRuleAccordingToCountry()
{
$factory = new Factory();
$this->assertEquals($factory->bic('DE'), $factory->bic('de'));
}
/**
* @expectedException Respect\Validation\Exceptions\ComponentException
* @expectedExceptionMessage Cannot provide bank validation for country "XX"
*/
public function testShouldThrowExceptionWhenFailedToGetBankRule()
{
$factory = new Factory();
$factory->bank('XX');
}
public function testShouldReturnBankRuleAccordingToCountry()
{
$factory = new Factory();
$this->assertInstanceOf('Respect\Validation\Validatable', $factory->bank('DE'));
}
public function testShouldNotBeCaseSensitiveToReturnBankRuleAccordingToCountry()
{
$factory = new Factory();
$this->assertEquals($factory->bank('DE'), $factory->bank('de'));
}
/**
* @expectedException Respect\Validation\Exceptions\ComponentException
* @expectedExceptionMessage Cannot provide bank account validation for country "XX" and bank "123"
*/
public function testShouldThrowExceptionWhenFailedToGetBankAccountRule()
{
$factory = new Factory();
$factory->bankAccount('XX', '123');
}
public function testShouldReturnBankAccountRuleAccordingToCountry()
{
$factory = new Factory();
$this->assertInstanceOf('Respect\Validation\Validatable', $factory->bankAccount('DE', '123'));
}
public function testShouldNotBeCaseSensitiveToReturnBankAccountRuleAccordingToCountry()
{
$factory = new Factory();
$this->assertEquals($factory->bankAccount('DE', '123'), $factory->bankAccount('de', '123'));
}
}

View file

@ -1,96 +0,0 @@
<?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\Locale;
use Respect\Validation\Rules\LocaleTestCase;
/**
* @group rule
* @covers Respect\Validation\Rules\Locale\GermanBankAccount
* @covers Respect\Validation\Exceptions\Locale\GermanBankAccountException
*/
class GermanBankAccountTest extends LocaleTestCase
{
public function testShouldAcceptBankOnConstructor()
{
$bank = '10000000';
$rule = new GermanBankAccount($bank);
$this->assertSame($bank, $rule->bank);
}
public function testShouldAcceptBAVInstanceOnConstructor()
{
$bank = '10000000';
$bav = $this->getBavMock();
$rule = new GermanBankAccount($bank, $bav);
$this->assertSame($bav, $rule->bav);
}
public function testShouldHaveAnInstanceOfBAVByDefault()
{
$bank = '10000000';
$rule = new GermanBankAccount($bank);
$this->assertInstanceOf('malkusch\bav\BAV', $rule->bav);
}
public function testShouldUseBAVInstanceToValidate()
{
$bank = '10000000';
$input = '67067';
$bav = $this->getBavMock();
$rule = new GermanBankAccount($bank, $bav);
$bav->expects($this->once())
->method('isValidBankAccount')
->with($bank, $input)
->will($this->returnValue(true));
$rule->validate($input);
}
public function testShouldReturnBAVInstanceResulteWhenValidating()
{
$bank = '10000000';
$input = '67067';
$bav = $this->getBavMock();
$rule = new GermanBankAccount($bank, $bav);
$bav->expects($this->any())
->method('isValidBankAccount')
->with($bank, $input)
->will($this->returnValue(true));
$this->assertTrue($rule->validate($input));
}
/**
* @expectedException Respect\Validation\Exceptions\Locale\GermanBankAccountException
* @expectedExceptionMessage "67067" must be a german bank account
*/
public function testShouldThowsTheRightExceptionWhenChecking()
{
$bank = '10000000';
$input = '67067';
$bav = $this->getBavMock();
$rule = new GermanBankAccount($bank, $bav);
$bav->expects($this->any())
->method('isValidBankAccount')
->with($bank, $input)
->will($this->returnValue(false));
$rule->check($input);
}
}

View file

@ -1,83 +0,0 @@
<?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\Locale;
use Respect\Validation\Rules\LocaleTestCase;
/**
* @group rule
* @covers Respect\Validation\Rules\Locale\GermanBank
* @covers Respect\Validation\Exceptions\Locale\GermanBankException
*/
class GermanBankTest extends LocaleTestCase
{
public function testShouldAcceptBAVInstanceOnConstrutor()
{
$bav = $this->getBavMock();
$rule = new GermanBank($bav);
$this->assertSame($bav, $rule->bav);
}
public function testShouldHaveAnInstanceOfBAVByDefault()
{
$rule = new GermanBank();
$this->assertInstanceOf('malkusch\bav\BAV', $rule->bav);
}
public function testShouldUseBAVInstanceToValidate()
{
$input = '10000000';
$bav = $this->getBavMock();
$rule = new GermanBank($bav);
$bav->expects($this->once())
->method('isValidBank')
->with($input)
->will($this->returnValue(true));
$rule->validate($input);
}
public function testShouldReturnBAVInstanceResulteWhenValidating()
{
$input = '10000000';
$bav = $this->getBavMock();
$rule = new GermanBank($bav);
$bav->expects($this->any())
->method('isValidBank')
->with($input)
->will($this->returnValue(true));
$this->assertTrue($rule->validate($input));
}
/**
* @expectedException Respect\Validation\Exceptions\Locale\GermanBankException
* @expectedExceptionMessage "10000000" must be a german bank
*/
public function testShouldThowsTheRightExceptionWhenChecking()
{
$input = '10000000';
$bav = $this->getBavMock();
$rule = new GermanBank($bav);
$bav->expects($this->any())
->method('isValidBank')
->with($input)
->will($this->returnValue(false));
$rule->check($input);
}
}

View file

@ -1,83 +0,0 @@
<?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\Locale;
use Respect\Validation\Rules\LocaleTestCase;
/**
* @group rule
* @covers Respect\Validation\Rules\Locale\GermanBic
* @covers Respect\Validation\Exceptions\Locale\GermanBicException
*/
class GermanBicTest extends LocaleTestCase
{
public function testShouldAcceptBAVInstanceOnConstrutor()
{
$bav = $this->getBavMock();
$rule = new GermanBic($bav);
$this->assertSame($bav, $rule->bav);
}
public function testShouldHaveAnInstanceOfBAVByDefault()
{
$rule = new GermanBic();
$this->assertInstanceOf('malkusch\bav\BAV', $rule->bav);
}
public function testShouldUseBAVInstanceToValidate()
{
$input = '10000000';
$bav = $this->getBavMock();
$rule = new GermanBic($bav);
$bav->expects($this->once())
->method('isValidBIC')
->with($input)
->will($this->returnValue(true));
$rule->validate($input);
}
public function testShouldReturnBAVInstanceResulteWhenValidating()
{
$input = '10000000';
$bav = $this->getBavMock();
$rule = new GermanBic($bav);
$bav->expects($this->any())
->method('isValidBIC')
->with($input)
->will($this->returnValue(true));
$this->assertTrue($rule->validate($input));
}
/**
* @expectedException Respect\Validation\Exceptions\Locale\GermanBicException
* @expectedExceptionMessage "10000000" must be a german BIC
*/
public function testShouldThowsTheRightExceptionWhenChecking()
{
$input = '10000000';
$bav = $this->getBavMock();
$rule = new GermanBic($bav);
$bav->expects($this->any())
->method('isValidBIC')
->with($input)
->will($this->returnValue(false));
$rule->check($input);
}
}