Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / IsbnValidatorTest.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\Isbn;
15 use Symfony\Component\Validator\Constraints\IsbnValidator;
16 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
17
18 /**
19  * @see https://en.wikipedia.org/wiki/Isbn
20  */
21 class IsbnValidatorTest extends ConstraintValidatorTestCase
22 {
23     protected function createValidator()
24     {
25         return new IsbnValidator();
26     }
27
28     public function getValidIsbn10()
29     {
30         return array(
31             array('2723442284'),
32             array('2723442276'),
33             array('2723455041'),
34             array('2070546810'),
35             array('2711858839'),
36             array('2756406767'),
37             array('2870971648'),
38             array('226623854X'),
39             array('2851806424'),
40             array('0321812700'),
41             array('0-45122-5244'),
42             array('0-4712-92311'),
43             array('0-9752298-0-X'),
44         );
45     }
46
47     public function getInvalidIsbn10()
48     {
49         return array(
50             array('27234422841', Isbn::TOO_LONG_ERROR),
51             array('272344228', Isbn::TOO_SHORT_ERROR),
52             array('0-4712-9231', Isbn::TOO_SHORT_ERROR),
53             array('1234567890', Isbn::CHECKSUM_FAILED_ERROR),
54             array('0987656789', Isbn::CHECKSUM_FAILED_ERROR),
55             array('7-35622-5444', Isbn::CHECKSUM_FAILED_ERROR),
56             array('0-4X19-92611', Isbn::CHECKSUM_FAILED_ERROR),
57             array('0_45122_5244', Isbn::INVALID_CHARACTERS_ERROR),
58             array('2870#971#648', Isbn::INVALID_CHARACTERS_ERROR),
59             array('0-9752298-0-x', Isbn::INVALID_CHARACTERS_ERROR),
60             array('1A34567890', Isbn::INVALID_CHARACTERS_ERROR),
61             // chr(1) evaluates to 0
62             // 2070546810 is valid
63             array('2'.chr(1).'70546810', Isbn::INVALID_CHARACTERS_ERROR),
64         );
65     }
66
67     public function getValidIsbn13()
68     {
69         return array(
70             array('978-2723442282'),
71             array('978-2723442275'),
72             array('978-2723455046'),
73             array('978-2070546817'),
74             array('978-2711858835'),
75             array('978-2756406763'),
76             array('978-2870971642'),
77             array('978-2266238540'),
78             array('978-2851806420'),
79             array('978-0321812704'),
80             array('978-0451225245'),
81             array('978-0471292319'),
82         );
83     }
84
85     public function getInvalidIsbn13()
86     {
87         return array(
88             array('978-27234422821', Isbn::TOO_LONG_ERROR),
89             array('978-272344228', Isbn::TOO_SHORT_ERROR),
90             array('978-2723442-82', Isbn::TOO_SHORT_ERROR),
91             array('978-2723442281', Isbn::CHECKSUM_FAILED_ERROR),
92             array('978-0321513774', Isbn::CHECKSUM_FAILED_ERROR),
93             array('979-0431225385', Isbn::CHECKSUM_FAILED_ERROR),
94             array('980-0474292319', Isbn::CHECKSUM_FAILED_ERROR),
95             array('0-4X19-92619812', Isbn::INVALID_CHARACTERS_ERROR),
96             array('978_2723442282', Isbn::INVALID_CHARACTERS_ERROR),
97             array('978#2723442282', Isbn::INVALID_CHARACTERS_ERROR),
98             array('978-272C442282', Isbn::INVALID_CHARACTERS_ERROR),
99             // chr(1) evaluates to 0
100             // 978-2070546817 is valid
101             array('978-2'.chr(1).'70546817', Isbn::INVALID_CHARACTERS_ERROR),
102         );
103     }
104
105     public function getValidIsbn()
106     {
107         return array_merge(
108             $this->getValidIsbn10(),
109             $this->getValidIsbn13()
110         );
111     }
112
113     public function getInvalidIsbn()
114     {
115         return array_merge(
116             $this->getInvalidIsbn10(),
117             $this->getInvalidIsbn13()
118         );
119     }
120
121     public function testNullIsValid()
122     {
123         $constraint = new Isbn(true);
124
125         $this->validator->validate(null, $constraint);
126
127         $this->assertNoViolation();
128     }
129
130     public function testEmptyStringIsValid()
131     {
132         $constraint = new Isbn(true);
133
134         $this->validator->validate('', $constraint);
135
136         $this->assertNoViolation();
137     }
138
139     /**
140      * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
141      */
142     public function testExpectsStringCompatibleType()
143     {
144         $constraint = new Isbn(true);
145
146         $this->validator->validate(new \stdClass(), $constraint);
147     }
148
149     /**
150      * @dataProvider getValidIsbn10
151      */
152     public function testValidIsbn10($isbn)
153     {
154         $constraint = new Isbn(array(
155             'type' => 'isbn10',
156         ));
157
158         $this->validator->validate($isbn, $constraint);
159
160         $this->assertNoViolation();
161     }
162
163     /**
164      * @dataProvider getInvalidIsbn10
165      */
166     public function testInvalidIsbn10($isbn, $code)
167     {
168         $constraint = new Isbn(array(
169             'type' => 'isbn10',
170             'isbn10Message' => 'myMessage',
171         ));
172
173         $this->validator->validate($isbn, $constraint);
174
175         $this->buildViolation('myMessage')
176             ->setParameter('{{ value }}', '"'.$isbn.'"')
177             ->setCode($code)
178             ->assertRaised();
179     }
180
181     /**
182      * @dataProvider getValidIsbn13
183      */
184     public function testValidIsbn13($isbn)
185     {
186         $constraint = new Isbn(array('type' => 'isbn13'));
187
188         $this->validator->validate($isbn, $constraint);
189
190         $this->assertNoViolation();
191     }
192
193     /**
194      * @dataProvider getInvalidIsbn13
195      */
196     public function testInvalidIsbn13($isbn, $code)
197     {
198         $constraint = new Isbn(array(
199             'type' => 'isbn13',
200             'isbn13Message' => 'myMessage',
201         ));
202
203         $this->validator->validate($isbn, $constraint);
204
205         $this->buildViolation('myMessage')
206             ->setParameter('{{ value }}', '"'.$isbn.'"')
207             ->setCode($code)
208             ->assertRaised();
209     }
210
211     /**
212      * @dataProvider getValidIsbn
213      */
214     public function testValidIsbnAny($isbn)
215     {
216         $constraint = new Isbn();
217
218         $this->validator->validate($isbn, $constraint);
219
220         $this->assertNoViolation();
221     }
222
223     /**
224      * @dataProvider getInvalidIsbn10
225      */
226     public function testInvalidIsbnAnyIsbn10($isbn, $code)
227     {
228         $constraint = new Isbn(array(
229             'bothIsbnMessage' => 'myMessage',
230         ));
231
232         $this->validator->validate($isbn, $constraint);
233
234         // Too long for an ISBN-10, but not long enough for an ISBN-13
235         if (Isbn::TOO_LONG_ERROR === $code) {
236             $code = Isbn::TYPE_NOT_RECOGNIZED_ERROR;
237         }
238
239         $this->buildViolation('myMessage')
240             ->setParameter('{{ value }}', '"'.$isbn.'"')
241             ->setCode($code)
242             ->assertRaised();
243     }
244
245     /**
246      * @dataProvider getInvalidIsbn13
247      */
248     public function testInvalidIsbnAnyIsbn13($isbn, $code)
249     {
250         $constraint = new Isbn(array(
251             'bothIsbnMessage' => 'myMessage',
252         ));
253
254         $this->validator->validate($isbn, $constraint);
255
256         // Too short for an ISBN-13, but not short enough for an ISBN-10
257         if (Isbn::TOO_SHORT_ERROR === $code) {
258             $code = Isbn::TYPE_NOT_RECOGNIZED_ERROR;
259         }
260
261         $this->buildViolation('myMessage')
262             ->setParameter('{{ value }}', '"'.$isbn.'"')
263             ->setCode($code)
264             ->assertRaised();
265     }
266 }