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