MediaWiki REL1_31
ConnectionManagerTest.php
Go to the documentation of this file.
1<?php
2
4
7use PHPUnit_Framework_MockObject_MockObject;
9
15class ConnectionManagerTest extends \PHPUnit\Framework\TestCase {
16
20 private function getIDatabaseMock() {
21 return $this->getMockBuilder( IDatabase::class )
22 ->getMock();
23 }
24
28 private function getLoadBalancerMock() {
29 $lb = $this->getMockBuilder( LoadBalancer::class )
30 ->disableOriginalConstructor()
31 ->getMock();
32
33 return $lb;
34 }
35
37 $database = $this->getIDatabaseMock();
38 $lb = $this->getLoadBalancerMock();
39
40 $lb->expects( $this->once() )
41 ->method( 'getConnection' )
42 ->with( DB_REPLICA, [ 'group1' ], 'someDbName' )
43 ->will( $this->returnValue( $database ) );
44
45 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
46 $actual = $manager->getReadConnection();
47
48 $this->assertSame( $database, $actual );
49 }
50
52 $database = $this->getIDatabaseMock();
53 $lb = $this->getLoadBalancerMock();
54
55 $lb->expects( $this->once() )
56 ->method( 'getConnection' )
57 ->with( DB_REPLICA, [ 'group2' ], 'someDbName' )
58 ->will( $this->returnValue( $database ) );
59
60 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
61 $actual = $manager->getReadConnection( [ 'group2' ] );
62
63 $this->assertSame( $database, $actual );
64 }
65
66 public function testGetWriteConnection() {
67 $database = $this->getIDatabaseMock();
68 $lb = $this->getLoadBalancerMock();
69
70 $lb->expects( $this->once() )
71 ->method( 'getConnection' )
72 ->with( DB_MASTER, [ 'group1' ], 'someDbName' )
73 ->will( $this->returnValue( $database ) );
74
75 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
76 $actual = $manager->getWriteConnection();
77
78 $this->assertSame( $database, $actual );
79 }
80
81 public function testReleaseConnection() {
82 $database = $this->getIDatabaseMock();
83 $lb = $this->getLoadBalancerMock();
84
85 $lb->expects( $this->once() )
86 ->method( 'reuseConnection' )
87 ->with( $database )
88 ->will( $this->returnValue( null ) );
89
90 $manager = new ConnectionManager( $lb );
91 $manager->releaseConnection( $database );
92 }
93
95 $database = $this->getIDatabaseMock();
96 $lb = $this->getLoadBalancerMock();
97
98 $lb->expects( $this->once() )
99 ->method( 'getConnectionRef' )
100 ->with( DB_REPLICA, [ 'group1' ], 'someDbName' )
101 ->will( $this->returnValue( $database ) );
102
103 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
104 $actual = $manager->getReadConnectionRef();
105
106 $this->assertSame( $database, $actual );
107 }
108
110 $database = $this->getIDatabaseMock();
111 $lb = $this->getLoadBalancerMock();
112
113 $lb->expects( $this->once() )
114 ->method( 'getConnectionRef' )
115 ->with( DB_REPLICA, [ 'group2' ], 'someDbName' )
116 ->will( $this->returnValue( $database ) );
117
118 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
119 $actual = $manager->getReadConnectionRef( [ 'group2' ] );
120
121 $this->assertSame( $database, $actual );
122 }
123
124 public function testGetWriteConnectionRef() {
125 $database = $this->getIDatabaseMock();
126 $lb = $this->getLoadBalancerMock();
127
128 $lb->expects( $this->once() )
129 ->method( 'getConnectionRef' )
130 ->with( DB_MASTER, [ 'group1' ], 'someDbName' )
131 ->will( $this->returnValue( $database ) );
132
133 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
134 $actual = $manager->getWriteConnectionRef();
135
136 $this->assertSame( $database, $actual );
137 }
138
139}
Database connection manager.
Database connection, tracking, load balancing, and transaction manager for a cluster.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:29