From 7928eca2920a64736965ebd073bf838afb1ff26e Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Fri, 21 Jul 2017 20:05:48 -0700 Subject: [PATCH] rdbms: Complete DatabaseDomain code coverage Add missing coverage for: * DatabaseDomain::equals() * DatabaseDomain::newUnspecified() * DatabaseDomain::__toString() Change-Id: I38863c5652ec395e7194bfb85b3485e3343a3b7e --- .../rdbms/database/DatabaseDomainTest.php | 104 ++++++++++++++---- 1 file changed, 81 insertions(+), 23 deletions(-) diff --git a/tests/phpunit/includes/libs/rdbms/database/DatabaseDomainTest.php b/tests/phpunit/includes/libs/rdbms/database/DatabaseDomainTest.php index 3dc7e287f6..a8dbdd39f8 100644 --- a/tests/phpunit/includes/libs/rdbms/database/DatabaseDomainTest.php +++ b/tests/phpunit/includes/libs/rdbms/database/DatabaseDomainTest.php @@ -8,16 +8,20 @@ use Wikimedia\Rdbms\DatabaseDomain; class DatabaseDomainTest extends PHPUnit_Framework_TestCase { public static function provideConstruct() { return [ - // All strings - [ 'foo', 'bar', 'baz', 'foo-bar-baz' ], - // Nothing - [ null, null, '', '' ], - // Invalid $database - [ 0, 'bar', '', '', true ], - // - in one of the fields - [ 'foo-bar', 'baz', 'baa', 'foo?hbar-baz-baa' ], - // ? in one of the fields - [ 'foo?bar', 'baz', 'baa', 'foo??bar-baz-baa' ], + 'All strings' => + [ 'foo', 'bar', 'baz', 'foo-bar-baz' ], + 'Nothing' => + [ null, null, '', '' ], + 'Invalid $database' => + [ 0, 'bar', '', '', true ], + 'Invalid $schema' => + [ 'foo', 0, '', '', true ], + 'Invalid $prefix' => + [ 'foo', 'bar', 0, '', true ], + 'Dash' => + [ 'foo-bar', 'baz', 'baa', 'foo?hbar-baz-baa' ], + 'Question mark' => + [ 'foo?bar', 'baz', 'baa', 'foo??bar-baz-baa' ], ]; } @@ -27,6 +31,8 @@ class DatabaseDomainTest extends PHPUnit_Framework_TestCase { public function testConstruct( $db, $schema, $prefix, $id, $exception = false ) { if ( $exception ) { $this->setExpectedException( InvalidArgumentException::class ); + new DatabaseDomain( $db, $schema, $prefix ); + return; } $domain = new DatabaseDomain( $db, $schema, $prefix ); @@ -35,23 +41,27 @@ class DatabaseDomainTest extends PHPUnit_Framework_TestCase { $this->assertEquals( $schema, $domain->getSchema() ); $this->assertEquals( $prefix, $domain->getTablePrefix() ); $this->assertEquals( $id, $domain->getId() ); + $this->assertEquals( $id, strval( $domain ), 'toString' ); } public static function provideNewFromId() { return [ - // basic - [ 'foo', 'foo', null, '' ], - // - - [ 'foo-bar', 'foo', null, 'bar' ], - [ 'foo-bar-baz', 'foo', 'bar', 'baz' ], - // ?h -> - - [ 'foo?hbar-baz-baa', 'foo-bar', 'baz', 'baa' ], - // ?? -> ? - [ 'foo??bar-baz-baa', 'foo?bar', 'baz', 'baa' ], - // ? is left alone - [ 'foo?bar-baz-baa', 'foo?bar', 'baz', 'baa' ], - // too many parts - [ 'foo-bar-baz-baa', '', '', '', true ], + 'Basic' => + [ 'foo', 'foo', null, '' ], + 'db+prefix' => + [ 'foo-bar', 'foo', null, 'bar' ], + 'db+schema+prefix' => + [ 'foo-bar-baz', 'foo', 'bar', 'baz' ], + '?h -> -' => + [ 'foo?hbar-baz-baa', 'foo-bar', 'baz', 'baa' ], + '?? -> ?' => + [ 'foo??bar-baz-baa', 'foo?bar', 'baz', 'baa' ], + '? is left alone' => + [ 'foo?bar-baz-baa', 'foo?bar', 'baz', 'baa' ], + 'too many parts' => + [ 'foo-bar-baz-baa', '', '', '', true ], + 'from instance' => + [ DatabaseDomain::newUnspecified(), null, null, '' ], ]; } @@ -61,6 +71,8 @@ class DatabaseDomainTest extends PHPUnit_Framework_TestCase { public function testNewFromId( $id, $db, $schema, $prefix, $exception = false ) { if ( $exception ) { $this->setExpectedException( InvalidArgumentException::class ); + DatabaseDomain::newFromId( $id ); + return; } $domain = DatabaseDomain::newFromId( $id ); $this->assertInstanceOf( DatabaseDomain::class, $domain ); @@ -68,4 +80,50 @@ class DatabaseDomainTest extends PHPUnit_Framework_TestCase { $this->assertEquals( $schema, $domain->getSchema() ); $this->assertEquals( $prefix, $domain->getTablePrefix() ); } + + public static function provideEquals() { + return [ + 'Basic' => + [ 'foo', 'foo', null, '' ], + 'db+prefix' => + [ 'foo-bar', 'foo', null, 'bar' ], + 'db+schema+prefix' => + [ 'foo-bar-baz', 'foo', 'bar', 'baz' ], + '?h -> -' => + [ 'foo?hbar-baz-baa', 'foo-bar', 'baz', 'baa' ], + '?? -> ?' => + [ 'foo??bar-baz-baa', 'foo?bar', 'baz', 'baa' ], + 'Nothing' => + [ '', null, null, '' ], + ]; + } + + /** + * @dataProvider provideEquals + * @covers Wikimedia\Rdbms\DatabaseDomain::equals + */ + public function testEquals( $id, $db, $schema, $prefix ) { + $fromId = DatabaseDomain::newFromId( $id ); + $this->assertInstanceOf( DatabaseDomain::class, $fromId ); + + $constructed = new DatabaseDomain( $db, $schema, $prefix ); + + $this->assertTrue( $constructed->equals( $id ), 'constructed equals string' ); + $this->assertTrue( $fromId->equals( $id ), 'fromId equals string' ); + + $this->assertTrue( $constructed->equals( $fromId ), 'compare constructed to newId' ); + $this->assertTrue( $fromId->equals( $constructed ), 'compare newId to constructed' ); + } + + /** + * @covers Wikimedia\Rdbms\DatabaseDomain::newUnspecified + */ + public function testNewUnspecified() { + $domain = DatabaseDomain::newUnspecified(); + $this->assertInstanceOf( DatabaseDomain::class, $domain ); + $this->assertTrue( $domain->equals( '' ) ); + $this->assertSame( null, $domain->getDatabase() ); + $this->assertSame( null, $domain->getSchema() ); + $this->assertSame( '', $domain->getTablePrefix() ); + } } -- 2.20.1