MediaWiki  REL1_31
DBConnRefTest.php
Go to the documentation of this file.
1 <?php
2 
9 
13 class DBConnRefTest extends PHPUnit\Framework\TestCase {
14 
15  use PHPUnit4And6Compat;
16 
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 
41  private function getDatabaseMock() {
42  $db = $this->getMockBuilder( Database::class )
43  ->disableOriginalConstructor()
44  ->getMock();
45 
46  $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
47  $db->method( '__toString' )->willReturn( 'MOCK_DB' );
48 
49  return $db;
50  }
51 
55  private function getDBConnRef( ILoadBalancer $lb = null ) {
56  $lb = $lb ?: $this->getLoadBalancerMock();
57  return new DBConnRef( $lb, $this->getDatabaseMock() );
58  }
59 
60  public function testConstruct() {
61  $lb = $this->getLoadBalancerMock();
62  $ref = new DBConnRef( $lb, $this->getDatabaseMock() );
63 
64  $this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
65  }
66 
67  public function testConstruct_params() {
68  $lb = $this->getMock( ILoadBalancer::class );
69 
70  $lb->expects( $this->once() )
71  ->method( 'getConnection' )
72  ->with( DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTOCOMMIT )
73  ->willReturnCallback(
74  function () {
75  return $this->getDatabaseMock();
76  }
77  );
78 
79  $ref = new DBConnRef(
80  $lb,
81  [ DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTOCOMMIT ]
82  );
83 
84  $this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
85  }
86 
87  public function testDestruct() {
88  $lb = $this->getLoadBalancerMock();
89 
90  $lb->expects( $this->once() )
91  ->method( 'reuseConnection' );
92 
93  $this->innerMethodForTestDestruct( $lb );
94  }
95 
96  private function innerMethodForTestDestruct( ILoadBalancer $lb ) {
97  $ref = $lb->getConnectionRef( DB_REPLICA );
98 
99  $this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
100  }
101 
102  public function testConstruct_failure() {
103  $this->setExpectedException( InvalidArgumentException::class, '' );
104 
105  $lb = $this->getLoadBalancerMock();
106  new DBConnRef( $lb, 17 ); // bad constructor argument
107  }
108 
109  public function testGetWikiID() {
110  $lb = $this->getMock( ILoadBalancer::class );
111 
112  // getWikiID is optimized to not create a connection
113  $lb->expects( $this->never() )
114  ->method( 'getConnection' );
115 
116  $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ] );
117 
118  $this->assertSame( 'dummy', $ref->getWikiID() );
119  }
120 
121  public function testGetDomainID() {
122  $lb = $this->getMock( ILoadBalancer::class );
123 
124  // getDomainID is optimized to not create a connection
125  $lb->expects( $this->never() )
126  ->method( 'getConnection' );
127 
128  $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ] );
129 
130  $this->assertSame( 'dummy', $ref->getDomainID() );
131  }
132 
133  public function testSelect() {
134  // select should get passed through normally
135  $ref = $this->getDBConnRef();
136  $this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
137  }
138 
139  public function testToString() {
140  $ref = $this->getDBConnRef();
141  $this->assertInternalType( 'string', $ref->__toString() );
142 
143  $lb = $this->getLoadBalancerMock();
144  $ref = new DBConnRef( $lb, [ DB_MASTER, [], 'test', 0 ] );
145  $this->assertInternalType( 'string', $ref->__toString() );
146  }
147 
148 }
Wikimedia\Rdbms\Database
Relational database abstraction object.
Definition: Database.php:48
DBConnRefTest\testGetDomainID
testGetDomainID()
Definition: DBConnRefTest.php:121
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
DBConnRefTest\testConstruct_params
testConstruct_params()
Definition: DBConnRefTest.php:67
DBConnRefTest\getDBConnRef
getDBConnRef(ILoadBalancer $lb=null)
Definition: DBConnRefTest.php:55
Wikimedia\Rdbms\ResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: ResultWrapper.php:24
DBConnRefTest\getLoadBalancerMock
getLoadBalancerMock()
Definition: DBConnRefTest.php:20
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:37
DBConnRefTest\testGetWikiID
testGetWikiID()
Definition: DBConnRefTest.php:109
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:96
DBConnRefTest\testSelect
testSelect()
Definition: DBConnRefTest.php:133
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:29
DBConnRefTest\getDatabaseMock
getDatabaseMock()
Definition: DBConnRefTest.php:41
Wikimedia\Rdbms\DBConnRef
Helper class to handle automatically marking connections as reusable (via RAII pattern) as well handl...
Definition: DBConnRef.php:15
DBConnRefTest\testToString
testToString()
Definition: DBConnRefTest.php:139
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:56
DBConnRefTest
Wikimedia\Rdbms\DBConnRef.
Definition: DBConnRefTest.php:13
DBConnRefTest\testConstruct
testConstruct()
Definition: DBConnRefTest.php:60
DBConnRefTest\testDestruct
testDestruct()
Definition: DBConnRefTest.php:87
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:102