Merge "HISTORY: Add MediaWiki 1.12 post-release change notes"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / rdbms / database / DBConnRefTest.php
1 <?php
2
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;
9
10 /**
11 * @covers Wikimedia\Rdbms\DBConnRef
12 */
13 class DBConnRefTest extends PHPUnit\Framework\TestCase {
14
15 use MediaWikiCoversValidator;
16 use PHPUnit4And6Compat;
17
18 /**
19 * @return ILoadBalancer
20 */
21 private function getLoadBalancerMock() {
22 $lb = $this->getMock( ILoadBalancer::class );
23
24 $lb->method( 'getConnection' )->willReturnCallback(
25 function () {
26 return $this->getDatabaseMock();
27 }
28 );
29
30 $lb->method( 'getConnectionRef' )->willReturnCallback(
31 function () use ( $lb ) {
32 return $this->getDBConnRef( $lb );
33 }
34 );
35
36 return $lb;
37 }
38
39 /**
40 * @return IDatabase
41 */
42 private function getDatabaseMock() {
43 $db = $this->getMockBuilder( Database::class )
44 ->disableOriginalConstructor()
45 ->getMock();
46
47 $open = true;
48 $db->method( 'select' )->willReturnCallback( function () use ( &$open ) {
49 if ( !$open ) {
50 throw new LogicException( "Not open" );
51 }
52
53 return new FakeResultWrapper( [] );
54 } );
55 $db->method( 'close' )->willReturnCallback( function () use ( &$open ) {
56 $open = false;
57
58 return true;
59 } );
60 $db->method( 'isOpen' )->willReturnCallback( function () use ( &$open ) {
61 return $open;
62 } );
63 $db->method( 'open' )->willReturnCallback( function () use ( &$open ) {
64 $open = true;
65
66 return $open;
67 } );
68 $db->method( '__toString' )->willReturn( 'MOCK_DB' );
69
70 return $db;
71 }
72
73 /**
74 * @return IDatabase
75 */
76 private function getDBConnRef( ILoadBalancer $lb = null ) {
77 $lb = $lb ?: $this->getLoadBalancerMock();
78 return new DBConnRef( $lb, $this->getDatabaseMock() );
79 }
80
81 public function testConstruct() {
82 $lb = $this->getLoadBalancerMock();
83 $ref = new DBConnRef( $lb, $this->getDatabaseMock() );
84
85 $this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
86 }
87
88 public function testConstruct_params() {
89 $lb = $this->getMock( ILoadBalancer::class );
90
91 $lb->expects( $this->once() )
92 ->method( 'getConnection' )
93 ->with( DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTOCOMMIT )
94 ->willReturnCallback(
95 function () {
96 return $this->getDatabaseMock();
97 }
98 );
99
100 $ref = new DBConnRef(
101 $lb,
102 [ DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTOCOMMIT ]
103 );
104
105 $this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
106 }
107
108 public function testDestruct() {
109 $lb = $this->getLoadBalancerMock();
110
111 $lb->expects( $this->once() )
112 ->method( 'reuseConnection' );
113
114 $this->innerMethodForTestDestruct( $lb );
115 }
116
117 private function innerMethodForTestDestruct( ILoadBalancer $lb ) {
118 $ref = $lb->getConnectionRef( DB_REPLICA );
119
120 $this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
121 }
122
123 public function testConstruct_failure() {
124 $this->setExpectedException( InvalidArgumentException::class, '' );
125
126 $lb = $this->getLoadBalancerMock();
127 new DBConnRef( $lb, 17 ); // bad constructor argument
128 }
129
130 /**
131 * @covers Wikimedia\Rdbms\DBConnRef::getDomainId
132 */
133 public function testGetDomainID() {
134 $lb = $this->getMock( ILoadBalancer::class );
135
136 // getDomainID is optimized to not create a connection
137 $lb->expects( $this->never() )
138 ->method( 'getConnection' );
139
140 $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ] );
141
142 $this->assertSame( 'dummy', $ref->getDomainID() );
143 }
144
145 /**
146 * @covers Wikimedia\Rdbms\DBConnRef::select
147 */
148 public function testSelect() {
149 // select should get passed through normally
150 $ref = $this->getDBConnRef();
151 $this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
152 }
153
154 public function testToString() {
155 $ref = $this->getDBConnRef();
156 $this->assertInternalType( 'string', $ref->__toString() );
157
158 $lb = $this->getLoadBalancerMock();
159 $ref = new DBConnRef( $lb, [ DB_MASTER, [], 'test', 0 ] );
160 $this->assertInternalType( 'string', $ref->__toString() );
161 }
162
163 /**
164 * @covers Wikimedia\Rdbms\DBConnRef::close
165 * @expectedException \Wikimedia\Rdbms\DBUnexpectedError
166 */
167 public function testClose() {
168 $lb = $this->getLoadBalancerMock();
169 $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ] );
170 $ref->close();
171 }
172 }