MediaWiki  1.33.0
DatabaseDomainTest.php
Go to the documentation of this file.
1 <?php
2 
4 
8 class DatabaseDomainTest extends PHPUnit\Framework\TestCase {
9 
10  use MediaWikiCoversValidator;
11  use PHPUnit4And6Compat;
12 
13  public static function provideConstruct() {
14  return [
15  'All strings' =>
16  [ 'foo', 'bar', 'baz_', 'foo-bar-baz_' ],
17  'Nothing' =>
18  [ null, null, '', '' ],
19  'Invalid $database' =>
20  [ 0, 'bar', '', '', true ],
21  'Invalid $schema' =>
22  [ 'foo', 0, '', '', true ],
23  'Invalid $prefix' =>
24  [ 'foo', 'bar', 0, '', true ],
25  'Dash' =>
26  [ 'foo-bar', 'baz', 'baa_', 'foo?hbar-baz-baa_' ],
27  'Question mark' =>
28  [ 'foo?bar', 'baz', 'baa_', 'foo??bar-baz-baa_' ],
29  ];
30  }
31 
35  public function testConstruct( $db, $schema, $prefix, $id, $exception = false ) {
36  if ( $exception ) {
37  $this->setExpectedException( InvalidArgumentException::class );
38  new DatabaseDomain( $db, $schema, $prefix );
39  return;
40  }
41 
42  $domain = new DatabaseDomain( $db, $schema, $prefix );
43  $this->assertInstanceOf( DatabaseDomain::class, $domain );
44  $this->assertEquals( $db, $domain->getDatabase() );
45  $this->assertEquals( $schema, $domain->getSchema() );
46  $this->assertEquals( $prefix, $domain->getTablePrefix() );
47  $this->assertEquals( $id, $domain->getId() );
48  $this->assertEquals( $id, strval( $domain ), 'toString' );
49  }
50 
51  public static function provideNewFromId() {
52  return [
53  'Basic' =>
54  [ 'foo', 'foo', null, '' ],
55  'db+prefix' =>
56  [ 'foo-bar_', 'foo', null, 'bar_' ],
57  'db+schema+prefix' =>
58  [ 'foo-bar-baz_', 'foo', 'bar', 'baz_' ],
59  '?h -> -' =>
60  [ 'foo?hbar-baz-baa_', 'foo-bar', 'baz', 'baa_' ],
61  '?? -> ?' =>
62  [ 'foo??bar-baz-baa_', 'foo?bar', 'baz', 'baa_' ],
63  '? is left alone' =>
64  [ 'foo?bar-baz-baa_', 'foo?bar', 'baz', 'baa_' ],
65  'too many parts' =>
66  [ 'foo-bar-baz-baa_', '', '', '', true ],
67  'from instance' =>
68  [ DatabaseDomain::newUnspecified(), null, null, '' ],
69  ];
70  }
71 
75  public function testNewFromId( $id, $db, $schema, $prefix, $exception = false ) {
76  if ( $exception ) {
77  $this->setExpectedException( InvalidArgumentException::class );
78  DatabaseDomain::newFromId( $id );
79  return;
80  }
81  $domain = DatabaseDomain::newFromId( $id );
82  $this->assertInstanceOf( DatabaseDomain::class, $domain );
83  $this->assertEquals( $db, $domain->getDatabase() );
84  $this->assertEquals( $schema, $domain->getSchema() );
85  $this->assertEquals( $prefix, $domain->getTablePrefix() );
86  }
87 
88  public static function provideEquals() {
89  return [
90  'Basic' =>
91  [ 'foo', 'foo', null, '' ],
92  'db+prefix' =>
93  [ 'foo-bar_', 'foo', null, 'bar_' ],
94  'db+schema+prefix' =>
95  [ 'foo-bar-baz_', 'foo', 'bar', 'baz_' ],
96  '?h -> -' =>
97  [ 'foo?hbar-baz-baa_', 'foo-bar', 'baz', 'baa_' ],
98  '?? -> ?' =>
99  [ 'foo??bar-baz-baa_', 'foo?bar', 'baz', 'baa_' ],
100  'Nothing' =>
101  [ '', null, null, '' ],
102  ];
103  }
104 
109  public function testEquals( $id, $db, $schema, $prefix ) {
110  $fromId = DatabaseDomain::newFromId( $id );
111  $this->assertInstanceOf( DatabaseDomain::class, $fromId );
112 
113  $constructed = new DatabaseDomain( $db, $schema, $prefix );
114 
115  $this->assertTrue( $constructed->equals( $id ), 'constructed equals string' );
116  $this->assertTrue( $fromId->equals( $id ), 'fromId equals string' );
117 
118  $this->assertTrue( $constructed->equals( $fromId ), 'compare constructed to newId' );
119  $this->assertTrue( $fromId->equals( $constructed ), 'compare newId to constructed' );
120  }
121 
125  public function testNewUnspecified() {
126  $domain = DatabaseDomain::newUnspecified();
127  $this->assertInstanceOf( DatabaseDomain::class, $domain );
128  $this->assertTrue( $domain->equals( '' ) );
129  $this->assertSame( null, $domain->getDatabase() );
130  $this->assertSame( null, $domain->getSchema() );
131  $this->assertSame( '', $domain->getTablePrefix() );
132  }
133 
134  public static function provideIsCompatible() {
135  return [
136  'Basic' =>
137  [ 'foo', 'foo', null, '', true ],
138  'db+prefix' =>
139  [ 'foo-bar_', 'foo', null, 'bar_', true ],
140  'db+schema+prefix' =>
141  [ 'foo-bar-baz_', 'foo', 'bar', 'baz_', true ],
142  'db+dontcare_schema+prefix' =>
143  [ 'foo-bar-baz_', 'foo', null, 'baz_', false ],
144  '?h -> -' =>
145  [ 'foo?hbar-baz-baa_', 'foo-bar', 'baz', 'baa_', true ],
146  '?? -> ?' =>
147  [ 'foo??bar-baz-baa_', 'foo?bar', 'baz', 'baa_', true ],
148  'Nothing' =>
149  [ '', null, null, '', true ],
150  'dontcaredb+dontcaredbschema+prefix' =>
151  [ 'mywiki-mediawiki-prefix_', null, null, 'prefix_', false ],
152  'db+dontcareschema+prefix' =>
153  [ 'mywiki-schema-prefix_', 'mywiki', null, 'prefix_', false ],
154  'postgres-db-jobqueue' =>
155  [ 'postgres-mediawiki-', 'postgres', null, '', false ]
156  ];
157  }
158 
163  public function testIsCompatible( $id, $db, $schema, $prefix, $transitive ) {
164  $compareIdObj = DatabaseDomain::newFromId( $id );
165  $this->assertInstanceOf( DatabaseDomain::class, $compareIdObj );
166 
167  $fromId = new DatabaseDomain( $db, $schema, $prefix );
168 
169  $this->assertTrue( $fromId->isCompatible( $id ), 'constructed equals string' );
170  $this->assertTrue( $fromId->isCompatible( $compareIdObj ), 'fromId equals string' );
171 
172  $this->assertEquals( $transitive, $compareIdObj->isCompatible( $fromId ),
173  'test transitivity of nulls components' );
174  }
175 
176  public static function provideIsCompatible2() {
177  return [
178  'db+schema+prefix' =>
179  [ 'mywiki-schema-prefix_', 'thatwiki', 'schema', 'prefix_' ],
180  'dontcaredb+dontcaredbschema+prefix' =>
181  [ 'thatwiki-mediawiki-otherprefix_', null, null, 'prefix_' ],
182  'db+dontcareschema+prefix' =>
183  [ 'notmywiki-schema-prefix_', 'mywiki', null, 'prefix_' ],
184  ];
185  }
186 
191  public function testIsCompatible2( $id, $db, $schema, $prefix ) {
192  $compareIdObj = DatabaseDomain::newFromId( $id );
193  $this->assertInstanceOf( DatabaseDomain::class, $compareIdObj );
194 
195  $fromId = new DatabaseDomain( $db, $schema, $prefix );
196 
197  $this->assertFalse( $fromId->isCompatible( $id ), 'constructed equals string' );
198  $this->assertFalse( $fromId->isCompatible( $compareIdObj ), 'fromId equals string' );
199  }
200 
204  public function testSchemaWithNoDB1() {
205  new DatabaseDomain( null, 'schema', '' );
206  }
207 
211  public function testSchemaWithNoDB2() {
212  DatabaseDomain::newFromId( '-schema-prefix' );
213  }
214 
218  public function testIsUnspecified() {
219  $domain = new DatabaseDomain( null, null, '' );
220  $this->assertTrue( $domain->isUnspecified() );
221  $domain = new DatabaseDomain( 'mywiki', null, '' );
222  $this->assertFalse( $domain->isUnspecified() );
223  $domain = new DatabaseDomain( 'mywiki', null, '' );
224  $this->assertFalse( $domain->isUnspecified() );
225  }
226 }
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
DatabaseDomainTest\provideEquals
static provideEquals()
Definition: DatabaseDomainTest.php:88
DatabaseDomainTest\testIsCompatible
testIsCompatible( $id, $db, $schema, $prefix, $transitive)
provideIsCompatible Wikimedia\Rdbms\DatabaseDomain::isCompatible
Definition: DatabaseDomainTest.php:163
DatabaseDomainTest
Wikimedia\Rdbms\DatabaseDomain.
Definition: DatabaseDomainTest.php:8
DatabaseDomainTest\testConstruct
testConstruct( $db, $schema, $prefix, $id, $exception=false)
provideConstruct
Definition: DatabaseDomainTest.php:35
DatabaseDomainTest\testSchemaWithNoDB1
testSchemaWithNoDB1()
InvalidArgumentException.
Definition: DatabaseDomainTest.php:204
DatabaseDomainTest\provideConstruct
static provideConstruct()
Definition: DatabaseDomainTest.php:13
DatabaseDomainTest\provideIsCompatible2
static provideIsCompatible2()
Definition: DatabaseDomainTest.php:176
DatabaseDomainTest\testIsCompatible2
testIsCompatible2( $id, $db, $schema, $prefix)
provideIsCompatible2 Wikimedia\Rdbms\DatabaseDomain::isCompatible
Definition: DatabaseDomainTest.php:191
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
DatabaseDomainTest\provideIsCompatible
static provideIsCompatible()
Definition: DatabaseDomainTest.php:134
DatabaseDomainTest\testNewUnspecified
testNewUnspecified()
Wikimedia\Rdbms\DatabaseDomain::newUnspecified.
Definition: DatabaseDomainTest.php:125
DatabaseDomainTest\testNewFromId
testNewFromId( $id, $db, $schema, $prefix, $exception=false)
provideNewFromId
Definition: DatabaseDomainTest.php:75
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
DatabaseDomainTest\testSchemaWithNoDB2
testSchemaWithNoDB2()
InvalidArgumentException.
Definition: DatabaseDomainTest.php:211
DatabaseDomainTest\provideNewFromId
static provideNewFromId()
Definition: DatabaseDomainTest.php:51
DatabaseDomainTest\testIsUnspecified
testIsUnspecified()
Wikimedia\Rdbms\DatabaseDomain::isUnspecified.
Definition: DatabaseDomainTest.php:218
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1985
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Wikimedia\Rdbms\DatabaseDomain
Class to handle database/prefix specification for IDatabase domains.
Definition: DatabaseDomain.php:28
DatabaseDomainTest\testEquals
testEquals( $id, $db, $schema, $prefix)
provideEquals Wikimedia\Rdbms\DatabaseDomain::equals
Definition: DatabaseDomainTest.php:109