|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Koded\Stdlib; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use PHPUnit\Framework\Error\Warning; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class UUIDTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
const NS3_1 = '4d436f52-5707-3cc3-b69d-ec060ccdbcba'; |
|
13
|
|
|
const NS3_2 = 'b7890c8d-f62d-3048-ab4e-9cff3ab590d2'; |
|
14
|
|
|
|
|
15
|
|
|
const NS5_1 = '832102e1-a4d7-5cf5-a554-5a8201259f49'; |
|
16
|
|
|
const NS5_2 = 'fc03d336-5199-5422-bd34-678dd0867129'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @test |
|
20
|
|
|
*/ |
|
21
|
|
|
public function validates_the_uuid_format() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->assertTrue(UUID::valid(UUID::v4())); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @test |
|
28
|
|
|
*/ |
|
29
|
|
|
public function v1_with_node() |
|
30
|
|
|
{ |
|
31
|
|
|
$uuid = UUID::v1('08:60:6e:11:c0:8e'); |
|
32
|
|
|
$this->assertTrue(UUID::matches($uuid, 1)); |
|
33
|
|
|
$this->assertSame('1', $uuid[14]); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @test |
|
38
|
|
|
*/ |
|
39
|
|
|
public function v1_with_integer_node() |
|
40
|
|
|
{ |
|
41
|
|
|
$uuid = UUID::v1(0x7fffffff); |
|
42
|
|
|
$this->assertTrue(UUID::matches($uuid, 1)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @test |
|
47
|
|
|
*/ |
|
48
|
|
|
public function v1_with_invalid_node() |
|
49
|
|
|
{ |
|
50
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
51
|
|
|
UUID::v1('127.0.0.1'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @test |
|
56
|
|
|
*/ |
|
57
|
|
|
public function v1_with_invalid_integer_node() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
60
|
|
|
UUID::v1(2987918954764484727721); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @test |
|
65
|
|
|
*/ |
|
66
|
|
|
public function v1_with_invalid_hexadecimal_node() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
69
|
|
|
UUID::v1('z7ba3e221'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @test |
|
74
|
|
|
*/ |
|
75
|
|
|
public function v1_created_without_node() |
|
76
|
|
|
{ |
|
77
|
|
|
$uuid = UUID::v1(); |
|
78
|
|
|
$this->assertTrue(UUID::matches($uuid, 1)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @test |
|
83
|
|
|
*/ |
|
84
|
|
|
public function v3_uuids_from_same_namespace_and_same_name_are_equal() |
|
85
|
|
|
{ |
|
86
|
|
|
$uuid1 = UUID::v3(self::NS3_1, 'foo/bar'); |
|
87
|
|
|
$uuid2 = UUID::v3(self::NS3_1, 'foo/bar'); |
|
88
|
|
|
$this->assertSame($uuid1, $uuid2); |
|
89
|
|
|
$this->assertSame('3', $uuid1[14]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @test |
|
94
|
|
|
*/ |
|
95
|
|
|
public function v3_uuids_from_same_namespace_and_different_names_are_different() |
|
96
|
|
|
{ |
|
97
|
|
|
$uuid1 = UUID::v3(self::NS3_1, '123'); |
|
98
|
|
|
$uuid2 = UUID::v3(self::NS3_1, '456'); |
|
99
|
|
|
$this->assertTrue($uuid1 !== $uuid2); |
|
100
|
|
|
$this->assertSame('3', $uuid1[14]); |
|
101
|
|
|
$this->assertSame('3', $uuid2[14]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @test |
|
106
|
|
|
*/ |
|
107
|
|
|
public function v3_uuids_from_different_namespace_and_same_name_are_different() |
|
108
|
|
|
{ |
|
109
|
|
|
$uuid1 = UUID::v3(self::NS3_1, 'foo'); |
|
110
|
|
|
$uuid2 = UUID::v3(self::NS3_2, 'foo'); |
|
111
|
|
|
$this->assertTrue($uuid1 !== $uuid2); |
|
112
|
|
|
$this->assertSame('3', $uuid1[14]); |
|
113
|
|
|
$this->assertSame('3', $uuid2[14]); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @test |
|
118
|
|
|
*/ |
|
119
|
|
|
public function v3_throws_exception_on_invalid_namespace() |
|
120
|
|
|
{ |
|
121
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
122
|
|
|
UUID::v3('foo', 'foo'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @test |
|
127
|
|
|
*/ |
|
128
|
|
|
public function check_the_v4_format() |
|
129
|
|
|
{ |
|
130
|
|
|
$v4 = UUID::v4(); |
|
131
|
|
|
$this->assertTrue(UUID::valid($v4)); |
|
132
|
|
|
|
|
133
|
|
|
// check v4 spec |
|
134
|
|
|
$this->assertEquals('4', $v4[14]); |
|
135
|
|
|
$this->assertTrue(in_array($v4[19], ['8', '9', 'a', 'b'])); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @test |
|
140
|
|
|
*/ |
|
141
|
|
|
public function v5_uuids_from_same_namespace_and_same_name_are_equal() |
|
142
|
|
|
{ |
|
143
|
|
|
$uuid1 = UUID::v5(self::NS5_1, 'foo/bar/baz'); |
|
144
|
|
|
$uuid2 = UUID::v5(self::NS5_1, 'foo/bar/baz'); |
|
145
|
|
|
$this->assertSame($uuid1, $uuid2); |
|
146
|
|
|
$this->assertSame('5', $uuid1[14]); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @test |
|
151
|
|
|
*/ |
|
152
|
|
|
public function v5_uuids_from_same_namespace_and_different_names_are_different() |
|
153
|
|
|
{ |
|
154
|
|
|
$uuid1 = UUID::v5(self::NS5_1, '123'); |
|
155
|
|
|
$uuid2 = UUID::v5(self::NS5_1, '456'); |
|
156
|
|
|
$this->assertTrue($uuid1 !== $uuid2); |
|
157
|
|
|
$this->assertSame('5', $uuid1[14]); |
|
158
|
|
|
$this->assertSame('5', $uuid2[14]); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @test |
|
163
|
|
|
*/ |
|
164
|
|
|
public function v5_uuids_from_different_namespace_and_same_name_are_different() |
|
165
|
|
|
{ |
|
166
|
|
|
$uuid1 = UUID::v5(self::NS5_1, 'foo'); |
|
167
|
|
|
$uuid2 = UUID::v5(self::NS5_2, 'foo'); |
|
168
|
|
|
$this->assertTrue($uuid1 !== $uuid2); |
|
169
|
|
|
$this->assertSame('5', $uuid1[14]); |
|
170
|
|
|
$this->assertSame('5', $uuid2[14]); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @test |
|
175
|
|
|
*/ |
|
176
|
|
|
public function v5_throws_exception_on_invalid_namespace() |
|
177
|
|
|
{ |
|
178
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
179
|
|
|
UUID::v5('foo', 'foo'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @test |
|
184
|
|
|
*/ |
|
185
|
|
|
public function method_matches_fails_on_unsupported_uuid_version() |
|
186
|
|
|
{ |
|
187
|
|
|
$this->expectException(Warning::class); |
|
188
|
|
|
$this->expectExceptionMessage('assert(): Expected UUID version 1, 3, 4 or 5 failed'); |
|
189
|
|
|
UUID::matches(UUID::NAMESPACE_OID, 0); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @test |
|
194
|
|
|
*/ |
|
195
|
|
|
public function verify_uuid_regex() |
|
196
|
|
|
{ |
|
197
|
|
|
$this->assertFalse(UUID::valid('00000000-0000-2000-0000-000000000000')); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @test |
|
202
|
|
|
*/ |
|
203
|
|
|
public function verify_uuid4() |
|
204
|
|
|
{ |
|
205
|
|
|
$this->assertFalse(UUID::valid('00000000-0000-4000-0000-000000000000')); |
|
206
|
|
|
$this->assertTrue(UUID::valid('00000000-0000-4000-a000-000000000000')); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @test |
|
211
|
|
|
*/ |
|
212
|
|
|
public function issue7() |
|
213
|
|
|
{ |
|
214
|
|
|
$this->assertSame('1cb8bac3-bb8e-3973-93dc-5119246f0585', UUID::v3(UUID::NAMESPACE_URL, 'fubar')); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|