2018-01-02 14:25:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use Wikimedia\Rdbms\Database;
|
|
|
|
|
use Wikimedia\Rdbms\DBConnRef;
|
|
|
|
|
use Wikimedia\Rdbms\FakeResultWrapper;
|
|
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
|
use Wikimedia\Rdbms\ILoadBalancer;
|
|
|
|
|
use Wikimedia\Rdbms\ResultWrapper;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Wikimedia\Rdbms\DBConnRef
|
|
|
|
|
*/
|
2018-02-17 12:29:13 +00:00
|
|
|
class DBConnRefTest extends PHPUnit\Framework\TestCase {
|
2018-01-02 14:25:17 +00:00
|
|
|
|
2018-07-06 19:19:51 +00:00
|
|
|
use MediaWikiCoversValidator;
|
2018-04-08 00:36:33 +00:00
|
|
|
use PHPUnit4And6Compat;
|
|
|
|
|
|
2018-01-02 14:25:17 +00:00
|
|
|
/**
|
|
|
|
|
* @return ILoadBalancer
|
|
|
|
|
*/
|
|
|
|
|
private function getLoadBalancerMock() {
|
|
|
|
|
$lb = $this->getMock( ILoadBalancer::class );
|
|
|
|
|
|
|
|
|
|
$lb->method( 'getConnection' )->willReturnCallback(
|
|
|
|
|
function () {
|
|
|
|
|
return $this->getDatabaseMock();
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$lb->method( 'getConnectionRef' )->willReturnCallback(
|
|
|
|
|
function () use ( $lb ) {
|
|
|
|
|
return $this->getDBConnRef( $lb );
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $lb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return IDatabase
|
|
|
|
|
*/
|
|
|
|
|
private function getDatabaseMock() {
|
|
|
|
|
$db = $this->getMockBuilder( Database::class )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
|
|
|
|
|
$db->method( '__toString' )->willReturn( 'MOCK_DB' );
|
|
|
|
|
|
|
|
|
|
return $db;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return IDatabase
|
|
|
|
|
*/
|
|
|
|
|
private function getDBConnRef( ILoadBalancer $lb = null ) {
|
|
|
|
|
$lb = $lb ?: $this->getLoadBalancerMock();
|
|
|
|
|
return new DBConnRef( $lb, $this->getDatabaseMock() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testConstruct() {
|
|
|
|
|
$lb = $this->getLoadBalancerMock();
|
|
|
|
|
$ref = new DBConnRef( $lb, $this->getDatabaseMock() );
|
|
|
|
|
|
|
|
|
|
$this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testConstruct_params() {
|
|
|
|
|
$lb = $this->getMock( ILoadBalancer::class );
|
|
|
|
|
|
|
|
|
|
$lb->expects( $this->once() )
|
|
|
|
|
->method( 'getConnection' )
|
2018-04-04 23:29:18 +00:00
|
|
|
->with( DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTOCOMMIT )
|
2018-01-02 14:25:17 +00:00
|
|
|
->willReturnCallback(
|
|
|
|
|
function () {
|
|
|
|
|
return $this->getDatabaseMock();
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$ref = new DBConnRef(
|
|
|
|
|
$lb,
|
2018-04-04 23:29:18 +00:00
|
|
|
[ DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTOCOMMIT ]
|
2018-01-02 14:25:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testDestruct() {
|
|
|
|
|
$lb = $this->getLoadBalancerMock();
|
|
|
|
|
|
|
|
|
|
$lb->expects( $this->once() )
|
|
|
|
|
->method( 'reuseConnection' );
|
|
|
|
|
|
|
|
|
|
$this->innerMethodForTestDestruct( $lb );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function innerMethodForTestDestruct( ILoadBalancer $lb ) {
|
|
|
|
|
$ref = $lb->getConnectionRef( DB_REPLICA );
|
|
|
|
|
|
|
|
|
|
$this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testConstruct_failure() {
|
|
|
|
|
$this->setExpectedException( InvalidArgumentException::class, '' );
|
|
|
|
|
|
|
|
|
|
$lb = $this->getLoadBalancerMock();
|
|
|
|
|
new DBConnRef( $lb, 17 ); // bad constructor argument
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-06 19:19:51 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Wikimedia\Rdbms\DBConnRef::getDomainId
|
|
|
|
|
*/
|
2018-01-02 14:25:17 +00:00
|
|
|
public function testGetDomainID() {
|
|
|
|
|
$lb = $this->getMock( ILoadBalancer::class );
|
|
|
|
|
|
|
|
|
|
// getDomainID is optimized to not create a connection
|
|
|
|
|
$lb->expects( $this->never() )
|
|
|
|
|
->method( 'getConnection' );
|
|
|
|
|
|
|
|
|
|
$ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ] );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( 'dummy', $ref->getDomainID() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSelect() {
|
|
|
|
|
// select should get passed through normally
|
|
|
|
|
$ref = $this->getDBConnRef();
|
|
|
|
|
$this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testToString() {
|
|
|
|
|
$ref = $this->getDBConnRef();
|
|
|
|
|
$this->assertInternalType( 'string', $ref->__toString() );
|
|
|
|
|
|
|
|
|
|
$lb = $this->getLoadBalancerMock();
|
|
|
|
|
$ref = new DBConnRef( $lb, [ DB_MASTER, [], 'test', 0 ] );
|
|
|
|
|
$this->assertInternalType( 'string', $ref->__toString() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|