| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter\Tests\Unit; |
| 4 |
|
| 5 |
use MediaWiki\Extension\AbuseFilter\CentralDBManager; |
| 6 |
use MediaWiki\Extension\AbuseFilter\CentralDBNotAvailableException; |
| 7 |
use MediaWikiUnitTestCase; |
| 8 |
use Wikimedia\Rdbms\IDatabase; |
| 9 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 10 |
use Wikimedia\Rdbms\LBFactory; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class CentralDBManagerTest extends MediaWikiUnitTestCase { |
| 18 |
public function testConstruct() { |
| 19 |
$this->assertInstanceOf( |
| 20 |
CentralDBManager::class, |
| 21 |
new CentralDBManager( |
| 22 |
$this->createMock( LBFactory::class ), |
| 23 |
'foo', |
| 24 |
true |
| 25 |
) |
| 26 |
); |
| 27 |
} |
| 28 |
|
| 29 |
public function testGetConnection() { |
| 30 |
$expected = $this->createMock( IDatabase::class ); |
| 31 |
$lb = $this->createMock( ILoadBalancer::class ); |
| 32 |
$lb->method( 'getConnection' )->willReturn( $expected ); |
| 33 |
$lbFactory = $this->createMock( LBFactory::class ); |
| 34 |
$lbFactory->method( 'getMainLB' )->willReturn( $lb ); |
| 35 |
$dbManager = new CentralDBManager( $lbFactory, 'foo', true ); |
| 36 |
$this->assertSame( $expected, $dbManager->getConnection( DB_REPLICA ) ); |
| 37 |
} |
| 38 |
|
| 39 |
public function testGetConnection_invalid() { |
| 40 |
$lbFactory = $this->createMock( LBFactory::class ); |
| 41 |
$dbManager = new CentralDBManager( $lbFactory, null, true ); |
| 42 |
$this->expectException( CentralDBNotAvailableException::class ); |
| 43 |
$dbManager->getConnection( DB_REPLICA ); |
| 44 |
} |
| 45 |
|
| 46 |
public function testGetCentralDBName() { |
| 47 |
$expected = 'foobar'; |
| 48 |
$lbFactory = $this->createMock( LBFactory::class ); |
| 49 |
$dbManager = new CentralDBManager( $lbFactory, $expected, true ); |
| 50 |
$this->assertSame( $expected, $dbManager->getCentralDBName() ); |
| 51 |
} |
| 52 |
|
| 53 |
public function testGetCentralDBName_invalid() { |
| 54 |
$lbFactory = $this->createMock( LBFactory::class ); |
| 55 |
$dbManager = new CentralDBManager( $lbFactory, null, true ); |
| 56 |
$this->expectException( CentralDBNotAvailableException::class ); |
| 57 |
$dbManager->getCentralDBName(); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function testFilterIsCentral( bool $value ) { |
| 65 |
$lbFactory = $this->createMock( LBFactory::class ); |
| 66 |
$dbManager = new CentralDBManager( $lbFactory, 'foo', $value ); |
| 67 |
$this->assertSame( $value, $dbManager->filterIsCentral() ); |
| 68 |
} |
| 69 |
|
| 70 |
public static function provideIsCentral() { |
| 71 |
return [ |
| 72 |
'central' => [ true ], |
| 73 |
'not central' => [ false ] |
| 74 |
]; |
| 75 |
} |
| 76 |
} |