IsbnValidatorTest.php 7.4 KB

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