Merge "Add semantic tags to license info text"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / rdbms / connectionmanager / ConnectionManagerTest.php
1 <?php
2
3 namespace Wikimedia\Tests\Rdbms;
4
5 use Wikimedia\Rdbms\IDatabase;
6 use Wikimedia\Rdbms\LoadBalancer;
7 use PHPUnit_Framework_MockObject_MockObject;
8 use Wikimedia\Rdbms\ConnectionManager;
9
10 /**
11 * @covers Wikimedia\Rdbms\ConnectionManager
12 *
13 * @license GPL-2.0+
14 * @author Daniel Kinzler
15 */
16 class ConnectionManagerTest extends \PHPUnit_Framework_TestCase {
17
18 /**
19 * @return IDatabase|PHPUnit_Framework_MockObject_MockObject
20 */
21 private function getIDatabaseMock() {
22 return $this->getMockBuilder( IDatabase::class )
23 ->getMock();
24 }
25
26 /**
27 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
28 */
29 private function getLoadBalancerMock() {
30 $lb = $this->getMockBuilder( LoadBalancer::class )
31 ->disableOriginalConstructor()
32 ->getMock();
33
34 return $lb;
35 }
36
37 public function testGetReadConnection_nullGroups() {
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
52 public function testGetReadConnection_withGroups() {
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
95 public function testGetReadConnectionRef_nullGroups() {
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
110 public function testGetReadConnectionRef_withGroups() {
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 }