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\Uuid;
15 use Symfony\Component\Validator\Constraints\UuidValidator;
16 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
19 * @author Colin O'Dell <colinodell@gmail.com>
21 class UuidValidatorTest extends ConstraintValidatorTestCase
23 protected function createValidator()
25 return new UuidValidator();
28 public function testNullIsValid()
30 $this->validator->validate(null, new Uuid());
32 $this->assertNoViolation();
35 public function testEmptyStringIsValid()
37 $this->validator->validate('', new Uuid());
39 $this->assertNoViolation();
43 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
45 public function testExpectsUuidConstraintCompatibleType()
47 $constraint = $this->getMockForAbstractClass('Symfony\\Component\\Validator\\Constraint');
49 $this->validator->validate('216fff40-98d9-11e3-a5e2-0800200c9a66', $constraint);
53 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
55 public function testExpectsStringCompatibleType()
57 $this->validator->validate(new \stdClass(), new Uuid());
61 * @dataProvider getValidStrictUuids
63 public function testValidStrictUuids($uuid, $versions = null)
65 $constraint = new Uuid();
67 if (null !== $versions) {
68 $constraint->versions = $versions;
71 $this->validator->validate($uuid, $constraint);
73 $this->assertNoViolation();
76 public function getValidStrictUuids()
79 array('216fff40-98d9-11e3-a5e2-0800200c9a66'), // Version 1 UUID in lowercase
80 array('216fff40-98d9-11e3-a5e2-0800200c9a66', array(Uuid::V1_MAC)),
81 array('216FFF40-98D9-11E3-A5E2-0800200C9A66'), // Version 1 UUID in UPPERCASE
82 array('456daefb-5aa6-41b5-8dbc-068b05a8b201'), // Version 4 UUID in lowercase
83 array('456daEFb-5AA6-41B5-8DBC-068B05A8B201'), // Version 4 UUID in mixed case
84 array('456daEFb-5AA6-41B5-8DBC-068B05A8B201', array(Uuid::V4_RANDOM)),
89 * @dataProvider getInvalidStrictUuids
91 public function testInvalidStrictUuids($uuid, $code, $versions = null)
93 $constraint = new Uuid(array(
94 'message' => 'testMessage',
97 if (null !== $versions) {
98 $constraint->versions = $versions;
101 $this->validator->validate($uuid, $constraint);
103 $this->buildViolation('testMessage')
104 ->setParameter('{{ value }}', '"'.$uuid.'"')
109 public function getInvalidStrictUuids()
112 array('216fff40-98d9-11e3-a5e2_0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
113 array('216gff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
114 array('216Gff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
115 array('216fff40-98d9-11e3-a5e-20800200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
116 array('216f-ff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
117 array('216fff40-98d9-11e3-a5e2-0800-200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
118 array('216fff40-98d9-11e3-a5e2-0800200c-9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
119 array('216fff40-98d9-11e3-a5e20800200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
120 array('216fff4098d911e3a5e20800200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
121 array('216fff40-98d9-11e3-a5e2-0800200c9a6', Uuid::TOO_SHORT_ERROR),
122 array('216fff40-98d9-11e3-a5e2-0800200c9a666', Uuid::TOO_LONG_ERROR),
123 array('216fff40-98d9-01e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
124 array('216fff40-98d9-61e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
125 array('216fff40-98d9-71e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
126 array('216fff40-98d9-81e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
127 array('216fff40-98d9-91e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
128 array('216fff40-98d9-a1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
129 array('216fff40-98d9-b1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
130 array('216fff40-98d9-c1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
131 array('216fff40-98d9-d1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
132 array('216fff40-98d9-e1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
133 array('216fff40-98d9-f1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
134 array('216fff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR, array(Uuid::V2_DCE, Uuid::V3_MD5, Uuid::V4_RANDOM, Uuid::V5_SHA1)),
135 array('216fff40-98d9-21e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR, array(Uuid::V1_MAC, Uuid::V3_MD5, Uuid::V4_RANDOM, Uuid::V5_SHA1)),
136 array('216fff40-98d9-11e3-05e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
137 array('216fff40-98d9-11e3-15e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
138 array('216fff40-98d9-11e3-25e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
139 array('216fff40-98d9-11e3-35e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
140 array('216fff40-98d9-11e3-45e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
141 array('216fff40-98d9-11e3-55e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
142 array('216fff40-98d9-11e3-65e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
143 array('216fff40-98d9-11e3-75e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
144 array('216fff40-98d9-11e3-c5e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
145 array('216fff40-98d9-11e3-d5e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
146 array('216fff40-98d9-11e3-e5e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
147 array('216fff40-98d9-11e3-f5e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
149 // Non-standard UUID allowed by some other systems
150 array('{216fff40-98d9-11e3-a5e2-0800200c9a66}', Uuid::INVALID_CHARACTERS_ERROR),
151 array('[216fff40-98d9-11e3-a5e2-0800200c9a66]', Uuid::INVALID_CHARACTERS_ERROR),
156 * @dataProvider getValidNonStrictUuids
158 public function testValidNonStrictUuids($uuid)
160 $constraint = new Uuid(array(
164 $this->validator->validate($uuid, $constraint);
166 $this->assertNoViolation();
169 public function getValidNonStrictUuids()
172 array('216fff40-98d9-11e3-a5e2-0800200c9a66'), // Version 1 UUID in lowercase
173 array('216FFF40-98D9-11E3-A5E2-0800200C9A66'), // Version 1 UUID in UPPERCASE
174 array('456daefb-5aa6-41b5-8dbc-068b05a8b201'), // Version 4 UUID in lowercase
175 array('456DAEFb-5AA6-41B5-8DBC-068b05a8B201'), // Version 4 UUID in mixed case
177 // Non-standard UUIDs allowed by some other systems
178 array('216f-ff40-98d9-11e3-a5e2-0800-200c-9a66'), // Non-standard dash positions (every 4 chars)
179 array('216fff40-98d911e3-a5e20800-200c9a66'), // Non-standard dash positions (every 8 chars)
180 array('216fff4098d911e3a5e20800200c9a66'), // No dashes at all
181 array('{216fff40-98d9-11e3-a5e2-0800200c9a66}'), // Wrapped with curly braces
182 array('[216fff40-98d9-11e3-a5e2-0800200c9a66]'), // Wrapped with squared braces
187 * @dataProvider getInvalidNonStrictUuids
189 public function testInvalidNonStrictUuids($uuid, $code)
191 $constraint = new Uuid(array(
193 'message' => 'myMessage',
196 $this->validator->validate($uuid, $constraint);
198 $this->buildViolation('myMessage')
199 ->setParameter('{{ value }}', '"'.$uuid.'"')
204 public function getInvalidNonStrictUuids()
207 array('216fff40-98d9-11e3-a5e2_0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
208 array('216gff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
209 array('216Gff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
210 array('216fff40-98d9-11e3-a5e2_0800200c9a6', Uuid::INVALID_CHARACTERS_ERROR),
211 array('216fff40-98d9-11e3-a5e-20800200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
212 array('216fff40-98d9-11e3-a5e2-0800200c9a6', Uuid::TOO_SHORT_ERROR),
213 array('216fff40-98d9-11e3-a5e2-0800200c9a666', Uuid::TOO_LONG_ERROR),