Merge "Align search result CSS with Wikimedia UI color palette"
[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 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->getMock( IDatabase::class );
23 }
24
25 /**
26 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
27 */
28 private function getLoadBalancerMock() {
29 $lb = $this->getMockBuilder( LoadBalancer::class )
30 ->disableOriginalConstructor()
31 ->getMock();
32
33 return $lb;
34 }
35
36 public function testGetReadConnection() {
37 $database = $this->getIDatabaseMock();
38 $lb = $this->getLoadBalancerMock();
39
40 $lb->expects( $this->once() )
41 ->method( 'getConnection' )
42 ->with( DB_REPLICA )
43 ->will( $this->returnValue( $database ) );
44
45 $manager = new SessionConsistentConnectionManager( $lb );
46 $actual = $manager->getReadConnection();
47
48 $this->assertSame( $database, $actual );
49 }
50
51 public function testGetReadConnectionReturnsWriteDbOnForceMatser() {
52 $database = $this->getIDatabaseMock();
53 $lb = $this->getLoadBalancerMock();
54
55 $lb->expects( $this->once() )
56 ->method( 'getConnection' )
57 ->with( DB_MASTER )
58 ->will( $this->returnValue( $database ) );
59
60 $manager = new SessionConsistentConnectionManager( $lb );
61 $manager->prepareForUpdates();
62 $actual = $manager->getReadConnection();
63
64 $this->assertSame( $database, $actual );
65 }
66
67 public function testGetWriteConnection() {
68 $database = $this->getIDatabaseMock();
69 $lb = $this->getLoadBalancerMock();
70
71 $lb->expects( $this->once() )
72 ->method( 'getConnection' )
73 ->with( DB_MASTER )
74 ->will( $this->returnValue( $database ) );
75
76 $manager = new SessionConsistentConnectionManager( $lb );
77 $actual = $manager->getWriteConnection();
78
79 $this->assertSame( $database, $actual );
80 }
81
82 public function testForceMaster() {
83 $database = $this->getIDatabaseMock();
84 $lb = $this->getLoadBalancerMock();
85
86 $lb->expects( $this->once() )
87 ->method( 'getConnection' )
88 ->with( DB_MASTER )
89 ->will( $this->returnValue( $database ) );
90
91 $manager = new SessionConsistentConnectionManager( $lb );
92 $manager->prepareForUpdates();
93 $manager->getReadConnection();
94 }
95
96 public function testReleaseConnection() {
97 $database = $this->getIDatabaseMock();
98 $lb = $this->getLoadBalancerMock();
99
100 $lb->expects( $this->once() )
101 ->method( 'reuseConnection' )
102 ->with( $database )
103 ->will( $this->returnValue( null ) );
104
105 $manager = new SessionConsistentConnectionManager( $lb );
106 $manager->releaseConnection( $database );
107 }
108
109 public function testBeginAtomicSection() {
110 $database = $this->getIDatabaseMock();
111 $lb = $this->getLoadBalancerMock();
112
113 $lb->expects( $this->exactly( 2 ) )
114 ->method( 'getConnection' )
115 ->with( DB_MASTER )
116 ->will( $this->returnValue( $database ) );
117
118 $database->expects( $this->once() )
119 ->method( 'startAtomic' )
120 ->will( $this->returnValue( null ) );
121
122 $manager = new SessionConsistentConnectionManager( $lb );
123 $manager->beginAtomicSection( 'TEST' );
124
125 // Should also ask for a DB_MASTER connection.
126 // This is asserted by the $lb mock.
127 $manager->getReadConnection();
128 }
129
130 public function testCommitAtomicSection() {
131 $database = $this->getIDatabaseMock();
132 $lb = $this->getLoadBalancerMock();
133
134 $lb->expects( $this->once() )
135 ->method( 'reuseConnection' )
136 ->with( $database )
137 ->will( $this->returnValue( null ) );
138
139 $database->expects( $this->once() )
140 ->method( 'endAtomic' )
141 ->will( $this->returnValue( null ) );
142
143 $manager = new SessionConsistentConnectionManager( $lb );
144 $manager->commitAtomicSection( $database, 'TEST' );
145 }
146
147 public function testRollbackAtomicSection() {
148 $database = $this->getIDatabaseMock();
149 $lb = $this->getLoadBalancerMock();
150
151 $lb->expects( $this->once() )
152 ->method( 'reuseConnection' )
153 ->with( $database )
154 ->will( $this->returnValue( null ) );
155
156 $database->expects( $this->once() )
157 ->method( 'rollback' )
158 ->will( $this->returnValue( null ) );
159
160 $manager = new SessionConsistentConnectionManager( $lb );
161 $manager->rollbackAtomicSection( $database, 'TEST' );
162 }
163
164 }