3 namespace Wikimedia\Tests\Rdbms
;
6 use Wikimedia\Rdbms\LoadBalancer
;
7 use PHPUnit_Framework_MockObject_MockObject
;
8 use Wikimedia\Rdbms\ConnectionManager
;
11 * @covers Wikimedia\Rdbms\ConnectionManager
14 * @author Daniel Kinzler
16 class ConnectionManagerTest
extends \PHPUnit_Framework_TestCase
{
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 $lb = $this->getMockBuilder( LoadBalancer
::class )
31 ->disableOriginalConstructor()
37 public function testGetReadConnection_nullGroups() {
38 $database = $this->getIDatabaseMock();
39 $lb = $this->getLoadBalancerMock();
41 $lb->expects( $this->once() )
42 ->method( 'getConnection' )
43 ->with( DB_REPLICA
, [ 'group1' ], 'someDbName' )
44 ->will( $this->returnValue( $database ) );
46 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
47 $actual = $manager->getReadConnection();
49 $this->assertSame( $database, $actual );
52 public function testGetReadConnection_withGroups() {
53 $database = $this->getIDatabaseMock();
54 $lb = $this->getLoadBalancerMock();
56 $lb->expects( $this->once() )
57 ->method( 'getConnection' )
58 ->with( DB_REPLICA
, [ 'group2' ], 'someDbName' )
59 ->will( $this->returnValue( $database ) );
61 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
62 $actual = $manager->getReadConnection( [ 'group2' ] );
64 $this->assertSame( $database, $actual );
67 public function testGetWriteConnection() {
68 $database = $this->getIDatabaseMock();
69 $lb = $this->getLoadBalancerMock();
71 $lb->expects( $this->once() )
72 ->method( 'getConnection' )
73 ->with( DB_MASTER
, [ 'group1' ], 'someDbName' )
74 ->will( $this->returnValue( $database ) );
76 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
77 $actual = $manager->getWriteConnection();
79 $this->assertSame( $database, $actual );
82 public function testReleaseConnection() {
83 $database = $this->getIDatabaseMock();
84 $lb = $this->getLoadBalancerMock();
86 $lb->expects( $this->once() )
87 ->method( 'reuseConnection' )
89 ->will( $this->returnValue( null ) );
91 $manager = new ConnectionManager( $lb );
92 $manager->releaseConnection( $database );
95 public function testGetReadConnectionRef_nullGroups() {
96 $database = $this->getIDatabaseMock();
97 $lb = $this->getLoadBalancerMock();
99 $lb->expects( $this->once() )
100 ->method( 'getConnectionRef' )
101 ->with( DB_REPLICA
, [ 'group1' ], 'someDbName' )
102 ->will( $this->returnValue( $database ) );
104 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
105 $actual = $manager->getReadConnectionRef();
107 $this->assertSame( $database, $actual );
110 public function testGetReadConnectionRef_withGroups() {
111 $database = $this->getIDatabaseMock();
112 $lb = $this->getLoadBalancerMock();
114 $lb->expects( $this->once() )
115 ->method( 'getConnectionRef' )
116 ->with( DB_REPLICA
, [ 'group2' ], 'someDbName' )
117 ->will( $this->returnValue( $database ) );
119 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
120 $actual = $manager->getReadConnectionRef( [ 'group2' ] );
122 $this->assertSame( $database, $actual );
125 public function testGetWriteConnectionRef() {
126 $database = $this->getIDatabaseMock();
127 $lb = $this->getLoadBalancerMock();
129 $lb->expects( $this->once() )
130 ->method( 'getConnectionRef' )
131 ->with( DB_MASTER
, [ 'group1' ], 'someDbName' )
132 ->will( $this->returnValue( $database ) );
134 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
135 $actual = $manager->getWriteConnectionRef();
137 $this->assertSame( $database, $actual );