/
UUIDTest.php
302 lines (261 loc) · 8.88 KB
/
UUIDTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/**
* @file
* Unit tests for UUID v3, v4, and v5.
*
* Created by Matt Farina on 2011-11-29.
*/
// Include the composer autoloader.
require_once __DIR__ . '/../vendor/autoload.php';
use Lootils\Uuid\Uuid;
class UUIDTest extends PHPUnit_Framework_TestCase {
public $uuid = NULL;
/**
* Test the method Uuid::isValid().
*/
public function testIsValid() {
$tests = array(
'6ba7b810-9dad-11d1-80b4-00c04fd430c8' => TRUE,
'6ba7b811-9dad-11d1-80b4-00c04fd430c8' => TRUE,
'6ba7b812-9dad-11d1-80b4-00c04fd430c8' => TRUE,
'6ba7b814-9dad-11d1-80b4-00c04fd430c8' => TRUE,
'00000000-0000-0000-0000-000000000000' => TRUE,
'6ba7b810-9dad-11d1-80b4-00c04fd4308' => FALSE,
'6ba7b810-9dad-11d1-80b4-00c04fd430c8ss' => FALSE,
'6ba7b84-9dad-11d1-80b4-00c04fd430c8' => FALSE,
);
foreach ($tests as $uuid => $expected) {
$this->assertEquals($expected, Uuid::isValid($uuid));
}
}
/**
* The the v4 random method created a properly formatted UUID.
*/
public function testV4() {
$var = 0;
do {
++$var;
$uuid = Uuid::createV4();
$this->assertTrue(Uuid::isValid($uuid));
$this->assertEquals('4', substr($uuid, 14, 1));
$this->assertTrue(in_array(substr($uuid, 19, 1), array('8', '9', 'a', 'b')));
} while ($var < 1000);
}
/**
* Thest that v4 is random.
*/
public function testV4IsRandom() {
$this->assertNotEquals(Uuid::createV4(), Uuid::createV4());
}
/**
* Test v3 UUID.
*/
public function testV3() {
$tests = array(
// From https://github.com/shadowhand/uuid/blob/3.1/master/tests/kohana/UUIDTest.php
'team' => array(
'expected' => 'db7ec69b-eb29-37ef-a76d-2e2ef553e92e',
'namespace' => Uuid::NIL,
),
// From http://docs.python.org/library/uuid.html
'python.org' => array(
'expected' => '6fa459ea-ee8a-3ca4-894e-db77e160355e',
'namespace' => Uuid::DNS,
),
// These were generated using Python.
'mattfarina.com' => array(
'expected' => '20438dde-20d8-349c-937e-4544a202f35a',
'namespace' => Uuid::DNS,
),
'http://mattfarina.com' => array(
'expected' => '67c6b6cf-7455-30ca-8c6a-1cb02a2ac3df',
'namespace' => Uuid::URL,
),
);
foreach ($tests as $name => $data) {
$uuid = Uuid::createV3($data['namespace'], $name)->getUuid();
$this->assertEquals($data['expected'], $uuid);
$this->assertEquals('3', substr($uuid, 14, 1));
$this->assertTrue(in_array(substr($uuid, 19, 1), array('8', '9', 'a', 'b')));
}
}
/**
* @expectedException \Lootils\Uuid\Exception
* @expectedExceptionMessage The UUID provided for the namespace is not valid.
*/
public function testInvalidNamespaceCreateV3Exception() {
$uuid = Uuid::createV3('foo', 'bar');
}
/**
* Test v5 UUID.
*/
public function testV5() {
$tests = array(
// From https://github.com/shadowhand/uuid/blob/3.1/master/tests/kohana/UUIDTest.php
'team' => array(
'expected' => 'd221f29a-4332-5f0d-b323-c5206a2e86ce',
'namespace' => Uuid::NIL,
),
// From http://docs.python.org/library/uuid.html
'python.org' => array(
'expected' => '886313e1-3b8a-5372-9b90-0c9aee199e5d',
'namespace' => Uuid::DNS,
),
// These were generated using Python.
'mattfarina.com' => array(
'expected' => '35e872b4-190a-5faa-a0f6-09da0d4f9c01',
'namespace' => Uuid::DNS,
),
'http://mattfarina.com' => array(
'expected' => '6b7decb1-f9ad-5821-b676-dc73006cd2d5',
'namespace' => Uuid::URL,
),
);
foreach ($tests as $name => $data) {
$uuid = Uuid::createV5($data['namespace'], $name)->getUuid();
$this->assertEquals($data['expected'], $uuid);
$this->assertEquals('5', substr($uuid, 14, 1));
$this->assertTrue(in_array(substr($uuid, 19, 1), array('8', '9', 'a', 'b')));
}
}
/**
* @expectedException \Lootils\Uuid\Exception
* @expectedExceptionMessage The UUID provided for the namespace is not valid.
*/
public function testInvalidNamespaceCreateV5Exception() {
$uuid = Uuid::createV5('foo', 'bar');
}
/**
* Test parsing a UUID as a string when wrapped by {}.
*/
public function testParseUUID() {
$uuid = new Uuid('{886313e1-3b8a-5372-9b90-0c9aee199e5d}');
$this->assertEquals('886313e1-3b8a-5372-9b90-0c9aee199e5d', $uuid->getUuid());
}
/**
* @expectedException \Lootils\Uuid\Exception
* @expectedExceptionMessage The UUID string supplied could not be parsed.
*/
public function testStringToPartsException() {
$uuid = new Uuid('{35e872b4-190a-5faa-a0f6-09da0d4f9c01-453}');
}
/**
* Test setting and getting information around a UUID.
*/
public function testData() {
$uuid = new Uuid('{35e872b4-190a-5faa-a0f6-09da0d4f9c01}', Uuid::V5, Uuid::DNS, 'mattfarina.com');
$this->assertEquals(Uuid::V5, $uuid->getVersion());
$this->assertEquals(Uuid::DNS, $uuid->getNamespace());
$this->assertEquals('mattfarina.com', $uuid->getName());
}
/**
* Test URL Support.
*/
public function testURN() {
$uuid = new Uuid('urn:uuid:35e872b4-190a-5faa-a0f6-09da0d4f9c01', Uuid::V5, Uuid::DNS, 'mattfarina.com');
$this->assertEquals('35e872b4-190a-5faa-a0f6-09da0d4f9c01', $uuid->getUuid());
$this->assertEquals('urn:uuid:35e872b4-190a-5faa-a0f6-09da0d4f9c01', $uuid->getURN());
}
/**
* Test passing in arrays as a URI type.
*/
public function testArrays() {
$uuid = new Uuid(array('35e872b4', '190a', '5faa', 'a0', 'f6', '09da0d4f9c01'), UUID::V5, UUID::DNS, 'mattfarina.com');
$this->assertEquals('35e872b4-190a-5faa-a0f6-09da0d4f9c01', $uuid->getUuid());
$array = array(
'time_low' => '35e872b4',
'time_mid' => '190a',
'time_hi_version' => '5faa',
'clock_seq_hi_variant' => 'a0',
'clock_seq_low' => 'f6',
'node' => '09da0d4f9c01',
);
$uuid2 = new Uuid($array, Uuid::V5, Uuid::DNS, 'mattfarina.com');
$this->assertEquals('35e872b4-190a-5faa-a0f6-09da0d4f9c01', $uuid2->getUuid());
}
/**
* @expectedException \Lootils\Uuid\Exception
* @expectedExceptionMessage The UUID array supplied could not be parsed.
*/
public function testParseLongArrayException() {
$uuid = new Uuid(array('35e872b4', '190a', '5faa', 'a0', 'f6', '09da0d4f9c01', 'foo'));
}
/**
* @expectedException \Lootils\Uuid\Exception
* @expectedExceptionMessage The UUID array supplied could not be parsed.
*/
public function testParseArrayStructureException() {
$array = array('foo', '35e872b4', '190a', '5faa', 'a0', 'f6', '09da0d4f9c01');
unset($array[0]);
$uuid = new Uuid($array);
}
/**
* @expectedException \Lootils\Uuid\Exception
* @expectedExceptionMessage The UUID string supplied could not be parsed.
*/
public function testParseStringException() {
$uuid = new Uuid('foo');
}
/**
* @expectedException \Lootils\Uuid\Exception
* @expectedExceptionMessage The UUID supplied could not be parsed.
*/
public function testParseUnknownType() {
$test = new StdClass();
$uuid = new Uuid($test);
}
/**
* @expectedException \Lootils\Uuid\Exception
* @expectedExceptionMessage An invalid UUID version was specified.
*/
public function testInvalidSetVersionException() {
$uuid = new Uuid(array('35e872b4', '190a', '5faa', 'a0', 'f6', '09da0d4f9c01'), 7);
}
/**
* @expectedException \Lootils\Uuid\Exception
* @expectedExceptionMessage The UUID provided for the namespace is not valid.
*/
public function testInvalidUuidBinException() {
$uuid = Uuid::bin('foo');
}
/**
* Make sure fields are properly getting into their place.
*/
public function testFields() {
$array = array(
'time_low' => '35e872b4',
'time_mid' => '190a',
'time_hi_version' => '5faa',
'clock_seq_hi_variant' => 'a0',
'clock_seq_low' => 'f6',
'node' => '09da0d4f9c01',
);
$uuid = new Uuid($array, Uuid::V5, Uuid::DNS, 'mattfarina.com');
$fields = $uuid->listFields();
foreach ($fields as $field) {
$this->assertEquals($array[$field], $uuid->getField($field));
}
}
/**
* @expectedException \Lootils\Uuid\Exception
* @expectedExceptionMessage A field value was requested for an invalid field name.
*/
public function testGetFieldException() {
$array = array(
'time_low' => '35e872b4',
'time_mid' => '190a',
'time_hi_version' => '5faa',
'clock_seq_hi_variant' => 'a0',
'clock_seq_low' => 'f6',
'node' => '09da0d4f9c01',
);
$uuid = new Uuid($array, Uuid::V5, Uuid::DNS, 'mattfarina.com');
$uuid->getField('foo');
}
/**
* Test __toString converting an object to a valid UUID.
*/
public function testToString() {
$this->assertEquals('20438dde-20d8-349c-937e-4544a202f35a', (string)Uuid::createV3(Uuid::DNS, 'mattfarina.com')->getUuid());
}
}