4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Tests\Constraints;
14 use Symfony\Component\Validator\Constraints\Isbn;
15 use Symfony\Component\Validator\Constraints\IsbnValidator;
16 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
19 * @see https://en.wikipedia.org/wiki/Isbn
21 class IsbnValidatorTest extends ConstraintValidatorTestCase
23 protected function createValidator()
25 return new IsbnValidator();
28 public function getValidIsbn10()
41 array('0-45122-5244'),
42 array('0-4712-92311'),
43 array('0-9752298-0-X'),
47 public function getInvalidIsbn10()
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),
67 public function getValidIsbn13()
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'),
85 public function getInvalidIsbn13()
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),
105 public function getValidIsbn()
108 $this->getValidIsbn10(),
109 $this->getValidIsbn13()
113 public function getInvalidIsbn()
116 $this->getInvalidIsbn10(),
117 $this->getInvalidIsbn13()
121 public function testNullIsValid()
123 $constraint = new Isbn(true);
125 $this->validator->validate(null, $constraint);
127 $this->assertNoViolation();
130 public function testEmptyStringIsValid()
132 $constraint = new Isbn(true);
134 $this->validator->validate('', $constraint);
136 $this->assertNoViolation();
140 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
142 public function testExpectsStringCompatibleType()
144 $constraint = new Isbn(true);
146 $this->validator->validate(new \stdClass(), $constraint);
150 * @dataProvider getValidIsbn10
152 public function testValidIsbn10($isbn)
154 $constraint = new Isbn(array(
158 $this->validator->validate($isbn, $constraint);
160 $this->assertNoViolation();
164 * @dataProvider getInvalidIsbn10
166 public function testInvalidIsbn10($isbn, $code)
168 $constraint = new Isbn(array(
170 'isbn10Message' => 'myMessage',
173 $this->validator->validate($isbn, $constraint);
175 $this->buildViolation('myMessage')
176 ->setParameter('{{ value }}', '"'.$isbn.'"')
182 * @dataProvider getValidIsbn13
184 public function testValidIsbn13($isbn)
186 $constraint = new Isbn(array('type' => 'isbn13'));
188 $this->validator->validate($isbn, $constraint);
190 $this->assertNoViolation();
194 * @dataProvider getInvalidIsbn13
196 public function testInvalidIsbn13($isbn, $code)
198 $constraint = new Isbn(array(
200 'isbn13Message' => 'myMessage',
203 $this->validator->validate($isbn, $constraint);
205 $this->buildViolation('myMessage')
206 ->setParameter('{{ value }}', '"'.$isbn.'"')
212 * @dataProvider getValidIsbn
214 public function testValidIsbnAny($isbn)
216 $constraint = new Isbn();
218 $this->validator->validate($isbn, $constraint);
220 $this->assertNoViolation();
224 * @dataProvider getInvalidIsbn10
226 public function testInvalidIsbnAnyIsbn10($isbn, $code)
228 $constraint = new Isbn(array(
229 'bothIsbnMessage' => 'myMessage',
232 $this->validator->validate($isbn, $constraint);
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;
239 $this->buildViolation('myMessage')
240 ->setParameter('{{ value }}', '"'.$isbn.'"')
246 * @dataProvider getInvalidIsbn13
248 public function testInvalidIsbnAnyIsbn13($isbn, $code)
250 $constraint = new Isbn(array(
251 'bothIsbnMessage' => 'myMessage',
254 $this->validator->validate($isbn, $constraint);
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;
261 $this->buildViolation('myMessage')
262 ->setParameter('{{ value }}', '"'.$isbn.'"')