|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Validator\Tests\Constraints; |
| 13 | + |
| 14 | +use Symfony\Component\Validator\Constraints\Uuid; |
| 15 | +use Symfony\Component\Validator\Constraints\UuidValidator; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Colin O'Dell <colinodell@gmail.com> |
| 19 | + */ |
| 20 | +class UuidValidatorTest extends \PHPUnit_Framework_TestCase |
| 21 | +{ |
| 22 | + protected $context; |
| 23 | + protected $validator; |
| 24 | + |
| 25 | + protected function setUp() |
| 26 | + { |
| 27 | + $this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false); |
| 28 | + $this->validator = new UuidValidator(); |
| 29 | + $this->validator->initialize($this->context); |
| 30 | + } |
| 31 | + |
| 32 | + protected function tearDown() |
| 33 | + { |
| 34 | + $this->context = null; |
| 35 | + $this->validator = null; |
| 36 | + } |
| 37 | + |
| 38 | + public function testNullIsValid() |
| 39 | + { |
| 40 | + $this->context->expects($this->never()) |
| 41 | + ->method('addViolation'); |
| 42 | + |
| 43 | + $this->validator->validate(null, new Uuid()); |
| 44 | + } |
| 45 | + |
| 46 | + public function testEmptyStringIsValid() |
| 47 | + { |
| 48 | + $this->context->expects($this->never()) |
| 49 | + ->method('addViolation'); |
| 50 | + |
| 51 | + $this->validator->validate('', new Uuid()); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException |
| 56 | + */ |
| 57 | + public function testExpectsStringCompatibleType() |
| 58 | + { |
| 59 | + $this->validator->validate(new \stdClass(), new Uuid()); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @dataProvider getValidStrictUuids |
| 64 | + */ |
| 65 | + public function testValidStrictUuids($uuid) |
| 66 | + { |
| 67 | + $this->context->expects($this->never()) |
| 68 | + ->method('addViolation'); |
| 69 | + |
| 70 | + $this->validator->validate($uuid, new Uuid()); |
| 71 | + } |
| 72 | + |
| 73 | + public function getValidStrictUuids() |
| 74 | + { |
| 75 | + return array( |
| 76 | + array('216fff40-98d9-11e3-a5e2-0800200c9a66'), // Version 1 UUID in lowercase |
| 77 | + array('216FFF40-98D9-11E3-A5E2-0800200C9A66'), // Version 1 UUID in UPPERCASE |
| 78 | + array('456daefb-5aa6-41b5-8dbc-068b05a8b201'), // Version 4 UUID in lowercase |
| 79 | + array('456DAEFb-5AA6-41B5-8DBC-068B05A8B201'), // Version 4 UUID in UPPERCASE |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @dataProvider getInvalidStrictUuids |
| 85 | + */ |
| 86 | + public function testInvalidStrictUuids($uuid) |
| 87 | + { |
| 88 | + $constraint = new Uuid(array( |
| 89 | + 'message' => 'testMessage' |
| 90 | + )); |
| 91 | + |
| 92 | + $this->context->expects($this->once()) |
| 93 | + ->method('addViolation') |
| 94 | + ->with('testMessage', array( |
| 95 | + '{{ value }}' => $uuid, |
| 96 | + )); |
| 97 | + |
| 98 | + $this->validator->validate($uuid, $constraint); |
| 99 | + } |
| 100 | + |
| 101 | + public function getInvalidStrictUuids() |
| 102 | + { |
| 103 | + return array( |
| 104 | + array('216fff40-98d9-11e3-a5e2-0800200c9a6'), // Too few characters |
| 105 | + array('216fff40-98d9-11e3-a5e2-0800200c9a666'), // Too many characters |
| 106 | + array('V16fff40-98d9-11e3-a5e2-0800200c9a66'), // Invalid character 'V' |
| 107 | + array('2-16fff-4098d-911e3a5e20-800-200c9-a66'), // Non-standard dash positions (randomly placed) |
| 108 | + |
| 109 | + // Non-standard UUIDs allowed by some other systems |
| 110 | + array('216f-ff40-98d9-11e3-a5e2-0800-200c-9a66'), // Non-standard dash positions (every 4 chars) |
| 111 | + array('216fff40-98d911e3-a5e20800-200c9a66'), // Non-standard dash positions (every 8 chars) |
| 112 | + array('216fff4098d911e3a5e20800200c9a66'), // No dashes at all |
| 113 | + array('{216fff40-98d9-11e3-a5e2-0800200c9a66}'), // Wrapped with curly braces |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @dataProvider getValidStrict
10000
Uuids |
| 119 | + */ |
| 120 | + public function testVersionConstraintIsValid($uuid) |
| 121 | + { |
| 122 | + $this->context->expects($this->never()) |
| 123 | + ->method('addViolation'); |
| 124 | + |
| 125 | + $constraint = new Uuid(array( |
| 126 | + 'versions' => array(Uuid::V1_MAC, Uuid::V4_RANDOM) |
| 127 | + )); |
| 128 | + |
| 129 | + $this->validator->validate($uuid, $constraint); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * @dataProvider getValidStrictUuids |
| 134 | + */ |
| 135 | + public function testVersionConstraintIsInvalid($uuid) |
| 136 | + { |
| 137 | + $constraint = new Uuid(array( |
| 138 | + 'versions' => array(Uuid::V2_DCE, Uuid::V3_MD5) |
| 139 | + )); |
| 140 | + |
| 141 | + $this->context->expects($this->once()) |
| 142 | + ->method('addViolation'); |
| 143 | + |
| 144 | + $this->validator->validate($uuid, $constraint); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * @dataProvider getValidNonStrictUuids |
| 149 | + */ |
| 150 | + public function testValidNonStrictUuids($uuid) |
| 151 | + { |
| 152 | + $constraint = new Uuid(array( |
| 153 | + 'strict' => false |
| 154 | + )); |
| 155 | + |
| 156 | + $this->context->expects($this->never()) |
| 157 | + ->method('addViolation'); |
| 158 | + |
| 159 | + $this->validator->validate($uuid, $constraint); |
| 160 | + } |
| 161 | + |
| 162 | + public function getValidNonStrictUuids() |
| 163 | + { |
| 164 | + return array( |
| 165 | + array('216fff40-98d9-11e3-a5e2-0800200c9a66'), // Version 1 UUID in lowercase |
| 166 | + array('216FFF40-98D9-11E3-A5E2-0800200C9A66'), // Version 1 UUID in UPPERCASE |
| 167 | + array('456daefb-5aa6-41b5-8dbc-068b05a8b201'), // Version 4 UUID in lowercase |
| 168 | + array('456DAEFb-5AA6-41B5-8DBC-068B05A8B201'), // Version 4 UUID in UPPERCASE |
| 169 | + |
| 170 | + // Non-standard UUIDs allowed by some other systems |
| 171 | + array('216f-ff40-98d9-11e3-a5e2-0800-200c-9a66'), // Non-standard dash positions (every 4 chars) |
| 172 | + array('216fff40-98d911e3-a5e20800-200c9a66'), // Non-standard dash positions (every 8 chars) |
| 173 | + array('216fff4098d911e3a5e20800200c9a66'), // No dashes at all |
| 174 | + array('{216fff40-98d9-11e3-a5e2-0800200c9a66}'), // Wrapped with curly braces |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * @dataProvider getInvalidNonStrictUuids |
| 180 | + */ |
| 181 | + public function testInvalidNonStrictUuids($uuid) |
| 182 | + { |
| 183 | + $constraint = new Uuid(array( |
| 184 | + 'strict' => false |
| 185 | + )); |
| 186 | + |
| 187 | + $this->context->expects($this->once()) |
| 188 | + ->method('addViolation'); |
| 189 | + |
| 190 | + $this->validator->validate($uuid, $constraint); |
| 191 | + } |
| 192 | + |
| 193 | + public function getInvalidNonStrictUuids() |
| 194 | + { |
| 195 | + return array( |
| 196 | + array('216fff40-98d9-11e3-a5e2-0800200c9a6'), // Too few characters |
| 197 | + array('216fff40-98d9-11e3-a5e2-0800200c9a666'), // Too many characters |
| 198 | + array('V16fff40-98d9-11e3-a5e2-0800200c9a66'), // Invalid character 'V' |
| 199 | + array('2-16fff-4098d-911e3a5e20-800-200c9-a66'), // Non-standard dash positions (randomly placed) |
| 200 | + ); |
| 201 | + } |
| 202 | +} |
0 commit comments