Debian Codegraph @

tests/phpunit/unit/includes/libs/rdbms/connectionmanager/ConnectionManagerTest.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Tests\Rdbms;
4
5 use MediaWikiUnitTestCase;
6 use Wikimedia\Rdbms\ConnectionManager;
7 use Wikimedia\Rdbms\IDatabase;
8 use Wikimedia\Rdbms\ILoadBalancer;
9 use Wikimedia\Rdbms\LoadBalancer;
10
11 /**
12 * @covers \Wikimedia\Rdbms\ConnectionManager
13 *
14 * @author Daniel Kinzler
15 */
16 class ConnectionManagerTest extends MediaWikiUnitTestCase {
17
18 public function testGetReadConnection_nullGroups() {
19 $database = $this->createMock( IDatabase::class );
20 $lb = $this->createMock( LoadBalancer::class );
21
22 $lb->expects( $this->once() )
23 ->method( 'getConnection' )
24 ->with( DB_REPLICA, [ 'group1' ], 'someDbName' )
25 ->willReturn( $database );
26
27 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
28 $actual = $manager->getReadConnection();
29
30 $this->assertSame( $database, $actual );
31 }
32
33 public function testGetReadConnection_withGroupsAndFlags() {
34 $database = $this->createMock( IDatabase::class );
35 $lb = $this->createMock( LoadBalancer::class );
36
37 $lb->expects( $this->once() )
38 ->method( 'getConnection' )
39 ->with( DB_REPLICA, [ 'group2' ], 'someDbName', ILoadBalancer::CONN_SILENCE_ERRORS )
40 ->willReturn( $database );
41
42 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
43 $actual = $manager->getReadConnection( [ 'group2' ], ILoadBalancer::CONN_SILENCE_ERRORS );
44
45 $this->assertSame( $database, $actual );
46 }
47
48 public function testGetWriteConnection() {
49 $database = $this->createMock( IDatabase::class );
50 $lb = $this->createMock( LoadBalancer::class );
51
52 $lb->expects( $this->once() )
53 ->method( 'getConnection' )
54 ->with( DB_PRIMARY, [ 'group1' ], 'someDbName' )
55 ->willReturn( $database );
56
57 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
58 $actual = $manager->getWriteConnection();
59
60 $this->assertSame( $database, $actual );
61 }
62
63 public function testGetWriteConnection_withFlags() {
64 $database = $this->createMock( IDatabase::class );
65 $lb = $this->createMock( LoadBalancer::class );
66
67 $lb->expects( $this->once() )
68 ->method( 'getConnection' )
69 ->with( DB_PRIMARY, [ 'group1' ], 'someDbName', ILoadBalancer::CONN_TRX_AUTOCOMMIT )
70 ->willReturn( $database );
71
72 $manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
73 $actual = $manager->getWriteConnection( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
74
75 $this->assertSame( $database, $actual );
76 }
77
78 }