4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Tests\Constraints;
14 use Symfony\Component\Validator\Constraints\Luhn;
15 use Symfony\Component\Validator\Constraints\LuhnValidator;
16 use Symfony\Component\Validator\Validation;
18 class LuhnValidatorTest extends AbstractConstraintValidatorTest
20 protected function getApiVersion()
22 return Validation::API_VERSION_2_5;
25 protected function createValidator()
27 return new LuhnValidator();
30 public function testNullIsValid()
32 $this->validator->validate(null, new Luhn());
34 $this->assertNoViolation();
37 public function testEmptyStringIsValid()
39 $this->validator->validate('', new Luhn());
41 $this->assertNoViolation();
45 * @dataProvider getValidNumbers
47 public function testValidNumbers($number)
49 $this->validator->validate($number, new Luhn());
51 $this->assertNoViolation();
54 public function getValidNumbers()
57 array('42424242424242424242'),
58 array('378282246310005'),
59 array('371449635398431'),
60 array('378734493671000'),
61 array('5610591081018250'),
62 array('30569309025904'),
63 array('38520000023237'),
64 array('6011111111111117'),
65 array('6011000990139424'),
66 array('3530111333300000'),
67 array('3566002020360505'),
68 array('5555555555554444'),
69 array('5105105105105100'),
70 array('4111111111111111'),
71 array('4012888888881881'),
72 array('4222222222222'),
73 array('5019717010103742'),
74 array('6331101999990016'),
79 * @dataProvider getInvalidNumbers
81 public function testInvalidNumbers($number, $code)
83 $constraint = new Luhn(array(
84 'message' => 'myMessage',
87 $this->validator->validate($number, $constraint);
89 $this->buildViolation('myMessage')
90 ->setParameter('{{ value }}', '"'.$number.'"')
95 public function getInvalidNumbers()
98 array('1234567812345678', Luhn::CHECKSUM_FAILED_ERROR),
99 array('4222222222222222', Luhn::CHECKSUM_FAILED_ERROR),
100 array('0000000000000000', Luhn::CHECKSUM_FAILED_ERROR),
101 array('000000!000000000', Luhn::INVALID_CHARACTERS_ERROR),
102 array('42-22222222222222', Luhn::INVALID_CHARACTERS_ERROR),
107 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
108 * @dataProvider getInvalidTypes
110 public function testInvalidTypes($number)
112 $constraint = new Luhn();
114 $this->validator->validate($number, $constraint);
117 public function getInvalidTypes()
122 array(42424242424242424242),
123 array(378282246310005),
124 array(371449635398431),