MediaWiki  1.32.5
SessionConsistentConnectionManagerTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Wikimedia\Tests\Rdbms;
4 
7 use PHPUnit_Framework_MockObject_MockObject;
9 
15 class SessionConsistentConnectionManagerTest extends \PHPUnit\Framework\TestCase {
16 
20  private function getIDatabaseMock() {
21  return $this->getMockBuilder( IDatabase::class )
22  ->getMock();
23  }
24 
28  private function getLoadBalancerMock() {
29  $lb = $this->getMockBuilder( LoadBalancer::class )
30  ->disableOriginalConstructor()
31  ->getMock();
32 
33  return $lb;
34  }
35 
36  public function testGetReadConnection() {
37  $database = $this->getIDatabaseMock();
38  $lb = $this->getLoadBalancerMock();
39 
40  $lb->expects( $this->once() )
41  ->method( 'getConnection' )
42  ->with( DB_REPLICA )
43  ->will( $this->returnValue( $database ) );
44 
45  $manager = new SessionConsistentConnectionManager( $lb );
46  $actual = $manager->getReadConnection();
47 
48  $this->assertSame( $database, $actual );
49  }
50 
52  $database = $this->getIDatabaseMock();
53  $lb = $this->getLoadBalancerMock();
54 
55  $lb->expects( $this->once() )
56  ->method( 'getConnection' )
57  ->with( DB_MASTER )
58  ->will( $this->returnValue( $database ) );
59 
60  $manager = new SessionConsistentConnectionManager( $lb );
61  $manager->prepareForUpdates();
62  $actual = $manager->getReadConnection();
63 
64  $this->assertSame( $database, $actual );
65  }
66 
67  public function testGetWriteConnection() {
68  $database = $this->getIDatabaseMock();
69  $lb = $this->getLoadBalancerMock();
70 
71  $lb->expects( $this->once() )
72  ->method( 'getConnection' )
73  ->with( DB_MASTER )
74  ->will( $this->returnValue( $database ) );
75 
76  $manager = new SessionConsistentConnectionManager( $lb );
77  $actual = $manager->getWriteConnection();
78 
79  $this->assertSame( $database, $actual );
80  }
81 
82  public function testForceMaster() {
83  $database = $this->getIDatabaseMock();
84  $lb = $this->getLoadBalancerMock();
85 
86  $lb->expects( $this->once() )
87  ->method( 'getConnection' )
88  ->with( DB_MASTER )
89  ->will( $this->returnValue( $database ) );
90 
91  $manager = new SessionConsistentConnectionManager( $lb );
92  $manager->prepareForUpdates();
93  $manager->getReadConnection();
94  }
95 
96  public function testReleaseConnection() {
97  $database = $this->getIDatabaseMock();
98  $lb = $this->getLoadBalancerMock();
99 
100  $lb->expects( $this->once() )
101  ->method( 'reuseConnection' )
102  ->with( $database )
103  ->will( $this->returnValue( null ) );
104 
105  $manager = new SessionConsistentConnectionManager( $lb );
106  $manager->releaseConnection( $database );
107  }
108 }
Wikimedia\Tests\Rdbms
Definition: ConnectionManagerTest.php:3
Wikimedia\Tests\Rdbms\SessionConsistentConnectionManagerTest\testReleaseConnection
testReleaseConnection()
Definition: SessionConsistentConnectionManagerTest.php:96
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
Wikimedia\Tests\Rdbms\SessionConsistentConnectionManagerTest\testGetReadConnectionReturnsWriteDbOnForceMatser
testGetReadConnectionReturnsWriteDbOnForceMatser()
Definition: SessionConsistentConnectionManagerTest.php:51
use
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
Definition: MIT-LICENSE.txt:10
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Wikimedia\Tests\Rdbms\SessionConsistentConnectionManagerTest\testGetReadConnection
testGetReadConnection()
Definition: SessionConsistentConnectionManagerTest.php:36
Wikimedia\Rdbms\LoadBalancer
Database connection, tracking, load balancing, and transaction manager for a cluster.
Definition: LoadBalancer.php:41
Wikimedia\Tests\Rdbms\SessionConsistentConnectionManagerTest\testGetWriteConnection
testGetWriteConnection()
Definition: SessionConsistentConnectionManagerTest.php:67
Wikimedia\Tests\Rdbms\SessionConsistentConnectionManagerTest\getLoadBalancerMock
getLoadBalancerMock()
Definition: SessionConsistentConnectionManagerTest.php:28
Wikimedia\Tests\Rdbms\SessionConsistentConnectionManagerTest\getIDatabaseMock
getIDatabaseMock()
Definition: SessionConsistentConnectionManagerTest.php:20
Wikimedia\Tests\Rdbms\SessionConsistentConnectionManagerTest
Wikimedia\Rdbms\SessionConsistentConnectionManager.
Definition: SessionConsistentConnectionManagerTest.php:15
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Wikimedia\Tests\Rdbms\SessionConsistentConnectionManagerTest\testForceMaster
testForceMaster()
Definition: SessionConsistentConnectionManagerTest.php:82
Wikimedia\Rdbms\SessionConsistentConnectionManager
Database connection manager.
Definition: SessionConsistentConnectionManager.php:45