MediaWiki  1.30.2
UIDGeneratorTest.php
Go to the documentation of this file.
1 <?php
2 
3 class UIDGeneratorTest extends PHPUnit_Framework_TestCase {
4 
5  protected function tearDown() {
6  // Bug: 44850
8  parent::tearDown();
9  }
10 
18  public function testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits ) {
19  $id = call_user_func( [ 'UIDGenerator', $method ] );
20  $this->assertEquals( true, ctype_digit( $id ), "UID made of digit characters" );
21  $this->assertLessThanOrEqual( $digitlen, strlen( $id ),
22  "UID has the right number of digits" );
23  $this->assertLessThanOrEqual( $bits, strlen( Wikimedia\base_convert( $id, 10, 2 ) ),
24  "UID has the right number of bits" );
25 
26  $ids = [];
27  for ( $i = 0; $i < 300; $i++ ) {
28  $ids[] = call_user_func( [ 'UIDGenerator', $method ] );
29  }
30 
31  $lastId = array_shift( $ids );
32 
33  $this->assertSame( array_unique( $ids ), $ids, "All generated IDs are unique." );
34 
35  foreach ( $ids as $id ) {
36  // Convert string to binary and pad to full length so we can
37  // extract segments
38  $id_bin = Wikimedia\base_convert( $id, 10, 2, $bits );
39  $lastId_bin = Wikimedia\base_convert( $lastId, 10, 2, $bits );
40 
41  $timestamp_bin = substr( $id_bin, 0, $tbits );
42  $last_timestamp_bin = substr( $lastId_bin, 0, $tbits );
43 
44  $this->assertGreaterThanOrEqual(
45  $last_timestamp_bin,
46  $timestamp_bin,
47  "timestamp ($timestamp_bin) of current ID ($id_bin) >= timestamp ($last_timestamp_bin) " .
48  "of prior one ($lastId_bin)" );
49 
50  $hostbits_bin = substr( $id_bin, -$hostbits );
51  $last_hostbits_bin = substr( $lastId_bin, -$hostbits );
52 
53  if ( $hostbits ) {
54  $this->assertEquals(
55  $hostbits_bin,
56  $last_hostbits_bin,
57  "Host ID ($hostbits_bin) of current ID ($id_bin) is same as host ID ($last_hostbits_bin) " .
58  "of prior one ($lastId_bin)." );
59  }
60 
61  $lastId = $id;
62  }
63  }
64 
69  public static function provider_testTimestampedUID() {
70  return [
71  [ 'newTimestampedUID128', 39, 128, 46, 48 ],
72  [ 'newTimestampedUID128', 39, 128, 46, 48 ],
73  [ 'newTimestampedUID88', 27, 88, 46, 32 ],
74  ];
75  }
76 
80  public function testUUIDv1() {
81  $ids = [];
82  for ( $i = 0; $i < 100; $i++ ) {
84  $this->assertEquals( true,
85  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 ),
86  "UID $id has the right format" );
87  $ids[] = $id;
88 
90  $this->assertEquals( true,
91  preg_match( '!^[0-9a-f]{12}1[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
92  "UID $id has the right format" );
93 
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" );
98  }
99 
100  $this->assertEquals( array_unique( $ids ), $ids, "All generated IDs are unique." );
101  }
102 
106  public function testUUIDv4() {
107  $ids = [];
108  for ( $i = 0; $i < 100; $i++ ) {
109  $id = UIDGenerator::newUUIDv4();
110  $ids[] = $id;
111  $this->assertEquals( true,
112  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 ),
113  "UID $id has the right format" );
114  }
115 
116  $this->assertEquals( array_unique( $ids ), $ids, 'All generated IDs are unique.' );
117  }
118 
122  public function testRawUUIDv4() {
123  for ( $i = 0; $i < 100; $i++ ) {
125  $this->assertEquals( true,
126  preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
127  "UID $id has the right format" );
128  }
129  }
130 
134  public function testRawUUIDv4QuickRand() {
135  for ( $i = 0; $i < 100; $i++ ) {
137  $this->assertEquals( true,
138  preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
139  "UID $id has the right format" );
140  }
141  }
142 
146  public function testNewSequentialID() {
147  $id1 = UIDGenerator::newSequentialPerNodeID( 'test', 32 );
148  $id2 = UIDGenerator::newSequentialPerNodeID( 'test', 32 );
149 
150  $this->assertInternalType( 'float', $id1, "ID returned as float" );
151  $this->assertInternalType( 'float', $id2, "ID returned as float" );
152  $this->assertGreaterThan( 0, $id1, "ID greater than 1" );
153  $this->assertGreaterThan( $id1, $id2, "IDs increasing in value" );
154  }
155 
159  public function testNewSequentialIDs() {
160  $ids = UIDGenerator::newSequentialPerNodeIDs( 'test', 32, 5 );
161  $lastId = null;
162  foreach ( $ids as $id ) {
163  $this->assertInternalType( 'float', $id, "ID returned as float" );
164  $this->assertGreaterThan( 0, $id, "ID greater than 1" );
165  if ( $lastId ) {
166  $this->assertGreaterThan( $lastId, $id, "IDs increasing in value" );
167  }
168  $lastId = $id;
169  }
170  }
171 }
UIDGenerator\QUICK_RAND
const QUICK_RAND
Definition: UIDGenerator.php:45
UIDGenerator\newUUIDv1
static newUUIDv1()
Return an RFC4122 compliant v1 UUID.
Definition: UIDGenerator.php:217
UIDGeneratorTest
Definition: UIDGeneratorTest.php:3
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
UIDGeneratorTest\provider_testTimestampedUID
static provider_testTimestampedUID()
array( method, length, bits, hostbits ) NOTE: When adding a new method name here please update the co...
Definition: UIDGeneratorTest.php:69
UIDGenerator\newRawUUIDv4
static newRawUUIDv4( $flags=0)
Return an RFC4122 compliant v4 UUID.
Definition: UIDGenerator.php:311
UIDGeneratorTest\testTimestampedUID
testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits)
Test that generated UIDs have the expected properties.
Definition: UIDGeneratorTest.php:18
UIDGeneratorTest\testUUIDv4
testUUIDv4()
UIDGenerator::newUUIDv4.
Definition: UIDGeneratorTest.php:106
UIDGenerator\newSequentialPerNodeID
static newSequentialPerNodeID( $bucket, $bits=48, $flags=0)
Return an ID that is sequential only for this node and bucket.
Definition: UIDGenerator.php:327
UIDGeneratorTest\testRawUUIDv4QuickRand
testRawUUIDv4QuickRand()
UIDGenerator::newRawUUIDv4.
Definition: UIDGeneratorTest.php:134
UIDGeneratorTest\testNewSequentialIDs
testNewSequentialIDs()
UIDGenerator::newSequentialPerNodeIDs.
Definition: UIDGeneratorTest.php:159
UIDGenerator\newUUIDv4
static newUUIDv4( $flags=0)
Return an RFC4122 compliant v4 UUID.
Definition: UIDGenerator.php:285
UIDGenerator\unitTestTearDown
static unitTestTearDown()
Cleanup resources when tearing down after a unit test.
Definition: UIDGenerator.php:620
UIDGenerator\newRawUUIDv1
static newRawUUIDv1()
Return an RFC4122 compliant v1 UUID.
Definition: UIDGenerator.php:232
UIDGeneratorTest\testUUIDv1
testUUIDv1()
UIDGenerator::newUUIDv1.
Definition: UIDGeneratorTest.php:80
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Wikimedia
UIDGeneratorTest\testRawUUIDv4
testRawUUIDv4()
UIDGenerator::newRawUUIDv4.
Definition: UIDGeneratorTest.php:122
UIDGeneratorTest\testNewSequentialID
testNewSequentialID()
UIDGenerator::newSequentialPerNodeID.
Definition: UIDGeneratorTest.php:146
UIDGenerator\newSequentialPerNodeIDs
static newSequentialPerNodeIDs( $bucket, $bits, $count, $flags=0)
Return IDs that are sequential only for this node and bucket.
Definition: UIDGenerator.php:342
UIDGeneratorTest\tearDown
tearDown()
Definition: UIDGeneratorTest.php:5