UuidValidatorTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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\Uuid;
  12. use Symfony\Component\Validator\Constraints\UuidValidator;
  13. use Symfony\Component\Validator\Validation;
  14. /**
  15. * @author Colin O'Dell <colinodell@gmail.com>
  16. */
  17. class UuidValidatorTest extends AbstractConstraintValidatorTest
  18. {
  19. protected function getApiVersion()
  20. {
  21. return Validation::API_VERSION_2_5;
  22. }
  23. protected function createValidator()
  24. {
  25. return new UuidValidator();
  26. }
  27. public function testNullIsValid()
  28. {
  29. $this->validator->validate(null, new Uuid());
  30. $this->assertNoViolation();
  31. }
  32. public function testEmptyStringIsValid()
  33. {
  34. $this->validator->validate('', new Uuid());
  35. $this->assertNoViolation();
  36. }
  37. /**
  38. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  39. */
  40. public function testExpectsUuidConstraintCompatibleType()
  41. {
  42. $constraint = $this->getMockForAbstractClass('Symfony\\Component\\Validator\\Constraint');
  43. $this->validator->validate('216fff40-98d9-11e3-a5e2-0800200c9a66', $constraint);
  44. }
  45. /**
  46. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  47. */
  48. public function testExpectsStringCompatibleType()
  49. {
  50. $this->validator->validate(new \stdClass(), new Uuid());
  51. }
  52. /**
  53. * @dataProvider getValidStrictUuids
  54. */
  55. public function testValidStrictUuids($uuid, $versions = null)
  56. {
  57. $constraint = new Uuid();
  58. if (null !== $versions) {
  59. $constraint->versions = $versions;
  60. }
  61. $this->validator->validate($uuid, $constraint);
  62. $this->assertNoViolation();
  63. }
  64. public function getValidStrictUuids()
  65. {
  66. return array(
  67. array('216fff40-98d9-11e3-a5e2-0800200c9a66'), // Version 1 UUID in lowercase
  68. array('216fff40-98d9-11e3-a5e2-0800200c9a66', array(Uuid::V1_MAC)),
  69. array('216FFF40-98D9-11E3-A5E2-0800200C9A66'), // Version 1 UUID in UPPERCASE
  70. array('456daefb-5aa6-41b5-8dbc-068b05a8b201'), // Version 4 UUID in lowercase
  71. array('456daEFb-5AA6-41B5-8DBC-068B05A8B201'), // Version 4 UUID in mixed case
  72. array('456daEFb-5AA6-41B5-8DBC-068B05A8B201', array(Uuid::V4_RANDOM)),
  73. );
  74. }
  75. /**
  76. * @dataProvider getInvalidStrictUuids
  77. */
  78. public function testInvalidStrictUuids($uuid, $code, $versions = null)
  79. {
  80. $constraint = new Uuid(array(
  81. 'message' => 'testMessage',
  82. ));
  83. if (null !== $versions) {
  84. $constraint->versions = $versions;
  85. }
  86. $this->validator->validate($uuid, $constraint);
  87. $this->buildViolation('testMessage')
  88. ->setParameter('{{ value }}', '"'.$uuid.'"')
  89. ->setCode($code)
  90. ->assertRaised();
  91. }
  92. public function getInvalidStrictUuids()
  93. {
  94. return array(
  95. array('216fff40-98d9-11e3-a5e2_0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
  96. array('216gff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
  97. array('216Gff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
  98. array('216fff40-98d9-11e3-a5e-20800200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
  99. array('216f-ff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
  100. array('216fff40-98d9-11e3-a5e2-0800-200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
  101. array('216fff40-98d9-11e3-a5e2-0800200c-9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
  102. array('216fff40-98d9-11e3-a5e20800200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
  103. array('216fff4098d911e3a5e20800200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
  104. array('216fff40-98d9-11e3-a5e2-0800200c9a6', Uuid::TOO_SHORT_ERROR),
  105. array('216fff40-98d9-11e3-a5e2-0800200c9a666', Uuid::TOO_LONG_ERROR),
  106. array('216fff40-98d9-01e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
  107. array('216fff40-98d9-61e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
  108. array('216fff40-98d9-71e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
  109. array('216fff40-98d9-81e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
  110. array('216fff40-98d9-91e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
  111. array('216fff40-98d9-a1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
  112. array('216fff40-98d9-b1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
  113. array('216fff40-98d9-c1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
  114. array('216fff40-98d9-d1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
  115. array('216fff40-98d9-e1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
  116. array('216fff40-98d9-f1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
  117. array('216fff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR, array(Uuid::V2_DCE, Uuid::V3_MD5, Uuid::V4_RANDOM, Uuid::V5_SHA1)),
  118. array('216fff40-98d9-21e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR, array(Uuid::V1_MAC, Uuid::V3_MD5, Uuid::V4_RANDOM, Uuid::V5_SHA1)),
  119. array('216fff40-98d9-11e3-05e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
  120. array('216fff40-98d9-11e3-15e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
  121. array('216fff40-98d9-11e3-25e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
  122. array('216fff40-98d9-11e3-35e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
  123. array('216fff40-98d9-11e3-45e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
  124. array('216fff40-98d9-11e3-55e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
  125. array('216fff40-98d9-11e3-65e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
  126. array('216fff40-98d9-11e3-75e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
  127. array('216fff40-98d9-11e3-c5e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
  128. array('216fff40-98d9-11e3-d5e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
  129. array('216fff40-98d9-11e3-e5e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
  130. array('216fff40-98d9-11e3-f5e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
  131. // Non-standard UUID allowed by some other systems
  132. array('{216fff40-98d9-11e3-a5e2-0800200c9a66}', Uuid::INVALID_CHARACTERS_ERROR),
  133. array('[216fff40-98d9-11e3-a5e2-0800200c9a66]', Uuid::INVALID_CHARACTERS_ERROR),
  134. );
  135. }
  136. /**
  137. * @dataProvider getValidNonStrictUuids
  138. */
  139. public function testValidNonStrictUuids($uuid)
  140. {
  141. $constraint = new Uuid(array(
  142. 'strict' => false,
  143. ));
  144. $this->validator->validate($uuid, $constraint);
  145. $this->assertNoViolation();
  146. }
  147. public function getValidNonStrictUuids()
  148. {
  149. return array(
  150. array('216fff40-98d9-11e3-a5e2-0800200c9a66'), // Version 1 UUID in lowercase
  151. array('216FFF40-98D9-11E3-A5E2-0800200C9A66'), // Version 1 UUID in UPPERCASE
  152. array('456daefb-5aa6-41b5-8dbc-068b05a8b201'), // Version 4 UUID in lowercase
  153. array('456DAEFb-5AA6-41B5-8DBC-068b05a8B201'), // Version 4 UUID in mixed case
  154. // Non-standard UUIDs allowed by some other systems
  155. array('216f-ff40-98d9-11e3-a5e2-0800-200c-9a66'), // Non-standard dash positions (every 4 chars)
  156. array('216fff40-98d911e3-a5e20800-200c9a66'), // Non-standard dash positions (every 8 chars)
  157. array('216fff4098d911e3a5e20800200c9a66'), // No dashes at all
  158. array('{216fff40-98d9-11e3-a5e2-0800200c9a66}'), // Wrapped with curly braces
  159. array('[216fff40-98d9-11e3-a5e2-0800200c9a66]'), // Wrapped with squared braces
  160. );
  161. }
  162. /**
  163. * @dataProvider getInvalidNonStrictUuids
  164. */
  165. public function testInvalidNonStrictUuids($uuid, $code)
  166. {
  167. $constraint = new Uuid(array(
  168. 'strict' => false,
  169. 'message' => 'myMessage',
  170. ));
  171. $this->validator->validate($uuid, $constraint);
  172. $this->buildViolation('myMessage')
  173. ->setParameter('{{ value }}', '"'.$uuid.'"')
  174. ->setCode($code)
  175. ->assertRaised();
  176. }
  177. public function getInvalidNonStrictUuids()
  178. {
  179. return array(
  180. array('216fff40-98d9-11e3-a5e2_0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
  181. array('216gff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
  182. array('216Gff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
  183. array('216fff40-98d9-11e3-a5e2_0800200c9a6', Uuid::INVALID_CHARACTERS_ERROR),
  184. array('216fff40-98d9-11e3-a5e-20800200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
  185. array('216fff40-98d9-11e3-a5e2-0800200c9a6', Uuid::TOO_SHORT_ERROR),
  186. array('216fff40-98d9-11e3-a5e2-0800200c9a666', Uuid::TOO_LONG_ERROR),
  187. );
  188. }
  189. }