3 class UIDGeneratorTest
extends PHPUnit\Framework\TestCase
{
5 use MediaWikiCoversValidator
;
7 protected function tearDown() {
9 UIDGenerator
::unitTestTearDown();
14 * Test that generated UIDs have the expected properties
16 * @dataProvider provider_testTimestampedUID
17 * @covers UIDGenerator::newTimestampedUID88
18 * @covers UIDGenerator::getTimestampedID88
19 * @covers UIDGenerator::newTimestampedUID128
20 * @covers UIDGenerator::getTimestampedID128
22 public function testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits ) {
23 $id = call_user_func( [ UIDGenerator
::class, $method ] );
24 $this->assertEquals( true, ctype_digit( $id ), "UID made of digit characters" );
25 $this->assertLessThanOrEqual( $digitlen, strlen( $id ),
26 "UID has the right number of digits" );
27 $this->assertLessThanOrEqual( $bits, strlen( Wikimedia\base_convert
( $id, 10, 2 ) ),
28 "UID has the right number of bits" );
31 for ( $i = 0; $i < 300; $i++
) {
32 $ids[] = call_user_func( [ UIDGenerator
::class, $method ] );
35 $lastId = array_shift( $ids );
37 $this->assertSame( array_unique( $ids ), $ids, "All generated IDs are unique." );
39 foreach ( $ids as $id ) {
40 // Convert string to binary and pad to full length so we can
42 $id_bin = Wikimedia\base_convert
( $id, 10, 2, $bits );
43 $lastId_bin = Wikimedia\base_convert
( $lastId, 10, 2, $bits );
45 $timestamp_bin = substr( $id_bin, 0, $tbits );
46 $last_timestamp_bin = substr( $lastId_bin, 0, $tbits );
48 $this->assertGreaterThanOrEqual(
51 "timestamp ($timestamp_bin) of current ID ($id_bin) >= timestamp ($last_timestamp_bin) " .
52 "of prior one ($lastId_bin)" );
54 $hostbits_bin = substr( $id_bin, -$hostbits );
55 $last_hostbits_bin = substr( $lastId_bin, -$hostbits );
61 "Host ID ($hostbits_bin) of current ID ($id_bin) is same as host ID ($last_hostbits_bin) " .
62 "of prior one ($lastId_bin)." );
70 * array( method, length, bits, hostbits )
71 * NOTE: When adding a new method name here please update the covers tags for the tests!
73 public static function provider_testTimestampedUID() {
75 [ 'newTimestampedUID128', 39, 128, 46, 48 ],
76 [ 'newTimestampedUID128', 39, 128, 46, 48 ],
77 [ 'newTimestampedUID88', 27, 88, 46, 32 ],
82 * @covers UIDGenerator::newUUIDv1
83 * @covers UIDGenerator::getUUIDv1
85 public function testUUIDv1() {
87 for ( $i = 0; $i < 100; $i++
) {
88 $id = UIDGenerator
::newUUIDv1();
89 $this->assertEquals( true,
90 preg_match( '!^[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$!', $id ),
91 "UID $id has the right format" );
94 $id = UIDGenerator
::newRawUUIDv1();
95 $this->assertEquals( true,
96 preg_match( '!^[0-9a-f]{12}1[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
97 "UID $id has the right format" );
99 $id = UIDGenerator
::newRawUUIDv1();
100 $this->assertEquals( true,
101 preg_match( '!^[0-9a-f]{12}1[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
102 "UID $id has the right format" );
105 $this->assertEquals( array_unique( $ids ), $ids, "All generated IDs are unique." );
109 * @covers UIDGenerator::newUUIDv4
111 public function testUUIDv4() {
113 for ( $i = 0; $i < 100; $i++
) {
114 $id = UIDGenerator
::newUUIDv4();
116 $this->assertEquals( true,
117 preg_match( '!^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$!', $id ),
118 "UID $id has the right format" );
121 $this->assertEquals( array_unique( $ids ), $ids, 'All generated IDs are unique.' );
125 * @covers UIDGenerator::newRawUUIDv4
127 public function testRawUUIDv4() {
128 for ( $i = 0; $i < 100; $i++
) {
129 $id = UIDGenerator
::newRawUUIDv4();
130 $this->assertEquals( true,
131 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
132 "UID $id has the right format" );
137 * @covers UIDGenerator::newRawUUIDv4
139 public function testRawUUIDv4QuickRand() {
140 for ( $i = 0; $i < 100; $i++
) {
141 $id = UIDGenerator
::newRawUUIDv4( UIDGenerator
::QUICK_RAND
);
142 $this->assertEquals( true,
143 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
144 "UID $id has the right format" );
149 * @covers UIDGenerator::newSequentialPerNodeID
151 public function testNewSequentialID() {
152 $id1 = UIDGenerator
::newSequentialPerNodeID( 'test', 32 );
153 $id2 = UIDGenerator
::newSequentialPerNodeID( 'test', 32 );
155 $this->assertInternalType( 'float', $id1, "ID returned as float" );
156 $this->assertInternalType( 'float', $id2, "ID returned as float" );
157 $this->assertGreaterThan( 0, $id1, "ID greater than 1" );
158 $this->assertGreaterThan( $id1, $id2, "IDs increasing in value" );
162 * @covers UIDGenerator::newSequentialPerNodeIDs
163 * @covers UIDGenerator::getSequentialPerNodeIDs
165 public function testNewSequentialIDs() {
166 $ids = UIDGenerator
::newSequentialPerNodeIDs( 'test', 32, 5 );
168 foreach ( $ids as $id ) {
169 $this->assertInternalType( 'float', $id, "ID returned as float" );
170 $this->assertGreaterThan( 0, $id, "ID greater than 1" );
172 $this->assertGreaterThan( $lastId, $id, "IDs increasing in value" );