Security update for Core, with self-updated composer
[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\Test\ConstraintValidatorTestCase;
17
18 class LuhnValidatorTest extends ConstraintValidatorTestCase
19 {
20     protected function createValidator()
21     {
22         return new LuhnValidator();
23     }
24
25     public function testNullIsValid()
26     {
27         $this->validator->validate(null, new Luhn());
28
29         $this->assertNoViolation();
30     }
31
32     public function testEmptyStringIsValid()
33     {
34         $this->validator->validate('', new Luhn());
35
36         $this->assertNoViolation();
37     }
38
39     /**
40      * @dataProvider getValidNumbers
41      */
42     public function testValidNumbers($number)
43     {
44         $this->validator->validate($number, new Luhn());
45
46         $this->assertNoViolation();
47     }
48
49     public function getValidNumbers()
50     {
51         return array(
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'),
70         );
71     }
72
73     /**
74      * @dataProvider getInvalidNumbers
75      */
76     public function testInvalidNumbers($number, $code)
77     {
78         $constraint = new Luhn(array(
79             'message' => 'myMessage',
80         ));
81
82         $this->validator->validate($number, $constraint);
83
84         $this->buildViolation('myMessage')
85             ->setParameter('{{ value }}', '"'.$number.'"')
86             ->setCode($code)
87             ->assertRaised();
88     }
89
90     public function getInvalidNumbers()
91     {
92         return array(
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),
98         );
99     }
100
101     /**
102      * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
103      * @dataProvider getInvalidTypes
104      */
105     public function testInvalidTypes($number)
106     {
107         $constraint = new Luhn();
108
109         $this->validator->validate($number, $constraint);
110     }
111
112     public function getInvalidTypes()
113     {
114         return array(
115             array(0),
116             array(123),
117             array(42424242424242424242),
118             array(378282246310005),
119             array(371449635398431),
120         );
121     }
122 }