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\Test\ConstraintValidatorTestCase;
18 class LuhnValidatorTest extends ConstraintValidatorTestCase
20 protected function createValidator()
22 return new LuhnValidator();
25 public function testNullIsValid()
27 $this->validator->validate(null, new Luhn());
29 $this->assertNoViolation();
32 public function testEmptyStringIsValid()
34 $this->validator->validate('', new Luhn());
36 $this->assertNoViolation();
40 * @dataProvider getValidNumbers
42 public function testValidNumbers($number)
44 $this->validator->validate($number, new Luhn());
46 $this->assertNoViolation();
49 public function getValidNumbers()
52 array('42424242424242424242'),
53 array('378282246310005'),
54 array('371449635398431'),
55 array('378734493671000'),
56 array('5610591081018250'),
57 array('30569309025904'),
58 array('38520000023237'),
59 array('6011111111111117'),
60 array('6011000990139424'),
61 array('3530111333300000'),
62 array('3566002020360505'),
63 array('5555555555554444'),
64 array('5105105105105100'),
65 array('4111111111111111'),
66 array('4012888888881881'),
67 array('4222222222222'),
68 array('5019717010103742'),
69 array('6331101999990016'),
74 * @dataProvider getInvalidNumbers
76 public function testInvalidNumbers($number, $code)
78 $constraint = new Luhn(array(
79 'message' => 'myMessage',
82 $this->validator->validate($number, $constraint);
84 $this->buildViolation('myMessage')
85 ->setParameter('{{ value }}', '"'.$number.'"')
90 public function getInvalidNumbers()
93 array('1234567812345678', Luhn::CHECKSUM_FAILED_ERROR),
94 array('4222222222222222', Luhn::CHECKSUM_FAILED_ERROR),
95 array('0000000000000000', Luhn::CHECKSUM_FAILED_ERROR),
96 array('000000!000000000', Luhn::INVALID_CHARACTERS_ERROR),
97 array('42-22222222222222', Luhn::INVALID_CHARACTERS_ERROR),
102 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
103 * @dataProvider getInvalidTypes
105 public function testInvalidTypes($number)
107 $constraint = new Luhn();
109 $this->validator->validate($number, $constraint);
112 public function getInvalidTypes()
117 array(42424242424242424242),
118 array(378282246310005),
119 array(371449635398431),