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
{
16 * @return ILoadBalancer
18 private function getLoadBalancerMock() {
19 $lb = $this->getMock( ILoadBalancer
::class );
21 $lb->method( 'getConnection' )->willReturnCallback(
23 return $this->getDatabaseMock();
27 $lb->method( 'getConnectionRef' )->willReturnCallback(
28 function () use ( $lb ) {
29 return $this->getDBConnRef( $lb );
39 private function getDatabaseMock() {
40 $db = $this->getMockBuilder( Database
::class )
41 ->disableOriginalConstructor()
44 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
45 $db->method( '__toString' )->willReturn( 'MOCK_DB' );
53 private function getDBConnRef( ILoadBalancer
$lb = null ) {
54 $lb = $lb ?
: $this->getLoadBalancerMock();
55 return new DBConnRef( $lb, $this->getDatabaseMock() );
58 public function testConstruct() {
59 $lb = $this->getLoadBalancerMock();
60 $ref = new DBConnRef( $lb, $this->getDatabaseMock() );
62 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
65 public function testConstruct_params() {
66 $lb = $this->getMock( ILoadBalancer
::class );
68 $lb->expects( $this->once() )
69 ->method( 'getConnection' )
70 ->with( DB_MASTER
, [ 'test' ], 'dummy', ILoadBalancer
::CONN_TRX_AUTO
)
73 return $this->getDatabaseMock();
79 [ DB_MASTER
, [ 'test' ], 'dummy', ILoadBalancer
::CONN_TRX_AUTO
]
82 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
85 public function testDestruct() {
86 $lb = $this->getLoadBalancerMock();
88 $lb->expects( $this->once() )
89 ->method( 'reuseConnection' );
91 $this->innerMethodForTestDestruct( $lb );
94 private function innerMethodForTestDestruct( ILoadBalancer
$lb ) {
95 $ref = $lb->getConnectionRef( DB_REPLICA
);
97 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
100 public function testConstruct_failure() {
101 $this->setExpectedException( InvalidArgumentException
::class, '' );
103 $lb = $this->getLoadBalancerMock();
104 new DBConnRef( $lb, 17 ); // bad constructor argument
107 public function testGetWikiID() {
108 $lb = $this->getMock( ILoadBalancer
::class );
110 // getWikiID is optimized to not create a connection
111 $lb->expects( $this->never() )
112 ->method( 'getConnection' );
114 $ref = new DBConnRef( $lb, [ DB_REPLICA
, [], 'dummy', 0 ] );
116 $this->assertSame( 'dummy', $ref->getWikiID() );
119 public function testGetDomainID() {
120 $lb = $this->getMock( ILoadBalancer
::class );
122 // getDomainID is optimized to not create a connection
123 $lb->expects( $this->never() )
124 ->method( 'getConnection' );
126 $ref = new DBConnRef( $lb, [ DB_REPLICA
, [], 'dummy', 0 ] );
128 $this->assertSame( 'dummy', $ref->getDomainID() );
131 public function testSelect() {
132 // select should get passed through normally
133 $ref = $this->getDBConnRef();
134 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
137 public function testToString() {
138 $ref = $this->getDBConnRef();
139 $this->assertInternalType( 'string', $ref->__toString() );
141 $lb = $this->getLoadBalancerMock();
142 $ref = new DBConnRef( $lb, [ DB_MASTER
, [], 'test', 0 ] );
143 $this->assertInternalType( 'string', $ref->__toString() );