MediaWiki  1.29.2
ConnectionManagerTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use IDatabase;
7 use PHPUnit_Framework_MockObject_MockObject;
9 
16 class ConnectionManagerTest extends \PHPUnit_Framework_TestCase {
17 
21  private function getIDatabaseMock() {
22  return $this->getMockBuilder( IDatabase::class )
23  ->getMock();
24  }
25 
29  private function getLoadBalancerMock() {
30  $lb = $this->getMockBuilder( LoadBalancer::class )
31  ->disableOriginalConstructor()
32  ->getMock();
33 
34  return $lb;
35  }
36 
38  $database = $this->getIDatabaseMock();
39  $lb = $this->getLoadBalancerMock();
40 
41  $lb->expects( $this->once() )
42  ->method( 'getConnection' )
43  ->with( DB_REPLICA, [ 'group1' ], 'someDbName' )
44  ->will( $this->returnValue( $database ) );
45 
46  $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
47  $actual = $manager->getReadConnection();
48 
49  $this->assertSame( $database, $actual );
50  }
51 
53  $database = $this->getIDatabaseMock();
54  $lb = $this->getLoadBalancerMock();
55 
56  $lb->expects( $this->once() )
57  ->method( 'getConnection' )
58  ->with( DB_REPLICA, [ 'group2' ], 'someDbName' )
59  ->will( $this->returnValue( $database ) );
60 
61  $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
62  $actual = $manager->getReadConnection( [ 'group2' ] );
63 
64  $this->assertSame( $database, $actual );
65  }
66 
67  public function testGetWriteConnection() {
68  $database = $this->getIDatabaseMock();
69  $lb = $this->getLoadBalancerMock();
70 
71  $lb->expects( $this->once() )
72  ->method( 'getConnection' )
73  ->with( DB_MASTER, [ 'group1' ], 'someDbName' )
74  ->will( $this->returnValue( $database ) );
75 
76  $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
77  $actual = $manager->getWriteConnection();
78 
79  $this->assertSame( $database, $actual );
80  }
81 
82  public function testReleaseConnection() {
83  $database = $this->getIDatabaseMock();
84  $lb = $this->getLoadBalancerMock();
85 
86  $lb->expects( $this->once() )
87  ->method( 'reuseConnection' )
88  ->with( $database )
89  ->will( $this->returnValue( null ) );
90 
91  $manager = new ConnectionManager( $lb );
92  $manager->releaseConnection( $database );
93  }
94 
96  $database = $this->getIDatabaseMock();
97  $lb = $this->getLoadBalancerMock();
98 
99  $lb->expects( $this->once() )
100  ->method( 'getConnectionRef' )
101  ->with( DB_REPLICA, [ 'group1' ], 'someDbName' )
102  ->will( $this->returnValue( $database ) );
103 
104  $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
105  $actual = $manager->getReadConnectionRef();
106 
107  $this->assertSame( $database, $actual );
108  }
109 
111  $database = $this->getIDatabaseMock();
112  $lb = $this->getLoadBalancerMock();
113 
114  $lb->expects( $this->once() )
115  ->method( 'getConnectionRef' )
116  ->with( DB_REPLICA, [ 'group2' ], 'someDbName' )
117  ->will( $this->returnValue( $database ) );
118 
119  $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
120  $actual = $manager->getReadConnectionRef( [ 'group2' ] );
121 
122  $this->assertSame( $database, $actual );
123  }
124 
125  public function testGetWriteConnectionRef() {
126  $database = $this->getIDatabaseMock();
127  $lb = $this->getLoadBalancerMock();
128 
129  $lb->expects( $this->once() )
130  ->method( 'getConnectionRef' )
131  ->with( DB_MASTER, [ 'group1' ], 'someDbName' )
132  ->will( $this->returnValue( $database ) );
133 
134  $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
135  $actual = $manager->getWriteConnectionRef();
136 
137  $this->assertSame( $database, $actual );
138  }
139 
140 }
Wikimedia\Tests\Rdbms
Definition: ConnectionManagerTest.php:3
Wikimedia\Tests\Rdbms\ConnectionManagerTest\testGetReadConnectionRef_withGroups
testGetReadConnectionRef_withGroups()
Definition: ConnectionManagerTest.php:110
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
Wikimedia\Tests\Rdbms\ConnectionManagerTest
Wikimedia\Rdbms\ConnectionManager.
Definition: ConnectionManagerTest.php:16
Wikimedia\Tests\Rdbms\ConnectionManagerTest\testGetWriteConnectionRef
testGetWriteConnectionRef()
Definition: ConnectionManagerTest.php:125
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
Wikimedia\Tests\Rdbms\ConnectionManagerTest\getLoadBalancerMock
getLoadBalancerMock()
Definition: ConnectionManagerTest.php:29
Wikimedia\Tests\Rdbms\ConnectionManagerTest\testGetReadConnectionRef_nullGroups
testGetReadConnectionRef_nullGroups()
Definition: ConnectionManagerTest.php:95
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
Wikimedia\Tests\Rdbms\ConnectionManagerTest\testGetWriteConnection
testGetWriteConnection()
Definition: ConnectionManagerTest.php:67
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Wikimedia\Tests\Rdbms\ConnectionManagerTest\testGetReadConnection_nullGroups
testGetReadConnection_nullGroups()
Definition: ConnectionManagerTest.php:37
Wikimedia\Rdbms\LoadBalancer
Database connection, tracking, load balancing, and transaction manager for a cluster.
Definition: LoadBalancer.php:41
Wikimedia\Tests\Rdbms\ConnectionManagerTest\testGetReadConnection_withGroups
testGetReadConnection_withGroups()
Definition: ConnectionManagerTest.php:52
Wikimedia\Rdbms\ConnectionManager
Database connection manager.
Definition: ConnectionManager.php:17
Wikimedia\Tests\Rdbms\ConnectionManagerTest\getIDatabaseMock
getIDatabaseMock()
Definition: ConnectionManagerTest.php:21
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
Wikimedia\Tests\Rdbms\ConnectionManagerTest\testReleaseConnection
testReleaseConnection()
Definition: ConnectionManagerTest.php:82