Debian Codegraph @

tests/phpunit/unit/includes/libs/rdbms/database/DBConnRefTest.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Tests\Rdbms;
4
5 use InvalidArgumentException;
6 use LogicException;
7 use MediaWikiCoversValidator;
8 use PHPUnit\Framework\MockObject\MockObject;
9 use PHPUnit\Framework\TestCase;
10 use Wikimedia\Rdbms\AndExpressionGroup;
11 use Wikimedia\Rdbms\DBConnRef;
12 use Wikimedia\Rdbms\DBReadOnlyRoleError;
13 use Wikimedia\Rdbms\DBUnexpectedError;
14 use Wikimedia\Rdbms\Expression;
15 use Wikimedia\Rdbms\FakeResultWrapper;
16 use Wikimedia\Rdbms\IDatabase;
17 use Wikimedia\Rdbms\ILoadBalancer;
18 use Wikimedia\Rdbms\IResultWrapper;
19 use Wikimedia\Rdbms\OrExpressionGroup;
20
21 /**
22 * @covers \Wikimedia\Rdbms\DBConnRef
23 */
24 class DBConnRefTest extends TestCase {
25
26 use MediaWikiCoversValidator;
27
28 /**
29 * @return ILoadBalancer|MockObject
30 */
31 private function getLoadBalancerMock() {
32 // getConnection() and getConnectionInternal() should keep returning the same connection
33 // on every call, unless that connection was closed. Then they should return a new
34 // connection.
35 $conn = $this->getDatabaseMock();
36 $getDatabaseMock = function () use ( &$conn ) {
37 if ( !$conn->isOpen() ) {
38 $conn = $this->getDatabaseMock();
39 }
40 return $conn;
41 };
42
43 $lb = $this->createMock( ILoadBalancer::class );
44 $lb->method( 'getConnection' )->willReturnCallback( $getDatabaseMock );
45 $lb->method( 'getConnectionInternal' )->willReturnCallback( $getDatabaseMock );
46
47 $lb->method( 'getConnectionRef' )->willReturnCallback(
48 function () use ( $lb ) {
49 return $this->getDBConnRef( $lb );
50 }
51 );
52
53 return $lb;
54 }
55
56 /**
57 * @return IDatabase
58 */
59 private function getDatabaseMock() {
60 $db = $this->createMock( IDatabase::class );
61
62 $open = true;
63 $db->method( 'select' )->willReturnCallback( static function () use ( &$open ) {
64 if ( !$open ) {
65 throw new LogicException( "Not open" );
66 }
67
68 return new FakeResultWrapper( [] );
69 } );
70 $db->method( 'close' )->willReturnCallback( static function () use ( &$open ) {
71 $open = false;
72
73 return true;
74 } );
75 $db->method( 'isOpen' )->willReturnCallback( static function () use ( &$open ) {
76 return $open;
77 } );
78
79 return $db;
80 }
81
82 /**
83 * @param ILoadBalancer|null $lb
84 * @return IDatabase
85 */
86 private function getDBConnRef( ?ILoadBalancer $lb = null ) {
87 $lb ??= $this->getLoadBalancerMock();
88 return new DBConnRef( $lb, [ DB_PRIMARY, [], 'mywiki', 0 ], DB_PRIMARY );
89 }
90
91 /**
92 * Test that bumping the modification counter causes the wrapped connection
93 * to be discarded and re-aquired.
94 */
95 public function testModCount() {
96 $lb = $this->getLoadBalancerMock();
97 $lb->expects( $this->exactly( 3 ) )->method( 'getConnectionInternal' );
98
99 $params = [ DB_PRIMARY, [], 'mywiki', 0 ];
100 $modcount = 0;
101 $ref = new DBConnRef( $lb, $params, DB_PRIMARY, $modcount );
102
103 $ref->select( 'test', '*' );
104 $ref->select( 'test', '*' );
105
106 $modcount++; // cause second call to getConnectionInternal
107 $ref->select( 'test', '*' );
108 $ref->select( 'test', '*' );
109
110 $modcount++; // cause third call to getConnectionInternal
111 $ref->select( 'test', '*' );
112 $ref->select( 'test', '*' );
113 }
114
115 public function testConstruct() {
116 $lb = $this->createMock( ILoadBalancer::class );
117
118 $lb->expects( $this->once() )
119 ->method( 'getConnectionInternal' )
120 ->with( DB_PRIMARY, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTOCOMMIT )
121 ->willReturn( $this->getDatabaseMock() );
122
123 $ref = new DBConnRef(
124 $lb,
125 [ DB_PRIMARY, [ 'test' ], 'dummy', $lb::CONN_TRX_AUTOCOMMIT ],
126 DB_PRIMARY
127 );
128
129 $this->assertInstanceOf( IResultWrapper::class, $ref->select( 'whatever', '*' ) );
130 $this->assertEquals( DB_PRIMARY, $ref->getReferenceRole() );
131
132 $ref2 = new DBConnRef(
133 $lb,
134 [ DB_PRIMARY, [ 'test' ], 'dummy', $lb::CONN_TRX_AUTOCOMMIT ],
135 DB_REPLICA
136 );
137 $this->assertEquals( DB_REPLICA, $ref2->getReferenceRole() );
138 }
139
140 public function testDestruct() {
141 $lb = $this->getLoadBalancerMock();
142
143 $this->innerMethodForTestDestruct( $lb );
144 }
145
146 private function innerMethodForTestDestruct( ILoadBalancer $lb ) {
147 $ref = $lb->getConnection( DB_REPLICA );
148
149 $this->assertInstanceOf( IResultWrapper::class, $ref->select( 'whatever', '*' ) );
150 }
151
152 public function testConstruct_failure() {
153 $this->expectException( InvalidArgumentException::class );
154
155 $lb = $this->getLoadBalancerMock();
156 new DBConnRef( $lb, 17, DB_REPLICA ); // bad constructor argument
157 }
158
159 public function testGetDomainID() {
160 $lb = $this->createMock( ILoadBalancer::class );
161
162 // getDomainID is optimized to not create a connection
163 $lb->expects( $this->never() )
164 ->method( 'getConnection' );
165
166 $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ], DB_REPLICA );
167
168 $this->assertSame( 'dummy', $ref->getDomainID() );
169 }
170
171 public function testSelect() {
172 // select should get passed through normally
173 $ref = $this->getDBConnRef();
174 $this->assertInstanceOf( IResultWrapper::class, $ref->select( 'whatever', '*' ) );
175 }
176
177 public function testExpr() {
178 $ref = $this->getDBConnRef();
179 $this->assertInstanceOf( Expression::class, $ref->expr( 'key', '=', null ) );
180 $this->assertInstanceOf( AndExpressionGroup::class, $ref->andExpr( [ 'key' => null, $ref->expr( 'key', '=', null ) ] ) );
181 $this->assertInstanceOf( OrExpressionGroup::class, $ref->orExpr( [ 'key' => null, $ref->expr( 'key', '=', null ) ] ) );
182 }
183
184 public function testToString() {
185 $ref = $this->getDBConnRef();
186 $this->assertIsString( $ref->__toString() );
187
188 $lb = $this->getLoadBalancerMock();
189 $ref = new DBConnRef( $lb, [ DB_PRIMARY, [], 'test', 0 ], DB_PRIMARY );
190 $this->assertIsString( $ref->__toString() );
191 }
192
193 public function testClose() {
194 $lb = $this->getLoadBalancerMock();
195 $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ], DB_PRIMARY );
196 $this->expectException( DBUnexpectedError::class );
197 $ref->close();
198 }
199
200 public function testGetReferenceRole() {
201 $lb = $this->getLoadBalancerMock();
202 $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ], DB_REPLICA );
203 $this->assertSame( DB_REPLICA, $ref->getReferenceRole() );
204
205 $ref = new DBConnRef( $lb, [ DB_PRIMARY, [], 'dummy', 0 ], DB_PRIMARY );
206 $this->assertSame( DB_PRIMARY, $ref->getReferenceRole() );
207
208 $ref = new DBConnRef( $lb, [ 1, [], 'dummy', 0 ], DB_REPLICA );
209 $this->assertSame( DB_REPLICA, $ref->getReferenceRole() );
210
211 $ref = new DBConnRef( $lb, [ 0, [], 'dummy', 0 ], DB_PRIMARY );
212 $this->assertSame( DB_PRIMARY, $ref->getReferenceRole() );
213 }
214
215 /**
216 * @dataProvider provideRoleExceptions
217 */
218 public function testRoleExceptions( $method, $args ) {
219 $lb = $this->getLoadBalancerMock();
220 $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ], DB_REPLICA );
221 $this->expectException( DBReadOnlyRoleError::class );
222 // T413675 - $ret is unused, but needed for PHP 8.5 and use of #[NoDiscard]
223 $ret = $ref->$method( ...$args );
224 }
225
226 public static function provideRoleExceptions() {
227 return [
228 [ 'insert', [ 'table', [ 'a' => 1 ] ] ],
229 [ 'update', [ 'table', [ 'a' => 1 ], [ 'a' => 2 ] ] ],
230 [ 'delete', [ 'table', [ 'a' => 1 ] ] ],
231 [ 'replace', [ 'table', [ 'a' ], [ 'a' => 1 ] ] ],
232 [ 'upsert', [ 'table', [ 'a' => 1 ], [ 'a' ], [ 'a = a + 1' ] ] ],
233 [ 'lock', [ 'k', 'method' ] ],
234 [ 'unlock', [ 'k', 'method' ] ],
235 [ 'getScopedLockAndFlush', [ 'k', 'method', 1 ] ]
236 ];
237 }
238 }