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() );
81 public function testConstruct() {
82 $lb = $this->getLoadBalancerMock();
83 $ref = new DBConnRef( $lb, $this->getDatabaseMock() );
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
]
105 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
108 public function testDestruct() {
109 $lb = $this->getLoadBalancerMock();
111 $lb->expects( $this->once() )
112 ->method( 'reuseConnection' );
114 $this->innerMethodForTestDestruct( $lb );
117 private function innerMethodForTestDestruct( ILoadBalancer
$lb ) {
118 $ref = $lb->getConnectionRef( DB_REPLICA
);
120 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
123 public function testConstruct_failure() {
124 $this->setExpectedException( InvalidArgumentException
::class, '' );
126 $lb = $this->getLoadBalancerMock();
127 new DBConnRef( $lb, 17 ); // bad constructor argument
131 * @covers Wikimedia\Rdbms\DBConnRef::getDomainId
133 public function testGetDomainID() {
134 $lb = $this->getMock( ILoadBalancer
::class );
136 // getDomainID is optimized to not create a connection
137 $lb->expects( $this->never() )
138 ->method( 'getConnection' );
140 $ref = new DBConnRef( $lb, [ DB_REPLICA
, [], 'dummy', 0 ] );
142 $this->assertSame( 'dummy', $ref->getDomainID() );
146 * @covers Wikimedia\Rdbms\DBConnRef::select
148 public function testSelect() {
149 // select should get passed through normally
150 $ref = $this->getDBConnRef();
151 $this->assertInstanceOf( ResultWrapper
::class, $ref->select( 'whatever', '*' ) );
154 public function testToString() {
155 $ref = $this->getDBConnRef();
156 $this->assertInternalType( 'string', $ref->__toString() );
158 $lb = $this->getLoadBalancerMock();
159 $ref = new DBConnRef( $lb, [ DB_MASTER
, [], 'test', 0 ] );
160 $this->assertInternalType( 'string', $ref->__toString() );
164 * @covers Wikimedia\Rdbms\DBConnRef::close
165 * @expectedException \Wikimedia\Rdbms\DBUnexpectedError
167 public function testClose() {
168 $lb = $this->getLoadBalancerMock();
169 $ref = new DBConnRef( $lb, [ DB_REPLICA
, [], 'dummy', 0 ] );