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