3 namespace Wikimedia\Tests\Rdbms
;
5 use Wikimedia\Rdbms\IDatabase
;
6 use Wikimedia\Rdbms\LoadBalancer
;
7 use PHPUnit_Framework_MockObject_MockObject
;
8 use Wikimedia\Rdbms\ConnectionManager
;
11 * @covers Wikimedia\Rdbms\ConnectionManager
13 * @author Daniel Kinzler
15 class ConnectionManagerTest
extends \PHPUnit\Framework\TestCase
{
16 use \PHPUnit4And6Compat
;
19 * @return IDatabase|PHPUnit_Framework_MockObject_MockObject
21 private function getIDatabaseMock() {
22 return $this->getMockBuilder( IDatabase
::class )
27 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
29 private function getLoadBalancerMock() {
30 return $this->createMock( LoadBalancer
::class );
33 public function testGetReadConnection_nullGroups() {
34 $database = $this->getIDatabaseMock();
35 $lb = $this->getLoadBalancerMock();
37 $lb->expects( $this->once() )
38 ->method( 'getConnection' )
39 ->with( DB_REPLICA
, [ 'group1' ], 'someDbName' )
40 ->will( $this->returnValue( $database ) );
42 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
43 $actual = $manager->getReadConnection();
45 $this->assertSame( $database, $actual );
48 public function testGetReadConnection_withGroups() {
49 $database = $this->getIDatabaseMock();
50 $lb = $this->getLoadBalancerMock();
52 $lb->expects( $this->once() )
53 ->method( 'getConnection' )
54 ->with( DB_REPLICA
, [ 'group2' ], 'someDbName' )
55 ->will( $this->returnValue( $database ) );
57 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
58 $actual = $manager->getReadConnection( [ 'group2' ] );
60 $this->assertSame( $database, $actual );
63 public function testGetWriteConnection() {
64 $database = $this->getIDatabaseMock();
65 $lb = $this->getLoadBalancerMock();
67 $lb->expects( $this->once() )
68 ->method( 'getConnection' )
69 ->with( DB_MASTER
, [ 'group1' ], 'someDbName' )
70 ->will( $this->returnValue( $database ) );
72 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
73 $actual = $manager->getWriteConnection();
75 $this->assertSame( $database, $actual );
78 public function testReleaseConnection() {
79 $database = $this->getIDatabaseMock();
80 $lb = $this->getLoadBalancerMock();
82 $lb->expects( $this->once() )
83 ->method( 'reuseConnection' )
85 ->will( $this->returnValue( null ) );
87 $manager = new ConnectionManager( $lb );
88 $manager->releaseConnection( $database );
91 public function testGetReadConnectionRef_nullGroups() {
92 $database = $this->getIDatabaseMock();
93 $lb = $this->getLoadBalancerMock();
95 $lb->expects( $this->once() )
96 ->method( 'getConnectionRef' )
97 ->with( DB_REPLICA
, [ 'group1' ], 'someDbName' )
98 ->will( $this->returnValue( $database ) );
100 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
101 $actual = $manager->getReadConnectionRef();
103 $this->assertSame( $database, $actual );
106 public function testGetReadConnectionRef_withGroups() {
107 $database = $this->getIDatabaseMock();
108 $lb = $this->getLoadBalancerMock();
110 $lb->expects( $this->once() )
111 ->method( 'getConnectionRef' )
112 ->with( DB_REPLICA
, [ 'group2' ], 'someDbName' )
113 ->will( $this->returnValue( $database ) );
115 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
116 $actual = $manager->getReadConnectionRef( [ 'group2' ] );
118 $this->assertSame( $database, $actual );
121 public function testGetWriteConnectionRef() {
122 $database = $this->getIDatabaseMock();
123 $lb = $this->getLoadBalancerMock();
125 $lb->expects( $this->once() )
126 ->method( 'getConnectionRef' )
127 ->with( DB_MASTER
, [ 'group1' ], 'someDbName' )
128 ->will( $this->returnValue( $database ) );
130 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
131 $actual = $manager->getWriteConnectionRef();
133 $this->assertSame( $database, $actual );