MediaWiki REL1_30
DatabaseDomainTest.php
Go to the documentation of this file.
1<?php
2
4
8class 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' ],
25 ];
26 }
27
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;
36 }
37
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' );
45 }
46
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, '' ],
65 ];
66 }
67
71 public function testNewFromId( $id, $db, $schema, $prefix, $exception = false ) {
72 if ( $exception ) {
73 $this->setExpectedException( InvalidArgumentException::class );
74 DatabaseDomain::newFromId( $id );
75 return;
76 }
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() );
82 }
83
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, '' ],
98 ];
99 }
100
105 public function testEquals( $id, $db, $schema, $prefix ) {
106 $fromId = DatabaseDomain::newFromId( $id );
107 $this->assertInstanceOf( DatabaseDomain::class, $fromId );
108
109 $constructed = new DatabaseDomain( $db, $schema, $prefix );
110
111 $this->assertTrue( $constructed->equals( $id ), 'constructed equals string' );
112 $this->assertTrue( $fromId->equals( $id ), 'fromId equals string' );
113
114 $this->assertTrue( $constructed->equals( $fromId ), 'compare constructed to newId' );
115 $this->assertTrue( $fromId->equals( $constructed ), 'compare newId to constructed' );
116 }
117
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() );
128 }
129}
Wikimedia\Rdbms\DatabaseDomain.
testNewUnspecified()
Wikimedia\Rdbms\DatabaseDomain::newUnspecified.
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
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:1976