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 PHPUnit4And6Compat
;
18 * @return ILoadBalancer
20 private function getLoadBalancerMock() {
21 $lb = $this->getMock( ILoadBalancer
::class );
23 $lb->method( 'getConnection' )->willReturnCallback(
25 return $this->getDatabaseMock();
29 $lb->method( 'getConnectionRef' )->willReturnCallback(
30 function () use ( $lb ) {
31 return $this->getDBConnRef( $lb );
41 private function getDatabaseMock() {
42 $db = $this->getMockBuilder( Database
::class )
43 ->disableOriginalConstructor()
46 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
47 $db->method( '__toString' )->willReturn( 'MOCK_DB' );
55 private function getDBConnRef( ILoadBalancer
$lb = null ) {
56 $lb = $lb ?
: $this->getLoadBalancerMock();
57 return new DBConnRef( $lb, $this->getDatabaseMock() );
60 public function testConstruct() {
61 $lb = $this->getLoadBalancerMock();
62 $ref = new DBConnRef( $lb, $this->getDatabaseMock() );
64 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
67 public function testConstruct_params() {
68 $lb = $this->getMock( ILoadBalancer
::class );
70 $lb->expects( $this->once() )
71 ->method( 'getConnection' )
72 ->with( DB_MASTER
, [ 'test' ], 'dummy', ILoadBalancer
::CONN_TRX_AUTOCOMMIT
)
75 return $this->getDatabaseMock();
81 [ DB_MASTER
, [ 'test' ], 'dummy', ILoadBalancer
::CONN_TRX_AUTOCOMMIT
]
84 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
87 public function testDestruct() {
88 $lb = $this->getLoadBalancerMock();
90 $lb->expects( $this->once() )
91 ->method( 'reuseConnection' );
93 $this->innerMethodForTestDestruct( $lb );
96 private function innerMethodForTestDestruct( ILoadBalancer
$lb ) {
97 $ref = $lb->getConnectionRef( DB_REPLICA
);
99 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
102 public function testConstruct_failure() {
103 $this->setExpectedException( InvalidArgumentException
::class, '' );
105 $lb = $this->getLoadBalancerMock();
106 new DBConnRef( $lb, 17 ); // bad constructor argument
109 public function testGetWikiID() {
110 $lb = $this->getMock( ILoadBalancer
::class );
112 // getWikiID is optimized to not create a connection
113 $lb->expects( $this->never() )
114 ->method( 'getConnection' );
116 $ref = new DBConnRef( $lb, [ DB_REPLICA
, [], 'dummy', 0 ] );
118 $this->assertSame( 'dummy', $ref->getWikiID() );
121 public function testGetDomainID() {
122 $lb = $this->getMock( ILoadBalancer
::class );
124 // getDomainID is optimized to not create a connection
125 $lb->expects( $this->never() )
126 ->method( 'getConnection' );
128 $ref = new DBConnRef( $lb, [ DB_REPLICA
, [], 'dummy', 0 ] );
130 $this->assertSame( 'dummy', $ref->getDomainID() );
133 public function testSelect() {
134 // select should get passed through normally
135 $ref = $this->getDBConnRef();
136 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
139 public function testToString() {
140 $ref = $this->getDBConnRef();
141 $this->assertInternalType( 'string', $ref->__toString() );
143 $lb = $this->getLoadBalancerMock();
144 $ref = new DBConnRef( $lb, [ DB_MASTER
, [], 'test', 0 ] );
145 $this->assertInternalType( 'string', $ref->__toString() );