File: DBConnRefTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (237 lines) | stat: -rw-r--r-- 7,137 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php

namespace Wikimedia\Tests\Rdbms;

use InvalidArgumentException;
use LogicException;
use MediaWikiCoversValidator;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Wikimedia\Rdbms\AndExpressionGroup;
use Wikimedia\Rdbms\DBConnRef;
use Wikimedia\Rdbms\DBReadOnlyRoleError;
use Wikimedia\Rdbms\DBUnexpectedError;
use Wikimedia\Rdbms\Expression;
use Wikimedia\Rdbms\FakeResultWrapper;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\ILoadBalancer;
use Wikimedia\Rdbms\IResultWrapper;
use Wikimedia\Rdbms\OrExpressionGroup;

/**
 * @covers \Wikimedia\Rdbms\DBConnRef
 */
class DBConnRefTest extends TestCase {

	use MediaWikiCoversValidator;

	/**
	 * @return ILoadBalancer|MockObject
	 */
	private function getLoadBalancerMock() {
		// getConnection() and getConnectionInternal() should keep returning the same connection
		// on every call, unless that connection was closed. Then they should return a new
		// connection.
		$conn = $this->getDatabaseMock();
		$getDatabaseMock = function () use ( &$conn ) {
			if ( !$conn->isOpen() ) {
				$conn = $this->getDatabaseMock();
			}
			return $conn;
		};

		$lb = $this->createMock( ILoadBalancer::class );
		$lb->method( 'getConnection' )->willReturnCallback( $getDatabaseMock );
		$lb->method( 'getConnectionInternal' )->willReturnCallback( $getDatabaseMock );

		$lb->method( 'getConnectionRef' )->willReturnCallback(
			function () use ( $lb ) {
				return $this->getDBConnRef( $lb );
			}
		);

		return $lb;
	}

	/**
	 * @return IDatabase
	 */
	private function getDatabaseMock() {
		$db = $this->createMock( IDatabase::class );

		$open = true;
		$db->method( 'select' )->willReturnCallback( static function () use ( &$open ) {
			if ( !$open ) {
				throw new LogicException( "Not open" );
			}

			return new FakeResultWrapper( [] );
		} );
		$db->method( 'close' )->willReturnCallback( static function () use ( &$open ) {
			$open = false;

			return true;
		} );
		$db->method( 'isOpen' )->willReturnCallback( static function () use ( &$open ) {
			return $open;
		} );

		return $db;
	}

	/**
	 * @param ILoadBalancer|null $lb
	 * @return IDatabase
	 */
	private function getDBConnRef( ?ILoadBalancer $lb = null ) {
		$lb ??= $this->getLoadBalancerMock();
		return new DBConnRef( $lb, [ DB_PRIMARY, [], 'mywiki', 0 ], DB_PRIMARY );
	}

	/**
	 * Test that bumping the modification counter causes the wrapped connection
	 * to be discarded and re-aquired.
	 */
	public function testModCount() {
		$lb = $this->getLoadBalancerMock();
		$lb->expects( $this->exactly( 3 ) )->method( 'getConnectionInternal' );

		$params = [ DB_PRIMARY, [], 'mywiki', 0 ];
		$modcount = 0;
		$ref = new DBConnRef( $lb, $params, DB_PRIMARY, $modcount );

		$ref->select( 'test', '*' );
		$ref->select( 'test', '*' );

		$modcount++; // cause second call to getConnectionInternal
		$ref->select( 'test', '*' );
		$ref->select( 'test', '*' );

		$modcount++; // cause third call to getConnectionInternal
		$ref->select( 'test', '*' );
		$ref->select( 'test', '*' );
	}

	public function testConstruct() {
		$lb = $this->createMock( ILoadBalancer::class );

		$lb->expects( $this->once() )
			->method( 'getConnectionInternal' )
			->with( DB_PRIMARY, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTOCOMMIT )
			->willReturn( $this->getDatabaseMock() );

		$ref = new DBConnRef(
			$lb,
			[ DB_PRIMARY, [ 'test' ], 'dummy', $lb::CONN_TRX_AUTOCOMMIT ],
			DB_PRIMARY
		);

		$this->assertInstanceOf( IResultWrapper::class, $ref->select( 'whatever', '*' ) );
		$this->assertEquals( DB_PRIMARY, $ref->getReferenceRole() );

		$ref2 = new DBConnRef(
			$lb,
			[ DB_PRIMARY, [ 'test' ], 'dummy', $lb::CONN_TRX_AUTOCOMMIT ],
			DB_REPLICA
		);
		$this->assertEquals( DB_REPLICA, $ref2->getReferenceRole() );
	}

