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()
48 $db->method( 'select' )->willReturnCallback( function () use ( &$open ) {
50 throw new LogicException( "Not open" );
53 return new FakeResultWrapper( [] );
55 $db->method( 'close' )->willReturnCallback( function () use ( &$open ) {
60 $db->method( 'isOpen' )->willReturnCallback( function () use ( &$open ) {
63 $db->method( 'open' )->willReturnCallback( function () use ( &$open ) {
68 $db->method( '__toString' )->willReturn( 'MOCK_DB' );
76 private function getDBConnRef( ILoadBalancer
$lb = null ) {
77 $lb = $lb ?
: $this->getLoadBalancerMock();
78 return new DBConnRef( $lb, $this->getDatabaseMock(), DB_MASTER
);
81 public function testConstruct() {
82 $lb = $this->getLoadBalancerMock();
83 $ref = new DBConnRef( $lb, $this->getDatabaseMock(), DB_MASTER
);
85 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
88 public function testConstruct_params() {
89 $lb = $this->getMock( ILoadBalancer
::class );
91 $lb->expects( $this->once() )
92 ->method( 'getConnection' )
93 ->with( DB_MASTER
, [ 'test' ], 'dummy', ILoadBalancer
::CONN_TRX_AUTOCOMMIT
)
96 return $this->getDatabaseMock();
100 $ref = new DBConnRef(
102 [ DB_MASTER
, [ 'test' ], 'dummy', ILoadBalancer
::CONN_TRX_AUTOCOMMIT
],
106 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
107 $this->assertEquals( DB_MASTER
, $ref->getReferenceRole() );
109 $ref2 = new DBConnRef(
111 [ DB_MASTER
, [ 'test' ], 'dummy', ILoadBalancer
::CONN_TRX_AUTOCOMMIT
],
114 $this->assertEquals( DB_REPLICA
, $ref2->getReferenceRole() );
117 public function testDestruct() {
118 $lb = $this->getLoadBalancerMock();
120 $lb->expects( $this->once() )
121 ->method( 'reuseConnection' );
123 $this->innerMethodForTestDestruct( $lb );
126 private function innerMethodForTestDestruct( ILoadBalancer
$lb ) {
127 $ref = $lb->getConnectionRef( DB_REPLICA
);
129 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
132 public function testConstruct_failure() {
133 $this->setExpectedException( InvalidArgumentException
::class, '' );
135 $lb = $this->getLoadBalancerMock();
136 new DBConnRef( $lb, 17, DB_REPLICA
); // bad constructor argument
140 * @covers Wikimedia\Rdbms\DBConnRef::getDomainId
142 public function testGetDomainID() {
143 $lb = $this->getMock( ILoadBalancer
::class );
145 // getDomainID is optimized to not create a connection
146 $lb->expects( $this->never() )
147 ->method( 'getConnection' );
149 $ref = new DBConnRef( $lb, [ DB_REPLICA
, [], 'dummy', 0 ], DB_REPLICA
);
151 $this->assertSame( 'dummy', $ref->getDomainID() );
155 * @covers Wikimedia\Rdbms\DBConnRef::select
157 public function testSelect() {
158 // select should get passed through normally
159 $ref = $this->getDBConnRef();
160 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
163 public function testToString() {
164 $ref = $this->getDBConnRef();
165 $this->assertInternalType( 'string', $ref->__toString() );
167 $lb = $this->getLoadBalancerMock();
168 $ref = new DBConnRef( $lb, [ DB_MASTER
, [], 'test', 0 ], DB_MASTER
);
169 $this->assertInternalType( 'string', $ref->__toString() );
173 * @covers Wikimedia\Rdbms\DBConnRef::close
174 * @expectedException \Wikimedia\Rdbms\DBUnexpectedError
176 public function testClose() {
177 $lb = $this->getLoadBalancerMock();
178 $ref = new DBConnRef( $lb, [ DB_REPLICA
, [], 'dummy', 0 ], DB_MASTER
);
183 * @covers Wikimedia\Rdbms\DBConnRef::getReferenceRole
185 public function testGetReferenceRole() {
186 $lb = $this->getLoadBalancerMock();
187 $ref = new DBConnRef( $lb, [ DB_REPLICA
, [], 'dummy', 0 ], DB_REPLICA
);
188 $this->assertSame( DB_REPLICA
, $ref->getReferenceRole() );
190 $ref = new DBConnRef( $lb, [ DB_MASTER
, [], 'dummy', 0 ], DB_MASTER
);
191 $this->assertSame( DB_MASTER
, $ref->getReferenceRole() );
193 $ref = new DBConnRef( $lb, [ 1, [], 'dummy', 0 ], DB_REPLICA
);
194 $this->assertSame( DB_REPLICA
, $ref->getReferenceRole() );
196 $ref = new DBConnRef( $lb, [ 0, [], 'dummy', 0 ], DB_MASTER
);
197 $this->assertSame( DB_MASTER
, $ref->getReferenceRole() );
201 * @covers Wikimedia\Rdbms\DBConnRef::getReferenceRole
202 * @expectedException Wikimedia\Rdbms\DBReadOnlyRoleError
203 * @dataProvider provideRoleExceptions
205 public function testRoleExceptions( $method, $args ) {
206 $lb = $this->getLoadBalancerMock();
207 $ref = new DBConnRef( $lb, [ DB_REPLICA
, [], 'dummy', 0 ], DB_REPLICA
);
208 $ref->$method( ...$args );
211 function provideRoleExceptions() {
213 [ 'insert', [ 'table', [ 'a' => 1 ] ] ],
214 [ 'update', [ 'table', [ 'a' => 1 ], [ 'a' => 2 ] ] ],
215 [ 'delete', [ 'table', [ 'a' => 1 ] ] ],
216 [ 'replace', [ 'table', [ 'a' ], [ 'a' => 1 ] ] ],
217 [ 'upsert', [ 'table', [ 'a' => 1 ], [ 'a' ], [ 'a = a + 1' ] ] ],
218 [ 'lock', [ 'k', 'method' ] ],
219 [ 'unlock', [ 'k', 'method' ] ],
220 [ 'getScopedLockAndFlush', [ 'k', 'method', 1 ] ]