Replace very trivial mock builders with createMock()
[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 * @author Daniel Kinzler
14 */
15 class ConnectionManagerTest extends \PHPUnit\Framework\TestCase {
16 use \PHPUnit4And6Compat;
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 return $this->createMock( LoadBalancer::class );
31 }
32
33 public function testGetReadConnection_nullGroups() {
34 $database = $this->getIDatabaseMock();
35 $lb = $this->getLoadBalancerMock();
36
37 $lb->expects( $this->once() )
38 ->method( 'getConnection' )
39 ->with( DB_REPLICA, [ 'group1' ], 'someDbName' )
40 ->will( $this->returnValue( $database ) );
41
42 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
43 $actual = $manager->getReadConnection();
44
45 $this->assertSame( $database, $actual );
46 }
47
48 public function testGetReadConnection_withGroups() {
49 $database = $this->getIDatabaseMock();
50 $lb = $this->getLoadBalancerMock();
51
52 $lb->expects( $this->once() )
53 ->method( 'getConnection' )
54 ->with( DB_REPLICA, [ 'group2' ], 'someDbName' )
55 ->will( $this->returnValue( $database ) );
56
57 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
58 $actual = $manager->getReadConnection( [ 'group2' ] );
59
60 $this->assertSame( $database, $actual );
61 }
62
63 public function testGetWriteConnection() {
64 $database = $this->getIDatabaseMock();
65 $lb = $this->getLoadBalancerMock();
66
67 $lb->expects( $this->once() )
68 ->method( 'getConnection' )
69 ->with( DB_MASTER, [ 'group1' ], 'someDbName' )
70 ->will( $this->returnValue( $database ) );
71
72 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
73 $actual = $manager->getWriteConnection();
74
75 $this->assertSame( $database, $actual );
76 }
77
78 public function testReleaseConnection() {
79 $database = $this->getIDatabaseMock();
80 $lb = $this->getLoadBalancerMock();
81
82 $lb->expects( $this->once() )
83 ->method( 'reuseConnection' )
84 ->with( $database )
85 ->will( $this->returnValue( null ) );
86
87 $manager = new ConnectionManager( $lb );
88 $manager->releaseConnection( $database );
89 }
90
91 public function testGetReadConnectionRef_nullGroups() {
92 $database = $this->getIDatabaseMock();
93 $lb = $this->getLoadBalancerMock();
94
95 $lb->expects( $this->once() )
96 ->method( 'getConnectionRef' )
97 ->with( DB_REPLICA, [ 'group1' ], 'someDbName' )
98 ->will( $this->returnValue( $database ) );
99
100 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
101 $actual = $manager->getReadConnectionRef();
102
103 $this->assertSame( $database, $actual );
104 }
105
106 public function testGetReadConnectionRef_withGroups() {
107 $database = $this->getIDatabaseMock();
108 $lb = $this->getLoadBalancerMock();
109
110 $lb->expects( $this->once() )
111 ->method( 'getConnectionRef' )
112 ->with( DB_REPLICA, [ 'group2' ], 'someDbName' )
113 ->will( $this->returnValue( $database ) );
114
115 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
116 $actual = $manager->getReadConnectionRef( [ 'group2' ] );
117
118 $this->assertSame( $database, $actual );
119 }
120
121 public function testGetWriteConnectionRef() {
122 $database = $this->getIDatabaseMock();
123 $lb = $this->getLoadBalancerMock();
124
125 $lb->expects( $this->once() )
126 ->method( 'getConnectionRef' )
127 ->with( DB_MASTER, [ 'group1' ], 'someDbName' )
128 ->will( $this->returnValue( $database ) );
129
130 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
131 $actual = $manager->getWriteConnectionRef();
132
133 $this->assertSame( $database, $actual );
134 }
135
136 }