3 namespace Wikimedia\Tests\Rdbms
;
7 use PHPUnit_Framework_MockObject_MockObject
;
8 use Wikimedia\Rdbms\SessionConsistentConnectionManager
;
11 * @covers Wikimedia\Rdbms\SessionConsistentConnectionManager
14 * @author Daniel Kinzler
16 class SessionConsistentConnectionManagerTest
extends \PHPUnit_Framework_TestCase
{
19 * @return IDatabase|PHPUnit_Framework_MockObject_MockObject
21 private function getIDatabaseMock() {
22 return $this->getMock( IDatabase
::class );
26 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
28 private function getLoadBalancerMock() {
29 $lb = $this->getMockBuilder( LoadBalancer
::class )
30 ->disableOriginalConstructor()
36 public function testGetReadConnection() {
37 $database = $this->getIDatabaseMock();
38 $lb = $this->getLoadBalancerMock();
40 $lb->expects( $this->once() )
41 ->method( 'getConnection' )
43 ->will( $this->returnValue( $database ) );
45 $manager = new SessionConsistentConnectionManager( $lb );
46 $actual = $manager->getReadConnection();
48 $this->assertSame( $database, $actual );
51 public function testGetReadConnectionReturnsWriteDbOnForceMatser() {
52 $database = $this->getIDatabaseMock();
53 $lb = $this->getLoadBalancerMock();
55 $lb->expects( $this->once() )
56 ->method( 'getConnection' )
58 ->will( $this->returnValue( $database ) );
60 $manager = new SessionConsistentConnectionManager( $lb );
61 $manager->prepareForUpdates();
62 $actual = $manager->getReadConnection();
64 $this->assertSame( $database, $actual );
67 public function testGetWriteConnection() {
68 $database = $this->getIDatabaseMock();
69 $lb = $this->getLoadBalancerMock();
71 $lb->expects( $this->once() )
72 ->method( 'getConnection' )
74 ->will( $this->returnValue( $database ) );
76 $manager = new SessionConsistentConnectionManager( $lb );
77 $actual = $manager->getWriteConnection();
79 $this->assertSame( $database, $actual );
82 public function testForceMaster() {
83 $database = $this->getIDatabaseMock();
84 $lb = $this->getLoadBalancerMock();
86 $lb->expects( $this->once() )
87 ->method( 'getConnection' )
89 ->will( $this->returnValue( $database ) );
91 $manager = new SessionConsistentConnectionManager( $lb );
92 $manager->prepareForUpdates();
93 $manager->getReadConnection();
96 public function testReleaseConnection() {
97 $database = $this->getIDatabaseMock();
98 $lb = $this->getLoadBalancerMock();
100 $lb->expects( $this->once() )
101 ->method( 'reuseConnection' )
103 ->will( $this->returnValue( null ) );
105 $manager = new SessionConsistentConnectionManager( $lb );
106 $manager->releaseConnection( $database );