3 use Wikimedia\Rdbms\DatabaseDomain
;
6 * @covers Wikimedia\Rdbms\DatabaseDomain
8 class DatabaseDomainTest
extends PHPUnit_Framework_TestCase
{
9 public static function provideConstruct() {
12 [ 'foo', 'bar', 'baz', 'foo-bar-baz' ],
14 [ null, null, '', '' ],
15 'Invalid $database' =>
16 [ 0, 'bar', '', '', true ],
18 [ 'foo', 0, '', '', true ],
20 [ 'foo', 'bar', 0, '', true ],
22 [ 'foo-bar', 'baz', 'baa', 'foo?hbar-baz-baa' ],
24 [ 'foo?bar', 'baz', 'baa', 'foo??bar-baz-baa' ],
29 * @dataProvider provideConstruct
31 public function testConstruct( $db, $schema, $prefix, $id, $exception = false ) {
33 $this->setExpectedException( InvalidArgumentException
::class );
34 new DatabaseDomain( $db, $schema, $prefix );
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() {
50 [ 'foo', 'foo', null, '' ],
52 [ 'foo-bar', 'foo', null, 'bar' ],
54 [ 'foo-bar-baz', 'foo', 'bar', 'baz' ],
56 [ 'foo?hbar-baz-baa', 'foo-bar', 'baz', 'baa' ],
58 [ 'foo??bar-baz-baa', 'foo?bar', 'baz', 'baa' ],
60 [ 'foo?bar-baz-baa', 'foo?bar', 'baz', 'baa' ],
62 [ 'foo-bar-baz-baa', '', '', '', true ],
64 [ DatabaseDomain
::newUnspecified(), null, null, '' ],
69 * @dataProvider provideNewFromId
71 public function testNewFromId( $id, $db, $schema, $prefix, $exception = false ) {
73 $this->setExpectedException( InvalidArgumentException
::class );
74 DatabaseDomain
::newFromId( $id );
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() {
87 [ 'foo', 'foo', null, '' ],
89 [ 'foo-bar', 'foo', null, 'bar' ],
91 [ 'foo-bar-baz', 'foo', 'bar', 'baz' ],
93 [ 'foo?hbar-baz-baa', 'foo-bar', 'baz', 'baa' ],
95 [ 'foo??bar-baz-baa', 'foo?bar', 'baz', 'baa' ],
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() );