MediaWiki REL1_32
DatabaseDomainTest.php
Go to the documentation of this file.
1<?php
2
4
8class 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 'dontcaredb+schema+prefix' =>
153 [ 'mywiki-schema-prefix', null, 'schema', 'prefix', false ],
154 'db+dontcareschema+prefix' =>
155 [ 'mywiki-schema-prefix', 'mywiki', null, 'prefix', false ],
156 'postgres-db-jobqueue' =>
157 [ 'postgres-mediawiki-', 'postgres', null, '', false ]
158 ];
159 }
160
165 public function testIsCompatible( $id, $db, $schema, $prefix, $transitive ) {
166 $compareIdObj = DatabaseDomain::newFromId( $id );
167 $this->assertInstanceOf( DatabaseDomain::class, $compareIdObj );
168
169 $fromId = new DatabaseDomain( $db, $schema, $prefix );
170
171 $this->assertTrue( $fromId->isCompatible( $id ), 'constructed equals string' );
172 $this->assertTrue( $fromId->isCompatible( $compareIdObj ), 'fromId equals string' );
173
174 $this->assertEquals( $transitive, $compareIdObj->isCompatible( $fromId ),
175 'test transitivity of nulls components' );
176 }
177
178 public static function provideIsCompatible2() {
179 return [
180 'db+schema+prefix' =>
181 [ 'mywiki-schema-prefix', 'thatwiki', 'schema', 'prefix' ],
182 'dontcaredb+dontcaredbschema+prefix' =>
183 [ 'thatwiki-mediawiki-otherprefix', null, null, 'prefix' ],
184 'dontcaredb+schema+prefix' =>
185 [ 'mywiki-otherschema-prefix', null, 'schema', 'prefix' ],
186 'db+dontcareschema+prefix' =>
187 [ 'notmywiki-schema-prefix', 'mywiki', null, 'prefix' ],
188 ];
189 }
190
195 public function testIsCompatible2( $id, $db, $schema, $prefix ) {
196 $compareIdObj = DatabaseDomain::newFromId( $id );
197 $this->assertInstanceOf( DatabaseDomain::class, $compareIdObj );
198
199 $fromId = new DatabaseDomain( $db, $schema, $prefix );
200
201 $this->assertFalse( $fromId->isCompatible( $id ), 'constructed equals string' );
202 $this->assertFalse( $fromId->isCompatible( $compareIdObj ), 'fromId equals string' );
203 }
204
208 public function testIsUnspecified() {
209 $domain = new DatabaseDomain( null, null, '' );
210 $this->assertTrue( $domain->isUnspecified() );
211 $domain = new DatabaseDomain( 'mywiki', null, '' );
212 $this->assertFalse( $domain->isUnspecified() );
213 $domain = new DatabaseDomain( 'mywiki', null, '' );
214 $this->assertFalse( $domain->isUnspecified() );
215 }
216}
Wikimedia\Rdbms\DatabaseDomain.
testNewUnspecified()
Wikimedia\Rdbms\DatabaseDomain::newUnspecified.
testIsUnspecified()
Wikimedia\Rdbms\DatabaseDomain::isUnspecified.
testEquals( $id, $db, $schema, $prefix)
provideEquals Wikimedia\Rdbms\DatabaseDomain::equals
testNewFromId( $id, $db, $schema, $prefix, $exception=false)
provideNewFromId
testConstruct( $db, $schema, $prefix, $id, $exception=false)
provideConstruct
testIsCompatible2( $id, $db, $schema, $prefix)
provideIsCompatible2 Wikimedia\Rdbms\DatabaseDomain::isCompatible
testIsCompatible( $id, $db, $schema, $prefix, $transitive)
provideIsCompatible Wikimedia\Rdbms\DatabaseDomain::isCompatible
Class to handle database/prefix specification for IDatabase domains.
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:2055
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187