Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / rdbms / connectionmanager / SessionConsistentConnectionManagerTest.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\SessionConsistentConnectionManager;
9
10 /**
11 * @covers Wikimedia\Rdbms\SessionConsistentConnectionManager
12 *
13 * @author Daniel Kinzler
14 */
15 class SessionConsistentConnectionManagerTest 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() {
34 $database = $this->getIDatabaseMock();
35 $lb = $this->getLoadBalancerMock();
36
37 $lb->expects( $this->once() )
38 ->method( 'getConnection' )
39 ->with( DB_REPLICA )
40 ->will( $this->returnValue( $database ) );
41
42 $manager = new SessionConsistentConnectionManager( $lb );
43 $actual = $manager->getReadConnection();
44
45 $this->assertSame( $database, $actual );
46 }
47
48 public function testGetReadConnectionReturnsWriteDbOnForceMatser() {
49 $database = $this->getIDatabaseMock();
50 $lb = $this->getLoadBalancerMock();
51
52 $lb->expects( $this->once() )
53 ->method( 'getConnection' )
54 ->with( DB_MASTER )
55 ->will( $this->returnValue( $database ) );
56
57 $manager = new SessionConsistentConnectionManager( $lb );
58 $manager->prepareForUpdates();
59 $actual = $manager->getReadConnection();
60
61 $this->assertSame( $database, $actual );
62 }
63
64 public function testGetWriteConnection() {
65 $database = $this->getIDatabaseMock();
66 $lb = $this->getLoadBalancerMock();
67
68 $lb->expects( $this->once() )
69 ->method( 'getConnection' )
70 ->with( DB_MASTER )
71 ->will( $this->returnValue( $database ) );
72
73 $manager = new SessionConsistentConnectionManager( $lb );
74 $actual = $manager->getWriteConnection();
75
76 $this->assertSame( $database, $actual );
77 }
78
79 public function testForceMaster() {
80 $database = $this->getIDatabaseMock();
81 $lb = $this->getLoadBalancerMock();
82
83 $lb->expects( $this->once() )
84 ->method( 'getConnection' )
85 ->with( DB_MASTER )
86 ->will( $this->returnValue( $database ) );
87
88 $manager = new SessionConsistentConnectionManager( $lb );
89 $manager->prepareForUpdates();
90 $manager->getReadConnection();
91 }
92
93 public function testReleaseConnection() {
94 $database = $this->getIDatabaseMock();
95 $lb = $this->getLoadBalancerMock();
96
97 $lb->expects( $this->once() )
98 ->method( 'reuseConnection' )
99 ->with( $database )
100 ->will( $this->returnValue( null ) );
101
102 $manager = new SessionConsistentConnectionManager( $lb );
103 $manager->releaseConnection( $database );
104 }
105 }