Version 1
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / IssnValidatorTest.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\Issn;
15 use Symfony\Component\Validator\Constraints\IssnValidator;
16 use Symfony\Component\Validator\Validation;
17
18 /**
19  * @see https://en.wikipedia.org/wiki/Issn
20  */
21 class IssnValidatorTest extends AbstractConstraintValidatorTest
22 {
23     protected function getApiVersion()
24     {
25         return Validation::API_VERSION_2_5;
26     }
27
28     protected function createValidator()
29     {
30         return new IssnValidator();
31     }
32
33     public function getValidLowerCasedIssn()
34     {
35         return array(
36             array('2162-321x'),
37             array('2160-200x'),
38             array('1537-453x'),
39             array('1937-710x'),
40             array('0002-922x'),
41             array('1553-345x'),
42             array('1553-619x'),
43         );
44     }
45
46     public function getValidNonHyphenatedIssn()
47     {
48         return array(
49             array('2162321X'),
50             array('01896016'),
51             array('15744647'),
52             array('14350645'),
53             array('07174055'),
54             array('20905076'),
55             array('14401592'),
56         );
57     }
58
59     public function getFullValidIssn()
60     {
61         return array(
62             array('1550-7416'),
63             array('1539-8560'),
64             array('2156-5376'),
65             array('1119-023X'),
66             array('1684-5315'),
67             array('1996-0786'),
68             array('1684-5374'),
69             array('1996-0794'),
70         );
71     }
72
73     public function getValidIssn()
74     {
75         return array_merge(
76             $this->getValidLowerCasedIssn(),
77             $this->getValidNonHyphenatedIssn(),
78             $this->getFullValidIssn()
79         );
80     }
81
82     public function getInvalidIssn()
83     {
84         return array(
85             array(0, Issn::TOO_SHORT_ERROR),
86             array('1539', Issn::TOO_SHORT_ERROR),
87             array('2156-537A', Issn::INVALID_CHARACTERS_ERROR),
88             array('1119-0231', Issn::CHECKSUM_FAILED_ERROR),
89             array('1684-5312', Issn::CHECKSUM_FAILED_ERROR),
90             array('1996-0783', Issn::CHECKSUM_FAILED_ERROR),
91             array('1684-537X', Issn::CHECKSUM_FAILED_ERROR),
92             array('1996-0795', Issn::CHECKSUM_FAILED_ERROR),
93         );
94     }
95
96     public function testNullIsValid()
97     {
98         $constraint = new Issn();
99
100         $this->validator->validate(null, $constraint);
101
102         $this->assertNoViolation();
103     }
104
105     public function testEmptyStringIsValid()
106     {
107         $constraint = new Issn();
108
109         $this->validator->validate('', $constraint);
110
111         $this->assertNoViolation();
112     }
113
114     /**
115      * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
116      */
117     public function testExpectsStringCompatibleType()
118     {
119         $constraint = new Issn();
120         $this->validator->validate(new \stdClass(), $constraint);
121     }
122
123     /**
124      * @dataProvider getValidLowerCasedIssn
125      */
126     public function testCaseSensitiveIssns($issn)
127     {
128         $constraint = new Issn(array(
129             'caseSensitive' => true,
130             'message' => 'myMessage',
131         ));
132
133         $this->validator->validate($issn, $constraint);
134
135         $this->buildViolation('myMessage')
136             ->setParameter('{{ value }}', '"'.$issn.'"')
137             ->setCode(Issn::INVALID_CASE_ERROR)
138             ->assertRaised();
139     }
140
141     /**
142      * @dataProvider getValidNonHyphenatedIssn
143      */
144     public function testRequireHyphenIssns($issn)
145     {
146         $constraint = new Issn(array(
147             'requireHyphen' => true,
148             'message' => 'myMessage',
149         ));
150
151         $this->validator->validate($issn, $constraint);
152
153         $this->buildViolation('myMessage')
154             ->setParameter('{{ value }}', '"'.$issn.'"')
155             ->setCode(Issn::MISSING_HYPHEN_ERROR)
156             ->assertRaised();
157     }
158
159     /**
160      * @dataProvider getValidIssn
161      */
162     public function testValidIssn($issn)
163     {
164         $constraint = new Issn();
165
166         $this->validator->validate($issn, $constraint);
167
168         $this->assertNoViolation();
169     }
170
171     /**
172      * @dataProvider getInvalidIssn
173      */
174     public function testInvalidIssn($issn, $code)
175     {
176         $constraint = new Issn(array(
177             'message' => 'myMessage',
178         ));
179
180         $this->validator->validate($issn, $constraint);
181
182         $this->buildViolation('myMessage')
183             ->setParameter('{{ value }}', '"'.$issn.'"')
184             ->setCode($code)
185             ->assertRaised();
186     }
187 }