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\Issn;
15 use Symfony\Component\Validator\Constraints\IssnValidator;
16 use Symfony\Component\Validator\Validation;
19 * @see https://en.wikipedia.org/wiki/Issn
21 class IssnValidatorTest extends AbstractConstraintValidatorTest
23 protected function getApiVersion()
25 return Validation::API_VERSION_2_5;
28 protected function createValidator()
30 return new IssnValidator();
33 public function getValidLowerCasedIssn()
46 public function getValidNonHyphenatedIssn()
59 public function getFullValidIssn()
73 public function getValidIssn()
76 $this->getValidLowerCasedIssn(),
77 $this->getValidNonHyphenatedIssn(),
78 $this->getFullValidIssn()
82 public function getInvalidIssn()
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),
96 public function testNullIsValid()
98 $constraint = new Issn();
100 $this->validator->validate(null, $constraint);
102 $this->assertNoViolation();
105 public function testEmptyStringIsValid()
107 $constraint = new Issn();
109 $this->validator->validate('', $constraint);
111 $this->assertNoViolation();
115 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
117 public function testExpectsStringCompatibleType()
119 $constraint = new Issn();
120 $this->validator->validate(new \stdClass(), $constraint);
124 * @dataProvider getValidLowerCasedIssn
126 public function testCaseSensitiveIssns($issn)
128 $constraint = new Issn(array(
129 'caseSensitive' => true,
130 'message' => 'myMessage',
133 $this->validator->validate($issn, $constraint);
135 $this->buildViolation('myMessage')
136 ->setParameter('{{ value }}', '"'.$issn.'"')
137 ->setCode(Issn::INVALID_CASE_ERROR)
142 * @dataProvider getValidNonHyphenatedIssn
144 public function testRequireHyphenIssns($issn)
146 $constraint = new Issn(array(
147 'requireHyphen' => true,
148 'message' => 'myMessage',
151 $this->validator->validate($issn, $constraint);
153 $this->buildViolation('myMessage')
154 ->setParameter('{{ value }}', '"'.$issn.'"')
155 ->setCode(Issn::MISSING_HYPHEN_ERROR)
160 * @dataProvider getValidIssn
162 public function testValidIssn($issn)
164 $constraint = new Issn();
166 $this->validator->validate($issn, $constraint);
168 $this->assertNoViolation();
172 * @dataProvider getInvalidIssn
174 public function testInvalidIssn($issn, $code)
176 $constraint = new Issn(array(
177 'message' => 'myMessage',
180 $this->validator->validate($issn, $constraint);
182 $this->buildViolation('myMessage')
183 ->setParameter('{{ value }}', '"'.$issn.'"')