This moves and refactors the ConsistentReadConnectionManager from Wikibase into the core rdbms lib. The refactoring also creates a generic ConnectionManager. This relates to Iff20a22f9f2bc7ceefd6defc0ed9a494a6fe62c0 which introduced a DB factory / connection manager in an extension revealing the need for this in multiple places. Change-Id: I0c58e15aed5bed88323d18cb95e5008f8d3381c5
139 lines
3.8 KiB
PHP
139 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace Wikimedia\Tests\Rdbms;
|
|
|
|
use IDatabase;
|
|
use LoadBalancer;
|
|
use PHPUnit_Framework_MockObject_MockObject;
|
|
use Wikimedia\Rdbms\ConnectionManager;
|
|
|
|
/**
|
|
* @covers Wikimedia\Rdbms\ConnectionManager
|
|
*
|
|
* @license GPL-2.0+
|
|
* @author Daniel Kinzler
|
|
*/
|
|
class ConnectionManagerTest extends \PHPUnit_Framework_TestCase {
|
|
|
|
/**
|
|
* @return IDatabase|PHPUnit_Framework_MockObject_MockObject
|
|
*/
|
|
private function getIDatabaseMock() {
|
|
return $this->getMock( IDatabase::class );
|
|
}
|
|
|
|
/**
|
|
* @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
|
|
*/
|
|
private function getLoadBalancerMock() {
|
|
$lb = $this->getMockBuilder( LoadBalancer::class )
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
|
|
return $lb;
|
|
}
|
|
|
|
public function testGetReadConnection_nullGroups() {
|
|
$database = $this->getIDatabaseMock();
|
|
$lb = $this->getLoadBalancerMock();
|
|
|
|
$lb->expects( $this->once() )
|
|
->method( 'getConnection' )
|
|
->with( DB_REPLICA, [ 'group1' ], 'someDbName' )
|
|
->will( $this->returnValue( $database ) );
|
|
|
|
$manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
|
|
$actual = $manager->getReadConnection();
|
|
|
|
$this->assertSame( $database, $actual );
|
|
}
|
|
|
|
public function testGetReadConnection_withGroups() {
|
|
$database = $this->getIDatabaseMock();
|
|
$lb = $this->getLoadBalancerMock();
|
|
|
|
$lb->expects( $this->once() )
|
|
->method( 'getConnection' )
|
|
->with( DB_REPLICA, [ 'group2' ], 'someDbName' )
|
|
->will( $this->returnValue( $database ) );
|
|
|
|
$manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
|
|
$actual = $manager->getReadConnection( [ 'group2' ] );
|
|
|
|
$this->assertSame( $database, $actual );
|
|
}
|
|
|
|
public function testGetWriteConnection() {
|
|
$database = $this->getIDatabaseMock();
|
|
$lb = $this->getLoadBalancerMock();
|
|
|
|
$lb->expects( $this->once() )
|
|
->method( 'getConnection' )
|
|
->with( DB_MASTER, [ 'group1' ], 'someDbName' )
|
|
->will( $this->returnValue( $database ) );
|
|
|
|
$manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
|
|
$actual = $manager->getWriteConnection();
|
|
|
|
$this->assertSame( $database, $actual );
|
|
}
|
|
|
|
public function testReleaseConnection() {
|
|
$database = $this->getIDatabaseMock();
|
|
$lb = $this->getLoadBalancerMock();
|
|
|
|
$lb->expects( $this->once() )
|
|
->method( 'reuseConnection' )
|
|
->with( $database )
|
|
->will( $this->returnValue( null ) );
|
|
|
|
$manager = new ConnectionManager( $lb );
|
|
$manager->releaseConnection( $database );
|
|
}
|
|
|
|
public function testGetReadConnectionRef_nullGroups() {
|
|
$database = $this->getIDatabaseMock();
|
|
$lb = $this->getLoadBalancerMock();
|
|
|
|
$lb->expects( $this->once() )
|
|
->method( 'getConnectionRef' )
|
|
->with( DB_REPLICA, [ 'group1' ], 'someDbName' )
|
|
->will( $this->returnValue( $database ) );
|
|
|
|
$manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
|
|
$actual = $manager->getReadConnectionRef();
|
|
|
|
$this->assertSame( $database, $actual );
|
|
}
|
|
|
|
public function testGetReadConnectionRef_withGroups() {
|
|
$database = $this->getIDatabaseMock();
|
|
$lb = $this->getLoadBalancerMock();
|
|
|
|
$lb->expects( $this->once() )
|
|
->method( 'getConnectionRef' )
|
|
->with( DB_REPLICA, [ 'group2' ], 'someDbName' )
|
|
->will( $this->returnValue( $database ) );
|
|
|
|
$manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
|
|
$actual = $manager->getReadConnectionRef( [ 'group2' ] );
|
|
|
|
$this->assertSame( $database, $actual );
|
|
}
|
|
|
|
public function testGetWriteConnectionRef() {
|
|
$database = $this->getIDatabaseMock();
|
|
$lb = $this->getLoadBalancerMock();
|
|
|
|
$lb->expects( $this->once() )
|
|
->method( 'getConnectionRef' )
|
|
->with( DB_MASTER, [ 'group1' ], 'someDbName' )
|
|
->will( $this->returnValue( $database ) );
|
|
|
|
$manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
|
|
$actual = $manager->getWriteConnectionRef();
|
|
|
|
$this->assertSame( $database, $actual );
|
|
}
|
|
|
|
}
|