Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tests of banking rules #248

Merged
merged 1 commit into from
Jan 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 50 additions & 28 deletions tests/library/Respect/Validation/Rules/BICTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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~~~'),
);
}
}

58 changes: 35 additions & 23 deletions tests/library/Respect/Validation/Rules/BankAccountTest.php
Original file line number Diff line number Diff line change
@@ -1,66 +1,78 @@
<?php
namespace Respect\Validation\Rules;

/**
* @covers Respect\Validation\Rules\BankAccount
*/
class BankAccountTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
if (false === class_exists('malkusch\\bav\\BAV')) {
$this->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'),
);
}
}

57 changes: 35 additions & 22 deletions tests/library/Respect/Validation/Rules/BankTest.php
Original file line number Diff line number Diff line change
@@ -1,64 +1,77 @@
<?php
namespace Respect\Validation\Rules;

class GermanBankTest extends \PHPUnit_Framework_TestCase
/**
* @covers Respect\Validation\Rules\Bank
*/
class BankTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
if (false === class_exists('malkusch\\bav\\BAV')) {
$this->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'),
);
}
}