Separate MediaWiki unit and integration tests
[lhc/web/wiklou.git] / tests / phpunit / unit / 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(), DB_MASTER );
79 }
80
81 public function testConstruct() {
82 $lb = $this->getLoadBalancerMock();
83 $ref = new DBConnRef( $lb, $this->getDatabaseMock(), DB_MASTER );
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 DB_MASTER
104 );
105
106 $this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
107 $this->assertEquals( DB_MASTER, $ref->getReferenceRole() );
108
109 $ref2 = new DBConnRef(
110 $lb,
111 [ DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTOCOMMIT ],
112 DB_REPLICA
113 );
114 $this->assertEquals( DB_REPLICA, $ref2->getReferenceRole() );
115 }
116
117 public function testDestruct() {
118 $lb = $this->getLoadBalancerMock();
119
120 $lb->expects( $this->once() )
121 ->method( 'reuseConnection' );
122
123 $this->innerMethodForTestDestruct( $lb );
124 }
125
126 private function innerMethodForTestDestruct( ILoadBalancer $lb ) {
127 $ref = $lb->getConnectionRef( DB_REPLICA );
128
129 $this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
130 }
131
132 public function testConstruct_failure() {
133 $this->setExpectedException( InvalidArgumentException::class, '' );
134
135 $lb = $this->getLoadBalancerMock();
136 new DBConnRef( $lb, 17, DB_REPLICA ); // bad constructor argument
137 }
138
139 /**
140 * @covers Wikimedia\Rdbms\DBConnRef::getDomainId
141 */
142 public function testGetDomainID() {
143 $lb = $this->getMock( ILoadBalancer::class );
144
145 // getDomainID is optimized to not create a connection
146 $lb->expects( $this->never() )
147 ->method( 'getConnection' );
148
149 $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ], DB_REPLICA );
150
151 $this->assertSame( 'dummy', $ref->getDomainID() );
152 }
153
154 /**
155 * @covers Wikimedia\Rdbms\DBConnRef::select
156 */
157 public function testSelect() {
158 // select should get passed through normally
159 $ref = $this->getDBConnRef();
160 $this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
161 }
162
163 public function testToString() {
164 $ref = $this->getDBConnRef();
165 $this->assertInternalType( 'string', $ref->__toString() );
166
167 $lb = $this->getLoadBalancerMock();
168 $ref = new DBConnRef( $lb, [ DB_MASTER, [], 'test', 0 ], DB_MASTER );
169 $this->assertInternalType( 'string', $ref->__toString() );
170 }
171
172 /**
173 * @covers Wikimedia\Rdbms\DBConnRef::close
174 * @expectedException \Wikimedia\Rdbms\DBUnexpectedError
175 */
176 public function testClose() {
177 $lb = $this->getLoadBalancerMock();
178 $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ], DB_MASTER );
179 $ref->close();
180 }
181
182 /**
183 * @covers Wikimedia\Rdbms\DBConnRef::getReferenceRole
184 */
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() );
189
190 $ref = new DBConnRef( $lb, [ DB_MASTER, [], 'dummy', 0 ], DB_MASTER );
191 $this->assertSame( DB_MASTER, $ref->getReferenceRole() );
192
193 $ref = new DBConnRef( $lb, [ 1, [], 'dummy', 0 ], DB_REPLICA );
194 $this->assertSame( DB_REPLICA, $ref->getReferenceRole() );
195
196 $ref = new DBConnRef( $lb, [ 0, [], 'dummy', 0 ], DB_MASTER );
197 $this->assertSame( DB_MASTER, $ref->getReferenceRole() );
198 }
199
200 /**
201 * @covers Wikimedia\Rdbms\DBConnRef::getReferenceRole
202 * @expectedException Wikimedia\Rdbms\DBReadOnlyRoleError
203 * @dataProvider provideRoleExceptions
204 */
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 );
209 }
210
211 function provideRoleExceptions() {
212 return [
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 ] ]
221 ];
222 }
223 }