|
| 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 | +namespaceSymfony\Component\Validator\Tests\Constraints; |
| 13 | + |
| 14 | +useSymfony\Component\Validator\Constraints\Uuid; |
| 15 | +useSymfony\Component\Validator\Constraints\UuidValidator; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Colin O'Dell <colinodell@gmail.com> |
| 19 | + */ |
| 20 | +class UuidValidatorTestextends \PHPUnit_Framework_TestCase |
| 21 | +{ |
| 22 | +protected$context; |
| 23 | +protected$validator; |
| 24 | + |
| 25 | +protectedfunctionsetUp() |
| 26 | + { |
| 27 | +$this->context =$this->getMock('Symfony\Component\Validator\ExecutionContext',array(),array(),'',false); |
| 28 | +$this->validator =newUuidValidator(); |
| 29 | +$this->validator->initialize($this->context); |
| 30 | + } |
| 31 | + |
| 32 | +protectedfunctiontearDown() |
| 33 | + { |
| 34 | +$this->context =null; |
| 35 | +$this->validator =null; |
| 36 | + } |
| 37 | + |
| 38 | +publicfunctiontestNullIsValid() |
| 39 | + { |
| 40 | +$this->context->expects($this->never()) |
| 41 | + ->method('addViolation'); |
| 42 | + |
| 43 | +$this->validator->validate(null,newUuid()); |
| 44 | + } |
| 45 | + |
| 46 | +publicfunctiontestEmptyStringIsValid() |
| 47 | + { |
| 48 | +$this->context->expects($this->never()) |
| 49 | + ->method('addViolation'); |
| 50 | + |
| 51 | +$this->validator->validate('',newUuid()); |
| 52 | + } |
| 53 | + |
| 54 | +/** |
| 55 | + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException |
| 56 | + */ |
| 57 | +publicfunctiontestExpectsStringCompatibleType() |
| 58 | + { |
| 59 | +$this->validator->validate(new \stdClass(),newUuid()); |
| 60 | + } |
| 61 | + |
| 62 | +/** |
| 63 | + * @dataProvider getValidStrictUuids |
| 64 | + */ |
| 65 | +publicfunctiontestValidStrictUuids($uuid) |
| 66 | + { |
| 67 | +$this->context->expects($this->never()) |
| 68 | + ->method('addViolation'); |
| 69 | + |
| 70 | +$this->validator->validate($uuid,newUuid()); |
| 71 | + } |
| 72 | + |
| 73 | +publicfunctiongetValidStrictUuids() |
| 74 | + { |
| 75 | +returnarray( |
| 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 | +publicfunctiontestInvalidStrictUuids($uuid) |
| 87 | + { |
| 88 | +$constraint =newUuid(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 | +publicfunctiongetInvalidStrictUuids() |
| 102 | + { |
| 103 | +returnarray( |
| 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 getValidStrictUuids |
| 119 | + */ |
| 120 | +publicfunctiontestVersionConstraintIsValid($uuid) |
| 121 | + { |
| 122 | +$this->context->expects($this->never()) |
| 123 | + ->method('addViolation'); |
| 124 | + |
| 125 | +$constraint =newUuid(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 | +publicfunctiontestVersionConstraintIsInvalid($uuid) |
| 136 | + { |
| 137 | +$constraint =newUuid(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 | +publicfunctiontestValidNonStrictUuids($uuid) |
| 151 | + { |
| 152 | +$constraint =newUuid(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 | +publicfunctiongetValidNonStrictUuids() |
| 163 | + { |
| 164 | +returnarray( |
| 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 | +publicfunctiontestInvalidNonStrictUuids($uuid) |
| 182 | + { |
| 183 | +$constraint =newUuid(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 | +publicfunctiongetInvalidNonStrictUuids() |
| 194 | + { |
| 195 | +returnarray( |
| 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 | +} |