	public function testDestruct() {
		$lb = $this->getLoadBalancerMock();

		$this->innerMethodForTestDestruct( $lb );
	}

	private function innerMethodForTestDestruct( ILoadBalancer $lb ) {
		$ref = $lb->getConnection( DB_REPLICA );

		$this->assertInstanceOf( IResultWrapper::class, $ref->select( 'whatever', '*' ) );
	}

	public function testConstruct_failure() {
		$this->expectException( InvalidArgumentException::class );

		$lb = $this->getLoadBalancerMock();
		new DBConnRef( $lb, 17, DB_REPLICA ); // bad constructor argument
	}

	public function testGetDomainID() {
		$lb = $this->createMock( ILoadBalancer::class );

		// getDomainID is optimized to not create a connection
		$lb->expects( $this->never() )
			->method( 'getConnection' );

		$ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ], DB_REPLICA );

		$this->assertSame( 'dummy', $ref->getDomainID() );
	}

	public function testSelect() {
		// select should get passed through normally
		$ref = $this->getDBConnRef();
		$this->assertInstanceOf( IResultWrapper::class, $ref->select( 'whatever', '*' ) );
	}

	public function testExpr() {
		$ref = $this->getDBConnRef();
		$this->assertInstanceOf( Expression::class, $ref->expr( 'key', '=', null ) );
		$this->assertInstanceOf( AndExpressionGroup::class, $ref->andExpr( [ 'key' => null, $ref->expr( 'key', '=', null ) ] ) );
		$this->assertInstanceOf( OrExpressionGroup::class, $ref->orExpr( [ 'key' => null, $ref->expr( 'key', '=', null ) ] ) );
	}

	public function testToString() {
		$ref = $this->getDBConnRef();
		$this->assertIsString( $ref->__toString() );

		$lb = $this->getLoadBalancerMock();
		$ref = new DBConnRef( $lb, [ DB_PRIMARY, [], 'test', 0 ], DB_PRIMARY );
		$this->assertIsString( $ref->__toString() );
	}

	public function testClose() {
		$lb = $this->getLoadBalancerMock();
		$ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ], DB_PRIMARY );
		$this->expectException( DBUnexpectedError::class );
		$ref->close();
	}

	public function testGetReferenceRole() {
		$lb = $this->getLoadBalancerMock();
		$ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ], DB_REPLICA );
		$this->assertSame( DB_REPLICA, $ref->getReferenceRole() );

		$ref = new DBConnRef( $lb, [ DB_PRIMARY, [], 'dummy', 0 ], DB_PRIMARY );
		$this->assertSame( DB_PRIMARY, $ref->getReferenceRole() );

		$ref = new DBConnRef( $lb, [ 1, [], 'dummy', 0 ], DB_REPLICA );
		$this->assertSame( DB_REPLICA, $ref->getReferenceRole() );

		$ref = new DBConnRef( $lb, [ 0, [], 'dummy', 0 ], DB_PRIMARY );
		$this->assertSame( DB_PRIMARY, $ref->getReferenceRole() );
	}

	/**
	 * @dataProvider provideRoleExceptions
	 */
	public function testRoleExceptions( $method, $args ) {
		$lb = $this->getLoadBalancerMock();
		$ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ], DB_REPLICA );
		$this->expectException( DBReadOnlyRoleError::class );
		$ref->$method( ...$args );
	}

	public static function provideRoleExceptions() {
		return [
			[ 'insert', [ 'table', [ 'a' => 1 ] ] ],
			[ 'update', [ 'table', [ 'a' => 1 ], [ 'a' => 2 ] ] ],
			[ 'delete', [ 'table', [ 'a' => 1 ] ] ],
			[ 'replace', [ 'table', [ 'a' ], [ 'a' => 1 ] ] ],
			[ 'upsert', [ 'table', [ 'a' => 1 ], [ 'a' ], [ 'a = a + 1' ] ] ],
			[ 'lock', [ 'k', 'method' ] ],
			[ 'unlock', [ 'k', 'method' ] ],
			[ 'getScopedLockAndFlush', [ 'k', 'method', 1 ] ]
		];
	}
}