diff --git a/composer.json b/composer.json index a869114c0..92e29ee4d 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/library/Exceptions/ValidationException.php b/library/Exceptions/ValidationException.php index 534d48cf1..6758dc412 100644 --- a/library/Exceptions/ValidationException.php +++ b/library/Exceptions/ValidationException.php @@ -305,6 +305,9 @@ public function hasCustomTemplate() 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(); diff --git a/library/Rules/Domain.php b/library/Rules/Domain.php index b6a365ca6..4290b496c 100644 --- a/library/Rules/Domain.php +++ b/library/Rules/Domain.php @@ -29,16 +29,12 @@ public function __construct($tldCheck = true) 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('-')) ); } diff --git a/tests/integration/issue-739.phpt b/tests/integration/issue-739.phpt new file mode 100644 index 000000000..489f9595e --- /dev/null +++ b/tests/integration/issue-739.phpt @@ -0,0 +1,16 @@ +--FILE-- +check('foo'); +} catch (ValidationException $exception) { + echo $exception->getMainMessage(); +} +?> +--EXPECT-- +"foo" is not valid diff --git a/tests/library/Rules/LocaleTestCase.php b/tests/library/Rules/LocaleTestCase.php deleted file mode 100644 index 023c314c1..000000000 --- a/tests/library/Rules/LocaleTestCase.php +++ /dev/null @@ -1,39 +0,0 @@ - - * - * 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); - } -} diff --git a/tests/unit/Rules/BankAccountTest.php b/tests/unit/Rules/BankAccountTest.php deleted file mode 100644 index 9564687ca..000000000 --- a/tests/unit/Rules/BankAccountTest.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * 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()); - } -} diff --git a/tests/unit/Rules/BankTest.php b/tests/unit/Rules/BankTest.php deleted file mode 100644 index f0813d564..000000000 --- a/tests/unit/Rules/BankTest.php +++ /dev/null @@ -1,45 +0,0 @@ - - * - * 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()); - } -} diff --git a/tests/unit/Rules/BicTest.php b/tests/unit/Rules/BicTest.php deleted file mode 100644 index 9c797021b..000000000 --- a/tests/unit/Rules/BicTest.php +++ /dev/null @@ -1,45 +0,0 @@ - - * - * 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()); - } -} diff --git a/tests/unit/Rules/DomainTest.php b/tests/unit/Rules/DomainTest.php index 1593e5da2..8324edf6f 100644 --- a/tests/unit/Rules/DomainTest.php +++ b/tests/unit/Rules/DomainTest.php @@ -67,6 +67,9 @@ public function providerForDomain() ['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 @@ public function providerForNotDomain() [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'], ]; diff --git a/tests/unit/Rules/Locale/FactoryTest.php b/tests/unit/Rules/Locale/FactoryTest.php deleted file mode 100644 index 5b95f2c7c..000000000 --- a/tests/unit/Rules/Locale/FactoryTest.php +++ /dev/null @@ -1,92 +0,0 @@ - - * - * 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')); - } -} diff --git a/tests/unit/Rules/Locale/GermanBankAccountTest.php b/tests/unit/Rules/Locale/GermanBankAccountTest.php deleted file mode 100644 index caac0f118..000000000 --- a/tests/unit/Rules/Locale/GermanBankAccountTest.php +++ /dev/null @@ -1,96 +0,0 @@ - - * - * 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); - } -} diff --git a/tests/unit/Rules/Locale/GermanBankTest.php b/tests/unit/Rules/Locale/GermanBankTest.php deleted file mode 100644 index c85d1edcc..000000000 --- a/tests/unit/Rules/Locale/GermanBankTest.php +++ /dev/null @@ -1,83 +0,0 @@ - - * - * 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); - } -} diff --git a/tests/unit/Rules/Locale/GermanBicTest.php b/tests/unit/Rules/Locale/GermanBicTest.php deleted file mode 100644 index f6f66db63..000000000 --- a/tests/unit/Rules/Locale/GermanBicTest.php +++ /dev/null @@ -1,83 +0,0 @@ - - * - * 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); - } -}