Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / libs / rdbms / connectionmanager / ConnectionManagerTest.php
blob1677851736d1ea275c7905520a041ba3022d2cd1
1 <?php
3 namespace Wikimedia\Tests\Rdbms;
5 use IDatabase;
6 use LoadBalancer;
7 use PHPUnit_Framework_MockObject_MockObject;
8 use Wikimedia\Rdbms\ConnectionManager;
10 /**
11 * @covers Wikimedia\Rdbms\ConnectionManager
13 * @license GPL-2.0+
14 * @author Daniel Kinzler
16 class ConnectionManagerTest extends \PHPUnit_Framework_TestCase {
18 /**
19 * @return IDatabase|PHPUnit_Framework_MockObject_MockObject
21 private function getIDatabaseMock() {
22 return $this->getMock( IDatabase::class );
25 /**
26 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
28 private function getLoadBalancerMock() {
29 $lb = $this->getMockBuilder( LoadBalancer::class )
30 ->disableOriginalConstructor()
31 ->getMock();
33 return $lb;
36 public function testGetReadConnection_nullGroups() {
37 $database = $this->getIDatabaseMock();
38 $lb = $this->getLoadBalancerMock();
40 $lb->expects( $this->once() )
41 ->method( 'getConnection' )
42 ->with( DB_REPLICA, [ 'group1' ], 'someDbName' )
43 ->will( $this->returnValue( $database ) );
45 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
46 $actual = $manager->getReadConnection();
48 $this->assertSame( $database, $actual );
51 public function testGetReadConnection_withGroups() {
52 $database = $this->getIDatabaseMock();
53 $lb = $this->getLoadBalancerMock();
55 $lb->expects( $this->once() )
56 ->method( 'getConnection' )
57 ->with( DB_REPLICA, [ 'group2' ], 'someDbName' )
58 ->will( $this->returnValue( $database ) );
60 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
61 $actual = $manager->getReadConnection( [ 'group2' ] );
63 $this->assertSame( $database, $actual );
66 public function testGetWriteConnection() {
67 $database = $this->getIDatabaseMock();
68 $lb = $this->getLoadBalancerMock();
70 $lb->expects( $this->once() )
71 ->method( 'getConnection' )
72 ->with( DB_MASTER, [ 'group1' ], 'someDbName' )
73 ->will( $this->returnValue( $database ) );
75 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
76 $actual = $manager->getWriteConnection();
78 $this->assertSame( $database, $actual );
81 public function testReleaseConnection() {
82 $database = $this->getIDatabaseMock();
83 $lb = $this->getLoadBalancerMock();
85 $lb->expects( $this->once() )
86 ->method( 'reuseConnection' )
87 ->with( $database )
88 ->will( $this->returnValue( null ) );
90 $manager = new ConnectionManager( $lb );
91 $manager->releaseConnection( $database );
94 public function testGetReadConnectionRef_nullGroups() {
95 $database = $this->getIDatabaseMock();
96 $lb = $this->getLoadBalancerMock();
98 $lb->expects( $this->once() )
99 ->method( 'getConnectionRef' )
100 ->with( DB_REPLICA, [ 'group1' ], 'someDbName' )
101 ->will( $this->returnValue( $database ) );
103 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
104 $actual = $manager->getReadConnectionRef();
106 $this->assertSame( $database, $actual );
109 public function testGetReadConnectionRef_withGroups() {
110 $database = $this->getIDatabaseMock();
111 $lb = $this->getLoadBalancerMock();
113 $lb->expects( $this->once() )
114 ->method( 'getConnectionRef' )
115 ->with( DB_REPLICA, [ 'group2' ], 'someDbName' )
116 ->will( $this->returnValue( $database ) );
118 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
119 $actual = $manager->getReadConnectionRef( [ 'group2' ] );
121 $this->assertSame( $database, $actual );
124 public function testGetWriteConnectionRef() {
125 $database = $this->getIDatabaseMock();
126 $lb = $this->getLoadBalancerMock();
128 $lb->expects( $this->once() )
129 ->method( 'getConnectionRef' )
130 ->with( DB_MASTER, [ 'group1' ], 'someDbName' )
131 ->will( $this->returnValue( $database ) );
133 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
134 $actual = $manager->getWriteConnectionRef();
136 $this->assertSame( $database, $actual );