LuhnValidatorTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Luhn;
  12. use Symfony\Component\Validator\Constraints\LuhnValidator;
  13. use Symfony\Component\Validator\Validation;
  14. class LuhnValidatorTest extends AbstractConstraintValidatorTest
  15. {
  16. protected function getApiVersion()
  17. {
  18. return Validation::API_VERSION_2_5;
  19. }
  20. protected function createValidator()
  21. {
  22. return new LuhnValidator();
  23. }
  24. public function testNullIsValid()
  25. {
  26. $this->validator->validate(null, new Luhn());
  27. $this->assertNoViolation();
  28. }
  29. public function testEmptyStringIsValid()
  30. {
  31. $this->validator->validate('', new Luhn());
  32. $this->assertNoViolation();
  33. }
  34. /**
  35. * @dataProvider getValidNumbers
  36. */
  37. public function testValidNumbers($number)
  38. {
  39. $this->validator->validate($number, new Luhn());
  40. $this->assertNoViolation();
  41. }
  42. public function getValidNumbers()
  43. {
  44. return array(
  45. array('42424242424242424242'),
  46. array('378282246310005'),
  47. array('371449635398431'),
  48. array('378734493671000'),
  49. array('5610591081018250'),
  50. array('30569309025904'),
  51. array('38520000023237'),
  52. array('6011111111111117'),
  53. array('6011000990139424'),
  54. array('3530111333300000'),
  55. array('3566002020360505'),
  56. array('5555555555554444'),
  57. array('5105105105105100'),
  58. array('4111111111111111'),
  59. array('4012888888881881'),
  60. array('4222222222222'),
  61. array('5019717010103742'),
  62. array('6331101999990016'),
  63. );
  64. }
  65. /**
  66. * @dataProvider getInvalidNumbers
  67. */
  68. public function testInvalidNumbers($number, $code)
  69. {
  70. $constraint = new Luhn(array(
  71. 'message' => 'myMessage',
  72. ));
  73. $this->validator->validate($number, $constraint);
  74. $this->buildViolation('myMessage')
  75. ->setParameter('{{ value }}', '"'.$number.'"')
  76. ->setCode($code)
  77. ->assertRaised();
  78. }
  79. public function getInvalidNumbers()
  80. {
  81. return array(
  82. array('1234567812345678', Luhn::CHECKSUM_FAILED_ERROR),
  83. array('4222222222222222', Luhn::CHECKSUM_FAILED_ERROR),
  84. array('0000000000000000', Luhn::CHECKSUM_FAILED_ERROR),
  85. array('000000!000000000', Luhn::INVALID_CHARACTERS_ERROR),
  86. array('42-22222222222222', Luhn::INVALID_CHARACTERS_ERROR),
  87. );
  88. }
  89. /**
  90. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  91. * @dataProvider getInvalidTypes
  92. */
  93. public function testInvalidTypes($number)
  94. {
  95. $constraint = new Luhn();
  96. $this->validator->validate($number, $constraint);
  97. }
  98. public function getInvalidTypes()
  99. {
  100. return array(
  101. array(0),
  102. array(123),
  103. array(42424242424242424242),
  104. array(378282246310005),
  105. array(371449635398431),
  106. );
  107. }
  108. }