Yaffs site version 1.1
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / LuhnValidatorTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Tests\Constraints;
13
14 use Symfony\Component\Validator\Constraints\Luhn;
15 use Symfony\Component\Validator\Constraints\LuhnValidator;
16 use Symfony\Component\Validator\Validation;
17
18 class LuhnValidatorTest extends AbstractConstraintValidatorTest
19 {
20     protected function getApiVersion()
21     {
22         return Validation::API_VERSION_2_5;
23     }
24
25     protected function createValidator()
26     {
27         return new LuhnValidator();
28     }
29
30     public function testNullIsValid()
31     {
32         $this->validator->validate(null, new Luhn());
33
34         $this->assertNoViolation();
35     }
36
37     public function testEmptyStringIsValid()
38     {
39         $this->validator->validate('', new Luhn());
40
41         $this->assertNoViolation();
42     }
43
44     /**
45      * @dataProvider getValidNumbers
46      */
47     public function testValidNumbers($number)
48     {
49         $this->validator->validate($number, new Luhn());
50
51         $this->assertNoViolation();
52     }
53
54     public function getValidNumbers()
55     {
56         return array(
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'),
75         );
76     }
77
78     /**
79      * @dataProvider getInvalidNumbers
80      */
81     public function testInvalidNumbers($number, $code)
82     {
83         $constraint = new Luhn(array(
84             'message' => 'myMessage',
85         ));
86
87         $this->validator->validate($number, $constraint);
88
89         $this->buildViolation('myMessage')
90             ->setParameter('{{ value }}', '"'.$number.'"')
91             ->setCode($code)
92             ->assertRaised();
93     }
94
95     public function getInvalidNumbers()
96     {
97         return array(
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),
103         );
104     }
105
106     /**
107      * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
108      * @dataProvider getInvalidTypes
109      */
110     public function testInvalidTypes($number)
111     {
112         $constraint = new Luhn();
113
114         $this->validator->validate($number, $constraint);
115     }
116
117     public function getInvalidTypes()
118     {
119         return array(
120             array(0),
121             array(123),
122             array(42424242424242424242),
123             array(378282246310005),
124             array(371449635398431),
125         );
126     }
127 }