CardSchemeValidatorTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Tests\Constraints;
  11. use Symfony\Component\Validator\Constraints\CardScheme;
  12. use Symfony\Component\Validator\Constraints\CardSchemeValidator;
  13. use Symfony\Component\Validator\Validation;
  14. class CardSchemeValidatorTest extends AbstractConstraintValidatorTest
  15. {
  16. protected function getApiVersion()
  17. {
  18. return Validation::API_VERSION_2_5;
  19. }
  20. protected function createValidator()
  21. {
  22. return new CardSchemeValidator();
  23. }
  24. public function testNullIsValid()
  25. {
  26. $this->validator->validate(null, new CardScheme(array('schemes' => array())));
  27. $this->assertNoViolation();
  28. }
  29. public function testEmptyStringIsValid()
  30. {
  31. $this->validator->validate('', new CardScheme(array('schemes' => array())));
  32. $this->assertNoViolation();
  33. }
  34. /**
  35. * @dataProvider getValidNumbers
  36. */
  37. public function testValidNumbers($scheme, $number)
  38. {
  39. $this->validator->validate($number, new CardScheme(array('schemes' => $scheme)));
  40. $this->assertNoViolation();
  41. }
  42. /**
  43. * @dataProvider getInvalidNumbers
  44. */
  45. public function testInvalidNumbers($scheme, $number, $code)
  46. {
  47. $constraint = new CardScheme(array(
  48. 'schemes' => $scheme,
  49. 'message' => 'myMessage',
  50. ));
  51. $this->validator->validate($number, $constraint);
  52. $this->buildViolation('myMessage')
  53. ->setParameter('{{ value }}', \is_string($number) ? '"'.$number.'"' : $number)
  54. ->setCode($code)
  55. ->assertRaised();
  56. }
  57. public function getValidNumbers()
  58. {
  59. return array(
  60. array('AMEX', '378282246310005'),
  61. array('AMEX', '371449635398431'),
  62. array('AMEX', '378734493671000'),
  63. array('AMEX', '347298508610146'),
  64. array('CHINA_UNIONPAY', '6228888888888888'),
  65. array('CHINA_UNIONPAY', '62288888888888888'),
  66. array('CHINA_UNIONPAY', '622888888888888888'),
  67. array('CHINA_UNIONPAY', '6228888888888888888'),
  68. array('DINERS', '30569309025904'),
  69. array('DINERS', '36088894118515'),
  70. array('DINERS', '38520000023237'),
  71. array('DISCOVER', '6011111111111117'),
  72. array('DISCOVER', '6011000990139424'),
  73. array('INSTAPAYMENT', '6372476031350068'),
  74. array('INSTAPAYMENT', '6385537775789749'),
  75. array('INSTAPAYMENT', '6393440808445746'),
  76. array('JCB', '3530111333300000'),
  77. array('JCB', '3566002020360505'),
  78. array('JCB', '213112345678901'),
  79. array('JCB', '180012345678901'),
  80. array('LASER', '6304678107004080'),
  81. array('LASER', '6706440607428128629'),
  82. array('LASER', '6771656738314582216'),
  83. array('MAESTRO', '6759744069209'),
  84. array('MAESTRO', '5020507657408074712'),
  85. array('MAESTRO', '5612559223580173965'),
  86. array('MAESTRO', '6759744069209'),
  87. array('MAESTRO', '6594371785970435599'),
  88. array('MASTERCARD', '5555555555554444'),
  89. array('MASTERCARD', '5105105105105100'),
  90. array('MASTERCARD', '2221005555554444'),
  91. array('MASTERCARD', '2230000000000000'),
  92. array('MASTERCARD', '2300000000000000'),
  93. array('MASTERCARD', '2699999999999999'),
  94. array('MASTERCARD', '2709999999999999'),
  95. array('MASTERCARD', '2720995105105100'),
  96. array('VISA', '4111111111111111'),
  97. array('VISA', '4012888888881881'),
  98. array('VISA', '4222222222222'),
  99. array('VISA', '4917610000000000003'),
  100. array(array('AMEX', 'VISA'), '4111111111111111'),
  101. array(array('AMEX', 'VISA'), '378282246310005'),
  102. array(array('JCB', 'MASTERCARD'), '5105105105105100'),
  103. array(array('VISA', 'MASTERCARD'), '5105105105105100'),
  104. );
  105. }
  106. public function getInvalidNumbers()
  107. {
  108. return array(
  109. array('VISA', '42424242424242424242', CardScheme::INVALID_FORMAT_ERROR),
  110. array('AMEX', '357298508610146', CardScheme::INVALID_FORMAT_ERROR),
  111. array('DINERS', '31569309025904', CardScheme::INVALID_FORMAT_ERROR),
  112. array('DINERS', '37088894118515', CardScheme::INVALID_FORMAT_ERROR),
  113. array('INSTAPAYMENT', '6313440808445746', CardScheme::INVALID_FORMAT_ERROR),
  114. array('CHINA_UNIONPAY', '622888888888888', CardScheme::INVALID_FORMAT_ERROR),
  115. array('CHINA_UNIONPAY', '62288888888888888888', CardScheme::INVALID_FORMAT_ERROR),
  116. array('AMEX', '30569309025904', CardScheme::INVALID_FORMAT_ERROR), // DINERS number
  117. array('AMEX', 'invalid', CardScheme::NOT_NUMERIC_ERROR), // A string
  118. array('AMEX', 0, CardScheme::INVALID_FORMAT_ERROR), // a lone number
  119. array('AMEX', '0', CardScheme::INVALID_FORMAT_ERROR), // a lone number
  120. array('AMEX', '000000000000', CardScheme::INVALID_FORMAT_ERROR), // a lone number
  121. array('DINERS', '3056930', CardScheme::INVALID_FORMAT_ERROR), // only first part of the number
  122. array('DISCOVER', '1117', CardScheme::INVALID_FORMAT_ERROR), // only last 4 digits
  123. array('MASTERCARD', '2721001234567890', CardScheme::INVALID_FORMAT_ERROR), // Not assigned yet
  124. array('MASTERCARD', '2220991234567890', CardScheme::INVALID_FORMAT_ERROR), // Not assigned yet
  125. );
  126. }
  127. }