MediaWiki  1.33.0
DBConnRefTest.php
Go to the documentation of this file.
1 <?php
2 
9 
13 class 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 ],
103  DB_MASTER
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 ],
112  DB_REPLICA
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 
126  private function innerMethodForTestDestruct( ILoadBalancer $lb ) {
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 }
Wikimedia\Rdbms\Database
Relational database abstraction object.
Definition: Database.php:48
DBConnRefTest\provideRoleExceptions
provideRoleExceptions()
Definition: DBConnRefTest.php:211
DBConnRefTest\testGetDomainID
testGetDomainID()
Wikimedia\Rdbms\DBConnRef::getDomainId.
Definition: DBConnRefTest.php:142
DBConnRefTest\testConstruct_params
testConstruct_params()
Definition: DBConnRefTest.php:88
DBConnRefTest\testGetReferenceRole
testGetReferenceRole()
Wikimedia\Rdbms\DBConnRef::getReferenceRole.
Definition: DBConnRefTest.php:185
DBConnRefTest\getDBConnRef
getDBConnRef(ILoadBalancer $lb=null)
Definition: DBConnRefTest.php:76
Wikimedia\Rdbms\ResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: ResultWrapper.php:24
DBConnRefTest\getLoadBalancerMock
getLoadBalancerMock()
Definition: DBConnRefTest.php:21
Wikimedia\Rdbms\FakeResultWrapper
Overloads the relevant methods of the real ResultsWrapper so it doesn't go anywhere near an actual da...
Definition: FakeResultWrapper.php:11
Wikimedia\Rdbms\ILoadBalancer\getConnectionRef
getConnectionRef( $i, $groups=[], $domain=false, $flags=0)
Get a database connection handle reference.
php
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:35
DBConnRefTest\testClose
testClose()
Wikimedia\Rdbms\DBConnRef::close \Wikimedia\Rdbms\DBUnexpectedError.
Definition: DBConnRefTest.php:176
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
DBConnRefTest\innerMethodForTestDestruct
innerMethodForTestDestruct(ILoadBalancer $lb)
Definition: DBConnRefTest.php:126
DBConnRefTest\testSelect
testSelect()
Wikimedia\Rdbms\DBConnRef::select.
Definition: DBConnRefTest.php:157
use
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
Definition: MIT-LICENSE.txt:10
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
DBConnRefTest\getDatabaseMock
getDatabaseMock()
Definition: DBConnRefTest.php:42
Wikimedia\Rdbms\DBConnRef
Helper class to handle automatically marking connections as reusable (via RAII pattern) as well handl...
Definition: DBConnRef.php:14
DBConnRefTest\testToString
testToString()
Definition: DBConnRefTest.php:163
$args
if( $line===false) $args
Definition: cdb.php:64
DBConnRefTest\testRoleExceptions
testRoleExceptions( $method, $args)
Wikimedia\Rdbms\DBConnRef::getReferenceRole Wikimedia\Rdbms\DBReadOnlyRoleError provideRoleExceptions...
Definition: DBConnRefTest.php:205
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
DBConnRefTest
Wikimedia\Rdbms\DBConnRef.
Definition: DBConnRefTest.php:13
DBConnRefTest\testConstruct
testConstruct()
Definition: DBConnRefTest.php:81
DBConnRefTest\testDestruct
testDestruct()
Definition: DBConnRefTest.php:117
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:78
DBConnRefTest\testConstruct_failure
testConstruct_failure()
Definition: DBConnRefTest.php:132