Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / rdbms / database / DBConnRefTest.php
1 <?php
2
3 use Wikimedia\Rdbms\IDatabase;
4 use Wikimedia\Rdbms\DBConnRef;
5 use Wikimedia\Rdbms\FakeResultWrapper;
6 use Wikimedia\Rdbms\ILoadBalancer;
7 use Wikimedia\Rdbms\IResultWrapper;
8
9 /**
10 * @covers Wikimedia\Rdbms\DBConnRef
11 */
12 class DBConnRefTest extends PHPUnit\Framework\TestCase {
13
14 use MediaWikiCoversValidator;
15 use PHPUnit4And6Compat;
16
17 /**
18 * @return ILoadBalancer
19 */
20 private function getLoadBalancerMock() {
21 $lb = $this->getMock( ILoadBalancer::class );
22
23 $lb->method( 'getConnection' )->willReturnCallback(
24 function () {
25 return $this->getDatabaseMock();
26 }
27 );
28
29 $lb->method( 'getConnectionRef' )->willReturnCallback(
30 function () use ( $lb ) {
31 return $this->getDBConnRef( $lb );
32 }
33 );
34
35 return $lb;
36 }
37
38 /**
39 * @return IDatabase
40 */
41 private function getDatabaseMock() {
42 $db = $this->getMockBuilder( IDatabase::class )
43 ->disableOriginalConstructor()
44 ->getMock();
45
46 $open = true;
47 $db->method( 'select' )->willReturnCallback( function () use ( &$open ) {
48 if ( !$open ) {
49 throw new LogicException( "Not open" );
50 }
51
52 return new FakeResultWrapper( [] );
53 } );
54 $db->method( 'close' )->willReturnCallback( function () use ( &$open ) {
55 $open = false;
56
57 return true;
58 } );
59 $db->method( 'isOpen' )->willReturnCallback( function () use ( &$open ) {
60 return $open;
61 } );
62
63 return $db;
64 }
65
66 /**
67 * @return IDatabase
68 */
69 private function getDBConnRef( ILoadBalancer $lb = null ) {
70 $lb = $lb ?: $this->getLoadBalancerMock();
71 return new DBConnRef( $lb, $this->getDatabaseMock(), DB_MASTER );
72 }
73
74 public function testConstruct() {
75 $lb = $this->getLoadBalancerMock();
76 $ref = new DBConnRef( $lb, $this->getDatabaseMock(), DB_MASTER );
77
78 $this->assertInstanceOf( IResultWrapper::class, $ref->select( 'whatever', '*' ) );
79 }
80
81 public function testConstruct_params() {
82 $lb = $this->getMock( ILoadBalancer::class );
83
84 $lb->expects( $this->once() )
85 ->method( 'getConnection' )
86 ->with( DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTOCOMMIT )
87 ->willReturnCallback(
88 function () {
89 return $this->getDatabaseMock();
90 }
91 );
92
93 $ref = new DBConnRef(
94 $lb,
95 [ DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTOCOMMIT ],
96 DB_MASTER
97 );
98
99 $this->assertInstanceOf( IResultWrapper::class, $ref->select( 'whatever', '*' ) );
100 $this->assertEquals( DB_MASTER, $ref->getReferenceRole() );
101
102 $ref2 = new DBConnRef(
103 $lb,
104 [ DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTOCOMMIT ],
105 DB_REPLICA
106 );
107 $this->assertEquals( DB_REPLICA, $ref2->getReferenceRole() );
108 }
109
110 public function testDestruct() {
111 $lb = $this->getLoadBalancerMock();
112
113 $lb->expects( $this->once() )
114 ->method( 'reuseConnection' );
115
116 $this->innerMethodForTestDestruct( $lb );
117 }
118
119 private function innerMethodForTestDestruct( ILoadBalancer $lb ) {
120 $ref = $lb->getConnectionRef( DB_REPLICA );
121
122 $this->assertInstanceOf( IResultWrapper::class, $ref->select( 'whatever', '*' ) );
123 }
124
125 public function testConstruct_failure() {
126 $this->setExpectedException( InvalidArgumentException::class, '' );
127
128 $lb = $this->getLoadBalancerMock();
129 new DBConnRef( $lb, 17, DB_REPLICA ); // bad constructor argument
130 }
131
132 /**
133 * @covers Wikimedia\Rdbms\DBConnRef::getDomainId
134 */
135 public function testGetDomainID() {
136 $lb = $this->getMock( ILoadBalancer::class );
137
138 // getDomainID is optimized to not create a connection
139 $lb->expects( $this->never() )
140 ->method( 'getConnection' );
141
142 $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ], DB_REPLICA );
143
144 $this->assertSame( 'dummy', $ref->getDomainID() );
145 }
146
147 /**
148 * @covers Wikimedia\Rdbms\DBConnRef::select
149 */
150 public function testSelect() {
151 // select should get passed through normally
152 $ref = $this->getDBConnRef();
153 $this->assertInstanceOf( IResultWrapper::class, $ref->select( 'whatever', '*' ) );
154 }
155
156 public function testToString() {
157 $ref = $this->getDBConnRef();
158 $this->assertInternalType( 'string', $ref->__toString() );
159
160 $lb = $this->getLoadBalancerMock();
161 $ref = new DBConnRef( $lb, [ DB_MASTER, [], 'test', 0 ], DB_MASTER );
162 $this->assertInternalType( 'string', $ref->__toString() );
163 }
164
165 /**
166 * @covers Wikimedia\Rdbms\DBConnRef::close
167 * @expectedException \Wikimedia\Rdbms\DBUnexpectedError
168 */
169 public function testClose() {
170 $lb = $this->getLoadBalancerMock();
171 $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ], DB_MASTER );
172 $ref->close();
173 }
174
175 /**
176 * @covers Wikimedia\Rdbms\DBConnRef::getReferenceRole
177 */
178 public function testGetReferenceRole() {
179 $lb = $this->getLoadBalancerMock();
180 $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ], DB_REPLICA );
181 $this->assertSame( DB_REPLICA, $ref->getReferenceRole() );
182
183 $ref = new DBConnRef( $lb, [ DB_MASTER, [], 'dummy', 0 ], DB_MASTER );
184 $this->assertSame( DB_MASTER, $ref->getReferenceRole() );
185
186 $ref = new DBConnRef( $lb, [ 1, [], 'dummy', 0 ], DB_REPLICA );
187 $this->assertSame( DB_REPLICA, $ref->getReferenceRole() );
188
189 $ref = new DBConnRef( $lb, [ 0, [], 'dummy', 0 ], DB_MASTER );
190 $this->assertSame( DB_MASTER, $ref->getReferenceRole() );
191 }
192
193 /**
194 * @covers Wikimedia\Rdbms\DBConnRef::getReferenceRole
195 * @expectedException Wikimedia\Rdbms\DBReadOnlyRoleError
196 * @dataProvider provideRoleExceptions
197 */
198 public function testRoleExceptions( $method, $args ) {
199 $lb = $this->getLoadBalancerMock();
200 $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ], DB_REPLICA );
201 $ref->$method( ...$args );
202 }
203
204 function provideRoleExceptions() {
205 return [
206 [ 'insert', [ 'table', [ 'a' => 1 ] ] ],
207 [ 'update', [ 'table', [ 'a' => 1 ], [ 'a' => 2 ] ] ],
208 [ 'delete', [ 'table', [ 'a' => 1 ] ] ],
209 [ 'replace', [ 'table', [ 'a' ], [ 'a' => 1 ] ] ],
210 [ 'upsert', [ 'table', [ 'a' => 1 ], [ 'a' ], [ 'a = a + 1' ] ] ],
211 [ 'lock', [ 'k', 'method' ] ],
212 [ 'unlock', [ 'k', 'method' ] ],
213 [ 'getScopedLockAndFlush', [ 'k', 'method', 1 ] ]
214 ];
215 }
216 }