summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/libs/rdbms/database/DBConnRefTest.php
blob: c3cddc61dc875713c192cde1135e512ce5c7cecf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php

use Wikimedia\Rdbms\Database;
use Wikimedia\Rdbms\DBConnRef;
use Wikimedia\Rdbms\FakeResultWrapper;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\ILoadBalancer;
use Wikimedia\Rdbms\ResultWrapper;

/**
 * @covers Wikimedia\Rdbms\DBConnRef
 */
class DBConnRefTest extends PHPUnit\Framework\TestCase {

	use PHPUnit4And6Compat;

	/**
	 * @return ILoadBalancer
	 */
	private function getLoadBalancerMock() {
		$lb = $this->getMock( ILoadBalancer::class );

		$lb->method( 'getConnection' )->willReturnCallback(
			function () {
				return $this->getDatabaseMock();
			}
		);

		$lb->method( 'getConnectionRef' )->willReturnCallback(
			function () use ( $lb ) {
				return $this->getDBConnRef( $lb );
			}
		);

		return $lb;
	}

	/**
	 * @return IDatabase
	 */
	private function getDatabaseMock() {
		$db = $this->getMockBuilder( Database::class )
			->disableOriginalConstructor()
			->getMock();

		$db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
		$db->method( '__toString' )->willReturn( 'MOCK_DB' );

		return $db;
	}

	/**
	 * @return IDatabase
	 */
	private function getDBConnRef( ILoadBalancer $lb = null ) {
		$lb = $lb ?: $this->getLoadBalancerMock();
		return new DBConnRef( $lb, $this->getDatabaseMock() );
	}

	public function testConstruct() {
		$lb = $this->getLoadBalancerMock();
		$ref = new DBConnRef( $lb, $this->getDatabaseMock() );

		$this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
	}

	public function testConstruct_params() {
		$lb = $this->getMock( ILoadBalancer::class );

		$lb->expects( $this->once() )
			->method( 'getConnection' )
			->with( DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTOCOMMIT )
			->willReturnCallback(
				function () {
					return $this->getDatabaseMock();
				}
			);

		$ref = new DBConnRef(
			$lb,
			[ DB_MASTER, [ 'test' ], 'dummy', ILoadBalancer::CONN_TRX_AUTOCOMMIT ]
		);

		$this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
	}

	public function testDestruct() {
		$lb = $this->getLoadBalancerMock();

		$lb->expects( $this->once() )
			->method( 'reuseConnection' );

		$this->innerMethodForTestDestruct( $lb );
	}

	private function innerMethodForTestDestruct( ILoadBalancer $lb ) {
		$ref = $lb->getConnectionRef( DB_REPLICA );

		$this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
	}

	public function testConstruct_failure() {
		$this->setExpectedException( InvalidArgumentException::class, '' );

		$lb = $this->getLoadBalancerMock();
		new DBConnRef( $lb, 17 ); // bad constructor argument
	}

	public function testGetWikiID() {
		$lb = $this->getMock( ILoadBalancer::class );

		// getWikiID is optimized to not create a connection
		$lb->expects( $this->never() )
			->method( 'getConnection' );

		$ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ] );

		$this->assertSame( 'dummy', $ref->getWikiID() );
	}

	public function testGetDomainID() {
		$lb = $this->getMock( ILoadBalancer::class );

		// getDomainID is optimized to not create a connection
		$lb->expects( $this->never() )
			->method( 'getConnection' );

		$ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ] );

		$this->assertSame( 'dummy', $ref->getDomainID() );
	}

	public function testSelect() {
		// select should get passed through normally
		$ref = $this->getDBConnRef();
		$this->assertInstanceOf( ResultWrapper::class, $ref->select( 'whatever', '*' ) );
	}

	public function testToString() {
		$ref = $this->getDBConnRef();
		$this->assertInternalType( 'string', $ref->__toString() );

		$lb = $this->getLoadBalancerMock();
		$ref = new DBConnRef( $lb, [ DB_MASTER, [], 'test', 0 ] );
		$this->assertInternalType( 'string', $ref->__toString() );
	}

}