Debian Codegraph @

tests/phpunit/unit/includes/libs/rdbms/database/DatabaseDomainTest.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Tests\Rdbms;
4
5 use InvalidArgumentException;
6 use MediaWikiCoversValidator;
7 use PHPUnit\Framework\TestCase;
8 use Wikimedia\Rdbms\DatabaseDomain;
9
10 /**
11 * @covers \Wikimedia\Rdbms\DatabaseDomain
12 */
13 class DatabaseDomainTest extends TestCase {
14
15 use MediaWikiCoversValidator;
16
17 public static function provideConstruct() {
18 return [
19 'All strings' =>
20 [ 'foo', 'bar', 'baz_', 'foo-bar-baz_' ],
21 'Nothing' =>
22 [ null, null, '', '' ],
23 'Invalid $database' =>
24 [ 0, 'bar', '', '', true ],
25 'Invalid $schema' =>
26 [ 'foo', 0, '', '', true ],
27 'Invalid $prefix' =>
28 [ 'foo', 'bar', 0, '', true ],
29 'Dash' =>
30 [ 'foo-bar', 'baz', 'baa_', 'foo?hbar-baz-baa_' ],
31 'Question mark' =>
32 [ 'foo?bar', 'baz', 'baa_', 'foo??bar-baz-baa_' ],
33 ];
34 }
35
36 /**
37 * @dataProvider provideConstruct
38 */
39 public function testConstruct( $db, $schema, $prefix, $id, $exception = false ) {
40 if ( $exception ) {
41 $this->expectException( InvalidArgumentException::class );
42 new DatabaseDomain( $db, $schema, $prefix );
43 return;
44 }
45
46 $domain = new DatabaseDomain( $db, $schema, $prefix );
47 $this->assertInstanceOf( DatabaseDomain::class, $domain );
48 $this->assertEquals( $db, $domain->getDatabase() );
49 $this->assertEquals( $schema, $domain->getSchema() );
50 $this->assertEquals( $prefix, $domain->getTablePrefix() );
51 $this->assertEquals( $id, $domain->getId() );
52 $this->assertEquals( $id, strval( $domain ), 'toString' );
53 }
54
55 public static function provideNewFromId() {
56 return [
57 'Basic' =>
58 [ 'foo', 'foo', null, '' ],
59 'db+prefix' =>
60 [ 'foo-bar_', 'foo', null, 'bar_' ],
61 'db+schema+prefix' =>
62 [ 'foo-bar-baz_', 'foo', 'bar', 'baz_' ],
63 '?h -> -' =>
64 [ 'foo?hbar-baz-baa_', 'foo-bar', 'baz', 'baa_' ],
65 '?? -> ?' =>
66 [ 'foo??bar-baz-baa_', 'foo?bar', 'baz', 'baa_' ],
67 '? is left alone' =>
68 [ 'foo?bar-baz-baa_', 'foo?bar', 'baz', 'baa_' ],
69 'too many parts' =>
70 [ 'foo-bar-baz-baa_', '', '', '', true ],
71 'from instance' =>
72 [ DatabaseDomain::newUnspecified(), null, null, '' ],
73 ];
74 }
75
76 /**
77 * @dataProvider provideNewFromId
78 */
79 public function testNewFromId( $id, $db, $schema, $prefix, $exception = false ) {
80 if ( $exception ) {
81 $this->expectException( InvalidArgumentException::class );
82 DatabaseDomain::newFromId( $id );
83 return;
84 }
85 $domain = DatabaseDomain::newFromId( $id );
86 $this->assertInstanceOf( DatabaseDomain::class, $domain );
87 $this->assertEquals( $db, $domain->getDatabase() );
88 $this->assertEquals( $schema, $domain->getSchema() );
89 $this->assertEquals( $prefix, $domain->getTablePrefix() );
90 }
91
92 public static function provideEquals() {
93 return [
94 'Basic' =>
95 [ 'foo', 'foo', null, '' ],
96 'db+prefix' =>
97 [ 'foo-bar_', 'foo', null, 'bar_' ],
98 'db+schema+prefix' =>
99 [ 'foo-bar-baz_', 'foo', 'bar', 'baz_' ],
100 '?h -> -' =>
101 [ 'foo?hbar-baz-baa_', 'foo-bar', 'baz', 'baa_' ],
102 '?? -> ?' =>
103 [ 'foo??bar-baz-baa_', 'foo?bar', 'baz', 'baa_' ],
104 'Nothing' =>
105 [ '', null, null, '' ],
106 ];
107 }
108
109 /**
110 * @dataProvider provideEquals
111 */
112 public function testEquals( $id, $db, $schema, $prefix ) {
113 $fromId = DatabaseDomain::newFromId( $id );
114 $this->assertInstanceOf( DatabaseDomain::class, $fromId );
115
116 $constructed = new DatabaseDomain( $db, $schema, $prefix );
117
118 $this->assertTrue( $constructed->equals( $id ), 'constructed equals string' );
119 $this->assertTrue( $fromId->equals( $id ), 'fromId equals string' );
120
121 $this->assertTrue( $constructed->equals( $fromId ), 'compare constructed to newId' );
122 $this->assertTrue( $fromId->equals( $constructed ), 'compare newId to constructed' );
123 }
124
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
159 /**
160 * @dataProvider provideIsCompatible
161 */
162 public function testIsCompatible( $id, $db, $schema, $prefix, $transitive ) {
163 $compareIdObj = DatabaseDomain::newFromId( $id );
164 $this->assertInstanceOf( DatabaseDomain::class, $compareIdObj );
165
166 $fromId = new DatabaseDomain( $db, $schema, $prefix );
167
168 $this->assertTrue( $fromId->isCompatible( $id ), 'constructed equals string' );
169 $this->assertTrue( $fromId->isCompatible( $compareIdObj ), 'fromId equals string' );
170
171 $this->assertEquals( $transitive, $compareIdObj->isCompatible( $fromId ),
172 'test transitivity of nulls components' );
173 }
174
175 public static function provideIsCompatible2() {
176 return [
177 'db+schema+prefix' =>
178 [ 'mywiki-schema-prefix_', 'thatwiki', 'schema', 'prefix_' ],
179 'dontcaredb+dontcaredbschema+prefix' =>
180 [ 'thatwiki-mediawiki-otherprefix_', null, null, 'prefix_' ],
181 'db+dontcareschema+prefix' =>
182 [ 'notmywiki-schema-prefix_', 'mywiki', null, 'prefix_' ],
183 ];
184 }
185
186 /**
187 * @dataProvider provideIsCompatible2
188 */
189 public function testIsCompatible2( $id, $db, $schema, $prefix ) {
190 $compareIdObj = DatabaseDomain::newFromId( $id );
191 $this->assertInstanceOf( DatabaseDomain::class, $compareIdObj );
192
193 $fromId = new DatabaseDomain( $db, $schema, $prefix );
194
195 $this->assertFalse( $fromId->isCompatible( $id ), 'constructed equals string' );
196 $this->assertFalse( $fromId->isCompatible( $compareIdObj ), 'fromId equals string' );
197 }
198
199 public function testSchemaWithNoDB1() {
200 $this->expectException( InvalidArgumentException::class );
201 new DatabaseDomain( null, 'schema', '' );
202 }
203
204 public function testSchemaWithNoDB2() {
205 $this->expectException( InvalidArgumentException::class );
206 DatabaseDomain::newFromId( '-schema-prefix' );
207 }
208
209 public function testIsUnspecified() {
210 $domain = new DatabaseDomain( null, null, '' );
211 $this->assertTrue( $domain->isUnspecified() );
212 $domain = new DatabaseDomain( 'mywiki', null, '' );
213 $this->assertFalse( $domain->isUnspecified() );
214 $domain = new DatabaseDomain( 'mywiki', null, '' );
215 $this->assertFalse( $domain->isUnspecified() );
216 }
217 }