MediaWiki REL1_31
DBConnRefTest.php
Go to the documentation of this file.
1<?php
2
9
13class DBConnRefTest extends PHPUnit\Framework\TestCase {
14
15 use PHPUnit4And6Compat;
16
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
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
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}
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Wikimedia\Rdbms\DBConnRef.
getDBConnRef(ILoadBalancer $lb=null)
innerMethodForTestDestruct(ILoadBalancer $lb)
Helper class to handle automatically marking connections as reusable (via RAII pattern) as well handl...
Definition DBConnRef.php:15
Relational database abstraction object.
Definition Database.php:48
Overloads the relevant methods of the real ResultsWrapper so it doesn't go anywhere near an actual da...
Result wrapper for grabbing data queried from an IDatabase object.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
Database cluster connection, tracking, load balancing, and transaction manager interface.
getConnectionRef( $i, $groups=[], $domain=false, $flags=0)
Get a database connection handle reference.
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:29