wiki.techinc.nl/tests/phpunit/includes/libs/rdbms/connectionmanager/ConnectionManagerTest.php
Timo Tijhof 447ce7e39a phpunit: Avoid use of deprecated getMock for PHPUnit 5 compat
The default will remain PHPUnit 4.x due to PHP 5.5 support.

But, we should allow developers to run tests with newer PHPUnit
versions which are noticably faster (especially for code coverage
reports).

* <https://github.com/sebastianbergmann/phpunit/wiki/Release-Announcement-for-PHPUnit-5.4.0>
  PHPUnit 5 deprecates the getMock() shortcut for getMockBuilder()->getMock().
  It instead introduces the shortcut createMock() which has better defaults
  than getMockBuilder(). For example, it sets 'disableArgumentCloning' and
  other things by default.

  Going forward, code should either use getMockBuilder directly and configure
  it using the setter methods (instead of the confusing variadic arguments
  of getMock) or simply use the new minimalistic createMock method. This patch
  backports the createMock method to MediaWikiTestCase so that we can start
  using it.

Change-Id: I091c0289b21d2b1c876adba89529dc3e72b99af2
2017-04-06 00:44:32 +00:00

140 lines
3.8 KiB
PHP

<?php
namespace Wikimedia\Tests\Rdbms;
use IDatabase;
use Wikimedia\Rdbms\LoadBalancer;
use PHPUnit_Framework_MockObject_MockObject;
use Wikimedia\Rdbms\ConnectionManager;
/**
* @covers Wikimedia\Rdbms\ConnectionManager
*
* @license GPL-2.0+
* @author Daniel Kinzler
*/
class ConnectionManagerTest extends \PHPUnit_Framework_TestCase {
/**
* @return IDatabase|PHPUnit_Framework_MockObject_MockObject
*/
private function getIDatabaseMock() {
return $this->getMockBuilder( IDatabase::class )
->getMock();
}
/**
* @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
*/
private function getLoadBalancerMock() {
$lb = $this->getMockBuilder( LoadBalancer::class )
->disableOriginalConstructor()
->getMock();
return $lb;
}
public function testGetReadConnection_nullGroups() {
$database = $this->getIDatabaseMock();
$lb = $this->getLoadBalancerMock();
$lb->expects( $this->once() )
->method( 'getConnection' )
->with( DB_REPLICA, [ 'group1' ], 'someDbName' )
->will( $this->returnValue( $database ) );
$manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
$actual = $manager->getReadConnection();
$this->assertSame( $database, $actual );
}
public function testGetReadConnection_withGroups() {
$database = $this->getIDatabaseMock();
$lb = $this->getLoadBalancerMock();
$lb->expects( $this->once() )
->method( 'getConnection' )
->with( DB_REPLICA, [ 'group2' ], 'someDbName' )
->will( $this->returnValue( $database ) );
$manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
$actual = $manager->getReadConnection( [ 'group2' ] );
$this->assertSame( $database, $actual );
}
public function testGetWriteConnection() {
$database = $this->getIDatabaseMock();
$lb = $this->getLoadBalancerMock();
$lb->expects( $this->once() )
->method( 'getConnection' )
->with( DB_MASTER, [ 'group1' ], 'someDbName' )
->will( $this->returnValue( $database ) );
$manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
$actual = $manager->getWriteConnection();
$this->assertSame( $database, $actual );
}
public function testReleaseConnection() {
$database = $this->getIDatabaseMock();
$lb = $this->getLoadBalancerMock();
$lb->expects( $this->once() )
->method( 'reuseConnection' )
->with( $database )
->will( $this->returnValue( null ) );
$manager = new ConnectionManager( $lb );
$manager->releaseConnection( $database );
}
public function testGetReadConnectionRef_nullGroups() {
$database = $this->getIDatabaseMock();
$lb = $this->getLoadBalancerMock();
$lb->expects( $this->once() )
->method( 'getConnectionRef' )
->with( DB_REPLICA, [ 'group1' ], 'someDbName' )
->will( $this->returnValue( $database ) );
$manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
$actual = $manager->getReadConnectionRef();
$this->assertSame( $database, $actual );
}
public function testGetReadConnectionRef_withGroups() {
$database = $this->getIDatabaseMock();
$lb = $this->getLoadBalancerMock();
$lb->expects( $this->once() )
->method( 'getConnectionRef' )
->with( DB_REPLICA, [ 'group2' ], 'someDbName' )
->will( $this->returnValue( $database ) );
$manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
$actual = $manager->getReadConnectionRef( [ 'group2' ] );
$this->assertSame( $database, $actual );
}
public function testGetWriteConnectionRef() {
$database = $this->getIDatabaseMock();
$lb = $this->getLoadBalancerMock();
$lb->expects( $this->once() )
->method( 'getConnectionRef' )
->with( DB_MASTER, [ 'group1' ], 'someDbName' )
->will( $this->returnValue( $database ) );
$manager = new ConnectionManager( $lb, 'someDbName', [ 'group1' ] );
$actual = $manager->getWriteConnectionRef();
$this->assertSame( $database, $actual );
}
}