IssnValidatorTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Tests\Constraints;
  11. use Symfony\Component\Validator\Constraints\Issn;
  12. use Symfony\Component\Validator\Constraints\IssnValidator;
  13. use Symfony\Component\Validator\Validation;
  14. /**
  15. * @see https://en.wikipedia.org/wiki/Issn
  16. */
  17. class IssnValidatorTest extends AbstractConstraintValidatorTest
  18. {
  19. protected function getApiVersion()
  20. {
  21. return Validation::API_VERSION_2_5;
  22. }
  23. protected function createValidator()
  24. {
  25. return new IssnValidator();
  26. }
  27. public function getValidLowerCasedIssn()
  28. {
  29. return array(
  30. array('2162-321x'),
  31. array('2160-200x'),
  32. array('1537-453x'),
  33. array('1937-710x'),
  34. array('0002-922x'),
  35. array('1553-345x'),
  36. array('1553-619x'),
  37. );
  38. }
  39. public function getValidNonHyphenatedIssn()
  40. {
  41. return array(
  42. array('2162321X'),
  43. array('01896016'),
  44. array('15744647'),
  45. array('14350645'),
  46. array('07174055'),
  47. array('20905076'),
  48. array('14401592'),
  49. );
  50. }
  51. public function getFullValidIssn()
  52. {
  53. return array(
  54. array('1550-7416'),
  55. array('1539-8560'),
  56. array('2156-5376'),
  57. array('1119-023X'),
  58. array('1684-5315'),
  59. array('1996-0786'),
  60. array('1684-5374'),
  61. array('1996-0794'),
  62. );
  63. }
  64. public function getValidIssn()
  65. {
  66. return array_merge(
  67. $this->getValidLowerCasedIssn(),
  68. $this->getValidNonHyphenatedIssn(),
  69. $this->getFullValidIssn()
  70. );
  71. }
  72. public function getInvalidIssn()
  73. {
  74. return array(
  75. array(0, Issn::TOO_SHORT_ERROR),
  76. array('1539', Issn::TOO_SHORT_ERROR),
  77. array('2156-537A', Issn::INVALID_CHARACTERS_ERROR),
  78. array('1119-0231', Issn::CHECKSUM_FAILED_ERROR),
  79. array('1684-5312', Issn::CHECKSUM_FAILED_ERROR),
  80. array('1996-0783', Issn::CHECKSUM_FAILED_ERROR),
  81. array('1684-537X', Issn::CHECKSUM_FAILED_ERROR),
  82. array('1996-0795', Issn::CHECKSUM_FAILED_ERROR),
  83. );
  84. }
  85. public function testNullIsValid()
  86. {
  87. $constraint = new Issn();
  88. $this->validator->validate(null, $constraint);
  89. $this->assertNoViolation();
  90. }
  91. public function testEmptyStringIsValid()
  92. {
  93. $constraint = new Issn();
  94. $this->validator->validate('', $constraint);
  95. $this->assertNoViolation();
  96. }
  97. /**
  98. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  99. */
  100. public function testExpectsStringCompatibleType()
  101. {
  102. $constraint = new Issn();
  103. $this->validator->validate(new \stdClass(), $constraint);
  104. }
  105. /**
  106. * @dataProvider getValidLowerCasedIssn
  107. */
  108. public function testCaseSensitiveIssns($issn)
  109. {
  110. $constraint = new Issn(array(
  111. 'caseSensitive' => true,
  112. 'message' => 'myMessage',
  113. ));
  114. $this->validator->validate($issn, $constraint);
  115. $this->buildViolation('myMessage')
  116. ->setParameter('{{ value }}', '"'.$issn.'"')
  117. ->setCode(Issn::INVALID_CASE_ERROR)
  118. ->assertRaised();
  119. }
  120. /**
  121. * @dataProvider getValidNonHyphenatedIssn
  122. */
  123. public function testRequireHyphenIssns($issn)
  124. {
  125. $constraint = new Issn(array(
  126. 'requireHyphen' => true,
  127. 'message' => 'myMessage',
  128. ));
  129. $this->validator->validate($issn, $constraint);
  130. $this->buildViolation('myMessage')
  131. ->setParameter('{{ value }}', '"'.$issn.'"')
  132. ->setCode(Issn::MISSING_HYPHEN_ERROR)
  133. ->assertRaised();
  134. }
  135. /**
  136. * @dataProvider getValidIssn
  137. */
  138. public function testValidIssn($issn)
  139. {
  140. $constraint = new Issn();
  141. $this->validator->validate($issn, $constraint);
  142. $this->assertNoViolation();
  143. }
  144. /**
  145. * @dataProvider getInvalidIssn
  146. */
  147. public function testInvalidIssn($issn, $code)
  148. {
  149. $constraint = new Issn(array(
  150. 'message' => 'myMessage',
  151. ));
  152. $this->validator->validate($issn, $constraint);
  153. $this->buildViolation('myMessage')
  154. ->setParameter('{{ value }}', '"'.$issn.'"')
  155. ->setCode($code)
  156. ->assertRaised();
  157. }
  158. }