-
-
Notifications
You must be signed in to change notification settings - Fork 499
/
UuidTest.php
1842 lines (1626 loc) · 64.4 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Rhumsaa\Uuid;
class UuidTest extends TestCase
{
protected function setUp()
{
Uuid::$timeOfDayTest = null;
Uuid::$force32Bit = false;
Uuid::$forceNoBigNumber = false;
Uuid::$forceNoOpensslRandomPseudoBytes = false;
Uuid::$ignoreSystemNode = false;
}
/**
* @covers Rhumsaa\Uuid\Uuid::fromString
* @covers Rhumsaa\Uuid\Uuid::__construct
*/
public function testFromString()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
$this->assertEquals('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', $uuid->toString());
}
/**
* @covers Rhumsaa\Uuid\Uuid::fromString
*/
public function testFromStringWithCurlyBraces()
{
$uuid = Uuid::fromString('{ff6f8cb0-c57d-11e1-9b21-0800200c9a66}');
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
$this->assertEquals('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', $uuid->toString());
}
/**
* @covers Rhumsaa\Uuid\Uuid::fromString
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Invalid UUID string:
*/
public function testFromStringWithInvalidUuidString()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21');
}
/**
* @covers Rhumsaa\Uuid\Uuid::fromString
*/
public function testFromStringWithUrn()
{
$uuid = Uuid::fromString('urn:uuid:ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
$this->assertEquals('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', $uuid->toString());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getBytes
*/
public function testGetBytes()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(16, strlen($uuid->getBytes()));
$this->assertEquals('/2+MsMV9EeGbIQgAIAyaZg==', base64_encode($uuid->getBytes()));
}
/**
* @covers Rhumsaa\Uuid\Uuid::getClockSeqHiAndReserved
*/
public function testGetClockSeqHiAndReserved()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(155, $uuid->getClockSeqHiAndReserved());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getClockSeqHiAndReservedHex
*/
public function testGetClockSeqHiAndReservedHex()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals('9b', $uuid->getClockSeqHiAndReservedHex());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getClockSeqLow
*/
public function testGetClockSeqLow()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(33, $uuid->getClockSeqLow());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getClockSeqLowHex
*/
public function testGetClockSeqLowHex()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals('21', $uuid->getClockSeqLowHex());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getClockSequence
*/
public function testGetClockSequence()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(6945, $uuid->getClockSequence());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getClockSequenceHex
*/
public function testGetClockSequenceHex()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals('1b21', $uuid->getClockSequenceHex());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getDateTime
*/
public function testGetDateTime()
{
// Check a recent date
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
$this->assertEquals('2012-07-04T02:14:34+00:00', $uuid->getDateTime()->format('c'));
// Check an old date
$uuid = Uuid::fromString('0901e600-0154-1000-9b21-0800200c9a66');
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
$this->assertEquals('1582-10-16T16:34:04+00:00', $uuid->getDateTime()->format('c'));
// Check a future date
$uuid = Uuid::fromString('ff9785f6-ffff-1fff-9669-00007ffffffe');
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
$this->assertEquals('5236-03-31T21:21:00+00:00', $uuid->getDateTime()->format('c'));
// Check the oldest date
$uuid = Uuid::fromString('00000000-0000-1000-9669-00007ffffffe');
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
$this->assertEquals('1582-10-15T00:00:00+00:00', $uuid->getDateTime()->format('c'));
}
/**
* @covers Rhumsaa\Uuid\Uuid::getDateTime
*/
public function testGetDateTime32Bit()
{
$this->skipIfNoMoontoastMath();
Uuid::$force32Bit = true;
// Check a recent date
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
$this->assertEquals('2012-07-04T02:14:34+00:00', $uuid->getDateTime()->format('c'));
// Check an old date
$uuid = Uuid::fromString('0901e600-0154-1000-9b21-0800200c9a66');
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
$this->assertEquals('1582-10-16T16:34:04+00:00', $uuid->getDateTime()->format('c'));
// Check a future date
$uuid = Uuid::fromString('ff9785f6-ffff-1fff-9669-00007ffffffe');
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
$this->assertEquals('5236-03-31T21:21:00+00:00', $uuid->getDateTime()->format('c'));
// Check the oldest date
$uuid = Uuid::fromString('00000000-0000-1000-9669-00007ffffffe');
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
$this->assertEquals('1582-10-15T00:00:00+00:00', $uuid->getDateTime()->format('c'));
}
/**
* @covers Rhumsaa\Uuid\Uuid::getDateTime
* @expectedException Rhumsaa\Uuid\Exception\UnsatisfiedDependencyException
*/
public function testGetDateTimeThrownException()
{
Uuid::$force32Bit = true;
Uuid::$forceNoBigNumber = true;
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$date = $uuid->getDateTime();
}
/**
* @covers Rhumsaa\Uuid\Uuid::getDateTime
* @covers Rhumsaa\Uuid\Exception\UnsupportedOperationException
* @expectedException Rhumsaa\Uuid\Exception\UnsupportedOperationException
* @expectedExceptionMessage Not a time-based UUID
*/
public function testGetDateTimeFromNonVersion1Uuid()
{
// Using a version 4 UUID to test
$uuid = Uuid::fromString('bf17b594-41f2-474f-bf70-4c90220f75de');
$date = $uuid->getDateTime();
}
/**
* @covers Rhumsaa\Uuid\Uuid::getFields
*/
public function testGetFields()
{
$this->skip64BitTest();
$fields = array(
'time_low' => 4285500592,
'time_mid' => 50557,
'time_hi_and_version' => 4577,
'clock_seq_hi_and_reserved' => 155,
'clock_seq_low' => 33,
'node' => 8796630719078,
);
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals($fields, $uuid->getFields());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getFields
* @expectedException Rhumsaa\Uuid\Exception\UnsatisfiedDependencyException
*/
public function testGetFields32Bit()
{
Uuid::$force32Bit = true;
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$fields = $uuid->getFields();
}
/**
* @covers Rhumsaa\Uuid\Uuid::getFieldsHex
*/
public function testGetFieldsHex()
{
$fields = array(
'time_low' => 'ff6f8cb0',
'time_mid' => 'c57d',
'time_hi_and_version' => '11e1',
'clock_seq_hi_and_reserved' => '9b',
'clock_seq_low' => '21',
'node' => '0800200c9a66',
);
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals($fields, $uuid->getFieldsHex());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getLeastSignificantBits
*/
public function testGetLeastSignificantBits()
{
$this->skipIfNoMoontoastMath();
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertInstanceOf('Moontoast\Math\BigNumber', $uuid->getLeastSignificantBits());
$this->assertEquals('11178224546741000806', $uuid->getLeastSignificantBits()->getValue());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getLeastSignificantBits
* @expectedException Rhumsaa\Uuid\Exception\UnsatisfiedDependencyException
*/
public function testGetLeastSignificantBitsException()
{
Uuid::$forceNoBigNumber = true;
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$bn = $uuid->getLeastSignificantBits();
}
/**
* @covers Rhumsaa\Uuid\Uuid::getLeastSignificantBitsHex
*/
public function testGetLeastSignificantBitsHex()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals('9b210800200c9a66', $uuid->getLeastSignificantBitsHex());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getMostSignificantBits
*/
public function testGetMostSignificantBits()
{
$this->skipIfNoMoontoastMath();
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertInstanceOf('Moontoast\Math\BigNumber', $uuid->getMostSignificantBits());
$this->assertEquals('18406084892941947361', $uuid->getMostSignificantBits()->getValue());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getMostSignificantBits
* @expectedException Rhumsaa\Uuid\Exception\UnsatisfiedDependencyException
*/
public function testGetMostSignificantBitsException()
{
Uuid::$forceNoBigNumber = true;
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$bn = $uuid->getMostSignificantBits();
}
/**
* @covers Rhumsaa\Uuid\Uuid::getMostSignificantBitsHex
*/
public function testGetMostSignificantBitsHex()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals('ff6f8cb0c57d11e1', $uuid->getMostSignificantBitsHex());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getNode
*/
public function testGetNode()
{
$this->skip64BitTest();
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(8796630719078, $uuid->getNode());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getNode
* @expectedException Rhumsaa\Uuid\Exception\UnsatisfiedDependencyException
*/
public function testGetNode32Bit()
{
Uuid::$force32Bit = true;
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$node = $uuid->getNode();
}
/**
* @covers Rhumsaa\Uuid\Uuid::getNodeHex
*/
public function testGetNodeHex()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals('0800200c9a66', $uuid->getNodeHex());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getTimeHiAndVersion
*/
public function testGetTimeHiAndVersion()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(4577, $uuid->getTimeHiAndVersion());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getTimeHiAndVersionHex
*/
public function testGetTimeHiAndVersionHex()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals('11e1', $uuid->getTimeHiAndVersionHex());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getTimeLow
*/
public function testGetTimeLow()
{
$this->skip64BitTest();
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(4285500592, $uuid->getTimeLow());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getTimeLow
* @expectedException Rhumsaa\Uuid\Exception\UnsatisfiedDependencyException
*/
public function testGetTimeLow32Bit()
{
Uuid::$force32Bit = true;
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$timeLow = $uuid->getTimeLow();
}
/**
* @covers Rhumsaa\Uuid\Uuid::getTimeLowHex
*/
public function testGetTimeLowHex()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals('ff6f8cb0', $uuid->getTimeLowHex());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getTimeMid
*/
public function testGetTimeMid()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(50557, $uuid->getTimeMid());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getTimeMidHex
*/
public function testGetTimeMidHex()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals('c57d', $uuid->getTimeMidHex());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getTimestamp
*/
public function testGetTimestamp()
{
$this->skip64BitTest();
// Check for a recent date
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(135606608744910000, $uuid->getTimestamp());
// Check for an old date
$uuid = Uuid::fromString('0901e600-0154-1000-9b21-0800200c9a66');
$this->assertEquals(1460440000000, $uuid->getTimestamp());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getTimestampHex
*/
public function testGetTimestampHex()
{
// Check for a recent date
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals('1e1c57dff6f8cb0', $uuid->getTimestampHex());
// Check for an old date
$uuid = Uuid::fromString('0901e600-0154-1000-9b21-0800200c9a66');
$this->assertEquals('00001540901e600', $uuid->getTimestampHex());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getTimestamp
* @covers Rhumsaa\Uuid\Exception\UnsupportedOperationException
* @expectedException Rhumsaa\Uuid\Exception\UnsupportedOperationException
* @expectedExceptionMessage Not a time-based UUID
*/
public function testGetTimestampFromNonVersion1Uuid()
{
// Using a version 4 UUID to test
$uuid = Uuid::fromString('bf17b594-41f2-474f-bf70-4c90220f75de');
$ts = $uuid->getTimestamp();
}
/**
* @covers Rhumsaa\Uuid\Uuid::getTimestampHex
* @covers Rhumsaa\Uuid\Exception\UnsupportedOperationException
* @expectedException Rhumsaa\Uuid\Exception\UnsupportedOperationException
* @expectedExceptionMessage Not a time-based UUID
*/
public function testGetTimestampHexFromNonVersion1Uuid()
{
// Using a version 4 UUID to test
$uuid = Uuid::fromString('bf17b594-41f2-474f-bf70-4c90220f75de');
$ts = $uuid->getTimestampHex();
}
/**
* @covers Rhumsaa\Uuid\Uuid::getTimestamp
* @expectedException Rhumsaa\Uuid\Exception\UnsatisfiedDependencyException
*/
public function testGetTimestamp32Bit()
{
Uuid::$force32Bit = true;
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$ts = $uuid->getTimestamp();
}
/**
* @covers Rhumsaa\Uuid\Uuid::getUrn
*/
public function testGetUrn()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals('urn:uuid:ff6f8cb0-c57d-11e1-9b21-0800200c9a66', $uuid->getUrn());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getVariant
*/
public function testGetVariantForReservedNcs()
{
$uuid1 = Uuid::fromString('ff6f8cb0-c57d-11e1-0b21-0800200c9a66');
$this->assertEquals(0, $uuid1->getVariant());
$uuid2 = Uuid::fromString('ff6f8cb0-c57d-11e1-1b21-0800200c9a66');
$this->assertEquals(0, $uuid2->getVariant());
$uuid3 = Uuid::fromString('ff6f8cb0-c57d-11e1-2b21-0800200c9a66');
$this->assertEquals(0, $uuid3->getVariant());
$uuid4 = Uuid::fromString('ff6f8cb0-c57d-11e1-3b21-0800200c9a66');
$this->assertEquals(0, $uuid4->getVariant());
$uuid5 = Uuid::fromString('ff6f8cb0-c57d-11e1-4b21-0800200c9a66');
$this->assertEquals(0, $uuid5->getVariant());
$uuid6 = Uuid::fromString('ff6f8cb0-c57d-11e1-5b21-0800200c9a66');
$this->assertEquals(0, $uuid6->getVariant());
$uuid7 = Uuid::fromString('ff6f8cb0-c57d-11e1-6b21-0800200c9a66');
$this->assertEquals(0, $uuid7->getVariant());
$uuid8 = Uuid::fromString('ff6f8cb0-c57d-11e1-7b21-0800200c9a66');
$this->assertEquals(0, $uuid8->getVariant());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getVariant
*/
public function testGetVariantForRfc4122()
{
$uuid1 = Uuid::fromString('ff6f8cb0-c57d-11e1-8b21-0800200c9a66');
$this->assertEquals(2, $uuid1->getVariant());
$uuid2 = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(2, $uuid2->getVariant());
$uuid3 = Uuid::fromString('ff6f8cb0-c57d-11e1-ab21-0800200c9a66');
$this->assertEquals(2, $uuid3->getVariant());
$uuid4 = Uuid::fromString('ff6f8cb0-c57d-11e1-bb21-0800200c9a66');
$this->assertEquals(2, $uuid4->getVariant());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getVariant
*/
public function testGetVariantForReservedMicrosoft()
{
$uuid1 = Uuid::fromString('ff6f8cb0-c57d-11e1-cb21-0800200c9a66');
$this->assertEquals(6, $uuid1->getVariant());
$uuid2 = Uuid::fromString('ff6f8cb0-c57d-11e1-db21-0800200c9a66');
$this->assertEquals(6, $uuid2->getVariant());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getVariant
*/
public function testGetVariantForReservedFuture()
{
$uuid1 = Uuid::fromString('ff6f8cb0-c57d-11e1-eb21-0800200c9a66');
$this->assertEquals(7, $uuid1->getVariant());
$uuid2 = Uuid::fromString('ff6f8cb0-c57d-11e1-fb21-0800200c9a66');
$this->assertEquals(7, $uuid2->getVariant());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getVersion
*/
public function testGetVersionForVersion1()
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals(1, $uuid->getVersion());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getVersion
*/
public function testGetVersionForVersion2()
{
$uuid = Uuid::fromString('6fa459ea-ee8a-2ca4-894e-db77e160355e');
$this->assertEquals(2, $uuid->getVersion());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getVersion
*/
public function testGetVersionForVersion3()
{
$uuid = Uuid::fromString('6fa459ea-ee8a-3ca4-894e-db77e160355e');
$this->assertEquals(3, $uuid->getVersion());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getVersion
*/
public function testGetVersionForVersion4()
{
$uuid = Uuid::fromString('6fabf0bc-603a-42f2-925b-d9f779bd0032');
$this->assertEquals(4, $uuid->getVersion());
}
/**
* @covers Rhumsaa\Uuid\Uuid::getVersion
*/
public function testGetVersionForVersion5()
{
$uuid = Uuid::fromString('886313e1-3b8a-5372-9b90-0c9aee199e5d');
$this->assertEquals(5, $uuid->getVersion());
}
/**
* @covers Rhumsaa\Uuid\Uuid::toString
* @covers Rhumsaa\Uuid\Uuid::__toString
*/
public function testToString()
{
// Check with a recent date
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$this->assertEquals('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', $uuid->toString());
$this->assertEquals('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', sprintf('%s', $uuid));
// Check with an old date
$uuid = Uuid::fromString('0901e600-0154-1000-9b21-0800200c9a66');
$this->assertEquals('0901e600-0154-1000-9b21-0800200c9a66', $uuid->toString());
$this->assertEquals('0901e600-0154-1000-9b21-0800200c9a66', sprintf('%s', $uuid));
}
/**
* @covers Rhumsaa\Uuid\Uuid::uuid1
* @covers Rhumsaa\Uuid\Uuid::getNodeFromSystem
*/
public function testUuid1()
{
$uuid = Uuid::uuid1();
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
$this->assertEquals(2, $uuid->getVariant());
$this->assertEquals(1, $uuid->getVersion());
}
/**
* @covers Rhumsaa\Uuid\Uuid::uuid1
*/
public function testUuid1WithNodeAndClockSequence()
{
$this->skip64BitTest();
$uuid = Uuid::uuid1(0x0800200c9a66, 0x1669);
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
$this->assertEquals(2, $uuid->getVariant());
$this->assertEquals(1, $uuid->getVersion());
$this->assertEquals(5737, $uuid->getClockSequence());
$this->assertEquals(8796630719078, $uuid->getNode());
$this->assertEquals('9669-0800200c9a66', substr($uuid->toString(), 19));
}
/**
* @covers Rhumsaa\Uuid\Uuid::uuid1
*/
public function testUuid1WithHexadecimalNode()
{
$uuid = Uuid::uuid1('7160355e');
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
$this->assertEquals(2, $uuid->getVariant());
$this->assertEquals(1, $uuid->getVersion());
$this->assertEquals('00007160355e', $uuid->getNodeHex());
if (PHP_INT_SIZE == 8) {
$this->assertEquals(1902130526, $uuid->getNode());
}
}
/**
* @covers Rhumsaa\Uuid\Uuid::uuid1
*/
public function testUuid1WithMixedCaseHexadecimalNode()
{
$uuid = Uuid::uuid1('71B0aD5e');
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
$this->assertEquals(2, $uuid->getVariant());
$this->assertEquals(1, $uuid->getVersion());
$this->assertEquals('000071b0ad5e', $uuid->getNodeHex());
if (PHP_INT_SIZE == 8) {
$this->assertEquals(1907404126, $uuid->getNode());
}
}
/**
* @covers Rhumsaa\Uuid\Uuid::uuid1
*/
public function testUuid1WithNodeAndClockSequence32Bit()
{
$uuid = Uuid::uuid1(0x7fffffff, 0x1669);
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
$this->assertEquals(2, $uuid->getVariant());
$this->assertEquals(1, $uuid->getVersion());
$this->assertEquals(5737, $uuid->getClockSequence());
$this->assertEquals('00007fffffff', $uuid->getNodeHex());
$this->assertEquals('9669-00007fffffff', substr($uuid->toString(), 19));
if (PHP_INT_SIZE == 8) {
$this->assertEquals(2147483647, $uuid->getNode());
}
}
/**
* @covers Rhumsaa\Uuid\Uuid::uuid1
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Invalid node value
*/
public function testUuid1WithOutOfBoundsNode()
{
$uuid = Uuid::uuid1(9223372036854775808);
}
/**
* @covers Rhumsaa\Uuid\Uuid::uuid1
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Invalid node value
*/
public function testUuid1WithNonHexadecimalNode()
{
$uuid = Uuid::uuid1('db77e160355g');
}
/**
* @covers Rhumsaa\Uuid\Uuid::uuid1
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Invalid node value
*/
public function testUuid1WithNon48bitNumber()
{
$uuid = Uuid::uuid1('db77e160355ef');
}
/**
* @covers Rhumsaa\Uuid\Uuid::uuid1
*/
public function testUuid1WithRandomNode()
{
Uuid::$ignoreSystemNode = true;
$uuid = Uuid::uuid1();
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
$this->assertEquals(2, $uuid->getVariant());
$this->assertEquals(1, $uuid->getVersion());
}
/**
* The "python.org" UUID is a known entity, so we're testing that this
* library generates a matching UUID for the same name.
* @see http://docs.python.org/library/uuid.html
*
* @covers Rhumsaa\Uuid\Uuid::uuid3
* @covers Rhumsaa\Uuid\Uuid::uuidFromHashedName
*/
public function testUuid3WithNamespaceAsUuidObject()
{
$nsUuid = Uuid::fromString(Uuid::NAMESPACE_DNS);
$uuid = Uuid::uuid3($nsUuid, 'python.org');
$this->assertEquals('6fa459ea-ee8a-3ca4-894e-db77e160355e', $uuid->toString());
$this->assertEquals(2, $uuid->getVariant());
$this->assertEquals(3, $uuid->getVersion());
}
/**
* The "python.org" UUID is a known entity, so we're testing that this
* library generates a matching UUID for the same name.
* @see http://docs.python.org/library/uuid.html
*
* @covers Rhumsaa\Uuid\Uuid::uuid3
* @covers Rhumsaa\Uuid\Uuid::uuidFromHashedName
*/
public function testUuid3WithNamespaceAsUuidString()
{
$uuid = Uuid::uuid3(Uuid::NAMESPACE_DNS, 'python.org');
$this->assertEquals('6fa459ea-ee8a-3ca4-894e-db77e160355e', $uuid->toString());
$this->assertEquals(2, $uuid->getVariant());
$this->assertEquals(3, $uuid->getVersion());
}
/**
* Tests more known version-3 UUIDs
*
* Taken from the Python UUID tests in
* http://hg.python.org/cpython/file/2f4c4db9aee5/Lib/test/test_uuid.py
*
* @covers Rhumsaa\Uuid\Uuid::uuid3
*/
public function testUuid3WithKnownUuids()
{
$uuids = array(
'6fa459ea-ee8a-3ca4-894e-db77e160355e' => Uuid::uuid3(Uuid::NAMESPACE_DNS, 'python.org'),
'9fe8e8c4-aaa8-32a9-a55c-4535a88b748d' => Uuid::uuid3(Uuid::NAMESPACE_URL, 'http://python.org/'),
'dd1a1cef-13d5-368a-ad82-eca71acd4cd1' => Uuid::uuid3(Uuid::NAMESPACE_OID, '1.3.6.1'),
'658d3002-db6b-3040-a1d1-8ddd7d189a4d' => Uuid::uuid3(Uuid::NAMESPACE_X500, 'c=ca'),
);
foreach ($uuids as $ustr => $uobj) {
$this->assertEquals(Uuid::RFC_4122, $uobj->getVariant());
$this->assertEquals(3, $uobj->getVersion());
$this->assertEquals(Uuid::fromString($ustr), $uobj);
$this->assertEquals((string) $uobj, $ustr);
}
}
/**
* @covers Rhumsaa\Uuid\Uuid::uuid4
* @covers Rhumsaa\Uuid\Uuid::generateBytes
* @covers Rhumsaa\Uuid\Uuid::uuidFromHashedName
*/
public function testUuid4()
{
$uuid = Uuid::uuid4();
$this->assertInstanceOf('Rhumsaa\Uuid\Uuid', $uuid);
$this->assertEquals(2, $uuid->getVariant());
$this->assertEquals(4, $uuid->getVersion());
}
/**
* @covers Rhumsaa\Uuid\Uuid::uuid4
* @covers Rhumsaa\Uuid\Uuid::generateBytes
* @covers Rhumsaa\Uuid\Uuid::uuidFromHashedName
*/
public function testUuid4WithoutOpensslRandomPseudoBytes()
{
Uuid::$forceNoOpensslRandomPseudoBytes = true;
$uuid = Uuid::uuid4();
$this->assertInstanceOf('Rhumsaa\Uuid\Uuid', $uuid);
$this->assertEquals(2, $uuid->getVariant());
$this->assertEquals(4, $uuid->getVersion());
}
/**
* The "python.org" UUID is a known entity, so we're testing that this
* library generates a matching UUID for the same name.
* @see http://docs.python.org/library/uuid.html
*
* @covers Rhumsaa\Uuid\Uuid::uuid5
* @covers Rhumsaa\Uuid\Uuid::uuidFromHashedName
*/
public function testUuid5WithNamespaceAsUuidObject()
{
$nsUuid = Uuid::fromString(Uuid::NAMESPACE_DNS);
$uuid = Uuid::uuid5($nsUuid, 'python.org');
$this->assertEquals('886313e1-3b8a-5372-9b90-0c9aee199e5d', $uuid->toString());
$this->assertEquals(2, $uuid->getVariant());
$this->assertEquals(5, $uuid->getVersion());
}
/**
* The "python.org" UUID is a known entity, so we're testing that this
* library generates a matching UUID for the same name.
* @see http://docs.python.org/library/uuid.html
*
* @covers Rhumsaa\Uuid\Uuid::uuid5
* @covers Rhumsaa\Uuid\Uuid::uuidFromHashedName
*/
public function testUuid5WithNamespaceAsUuidString()
{
$uuid = Uuid::uuid5(Uuid::NAMESPACE_DNS, 'python.org');
$this->assertEquals('886313e1-3b8a-5372-9b90-0c9aee199e5d', $uuid->toString());
$this->assertEquals(2, $uuid->getVariant());
$this->assertEquals(5, $uuid->getVersion());
}
/**
* Tests more known version-5 UUIDs
*
* Taken from the Python UUID tests in
* http://hg.python.org/cpython/file/2f4c4db9aee5/Lib/test/test_uuid.py
*
* @covers Rhumsaa\Uuid\Uuid::uuid5
*/
public function testUuid5WithKnownUuids()
{
$uuids = array(
'886313e1-3b8a-5372-9b90-0c9aee199e5d' => Uuid::uuid5(Uuid::NAMESPACE_DNS, 'python.org'),
'4c565f0d-3f5a-5890-b41b-20cf47701c5e' => Uuid::uuid5(Uuid::NAMESPACE_URL, 'http://python.org/'),
'1447fa61-5277-5fef-a9b3-fbc6e44f4af3' => Uuid::uuid5(Uuid::NAMESPACE_OID, '1.3.6.1'),
'cc957dd1-a972-5349-98cd-874190002798' => Uuid::uuid5(Uuid::NAMESPACE_X500, 'c=ca'),
);
foreach ($uuids as $ustr => $uobj) {
$this->assertEquals(Uuid::RFC_4122, $uobj->getVariant());
$this->assertEquals(5, $uobj->getVersion());
$this->assertEquals(Uuid::fromString($ustr), $uobj);
$this->assertEquals((string) $uobj, $ustr);
}
}
/**
* @covers Rhumsaa\Uuid\Uuid::compareTo
*/
public function testCompareTo()
{
// $uuid1 and $uuid2 are identical
$uuid1 = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
$uuid2 = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
// The next three UUIDs are used for comparing msb and lsb in
// the compareTo() method
// msb are less than $uuid4, lsb are greater than $uuid5
$uuid3 = Uuid::fromString('44cca71e-d13d-11e1-a959-c8bcc8a476f4');
// msb are greater than $uuid3, lsb are equal to those in $uuid3
$uuid4 = Uuid::fromString('44cca71e-d13d-11e2-a959-c8bcc8a476f4');
// msb are equal to those in $uuid3, lsb are less than in $uuid3
$uuid5 = Uuid::fromString('44cca71e-d13d-11e1-a959-c8bcc8a476f3');
$this->assertEquals(0, $uuid1->compareTo($uuid2));
$this->assertEquals(0, $uuid2->compareTo($uuid1));
$this->assertEquals(-1, $uuid3->compareTo($uuid4));
$this->assertEquals(1, $uuid4->compareTo($uuid3));
$this->assertEquals(-1, $uuid5->compareTo($uuid3));
$this->assertEquals(1, $uuid3->compareto($uuid5));
}
/**
* @covers Rhumsaa\Uuid\Uuid::equals
*/
public function testEquals()
{
$uuid1 = Uuid::uuid5(Uuid::NAMESPACE_DNS, 'python.org');
$uuid2 = Uuid::uuid5(Uuid::NAMESPACE_DNS, 'python.org');
$uuid3 = Uuid::uuid5(Uuid::NAMESPACE_DNS, 'php.net');
$this->assertTrue($uuid1->equals($uuid2));
$this->assertFalse($uuid1->equals($uuid3));
$this->assertFalse($uuid1->equals(null));
$this->assertFalse($uuid1->equals(new \stdClass()));
}
/**
* @covers Rhumsaa\Uuid\Uuid::uuid1
* @covers Rhumsaa\Uuid\Uuid::calculateUuidTime
*/
public function testCalculateUuidTime()
{
$timeOfDay = array(
'sec' => 1348845514,
'usec' => 277885,
'minuteswest' => 0,
'dsttime' => 0,
);
// For usec = 277885
Uuid::$timeOfDayTest = $timeOfDay;
$uuidA = Uuid::uuid1(0x00007ffffffe, 0x1669);
$this->assertEquals('c4dbe7e2-097f-11e2-9669-00007ffffffe', (string) $uuidA);
$this->assertEquals('c4dbe7e2', $uuidA->getTimeLowHex());
$this->assertEquals('097f', $uuidA->getTimeMidHex());
$this->assertEquals('11e2', $uuidA->getTimeHiAndVersionHex());
// For usec = 0
Uuid::$timeOfDayTest['usec'] = 0;
$uuidB = Uuid::uuid1(0x00007ffffffe, 0x1669);
$this->assertEquals('c4b18100-097f-11e2-9669-00007ffffffe', (string) $uuidB);
$this->assertEquals('c4b18100', $uuidB->getTimeLowHex());
$this->assertEquals('097f', $uuidB->getTimeMidHex());
$this->assertEquals('11e2', $uuidB->getTimeHiAndVersionHex());
// For usec = 999999
Uuid::$timeOfDayTest['usec'] = 999999;
$uuidC = Uuid::uuid1(0x00007ffffffe, 0x1669);
$this->assertEquals('c54a1776-097f-11e2-9669-00007ffffffe', (string) $uuidC);
$this->assertEquals('c54a1776', $uuidC->getTimeLowHex());
$this->assertEquals('097f', $uuidC->getTimeMidHex());
$this->assertEquals('11e2', $uuidC->getTimeHiAndVersionHex());
}
/**
* @covers Rhumsaa\Uuid\Uuid::uuid1
* @covers Rhumsaa\Uuid\Uuid::calculateUuidTime
*/
public function testCalculateUuidTimeForce32BitPath()
{
$this->skipIfNoMoontoastMath();
Uuid::$force32Bit = true;
$timeOfDay = array(
'sec' => 1348845514,
'usec' => 277885,
'minuteswest' => 0,
'dsttime' => 0,
);
// For usec = 277885
Uuid::$timeOfDayTest = $timeOfDay;
$uuidA = Uuid::uuid1(0x00007ffffffe, 0x1669);