CreditCardTest.php 8.0 KB

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