CreditCardTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. '4111111111111111' => true,
  50. '5404000000000001' => true,
  51. '374200000000004' => true,
  52. '4444555566667777' => false,
  53. 'ABCDEF' => false
  54. );
  55. foreach ($valuesExpected as $input => $result) {
  56. $this->assertEquals($result, $validator->isValid($input), 'Test failed at ' . $input);
  57. }
  58. }
  59. /**
  60. * Ensures that getMessages() returns expected default value
  61. *
  62. * @return void
  63. */
  64. public function testGetMessages()
  65. {
  66. $validator = new Zend_Validate_CreditCard();
  67. $this->assertEquals(array(), $validator->getMessages());
  68. }
  69. /**
  70. * Ensures that get and setType works as expected
  71. *
  72. * @return void
  73. */
  74. public function testGetSetType()
  75. {
  76. $validator = new Zend_Validate_CreditCard();
  77. $this->assertEquals(11, count($validator->getType()));
  78. $validator->setType(Zend_Validate_CreditCard::MAESTRO);
  79. $this->assertEquals(array(Zend_Validate_CreditCard::MAESTRO), $validator->getType());
  80. $validator->setType(
  81. array(
  82. Zend_Validate_CreditCard::AMERICAN_EXPRESS,
  83. Zend_Validate_CreditCard::MAESTRO
  84. )
  85. );
  86. $this->assertEquals(
  87. array(
  88. Zend_Validate_CreditCard::AMERICAN_EXPRESS,
  89. Zend_Validate_CreditCard::MAESTRO
  90. ),
  91. $validator->getType()
  92. );
  93. $validator->addType(
  94. Zend_Validate_CreditCard::MASTERCARD
  95. );
  96. $this->assertEquals(
  97. array(
  98. Zend_Validate_CreditCard::AMERICAN_EXPRESS,
  99. Zend_Validate_CreditCard::MAESTRO,
  100. Zend_Validate_CreditCard::MASTERCARD
  101. ),
  102. $validator->getType()
  103. );
  104. }
  105. /**
  106. * Test specific provider
  107. *
  108. * @return void
  109. */
  110. public function testProvider()
  111. {
  112. $validator = new Zend_Validate_CreditCard(Zend_Validate_CreditCard::VISA);
  113. $valuesExpected = array(
  114. '4111111111111111' => true,
  115. '5404000000000001' => false,
  116. '374200000000004' => false,
  117. '4444555566667777' => false,
  118. 'ABCDEF' => false
  119. );
  120. foreach ($valuesExpected as $input => $result) {
  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. }