MediaWiki REL1_32
DBConnRefTest.php
Go to the documentation of this file.
1<?php
2
9
13class DBConnRefTest extends PHPUnit\Framework\TestCase {
14
15 use MediaWikiCoversValidator;
16 use PHPUnit4And6Compat;
17
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
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
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
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}
Wikimedia\Rdbms\DBConnRef.
getDBConnRef(ILoadBalancer $lb=null)
testGetDomainID()
Wikimedia\Rdbms\DBConnRef::getDomainId.
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.
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
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:26