cvs: pear /Validate/tests credit_card.php

From: Date: Sun, 13 Mar 2005 18:05:07 +0000
Subject: cvs: pear /Validate/tests credit_card.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-30250@lists.php.net to get a copy of this message
jausions Sun Mar 13 13:05:07 2005 EDT Modified files: /pear/Validate/tests credit_card.php Log: Fixed test cases for class name and tests http://cvs.php.net/diff.php/pear/Validate/tests/credit_card.php?r1=1.5&r2=1.6&ty=u Index: pear/Validate/tests/credit_card.php diff -u pear/Validate/tests/credit_card.php:1.5 pear/Validate/tests/credit_card.php:1.6 --- pear/Validate/tests/credit_card.php:1.5 Thu Jan 20 10:19:46 2005 +++ pear/Validate/tests/credit_card.php Sun Mar 13 13:05:05 2005 @@ -2,7 +2,7 @@ require_once 'PHPUnit.php'; require_once 'Validate/Finance/CreditCard.php'; -class Validate_CreditCard_Test extends PHPUnit_TestCase +class Validate_Finance_CreditCard_Test extends PHPUnit_TestCase { var $cards = array( 0 => false, @@ -10,21 +10,22 @@ '6762195515061813' => true, '6762 1955 1506 1813' => true, '6762195515061814' => false, + '0x1A6A195515061813' => false, '4405771709596026' => true, - 'Big brother is watching you' => false + 'Big brother watch' => false ); - // Test cards + // Test card type detection var $cardTypes = array( - 'VISA' => '4111 1111 1111 1111', - 'VISA' => '4111-1111-1111-1111', - 'VISA' => '4111111111111111', - 'MasterCard' => '5500 0000 0000 0004', - 'AMEX' => '3400 0000 0000 009', - 'JCB' => '3088 0000 0000 0009', - 'ENROUTE' => '2014 0000 0000 009', - 'DISCOVER' => '6011 0000 0000 0004', - 'DINERSCLUB' => '3000 0000 0000 04'); + '4111 1111 1111 1111' => 'VISA', + '4111-1111-1111-1111' => 'VISA', + '4111111111111111' => 'VISA', + '5500 0000 0000 0004' => 'MasterCard', + '3400 0000 0000 009' => 'AMEX', + '3088 0000 0000 0009' => 'JCB', + '2014 0000 0000 009' => 'ENROUTE', + '6011 0000 0000 0004' => 'DISCOVER', + '3000 0000 0000 04' => 'DINERSCLUB'); // Test valid LUHN, but invalid cards var $shortCards = array( @@ -36,50 +37,75 @@ 'DISCOVER' => '60110004', 'DINERSCLUB' => '300004'); - function Validate_CreditCard_Test($name) + function Validate_Finance_CreditCard_Test($name) { $this->PHPUnit_TestCase($name); } - /* will be used later */ - function setup () - { - } - - function tearDown() + function testCreditCardNumberOnly() { + $pass = true; + $msg = ''; + foreach ($this->cards as $card => $expected_result) { + if ($expected_result !== Validate_Finance_CreditCard::isValid($card)) { + $msg = ' on "' . $card . '" ' . (($expected_result) ? 'valid,' : 'invalid,'); + $pass = false; + break; + } + } + $this->assertTrue($pass, $msg); } - function testCreditCard() + function testCreditCardTypeOnly() { - foreach ($this->cards as $card => $expected_result) { - $r = Validate_CreditCard::creditCard($card); - $this->assertEquals($r, $expected_result); + $pass = true; + $msg = ''; + $cardBrands = array_reverse(array_unique($this->cardTypes)); + foreach ($cardBrands as $brand) { + foreach ($this->cardTypes as $card => $type) { + $expected_result = (bool) ($type === $brand); + if ($expected_result !== Validate_Finance_CreditCard::isType($card, $brand)) { + $msg = ' on "' . $card . '" is ' . (($type == $brand) ? '' : 'not') . ' a "' . $brand . '",'; + $pass = false; + break; + } + } } + $this->assertTrue($pass, $msg); } - function testDetectCreditCardType() + function testCreditCardNumberWithType() { - foreach (array_keys($this->cardTypes) as $comp_type) { - foreach ($this->cardTypes as $type => $number) { - $r = Validate_CreditCard::creditCardType($number, $comp_type); - $this->assertEquals($r, (bool) ($type == $comp_type)); + $pass = true; + $msg = ''; + $cardBrands = array_reverse(array_unique($this->cardTypes)); + foreach ($cardBrands as $brand) { + foreach ($this->cardTypes as $card => $type) { + $expected_result = (bool) ($type === $brand); + if ($expected_result !== Validate_Finance_CreditCard::isValid($card, $brand)) { + $msg = ' on "' . $card . '" is ' . (($type == $brand) ? '' : 'not') . ' a valid "' . $brand . '",'; + $pass = false; + break; + } } } + $this->assertTrue($pass, $msg); } - function testValidLuhnCheckBadNumberCreditCardType() +/* function testValidLuhn() { + $pass = false; foreach ($this->shortCards as $type => $number) { - $r = Validate_CreditCard::creditCard($number, $type); - $this->assertEquals($r, false); + $r = Validate_Finance_CreditCard::isValid($number, $type); + $pass |= $r; } - } + $this->assertFalse($pass); + } */ } // runs the tests -$suite = new PHPUnit_TestSuite('Validate_CreditCard_Test'); +$suite = new PHPUnit_TestSuite('Validate_Finance_CreditCard_Test'); $result = PHPUnit::run($suite); // prints the tests echo $result->toString();

« previous php.pear.cvs (#30250) next »