MediaWiki REL1_33
DBConnRefTest.php
Go to the documentation of this file.
1<?php
2
9
13class DBConnRefTest extends PHPUnit\Framework\TestCase {
14
15 use MediaWikiCoversValidator;
16 use PHPUnit4And6Compat;
17
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
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
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 ],
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 ],
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
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
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
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
176 public function testClose() {
177 $lb = $this->getLoadBalancerMock();
178 $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ], DB_MASTER );
179 $ref->close();
180 }
181
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
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
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}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
if( $line===false) $args
Definition cdb.php:64
Wikimedia\Rdbms\DBConnRef.
testClose()
Wikimedia\Rdbms\DBConnRef::close \Wikimedia\Rdbms\DBUnexpectedError.
getDBConnRef(ILoadBalancer $lb=null)
testSelect()
Wikimedia\Rdbms\DBConnRef::select.
testRoleExceptions( $method, $args)
Wikimedia\Rdbms\DBConnRef::getReferenceRole Wikimedia\Rdbms\DBReadOnlyRoleError provideRoleExceptions...
testGetDomainID()
Wikimedia\Rdbms\DBConnRef::getDomainId.
testGetReferenceRole()
Wikimedia\Rdbms\DBConnRef::getReferenceRole.
innerMethodForTestDestruct(ILoadBalancer $lb)
Helper class to handle automatically marking connections as reusable (via RAII pattern) as well handl...
Definition DBConnRef.php:14
Relational database abstraction object.
Definition Database.php:49
Overloads the relevant methods of the real ResultsWrapper so it doesn't go anywhere near an actual da...
Result wrapper for grabbing data queried from an IDatabase object.
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
Database cluster connection, tracking, load balancing, and transaction manager interface.
getConnectionRef( $i, $groups=[], $domain=false, $flags=0)
Get a database connection handle reference.
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26