3 use Wikimedia\Rdbms\Database
;
4 use Wikimedia\Rdbms\DBConnRef
;
5 use Wikimedia\Rdbms\FakeResultWrapper
;
6 use Wikimedia\Rdbms\IDatabase
;
7 use Wikimedia\Rdbms\ILoadBalancer
;
8 use Wikimedia\Rdbms\ResultWrapper
;
11 * @covers Wikimedia\Rdbms\DBConnRef
13 class DBConnRefTest
extends PHPUnit\Framework\TestCase
{
15 use MediaWikiCoversValidator
;
16 use PHPUnit4And6Compat
;
19 * @return ILoadBalancer
21 private function getLoadBalancerMock() {
22 $lb = $this->getMock( ILoadBalancer
::class );
24 $lb->method( 'getConnection' )->willReturnCallback(
26 return $this->getDatabaseMock();
30 $lb->method( 'getConnectionRef' )->willReturnCallback(
31 function () use ( $lb ) {
32 return $this->getDBConnRef( $lb );
42 private function getDatabaseMock() {
43 $db = $this->getMockBuilder( Database
::class )
44 ->disableOriginalConstructor()
47 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
48 $db->method( '__toString' )->willReturn( 'MOCK_DB' );
56 private function getDBConnRef( ILoadBalancer
$lb = null ) {
57 $lb = $lb ?
: $this->getLoadBalancerMock();
58 return new DBConnRef( $lb, $this->getDatabaseMock() );
61 public function testConstruct() {
62 $lb = $this->getLoadBalancerMock();
63 $ref = new DBConnRef( $lb, $this->getDatabaseMock() );
65 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
68 public function testConstruct_params() {
69 $lb = $this->getMock( ILoadBalancer
::class );
71 $lb->expects( $this->once() )
72 ->method( 'getConnection' )
73 ->with( DB_MASTER
, [ 'test' ], 'dummy', ILoadBalancer
::CONN_TRX_AUTOCOMMIT
)
76 return $this->getDatabaseMock();
82 [ DB_MASTER
, [ 'test' ], 'dummy', ILoadBalancer
::CONN_TRX_AUTOCOMMIT
]
85 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
88 public function testDestruct() {
89 $lb = $this->getLoadBalancerMock();
91 $lb->expects( $this->once() )
92 ->method( 'reuseConnection' );
94 $this->innerMethodForTestDestruct( $lb );
97 private function innerMethodForTestDestruct( ILoadBalancer
$lb ) {
98 $ref = $lb->getConnectionRef( DB_REPLICA
);
100 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
103 public function testConstruct_failure() {
104 $this->setExpectedException( InvalidArgumentException
::class, '' );
106 $lb = $this->getLoadBalancerMock();
107 new DBConnRef( $lb, 17 ); // bad constructor argument
111 * @covers Wikimedia\Rdbms\DBConnRef::getDomainId
113 public function testGetDomainID() {
114 $lb = $this->getMock( ILoadBalancer
::class );
116 // getDomainID is optimized to not create a connection
117 $lb->expects( $this->never() )
118 ->method( 'getConnection' );
120 $ref = new DBConnRef( $lb, [ DB_REPLICA
, [], 'dummy', 0 ] );
122 $this->assertSame( 'dummy', $ref->getDomainID() );
125 public function testSelect() {
126 // select should get passed through normally
127 $ref = $this->getDBConnRef();
128 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
131 public function testToString() {
132 $ref = $this->getDBConnRef();
133 $this->assertInternalType( 'string', $ref->__toString() );
135 $lb = $this->getLoadBalancerMock();
136 $ref = new DBConnRef( $lb, [ DB_MASTER
, [], 'test', 0 ] );
137 $this->assertInternalType( 'string', $ref->__toString() );