createNoOpMock() method for PHPUnit tests
This is just a shortcut for a commonly-used pattern, when you want to create a mock that's never supposed to have methods called. Change-Id: Ia7267e3d3108c1ff94485f7e44bf409808a762be
This commit is contained in:
parent
eb4ac89d66
commit
709773ab57
4 changed files with 32 additions and 43 deletions
|
|
@ -17,4 +17,16 @@ trait MediaWikiTestCaseTrait {
|
|||
...array_map( [ $this, 'matches' ], $values )
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a PHPUnit mock that is expected to never have any methods called on it.
|
||||
*
|
||||
* @param string $type
|
||||
* @return object
|
||||
*/
|
||||
protected function createNoOpMock( $type ) {
|
||||
$mock = $this->createMock( $type );
|
||||
$mock->expects( $this->never() )->method( $this->anything() );
|
||||
return $mock;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,16 +11,6 @@ use Wikimedia\Rdbms\LoadBalancer;
|
|||
* @group Database
|
||||
*/
|
||||
class MovePageTest extends MediaWikiTestCase {
|
||||
/**
|
||||
* @param string $class
|
||||
* @return object A mock that throws on any method call
|
||||
*/
|
||||
private function getNoOpMock( $class ) {
|
||||
$mock = $this->createMock( $class );
|
||||
$mock->expects( $this->never() )->method( $this->anythingBut( '__destruct' ) );
|
||||
return $mock;
|
||||
}
|
||||
|
||||
/**
|
||||
* The only files that exist are 'File:Existent.jpg', 'File:Existent2.jpg', and
|
||||
* 'File:Existent-file-no-page.jpg'. Calling unexpected methods causes a test failure.
|
||||
|
|
@ -72,7 +62,7 @@ class MovePageTest extends MediaWikiTestCase {
|
|||
private function newMovePage( $old, $new, array $params = [] ) : MovePage {
|
||||
$mockLB = $this->createMock( LoadBalancer::class );
|
||||
$mockLB->method( 'getConnection' )
|
||||
->willReturn( $params['db'] ?? $this->getNoOpMock( IDatabase::class ) );
|
||||
->willReturn( $params['db'] ?? $this->createNoOpMock( IDatabase::class ) );
|
||||
$mockLB->expects( $this->never() )
|
||||
->method( $this->anythingBut( 'getConnection', '__destruct' ) );
|
||||
|
||||
|
|
@ -98,8 +88,8 @@ class MovePageTest extends MediaWikiTestCase {
|
|||
),
|
||||
$mockLB,
|
||||
$params['nsInfo'] ?? $mockNsInfo,
|
||||
$params['wiStore'] ?? $this->getNoOpMock( WatchedItemStore::class ),
|
||||
$params['permMgr'] ?? $this->getNoOpMock( PermissionManager::class ),
|
||||
$params['wiStore'] ?? $this->createNoOpMock( WatchedItemStore::class ),
|
||||
$params['permMgr'] ?? $this->createNoOpMock( PermissionManager::class ),
|
||||
$params['repoGroup'] ?? $this->getMockRepoGroup()
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1837,8 +1837,7 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
|
|||
new WatchedItem( $user, $targets[0], '20151212010101' ),
|
||||
new WatchedItem( $user, $targets[1], null ),
|
||||
];
|
||||
$mockDb = $this->getMockDb();
|
||||
$mockDb->expects( $this->never() )->method( $this->anything() );
|
||||
$mockDb = $this->createNoOpMock( IDatabase::class );
|
||||
|
||||
$mockCache = $this->getMockCache();
|
||||
$mockCache->expects( $this->at( 1 ) )
|
||||
|
|
@ -1864,16 +1863,18 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
|
|||
}
|
||||
|
||||
public function testGetNotificationTimestampsBatch_anonymousUser() {
|
||||
if ( defined( 'HHVM_VERSION' ) ) {
|
||||
$this->markTestSkipped( 'HHVM Reflection buggy' );
|
||||
}
|
||||
|
||||
$targets = [
|
||||
new TitleValue( 0, 'SomeDbKey' ),
|
||||
new TitleValue( 1, 'AnotherDbKey' ),
|
||||
];
|
||||
|
||||
$mockDb = $this->getMockDb();
|
||||
$mockDb->expects( $this->never() )->method( $this->anything() );
|
||||
$mockDb = $this->createNoOpMock( IDatabase::class );
|
||||
|
||||
$mockCache = $this->getMockCache();
|
||||
$mockCache->expects( $this->never() )->method( $this->anything() );
|
||||
$mockCache = $this->createNoOpMock( HashBagOStuff::class );
|
||||
|
||||
$store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
|
||||
|
||||
|
|
@ -2086,8 +2087,7 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
|
|||
|
||||
$mockQueueGroup = $this->getMockJobQueueGroup();
|
||||
|
||||
$mockRevisionRecord = $this->createMock( RevisionRecord::class );
|
||||
$mockRevisionRecord->expects( $this->never() )->method( $this->anything() );
|
||||
$mockRevisionRecord = $this->createNoOpMock( RevisionRecord::class );
|
||||
|
||||
$mockRevisionLookup = $this->getMockRevisionLookup( [
|
||||
'getTimestampFromId' => function () {
|
||||
|
|
@ -2144,11 +2144,8 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
|
|||
$oldid = 22;
|
||||
$title = new TitleValue( 0, 'SomeDbKey' );
|
||||
|
||||
$mockRevision = $this->createMock( RevisionRecord::class );
|
||||
$mockRevision->expects( $this->never() )->method( $this->anything() );
|
||||
|
||||
$mockNextRevision = $this->createMock( RevisionRecord::class );
|
||||
$mockNextRevision->expects( $this->never() )->method( $this->anything() );
|
||||
$mockRevision = $this->createNoOpMock( RevisionRecord::class );
|
||||
$mockNextRevision = $this->createNoOpMock( RevisionRecord::class );
|
||||
|
||||
$mockDb = $this->getMockDb();
|
||||
$mockDb->expects( $this->once() )
|
||||
|
|
@ -2258,11 +2255,8 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
|
|||
|
||||
$mockQueueGroup = $this->getMockJobQueueGroup();
|
||||
|
||||
$mockRevision = $this->createMock( RevisionRecord::class );
|
||||
$mockRevision->expects( $this->never() )->method( $this->anything() );
|
||||
|
||||
$mockNextRevision = $this->createMock( RevisionRecord::class );
|
||||
$mockNextRevision->expects( $this->never() )->method( $this->anything() );
|
||||
$mockRevision = $this->createNoOpMock( RevisionRecord::class );
|
||||
$mockNextRevision = $this->createNoOpMock( RevisionRecord::class );
|
||||
|
||||
$mockRevisionLookup = $this->getMockRevisionLookup(
|
||||
[
|
||||
|
|
@ -2350,11 +2344,8 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
|
|||
|
||||
$mockQueueGroup = $this->getMockJobQueueGroup();
|
||||
|
||||
$mockRevision = $this->createMock( RevisionRecord::class );
|
||||
$mockRevision->expects( $this->never() )->method( $this->anything() );
|
||||
|
||||
$mockNextRevision = $this->createMock( RevisionRecord::class );
|
||||
$mockNextRevision->expects( $this->never() )->method( $this->anything() );
|
||||
$mockRevision = $this->createNoOpMock( RevisionRecord::class );
|
||||
$mockNextRevision = $this->createNoOpMock( RevisionRecord::class );
|
||||
|
||||
$mockRevisionLookup = $this->getMockRevisionLookup(
|
||||
[
|
||||
|
|
@ -2442,11 +2433,8 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
|
|||
|
||||
$mockQueueGroup = $this->getMockJobQueueGroup();
|
||||
|
||||
$mockRevision = $this->createMock( RevisionRecord::class );
|
||||
$mockRevision->expects( $this->never() )->method( $this->anything() );
|
||||
|
||||
$mockNextRevision = $this->createMock( RevisionRecord::class );
|
||||
$mockNextRevision->expects( $this->never() )->method( $this->anything() );
|
||||
$mockRevision = $this->createNoOpMock( RevisionRecord::class );
|
||||
$mockNextRevision = $this->createNoOpMock( RevisionRecord::class );
|
||||
|
||||
$mockRevisionLookup = $this->getMockRevisionLookup(
|
||||
[
|
||||
|
|
|
|||
|
|
@ -10,8 +10,7 @@ use Wikimedia\Rdbms\LBFactory;
|
|||
*/
|
||||
class LockManagerGroupFactoryTest extends MediaWikiUnitTestCase {
|
||||
public function testGetLockManagerGroup() {
|
||||
$mockLbFactory = $this->createMock( LBFactory::class );
|
||||
$mockLbFactory->expects( $this->never() )->method( $this->anything() );
|
||||
$mockLbFactory = $this->createNoOpMock( LBFactory::class );
|
||||
|
||||
$factory = new LockManagerGroupFactory( 'defaultDomain', [], $mockLbFactory );
|
||||
$lbmUnspecified = $factory->getLockManagerGroup();
|
||||
|
|
|
|||
Loading…
Reference in a new issue