From: Aryeh Gregor Date: Wed, 28 Aug 2019 10:01:39 +0000 (+0300) Subject: createNoOpMock() method for PHPUnit tests X-Git-Tag: 1.34.0-rc.0~495^2 X-Git-Url: http://git.cyclocoop.org/?a=commitdiff_plain;h=709773ab57478dfc9cbc7dae9edf158e2f42f4fd;p=lhc%2Fweb%2Fwiklou.git 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 --- diff --git a/tests/phpunit/MediaWikiTestCaseTrait.php b/tests/phpunit/MediaWikiTestCaseTrait.php index 77d7c04482..f047d826b5 100644 --- a/tests/phpunit/MediaWikiTestCaseTrait.php +++ b/tests/phpunit/MediaWikiTestCaseTrait.php @@ -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; + } } diff --git a/tests/phpunit/includes/MovePageTest.php b/tests/phpunit/includes/MovePageTest.php index 2895fa286c..de92d90c54 100644 --- a/tests/phpunit/includes/MovePageTest.php +++ b/tests/phpunit/includes/MovePageTest.php @@ -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() ); } diff --git a/tests/phpunit/includes/watcheditem/WatchedItemStoreUnitTest.php b/tests/phpunit/includes/watcheditem/WatchedItemStoreUnitTest.php index 9616672bbd..fbb893e204 100644 --- a/tests/phpunit/includes/watcheditem/WatchedItemStoreUnitTest.php +++ b/tests/phpunit/includes/watcheditem/WatchedItemStoreUnitTest.php @@ -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( [ diff --git a/tests/phpunit/unit/includes/filebackend/lockmanager/LockManagerGroupFactoryTest.php b/tests/phpunit/unit/includes/filebackend/lockmanager/LockManagerGroupFactoryTest.php index 38fcf29363..ca39341032 100644 --- a/tests/phpunit/unit/includes/filebackend/lockmanager/LockManagerGroupFactoryTest.php +++ b/tests/phpunit/unit/includes/filebackend/lockmanager/LockManagerGroupFactoryTest.php @@ -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();