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}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
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