Skip to content

Instantly share code, notes, and snippets.

@baldurrensch
Created August 15, 2012 21:12
Show Gist options
  • Select an option

  • Save baldurrensch/3363726 to your computer and use it in GitHub Desktop.

Select an option

Save baldurrensch/3363726 to your computer and use it in GitHub Desktop.
Credit Card Unit Tests
<?php
namespace Acme\DV2Bundle\Tests\Model;
use Acme\DV2Bundle\Model\CreditCard;
class CreditCardTest extends \PHPUnit_Framework_TestCase
{
public function testMod10()
{
// The valid credit card numbers should be:
// - American Express
// - Discover
// - MasterCard
// - Visa
// - Visa Commercial Card
// - Visa Corporate Card II
// - Visa Purchasing Card III
// - MasterCard II
// - MasterCard III
// - MasterCard Diner's
// - Diner's Club / Cart Blanche
$validNumbers = array(
'371144371144376',
'341134113411347',
'370000000000002',
'378282246310005',
'6011016011016011',
'6559906559906557',
'6011000000000012',
'6011111111111117',
'5111005111051128',
'5112345112345114',
'5424000000000015',
'5105105105105100',
'4112344112344113',
'4007000000027',
'4111111111111111',
'4110144110144115',
'4114360123456785',
'4061724061724061',
'5115915115915118',
'5116601234567894',
'36111111111111',
'36110361103612',
'36438936438936',
'30569309025904',
);
// Invalid numbers should be:
// - JCB
// - Unknown Type
$invalidNumbers = array(
'3566003566003566',
'3530111333300000',
'111',
);
$card = new CreditCard();
foreach ($validNumbers as $number) {
$card->setCreditCardNumber($number);
//echo "$number: " . ($card->isValidCreditCard() ? 'T' : 'F') . "\n";
$this->assertTrue($card->isValidCreditCard());
}
foreach ($invalidNumbers as $number) {
$card->setCreditCardNumber($number);
//echo "$number: " . ($card->isValidCreditCard() ? 'T' : 'F') . "\n";
$this->assertFalse($card->isValidCreditCard());
}
}
public function testExpirationDates() {
$testCases = array(
array('month' => '0', 'year'=>'2100', 'result' => false),
array('month' => '1', 'year'=>'2100', 'result' => true),
array('month' => '13', 'year'=>'2100', 'result' => false),
array('month' => '1', 'year'=>'1900', 'result' => false),
array('month' => '1', 'year'=> date('Y') + 1, 'result' => true),
array('month' => '1', 'year'=> date('Y') - 1, 'result' => false),
array('month' => date('n'), 'year'=> date('Y'), 'result' => true),
);
$currentMonth = date('n');
if ($currentMonth == 12) {
$testCases []= array('month' => 1, 'year'=> date('Y') + 1, 'result' => true);
} else if ($currentMonth == 1) {
$testCases []= array('month' => 12, 'year'=> date('Y') - 1, 'result' => true);
}
$card = new CreditCard();
foreach ($testCases as $case) {
$card->setCreditCardExpMonth($case['month']);
$card->setCreditCardExpYear($case['year']);
$this->assertEquals($card->isValidCCExpiration(), $case['result']);
}
}
}
@vailouk
Copy link
Copy Markdown

vailouk commented Jan 14, 2022

Can be help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment