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
12namespace Symfony\Component\Validator\Tests\Constraints;
13
14use Symfony\Component\Validator\Constraints\Luhn;
15use Symfony\Component\Validator\Constraints\LuhnValidator;
16use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
17
18class 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 [
52            ['42424242424242424242'],
53            ['378282246310005'],
54            ['371449635398431'],
55            ['378734493671000'],
56            ['5610591081018250'],
57            ['30569309025904'],
58            ['38520000023237'],
59            ['6011111111111117'],
60            ['6011000990139424'],
61            ['3530111333300000'],
62            ['3566002020360505'],
63            ['5555555555554444'],
64            ['5105105105105100'],
65            ['4111111111111111'],
66            ['4012888888881881'],
67            ['4222222222222'],
68            ['5019717010103742'],
69            ['6331101999990016'],
70        ];
71    }
72
73    /**
74     * @dataProvider getInvalidNumbers
75     */
76    public function testInvalidNumbers($number, $code)
77    {
78        $constraint = new Luhn([
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 [
93            ['1234567812345678', Luhn::CHECKSUM_FAILED_ERROR],
94            ['4222222222222222', Luhn::CHECKSUM_FAILED_ERROR],
95            ['0000000000000000', Luhn::CHECKSUM_FAILED_ERROR],
96            ['000000!000000000', Luhn::INVALID_CHARACTERS_ERROR],
97            ['42-22222222222222', Luhn::INVALID_CHARACTERS_ERROR],
98        ];
99    }
100
101    /**
102     * @dataProvider getInvalidTypes
103     */
104    public function testInvalidTypes($number)
105    {
106        $this->expectException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
107        $constraint = new Luhn();
108
109        $this->validator->validate($number, $constraint);
110    }
111
112    public function getInvalidTypes()
113    {
114        return [
115            [0],
116            [123],
117            [42424242424242424242],
118            [378282246310005],
119            [371449635398431],
120        ];
121    }
122}
123