3 use Wikimedia\Rdbms\IDatabase
;
4 use Wikimedia\Rdbms\DBConnRef
;
5 use Wikimedia\Rdbms\FakeResultWrapper
;
6 use Wikimedia\Rdbms\ILoadBalancer
;
7 use Wikimedia\Rdbms\IResultWrapper
;
10 * @covers Wikimedia\Rdbms\DBConnRef
12 class DBConnRefTest
extends PHPUnit\Framework\TestCase
{
14 use MediaWikiCoversValidator
;
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( IDatabase
::class )
43 ->disableOriginalConstructor()
47 $db->method( 'select' )->willReturnCallback( function () use ( &$open ) {
49 throw new LogicException( "Not open" );
52 return new FakeResultWrapper( [] );
54 $db->method( 'close' )->willReturnCallback( function () use ( &$open ) {
59 $db->method( 'isOpen' )->willReturnCallback( function () use ( &$open ) {
69 private function getDBConnRef( ILoadBalancer
$lb = null ) {
70 $lb = $lb ?
: $this->getLoadBalancerMock();
71 return new DBConnRef( $lb, $this->getDatabaseMock(), DB_MASTER
);
74 public function testConstruct() {
75 $lb = $this->getLoadBalancerMock();
76 $ref = new DBConnRef( $lb, $this->getDatabaseMock(), DB_MASTER
);
78 $this->assertInstanceOf( IResultWrapper
::class, $ref->select( 'whatever', '*' ) );
81 public function testConstruct_params() {
82 $lb = $this->getMock( ILoadBalancer
::class );
84 $lb->expects( $this->once() )
85 ->method( 'getConnection' )
86 ->with( DB_MASTER
, [ 'test' ], 'dummy', ILoadBalancer
::CONN_TRX_AUTOCOMMIT
)
89 return $this->getDatabaseMock();
95 [ DB_MASTER
, [ 'test' ], 'dummy', ILoadBalancer
::CONN_TRX_AUTOCOMMIT
],
99 $this->assertInstanceOf( IResultWrapper
::class, $ref->select( 'whatever', '*' ) );
100 $this->assertEquals( DB_MASTER
, $ref->getReferenceRole() );
102 $ref2 = new DBConnRef(
104 [ DB_MASTER
, [ 'test' ], 'dummy', ILoadBalancer
::CONN_TRX_AUTOCOMMIT
],
107 $this->assertEquals( DB_REPLICA
, $ref2->getReferenceRole() );
110 public function testDestruct() {
111 $lb = $this->getLoadBalancerMock();
113 $lb->expects( $this->once() )
114 ->method( 'reuseConnection' );
116 $this->innerMethodForTestDestruct( $lb );
119 private function innerMethodForTestDestruct( ILoadBalancer
$lb ) {
120 $ref = $lb->getConnectionRef( DB_REPLICA
);
122 $this->assertInstanceOf( IResultWrapper
::class, $ref->select( 'whatever', '*' ) );
125 public function testConstruct_failure() {
126 $this->setExpectedException( InvalidArgumentException
::class, '' );
128 $lb = $this->getLoadBalancerMock();
129 new DBConnRef( $lb, 17, DB_REPLICA
); // bad constructor argument
133 * @covers Wikimedia\Rdbms\DBConnRef::getDomainId
135 public function testGetDomainID() {
136 $lb = $this->getMock( ILoadBalancer
::class );
138 // getDomainID is optimized to not create a connection
139 $lb->expects( $this->never() )
140 ->method( 'getConnection' );
142 $ref = new DBConnRef( $lb, [ DB_REPLICA
, [], 'dummy', 0 ], DB_REPLICA
);
144 $this->assertSame( 'dummy', $ref->getDomainID() );
148 * @covers Wikimedia\Rdbms\DBConnRef::select
150 public function testSelect() {
151 // select should get passed through normally
152 $ref = $this->getDBConnRef();
153 $this->assertInstanceOf( IResultWrapper
::class, $ref->select( 'whatever', '*' ) );
156 public function testToString() {
157 $ref = $this->getDBConnRef();
158 $this->assertInternalType( 'string', $ref->__toString() );
160 $lb = $this->getLoadBalancerMock();
161 $ref = new DBConnRef( $lb, [ DB_MASTER
, [], 'test', 0 ], DB_MASTER
);
162 $this->assertInternalType( 'string', $ref->__toString() );
166 * @covers Wikimedia\Rdbms\DBConnRef::close
167 * @expectedException \Wikimedia\Rdbms\DBUnexpectedError
169 public function testClose() {
170 $lb = $this->getLoadBalancerMock();
171 $ref = new DBConnRef( $lb, [ DB_REPLICA
, [], 'dummy', 0 ], DB_MASTER
);
176 * @covers Wikimedia\Rdbms\DBConnRef::getReferenceRole
178 public function testGetReferenceRole() {
179 $lb = $this->getLoadBalancerMock();
180 $ref = new DBConnRef( $lb, [ DB_REPLICA
, [], 'dummy', 0 ], DB_REPLICA
);
181 $this->assertSame( DB_REPLICA
, $ref->getReferenceRole() );
183 $ref = new DBConnRef( $lb, [ DB_MASTER
, [], 'dummy', 0 ], DB_MASTER
);
184 $this->assertSame( DB_MASTER
, $ref->getReferenceRole() );
186 $ref = new DBConnRef( $lb, [ 1, [], 'dummy', 0 ], DB_REPLICA
);
187 $this->assertSame( DB_REPLICA
, $ref->getReferenceRole() );
189 $ref = new DBConnRef( $lb, [ 0, [], 'dummy', 0 ], DB_MASTER
);
190 $this->assertSame( DB_MASTER
, $ref->getReferenceRole() );
194 * @covers Wikimedia\Rdbms\DBConnRef::getReferenceRole
195 * @expectedException Wikimedia\Rdbms\DBReadOnlyRoleError
196 * @dataProvider provideRoleExceptions
198 public function testRoleExceptions( $method, $args ) {
199 $lb = $this->getLoadBalancerMock();
200 $ref = new DBConnRef( $lb, [ DB_REPLICA
, [], 'dummy', 0 ], DB_REPLICA
);
201 $ref->$method( ...$args );
204 function provideRoleExceptions() {
206 [ 'insert', [ 'table', [ 'a' => 1 ] ] ],
207 [ 'update', [ 'table', [ 'a' => 1 ], [ 'a' => 2 ] ] ],
208 [ 'delete', [ 'table', [ 'a' => 1 ] ] ],
209 [ 'replace', [ 'table', [ 'a' ], [ 'a' => 1 ] ] ],
210 [ 'upsert', [ 'table', [ 'a' => 1 ], [ 'a' ], [ 'a = a + 1' ] ] ],
211 [ 'lock', [ 'k', 'method' ] ],
212 [ 'unlock', [ 'k', 'method' ] ],
213 [ 'getScopedLockAndFlush', [ 'k', 'method', 1 ] ]