From f2285f6b5327e08c40f264ffb6e455a02bb7513e Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Sat, 17 Jan 2015 12:03:31 -0200 Subject: [PATCH] Improve tests of banking rules --- .../Respect/Validation/Rules/BICTest.php | 78 ++++++++++++------- .../Validation/Rules/BankAccountTest.php | 58 ++++++++------ .../Respect/Validation/Rules/BankTest.php | 57 ++++++++------ 3 files changed, 120 insertions(+), 73 deletions(-) diff --git a/tests/library/Respect/Validation/Rules/BICTest.php b/tests/library/Respect/Validation/Rules/BICTest.php index 9a51b1264..8ed8d722e 100644 --- a/tests/library/Respect/Validation/Rules/BICTest.php +++ b/tests/library/Respect/Validation/Rules/BICTest.php @@ -6,83 +6,105 @@ use malkusch\bav\PDODataBackendContainer; /** - * @large + * @covers Respect\Validation\Rules\BIC */ class BICTest extends \PHPUnit_Framework_TestCase { - + protected static function isAvailable() + { + return class_exists('malkusch\\bav\\BAV'); + } + + protected function setUp() + { + if (false === self::isAvailable()) { + $this->markTestSkipped('"malkusch/bav" is not installed.'); + } + } + public static function setUpBeforeClass() { + if (false === self::isAvailable()) { + return; + } + $configuration = new DefaultConfiguration(); - + $pdo = new \PDO('sqlite::memory:'); $configuration->setDataBackendContainer(new PDODataBackendContainer($pdo)); - + ConfigurationRegistry::setConfiguration($configuration); } - + public static function tearDownAfterClass() { + if (false === self::isAvailable()) { + return; + } + ConfigurationRegistry::setConfiguration(new DefaultConfiguration()); } - + /** * @expectedException Respect\Validation\Exceptions\ComponentException + * @expectedExceptionMessage Cannot validate BIC for country 'xx'. */ public function testUnsupportedCountryCodeRaisesException() { - $validator = new BIC("xx"); + new BIC('xx'); } - + public function testCountryCodeIsCaseUnsensitive() { - $validator1 = new BIC("de"); - $validator1->validate("foo"); - - $validator2 = new BIC("DE"); - $validator2->validate("foo"); + $rule1 = new BIC('de'); + $rule2 = new BIC('DE'); + + $this->assertSame($rule1->validate('foo'), $rule2->validate('foo')); } - + /** * @dataProvider providerForValidBIC */ - public function testValidBICShouldReturnTrue(BIC $validator, $bic) + public function testValidBICShouldReturnTrue($countryCode, $bic) { - $this->assertTrue($validator->__invoke($bic)); - $this->assertTrue($validator->assert($bic)); - $this->assertTrue($validator->check($bic)); + $rule = new BIC($countryCode); + + $this->assertTrue($rule->validate($bic)); } /** * @dataProvider providerForNotBIC * @expectedException Respect\Validation\Exceptions\BICException + * @expectedExceptionMessageRegExp /^"[^"]+" must be a BIC$/ */ - public function testInvalidBICShouldRaiseException(BIC $validator, $bic) + public function testInvalidBICShouldRaiseException($countryCode, $bic) { - $this->assertFalse($validator->check($bic)); + $rule = new BIC($countryCode); + $rule->check($bic); } /** * @dataProvider providerForNotBIC */ - public function testInvalidBICShouldReturnFalse(BIC $validator, $bic) + public function testInvalidBICShouldReturnFalse($countryCode, $bic) { - $this->assertFalse($validator->__invoke($bic)); + $rule = new BIC($countryCode); + + $this->assertFalse($rule->validate($bic)); } - + public function providerForValidBIC() { return array( - array(new BIC("de"), "VZVDDED1XXX"), - array(new BIC("de"), "VZVDDED1") + array('de', 'VZVDDED1XXX'), + array('de', 'VZVDDED1'), ); } - + public function providerForNotBIC() { return array( - array(new BIC("de"), "VZVDDED1~~~") + array('de', 'VZVDDED1~~~'), ); } } - diff --git a/tests/library/Respect/Validation/Rules/BankAccountTest.php b/tests/library/Respect/Validation/Rules/BankAccountTest.php index 99d7fcca2..e98bc1394 100644 --- a/tests/library/Respect/Validation/Rules/BankAccountTest.php +++ b/tests/library/Respect/Validation/Rules/BankAccountTest.php @@ -1,66 +1,78 @@ markTestSkipped('"malkusch/bav" is not installed.'); + } + } + /** * @expectedException Respect\Validation\Exceptions\ComponentException */ public function testUnsupportedCountryCodeRaisesException() { - $validator = new BankAccount("xx", "bank"); + new BankAccount('xx', 'bank'); } - + public function testCountryCodeIsCaseUnsensitive() { - $validator1 = new BankAccount("de", "bank"); - $validator1->validate("foo"); - - $validator2 = new BankAccount("DE", "bank"); - $validator2->validate("foo"); + $rule1 = new BankAccount('de', 'bank'); + $rule2 = new BankAccount('DE', 'bank'); + + $this->assertSame($rule1->validate('foo'), $rule2->validate('foo')); } - + /** * @dataProvider providerForBankAccount */ - public function testValidAccountShouldReturnTrue(BankAccount $validator, $account) + public function testValidAccountShouldReturnTrue($countryCode, $bank, $account) { - $this->assertTrue($validator->__invoke($account)); - $this->assertTrue($validator->assert($account)); - $this->assertTrue($validator->check($account)); + $rule = new BankAccount($countryCode, $bank); + + $this->assertTrue($rule->validate($account)); } /** * @dataProvider providerForNotBankAccount * @expectedException Respect\Validation\Exceptions\BankAccountException + * @expectedExceptionMessageRegExp /^"[^"]+" must be a bank account$/ */ - public function testInvalidAccountShouldRaiseException(BankAccount $validator, $account) + public function testInvalidAccountShouldRaiseException($countryCode, $bank, $account) { - $this->assertFalse($validator->check($account)); + $rule = new BankAccount($countryCode, $bank); + $rule->check($account); } /** * @dataProvider providerForNotBankAccount */ - public function testInvalidAccountShouldReturnFalse(BankAccount $validator, $account) + public function testInvalidAccountShouldReturnFalse($countryCode, $bank, $account) { - $this->assertFalse($validator->__invoke($account)); + $rule = new BankAccount($countryCode, $bank); + + $this->assertFalse($rule->validate($account)); } - + public function providerForBankAccount() { return array( - array(new BankAccount("de", "70169464"), "1112"), - array(new BankAccount("de", "70169464"), "67067"), + array('de', '70169464', '1112'), + array('de', '70169464', '67067'), ); } - + public function providerForNotBankAccount() { return array( - array(new BankAccount("de", "70169464"), "1234"), - array(new BankAccount("de", "1234"), "1234") + array('de', '70169464', '1234'), + array('de', '1234', '1234'), ); } } - diff --git a/tests/library/Respect/Validation/Rules/BankTest.php b/tests/library/Respect/Validation/Rules/BankTest.php index 78670b0b3..5e9a709a0 100644 --- a/tests/library/Respect/Validation/Rules/BankTest.php +++ b/tests/library/Respect/Validation/Rules/BankTest.php @@ -1,64 +1,77 @@ markTestSkipped('"malkusch/bav" is not installed.'); + } + } + /** * @expectedException Respect\Validation\Exceptions\ComponentException + * @expectedExceptionMessage Cannot validate bank for country 'xx'. */ public function testUnsupportedCountryCodeRaisesException() { - new Bank("xx"); + new Bank('xx'); } - + public function testCountryCodeIsCaseUnsensitive() { - $validator1 = new Bank("de"); - $validator1->validate("foo"); - - $validator2 = new Bank("DE"); - $validator2->validate("foo"); + $rule1 = new Bank('de'); + $rule2 = new Bank('DE'); + + $this->assertSame($rule1->validate('foo'), $rule2->validate('foo')); } - + /** * @dataProvider providerForBank */ - public function testValidBankShouldReturnTrue(Bank $validator, $bank) + public function testValidBankShouldReturnTrue($countryCode, $bank) { - $this->assertTrue($validator->__invoke($bank)); - $this->assertTrue($validator->assert($bank)); - $this->assertTrue($validator->check($bank)); + $rule = new Bank($countryCode); + + $this->assertTrue($rule->validate($bank)); } /** * @dataProvider providerForNotBank * @expectedException Respect\Validation\Exceptions\BankException + * @expectedExceptionMessageRegExp /^"[^"]+" must be a bank\.$/ */ - public function testInvalidBankShouldRaiseException(Bank $validator, $bank) + public function testInvalidBankShouldRaiseException($countryCode, $bank) { - $this->assertFalse($validator->check($bank)); + $rule = new Bank($countryCode); + $rule->check($bank); } /** * @dataProvider providerForNotBank */ - public function testInvalidBankShouldReturnFalse(Bank $validator, $bank) + public function testInvalidBankShouldReturnFalse($countryCode, $bank) { - $this->assertFalse($validator->__invoke($bank)); + $rule = new Bank($countryCode); + + $this->assertFalse($rule->validate($bank)); } - + public function providerForNotBank() { return array( - array(new Bank("de"), "1234") + array('de', '1234'), ); } - + public function providerForBank() { return array( - array(new Bank("de"), "10000000") + array('de', '10000000'), ); } } -