rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / phpunit / includes / libs / rdbms / connectionmanager / SessionConsistentConnectionManagerTest.php
blob4e76f2a83927eb07023dad4ab0c9c1a4d561cb37
1 <?php
3 namespace Wikimedia\Tests\Rdbms;
5 use IDatabase;
6 use Wikimedia\Rdbms\LoadBalancer;
7 use PHPUnit_Framework_MockObject_MockObject;
8 use Wikimedia\Rdbms\SessionConsistentConnectionManager;
10 /**
11 * @covers Wikimedia\Rdbms\SessionConsistentConnectionManager
13 * @license GPL-2.0+
14 * @author Daniel Kinzler
16 class SessionConsistentConnectionManagerTest extends \PHPUnit_Framework_TestCase {
18 /**
19 * @return IDatabase|PHPUnit_Framework_MockObject_MockObject
21 private function getIDatabaseMock() {
22 return $this->getMockBuilder( IDatabase::class )
23 ->getMock();
26 /**
27 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
29 private function getLoadBalancerMock() {
30 $lb = $this->getMockBuilder( LoadBalancer::class )
31 ->disableOriginalConstructor()
32 ->getMock();
34 return $lb;
37 public function testGetReadConnection() {
38 $database = $this->getIDatabaseMock();
39 $lb = $this->getLoadBalancerMock();
41 $lb->expects( $this->once() )
42 ->method( 'getConnection' )
43 ->with( DB_REPLICA )
44 ->will( $this->returnValue( $database ) );
46 $manager = new SessionConsistentConnectionManager( $lb );
47 $actual = $manager->getReadConnection();
49 $this->assertSame( $database, $actual );
52 public function testGetReadConnectionReturnsWriteDbOnForceMatser() {
53 $database = $this->getIDatabaseMock();
54 $lb = $this->getLoadBalancerMock();
56 $lb->expects( $this->once() )
57 ->method( 'getConnection' )
58 ->with( DB_MASTER )
59 ->will( $this->returnValue( $database ) );
61 $manager = new SessionConsistentConnectionManager( $lb );
62 $manager->prepareForUpdates();
63 $actual = $manager->getReadConnection();
65 $this->assertSame( $database, $actual );
68 public function testGetWriteConnection() {
69 $database = $this->getIDatabaseMock();
70 $lb = $this->getLoadBalancerMock();
72 $lb->expects( $this->once() )
73 ->method( 'getConnection' )
74 ->with( DB_MASTER )
75 ->will( $this->returnValue( $database ) );
77 $manager = new SessionConsistentConnectionManager( $lb );
78 $actual = $manager->getWriteConnection();
80 $this->assertSame( $database, $actual );
83 public function testForceMaster() {
84 $database = $this->getIDatabaseMock();
85 $lb = $this->getLoadBalancerMock();
87 $lb->expects( $this->once() )
88 ->method( 'getConnection' )
89 ->with( DB_MASTER )
90 ->will( $this->returnValue( $database ) );
92 $manager = new SessionConsistentConnectionManager( $lb );
93 $manager->prepareForUpdates();
94 $manager->getReadConnection();
97 public function testReleaseConnection() {
98 $database = $this->getIDatabaseMock();
99 $lb = $this->getLoadBalancerMock();
101 $lb->expects( $this->once() )
102 ->method( 'reuseConnection' )
103 ->with( $database )
104 ->will( $this->returnValue( null ) );
106 $manager = new SessionConsistentConnectionManager( $lb );
107 $manager->releaseConnection( $database );