rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / phpunit / includes / libs / rdbms / database / DatabaseDomainTest.php
bloba8dbdd39f8d068c88f7bb1df0dc156065e69f7c8
1 <?php
3 use Wikimedia\Rdbms\DatabaseDomain;
5 /**
6 * @covers Wikimedia\Rdbms\DatabaseDomain
7 */
8 class DatabaseDomainTest extends PHPUnit_Framework_TestCase {
9 public static function provideConstruct() {
10 return [
11 'All strings' =>
12 [ 'foo', 'bar', 'baz', 'foo-bar-baz' ],
13 'Nothing' =>
14 [ null, null, '', '' ],
15 'Invalid $database' =>
16 [ 0, 'bar', '', '', true ],
17 'Invalid $schema' =>
18 [ 'foo', 0, '', '', true ],
19 'Invalid $prefix' =>
20 [ 'foo', 'bar', 0, '', true ],
21 'Dash' =>
22 [ 'foo-bar', 'baz', 'baa', 'foo?hbar-baz-baa' ],
23 'Question mark' =>
24 [ 'foo?bar', 'baz', 'baa', 'foo??bar-baz-baa' ],
28 /**
29 * @dataProvider provideConstruct
31 public function testConstruct( $db, $schema, $prefix, $id, $exception = false ) {
32 if ( $exception ) {
33 $this->setExpectedException( InvalidArgumentException::class );
34 new DatabaseDomain( $db, $schema, $prefix );
35 return;
38 $domain = new DatabaseDomain( $db, $schema, $prefix );
39 $this->assertInstanceOf( DatabaseDomain::class, $domain );
40 $this->assertEquals( $db, $domain->getDatabase() );
41 $this->assertEquals( $schema, $domain->getSchema() );
42 $this->assertEquals( $prefix, $domain->getTablePrefix() );
43 $this->assertEquals( $id, $domain->getId() );
44 $this->assertEquals( $id, strval( $domain ), 'toString' );
47 public static function provideNewFromId() {
48 return [
49 'Basic' =>
50 [ 'foo', 'foo', null, '' ],
51 'db+prefix' =>
52 [ 'foo-bar', 'foo', null, 'bar' ],
53 'db+schema+prefix' =>
54 [ 'foo-bar-baz', 'foo', 'bar', 'baz' ],
55 '?h -> -' =>
56 [ 'foo?hbar-baz-baa', 'foo-bar', 'baz', 'baa' ],
57 '?? -> ?' =>
58 [ 'foo??bar-baz-baa', 'foo?bar', 'baz', 'baa' ],
59 '? is left alone' =>
60 [ 'foo?bar-baz-baa', 'foo?bar', 'baz', 'baa' ],
61 'too many parts' =>
62 [ 'foo-bar-baz-baa', '', '', '', true ],
63 'from instance' =>
64 [ DatabaseDomain::newUnspecified(), null, null, '' ],
68 /**
69 * @dataProvider provideNewFromId
71 public function testNewFromId( $id, $db, $schema, $prefix, $exception = false ) {
72 if ( $exception ) {
73 $this->setExpectedException( InvalidArgumentException::class );
74 DatabaseDomain::newFromId( $id );
75 return;
77 $domain = DatabaseDomain::newFromId( $id );
78 $this->assertInstanceOf( DatabaseDomain::class, $domain );
79 $this->assertEquals( $db, $domain->getDatabase() );
80 $this->assertEquals( $schema, $domain->getSchema() );
81 $this->assertEquals( $prefix, $domain->getTablePrefix() );
84 public static function provideEquals() {
85 return [
86 'Basic' =>
87 [ 'foo', 'foo', null, '' ],
88 'db+prefix' =>
89 [ 'foo-bar', 'foo', null, 'bar' ],
90 'db+schema+prefix' =>
91 [ 'foo-bar-baz', 'foo', 'bar', 'baz' ],
92 '?h -> -' =>
93 [ 'foo?hbar-baz-baa', 'foo-bar', 'baz', 'baa' ],
94 '?? -> ?' =>
95 [ 'foo??bar-baz-baa', 'foo?bar', 'baz', 'baa' ],
96 'Nothing' =>
97 [ '', null, null, '' ],
102 * @dataProvider provideEquals
103 * @covers Wikimedia\Rdbms\DatabaseDomain::equals
105 public function testEquals( $id, $db, $schema, $prefix ) {
106 $fromId = DatabaseDomain::newFromId( $id );
107 $this->assertInstanceOf( DatabaseDomain::class, $fromId );
109 $constructed = new DatabaseDomain( $db, $schema, $prefix );
111 $this->assertTrue( $constructed->equals( $id ), 'constructed equals string' );
112 $this->assertTrue( $fromId->equals( $id ), 'fromId equals string' );
114 $this->assertTrue( $constructed->equals( $fromId ), 'compare constructed to newId' );
115 $this->assertTrue( $fromId->equals( $constructed ), 'compare newId to constructed' );
119 * @covers Wikimedia\Rdbms\DatabaseDomain::newUnspecified
121 public function testNewUnspecified() {
122 $domain = DatabaseDomain::newUnspecified();
123 $this->assertInstanceOf( DatabaseDomain::class, $domain );
124 $this->assertTrue( $domain->equals( '' ) );
125 $this->assertSame( null, $domain->getDatabase() );
126 $this->assertSame( null, $domain->getSchema() );
127 $this->assertSame( '', $domain->getTablePrefix() );