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