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