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\Validation;
19 * @see https://en.wikipedia.org/wiki/Isbn
21 class IsbnValidatorTest extends AbstractConstraintValidatorTest
23 protected function getApiVersion()
25 return Validation::API_VERSION_2_5;
28 protected function createValidator()
30 return new IsbnValidator();
33 public function getValidIsbn10()
46 array('0-45122-5244'),
47 array('0-4712-92311'),
48 array('0-9752298-0-X'),
52 public function getInvalidIsbn10()
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),
72 public function getValidIsbn13()
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'),
90 public function getInvalidIsbn13()
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),
110 public function getValidIsbn()
113 $this->getValidIsbn10(),
114 $this->getValidIsbn13()
118 public function getInvalidIsbn()
121 $this->getInvalidIsbn10(),
122 $this->getInvalidIsbn13()
126 public function testNullIsValid()
128 $constraint = new Isbn(true);
130 $this->validator->validate(null, $constraint);
132 $this->assertNoViolation();
135 public function testEmptyStringIsValid()
137 $constraint = new Isbn(true);
139 $this->validator->validate('', $constraint);
141 $this->assertNoViolation();
145 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
147 public function testExpectsStringCompatibleType()
149 $constraint = new Isbn(true);
151 $this->validator->validate(new \stdClass(), $constraint);
155 * @dataProvider getValidIsbn10
157 public function testValidIsbn10($isbn)
159 $constraint = new Isbn(array(
163 $this->validator->validate($isbn, $constraint);
165 $this->assertNoViolation();
169 * @dataProvider getInvalidIsbn10
171 public function testInvalidIsbn10($isbn, $code)
173 $constraint = new Isbn(array(
175 'isbn10Message' => 'myMessage',
178 $this->validator->validate($isbn, $constraint);
180 $this->buildViolation('myMessage')
181 ->setParameter('{{ value }}', '"'.$isbn.'"')
187 * @dataProvider getValidIsbn13
189 public function testValidIsbn13($isbn)
191 $constraint = new Isbn(array('type' => 'isbn13'));
193 $this->validator->validate($isbn, $constraint);
195 $this->assertNoViolation();
199 * @dataProvider getInvalidIsbn13
201 public function testInvalidIsbn13($isbn, $code)
203 $constraint = new Isbn(array(
205 'isbn13Message' => 'myMessage',
208 $this->validator->validate($isbn, $constraint);
210 $this->buildViolation('myMessage')
211 ->setParameter('{{ value }}', '"'.$isbn.'"')
217 * @dataProvider getValidIsbn
219 public function testValidIsbnAny($isbn)
221 $constraint = new Isbn();
223 $this->validator->validate($isbn, $constraint);
225 $this->assertNoViolation();
229 * @dataProvider getInvalidIsbn10
231 public function testInvalidIsbnAnyIsbn10($isbn, $code)
233 $constraint = new Isbn(array(
234 'bothIsbnMessage' => 'myMessage',
237 $this->validator->validate($isbn, $constraint);
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;
244 $this->buildViolation('myMessage')
245 ->setParameter('{{ value }}', '"'.$isbn.'"')
251 * @dataProvider getInvalidIsbn13
253 public function testInvalidIsbnAnyIsbn13($isbn, $code)
255 $constraint = new Isbn(array(
256 'bothIsbnMessage' => 'myMessage',
259 $this->validator->validate($isbn, $constraint);
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;
266 $this->buildViolation('myMessage')
267 ->setParameter('{{ value }}', '"'.$isbn.'"')