CreditCardTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id:$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. /**
  27. * @see Zend_Validate_CreditCard
  28. */
  29. require_once 'Zend/Validate/CreditCard.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Validate
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Validate
  37. */
  38. class Zend_Validate_CreditCardTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Ensures that the validator follows expected behavior
  42. *
  43. * @return void
  44. */
  45. public function testBasic()
  46. {
  47. $validator = new Zend_Validate_CreditCard();
  48. $valuesExpected = array(
  49. array('4111111111111111', true),
  50. array('5404000000000001', true),
  51. array('374200000000004', true),
  52. array('4444555566667777', false),
  53. array('ABCDEF', false)
  54. );
  55. foreach ($valuesExpected as $test) {
  56. $input = $test[0];
  57. $result = $test[1];
  58. $this->assertEquals($result, $validator->isValid($input), 'Test failed at ' . $input);
  59. }
  60. }
  61. /**
  62. * Ensures that getMessages() returns expected default value
  63. *
  64. * @return void
  65. */
  66. public function testGetMessages()
  67. {
  68. $validator = new Zend_Validate_CreditCard();
  69. $this->assertEquals(array(), $validator->getMessages());
  70. }
  71. /**
  72. * Ensures that get and setType works as expected
  73. *
  74. * @return void
  75. */
  76. public function testGetSetType()
  77. {
  78. $validator = new Zend_Validate_CreditCard();
  79. $this->assertEquals(11, count($validator->getType()));
  80. $validator->setType(Zend_Validate_CreditCard::MAESTRO);
  81. $this->assertEquals(array(Zend_Validate_CreditCard::MAESTRO), $validator->getType());
  82. $validator->setType(
  83. array(
  84. Zend_Validate_CreditCard::AMERICAN_EXPRESS,
  85. Zend_Validate_CreditCard::MAESTRO
  86. )
  87. );
  88. $this->assertEquals(
  89. array(
  90. Zend_Validate_CreditCard::AMERICAN_EXPRESS,
  91. Zend_Validate_CreditCard::MAESTRO
  92. ),
  93. $validator->getType()
  94. );
  95. $validator->addType(
  96. Zend_Validate_CreditCard::MASTERCARD
  97. );
  98. $this->assertEquals(
  99. array(
  100. Zend_Validate_CreditCard::AMERICAN_EXPRESS,
  101. Zend_Validate_CreditCard::MAESTRO,
  102. Zend_Validate_CreditCard::MASTERCARD
  103. ),
  104. $validator->getType()
  105. );
  106. }
  107. /**
  108. * Test specific provider
  109. *
  110. * @return void
  111. */
  112. public function testProvider()
  113. {
  114. $validator = new Zend_Validate_CreditCard(Zend_Validate_CreditCard::VISA);
  115. $valuesExpected = array(
  116. array('4111111111111111', true),
  117. array('5404000000000001', false),
  118. array('374200000000004', false),
  119. array('4444555566667777', false),
  120. array('ABCDEF', false)
  121. );
  122. foreach ($valuesExpected as $test) {
  123. $input = $test[0];
  124. $result = $test[1];
  125. $this->assertEquals($result, $validator->isValid($input));
  126. }
  127. }
  128. /**
  129. * Test non string input
  130. *
  131. * @return void
  132. */
  133. public function testIsValidWithNonString()
  134. {
  135. $validator = new Zend_Validate_CreditCard(Zend_Validate_CreditCard::VISA);
  136. $this->assertFalse($validator->isValid(array('something')));
  137. }
  138. /**
  139. * Test service class with invalid validation
  140. *
  141. * @return void
  142. */
  143. public function testServiceClass()
  144. {
  145. $validator = new Zend_Validate_CreditCard();
  146. $this->assertEquals(null, $validator->getService());
  147. $validator->setService(array('Zend_Validate_CreditCardTest', 'staticCallback'));
  148. $valuesExpected = array(
  149. '4111111111111111' => false,
  150. '5404000000000001' => false,
  151. '374200000000004' => false,
  152. '4444555566667777' => false,
  153. 'ABCDEF' => false
  154. );
  155. foreach ($valuesExpected as $input => $result) {
  156. $this->assertEquals($result, $validator->isValid($input));
  157. }
  158. }
  159. /**
  160. * Test non string input
  161. *
  162. * @return void
  163. */
  164. public function testConstructionWithOptions()
  165. {
  166. $validator = new Zend_Validate_CreditCard(
  167. array(
  168. 'type' => Zend_Validate_CreditCard::VISA,
  169. 'service' => array('Zend_Validate_CreditCardTest', 'staticCallback')
  170. )
  171. );
  172. $valuesExpected = array(
  173. '4111111111111111' => false,
  174. '5404000000000001' => false,
  175. '374200000000004' => false,
  176. '4444555566667777' => false,
  177. 'ABCDEF' => false
  178. );
  179. foreach ($valuesExpected as $input => $result) {
  180. $this->assertEquals($result, $validator->isValid($input));
  181. }
  182. }
  183. /**
  184. * Test a invalid service class
  185. *
  186. * @return void
  187. */
  188. public function testInvalidServiceClass()
  189. {
  190. $validator = new Zend_Validate_CreditCard();
  191. $this->assertEquals(null, $validator->getService());
  192. try {
  193. $validator->setService(array('Zend_Validate_CreditCardTest', 'nocallback'));
  194. $this->fail('Exception expected');
  195. } catch(Zend_Exception $e) {
  196. $this->assertContains('Invalid callback given', $e->getMessage());
  197. }
  198. }
  199. /**
  200. * Test a config object
  201. *
  202. * @return void
  203. */
  204. public function testConfigObject()
  205. {
  206. require_once 'Zend/Config.php';
  207. $options = array('type' => 'Visa');
  208. $config = new Zend_Config($options, false);
  209. $validator = new Zend_Validate_CreditCard($config);
  210. $this->assertEquals(array('Visa'), $validator->getType());
  211. }
  212. /**
  213. * Test optional parameters with config object
  214. *
  215. * @return void
  216. */
  217. public function testOptionalConstructorParameterByConfigObject()
  218. {
  219. require_once 'Zend/Config.php';
  220. $config = new Zend_Config(array('type' => 'Visa', 'service' => array('Zend_Validate_CreditCardTest', 'staticCallback')));
  221. $validator = new Zend_Validate_CreditCard($config);
  222. $this->assertEquals(array('Visa'), $validator->getType());
  223. $this->assertEquals(array('Zend_Validate_CreditCardTest', 'staticCallback'), $validator->getService());
  224. }
  225. /**
  226. * Test optional constructor parameters
  227. *
  228. * @return void
  229. */
  230. public function testOptionalConstructorParameter()
  231. {
  232. $validator = new Zend_Validate_CreditCard('Visa', array('Zend_Validate_CreditCardTest', 'staticCallback'));
  233. $this->assertEquals(array('Visa'), $validator->getType());
  234. $this->assertEquals(array('Zend_Validate_CreditCardTest', 'staticCallback'), $validator->getService());
  235. }
  236. /**
  237. * @group ZF-9477
  238. */
  239. public function testMultiInstitute() {
  240. $validator = new Zend_Validate_CreditCard(array('type' => Zend_Validate_CreditCard::MASTERCARD));
  241. $this->assertFalse($validator->isValid('4111111111111111'));
  242. $message = $validator->getMessages();
  243. $this->assertContains('not from an allowed institute', current($message));
  244. }
  245. public static function staticCallback($value)
  246. {
  247. return false;
  248. }
  249. }