Merge "phpunit: Avoid use of deprecated getMock for PHPUnit 5 compat"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / rdbms / connectionmanager / SessionConsistentConnectionManagerTest.php
1 <?php
2
3 namespace Wikimedia\Tests\Rdbms;
4
5 use IDatabase;
6 use Wikimedia\Rdbms\LoadBalancer;
7 use PHPUnit_Framework_MockObject_MockObject;
8 use Wikimedia\Rdbms\SessionConsistentConnectionManager;
9
10 /**
11 * @covers Wikimedia\Rdbms\SessionConsistentConnectionManager
12 *
13 * @license GPL-2.0+
14 * @author Daniel Kinzler
15 */
16 class SessionConsistentConnectionManagerTest 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() {
38 $database = $this->getIDatabaseMock();
39 $lb = $this->getLoadBalancerMock();
40
41 $lb->expects( $this->once() )
42 ->method( 'getConnection' )
43 ->with( DB_REPLICA )
44 ->will( $this->returnValue( $database ) );
45
46 $manager = new SessionConsistentConnectionManager( $lb );
47 $actual = $manager->getReadConnection();
48
49 $this->assertSame( $database, $actual );
50 }
51
52 public function testGetReadConnectionReturnsWriteDbOnForceMatser() {
53 $database = $this->getIDatabaseMock();
54 $lb = $this->getLoadBalancerMock();
55
56 $lb->expects( $this->once() )
57 ->method( 'getConnection' )
58 ->with( DB_MASTER )
59 ->will( $this->returnValue( $database ) );
60
61 $manager = new SessionConsistentConnectionManager( $lb );
62 $manager->prepareForUpdates();
63 $actual = $manager->getReadConnection();
64
65 $this->assertSame( $database, $actual );
66 }
67
68 public function testGetWriteConnection() {
69 $database = $this->getIDatabaseMock();
70 $lb = $this->getLoadBalancerMock();
71
72 $lb->expects( $this->once() )
73 ->method( 'getConnection' )
74 ->with( DB_MASTER )
75 ->will( $this->returnValue( $database ) );
76
77 $manager = new SessionConsistentConnectionManager( $lb );
78 $actual = $manager->getWriteConnection();
79
80 $this->assertSame( $database, $actual );
81 }
82
83 public function testForceMaster() {
84 $database = $this->getIDatabaseMock();
85 $lb = $this->getLoadBalancerMock();
86
87 $lb->expects( $this->once() )
88 ->method( 'getConnection' )
89 ->with( DB_MASTER )
90 ->will( $this->returnValue( $database ) );
91
92 $manager = new SessionConsistentConnectionManager( $lb );
93 $manager->prepareForUpdates();
94 $manager->getReadConnection();
95 }
96
97 public function testReleaseConnection() {
98 $database = $this->getIDatabaseMock();
99 $lb = $this->getLoadBalancerMock();
100
101 $lb->expects( $this->once() )
102 ->method( 'reuseConnection' )
103 ->with( $database )
104 ->will( $this->returnValue( null ) );
105
106 $manager = new SessionConsistentConnectionManager( $lb );
107 $manager->releaseConnection( $database );
108 }
109 }