Merge "Add semantic tags to license info text"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / rdbms / database / DBConnRefTest.php
1 <?php
2
3 use Wikimedia\Rdbms\Database;
4 use Wikimedia\Rdbms\DBConnRef;
5 use Wikimedia\Rdbms\FakeResultWrapper;
6 use Wikimedia\Rdbms\IDatabase;
7 use Wikimedia\Rdbms\ILoadBalancer;
8 use Wikimedia\Rdbms\ResultWrapper;
9
10 /**
11 * @covers Wikimedia\Rdbms\DBConnRef
12 */
13 class DBConnRefTest extends PHPUnit_Framework_TestCase {
14
15 /**
16 * @return ILoadBalancer
17 */
18 private function getLoadBalancerMock() {
19 $lb = $this->getMock( ILoadBalancer::class );
20
21 $lb->method( 'getConnection' )->willReturnCallback(
22 function () {
23 return $this->getDatabaseMock();
24 }
25 );
26
27 $lb->method( 'getConnectionRef' )->willReturnCallback(
28 function () use ( $lb ) {
29 return $this->getDBConnRef( $lb );
30 }
31 );
32
33 return $lb;
34 }
35
36 /**
37 * @return IDatabase
38 */
39 private function getDatabaseMock() {
40 $db = $this->getMockBuilder( Database::class )
41 ->disableOriginalConstructor()
42 ->getMock();
43
44 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
45 $db->method( '__toString' )->willReturn( 'MOCK_DB' );
46
47 return $db;
48 }
49
50 /**
51 * @return IDatabase
52 */
53 private function getDBConnRef( ILoadBalancer $lb = null ) {
54 $lb = $lb ?: $this->getLoadBalancerMock();
55 return new DBConnRef( $lb, $this->getDatabaseMock() );
56 }
57
58 public function testConstruct() {
59 $lb = $this->getLoadBalancerMock();
60 $ref = new DBConnRef( $lb, $this->getDatabaseMock() );
61
62 $this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
63 }
64
65 public function testConstruct_params() {
66 $lb = $this->getMock( ILoadBalancer::class );
67
68 $lb->expects( $this->once() )
69 ->method( 'getConnection' )
70 ->with( DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTO )
71 ->willReturnCallback(
72 function () {
73 return $this->getDatabaseMock();
74 }
75 );
76
77 $ref = new DBConnRef(
78 $lb,
79 [ DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTO ]
80 );
81
82 $this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
83 }
84
85 public function testDestruct() {
86 $lb = $this->getLoadBalancerMock();
87
88 $lb->expects( $this->once() )
89 ->method( 'reuseConnection' );
90
91 $this->innerMethodForTestDestruct( $lb );
92 }
93
94 private function innerMethodForTestDestruct( ILoadBalancer $lb ) {
95 $ref = $lb->getConnectionRef( DB_REPLICA );
96
97 $this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
98 }
99
100 public function testConstruct_failure() {
101 $this->setExpectedException( InvalidArgumentException::class, '' );
102
103 $lb = $this->getLoadBalancerMock();
104 new DBConnRef( $lb, 17 ); // bad constructor argument
105 }
106
107 public function testGetWikiID() {
108 $lb = $this->getMock( ILoadBalancer::class );
109
110 // getWikiID is optimized to not create a connection
111 $lb->expects( $this->never() )
112 ->method( 'getConnection' );
113
114 $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ] );
115
116 $this->assertSame( 'dummy', $ref->getWikiID() );
117 }
118
119 public function testGetDomainID() {
120 $lb = $this->getMock( ILoadBalancer::class );
121
122 // getDomainID is optimized to not create a connection
123 $lb->expects( $this->never() )
124 ->method( 'getConnection' );
125
126 $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ] );
127
128 $this->assertSame( 'dummy', $ref->getDomainID() );
129 }
130
131 public function testSelect() {
132 // select should get passed through normally
133 $ref = $this->getDBConnRef();
134 $this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
135 }
136
137 public function testToString() {
138 $ref = $this->getDBConnRef();
139 $this->assertInternalType( 'string', $ref->__toString() );
140
141 $lb = $this->getLoadBalancerMock();
142 $ref = new DBConnRef( $lb, [ DB_MASTER, [], 'test', 0 ] );
143 $this->assertInternalType( 'string', $ref->__toString() );
144 }
145
146 }