Use ::class together with createMock in unit tests

This makes it easier for IDEs to find usage
This works even for non-existing classes

Change-Id: I4a6389a9bc0b3c212633841d69bd4f48a7ed6f56
This commit is contained in:
Umherirrender 2020-10-30 14:45:37 +01:00
parent 32491cb005
commit 5d41326891
6 changed files with 33 additions and 31 deletions

View file

@ -628,7 +628,7 @@ class WatchActionTest extends MediaWikiIntegrationTestCase {
// Override WatchedItemStore to think the page is watched, if applicable.
if ( $isWatched ) {
$this->overrideMwServices();
$mock = $this->createMock( 'WatchedItemStore' );
$mock = $this->createMock( WatchedItemStore::class );
$mock->method( 'getWatchedItem' )->willReturn( new WatchedItem(
$user,
$this->watchAction->getTitle(),

View file

@ -6,7 +6,7 @@
class ParserTest extends MediaWikiIntegrationTestCase {
public function provideConstructorArguments() {
// Create a mock Config object that will satisfy ServiceOptions::__construct
$mockConfig = $this->createMock( 'Config' );
$mockConfig = $this->createMock( Config::class );
$mockConfig->method( 'has' )->willReturn( true );
$mockConfig->method( 'get' )->willReturn( 'I like otters.' );
@ -29,23 +29,23 @@ class ParserTest extends MediaWikiIntegrationTestCase {
->method( 'getVariableIDs' )->willReturn( [] );
$newArgs = [
$this->createMock( 'MediaWiki\Config\ServiceOptions' ),
$this->createMock( MediaWiki\Config\ServiceOptions::class ),
$mwFactory,
$this->createMock( 'Language' ),
$this->createMock( 'ParserFactory' ),
$this->createMock( Language::class ),
$this->createMock( ParserFactory::class ),
'a snail can sleep for three years',
$this->createMock( 'MediaWiki\Special\SpecialPageFactory' ),
$this->createMock( 'MediaWiki\Linker\LinkRendererFactory' ),
$this->createMock( 'NamespaceInfo' )
$this->createMock( MediaWiki\Special\SpecialPageFactory::class ),
$this->createMock( MediaWiki\Linker\LinkRendererFactory::class ),
$this->createMock( NamespaceInfo::class )
];
$oldArgs = [
[],
$mwFactory,
$this->createMock( 'Language' ),
$this->createMock( 'ParserFactory' ),
$this->createMock( Language::class ),
$this->createMock( ParserFactory::class ),
'a snail can sleep for three years',
$this->createMock( 'MediaWiki\Special\SpecialPageFactory' )
$this->createMock( MediaWiki\Special\SpecialPageFactory::class )
];
yield 'current_args_without_namespace_info' => [
@ -63,15 +63,15 @@ class ParserTest extends MediaWikiIntegrationTestCase {
yield 'backward_compatible_args_with_link_renderer' => [
array_merge( $oldArgs, [
$mockConfig,
$this->createMock( 'MediaWiki\Linker\LinkRendererFactory' )
$this->createMock( MediaWiki\Linker\LinkRendererFactory::class )
] ),
];
yield 'backward_compatible_args_with_ns_info' => [
array_merge( $oldArgs, [
$mockConfig,
$this->createMock( 'MediaWiki\Linker\LinkRendererFactory' ),
$this->createMock( 'NamespaceInfo' )
$this->createMock( MediaWiki\Linker\LinkRendererFactory::class ),
$this->createMock( NamespaceInfo::class )
] ),
];
}

View file

@ -10,6 +10,7 @@ use MediaWiki\Revision\RevisionStoreRecord;
use MediaWiki\Storage\EditResult;
use MediaWiki\Storage\SlotRecord;
use MediaWikiIntegrationTestCase;
use OutputPage;
use RequestContext;
use Title;
use User;
@ -62,7 +63,7 @@ class UndoIntegrationTest extends MediaWikiIntegrationTestCase {
$request->setVal( 'wpSave', '' );
$context->setRequest( $request );
$outputPage = $this->createMock( 'OutputPage' );
$outputPage = $this->createMock( OutputPage::class );
$context->setOutput( $outputPage );
$context->setUser( $this->getTestSysop()->getUser() );

View file

@ -2,6 +2,7 @@
namespace MediaWiki\Tests\Maintenance;
use Config;
use Maintenance;
use MediaWiki\MediaWikiServices;
use Wikimedia\TestingAccessWrapper;
@ -494,7 +495,7 @@ class MaintenanceTest extends MaintenanceBaseTestCase {
* @covers Maintenance::setConfig
*/
public function testSetConfig() {
$conf = $this->createMock( 'Config' );
$conf = $this->createMock( Config::class );
$this->maintenance->setConfig( $conf );
$this->assertSame( $conf, $this->maintenance->getConfig() );
}

View file

@ -216,7 +216,7 @@ class RevertedTagUpdateTest extends MediaWikiUnitTestCase {
array $softwareChangeTags,
int $revertedTagMaxDepth
) {
$futureChangeTags = $this->getMockBuilder( 'FutureChangeTags' )
$futureChangeTags = $this->getMockBuilder( \FutureChangeTags::class )
->allowMockingUnknownTypes()
->setMethods( [ 'addTags' ] )
->getMock();
@ -272,7 +272,7 @@ class RevertedTagUpdateTest extends MediaWikiUnitTestCase {
* @param EditResult $editResult
*/
public function testInvalidEditResult( EditResult $editResult ) {
$futureChangeTags = $this->getMockBuilder( 'FutureChangeTags' )
$futureChangeTags = $this->getMockBuilder( \FutureChangeTags::class )
->allowMockingUnknownTypes()
->setMethods( [ 'addTags' ] )
->getMock();
@ -308,7 +308,7 @@ class RevertedTagUpdateTest extends MediaWikiUnitTestCase {
'Could not find the newest or oldest reverted revision in the database.'
);
$futureChangeTags = $this->getMockBuilder( 'FutureChangeTags' )
$futureChangeTags = $this->getMockBuilder( \FutureChangeTags::class )
->allowMockingUnknownTypes()
->setMethods( [ 'addTags' ] )
->getMock();
@ -354,7 +354,7 @@ class RevertedTagUpdateTest extends MediaWikiUnitTestCase {
'Could not find the revert revision in the database.'
);
$futureChangeTags = $this->getMockBuilder( 'FutureChangeTags' )
$futureChangeTags = $this->getMockBuilder( \FutureChangeTags::class )
->allowMockingUnknownTypes()
->setMethods( [ 'addTags' ] )
->getMock();
@ -425,7 +425,7 @@ class RevertedTagUpdateTest extends MediaWikiUnitTestCase {
'The revert and reverted revisions belong to different pages.'
);
$futureChangeTags = $this->getMockBuilder( 'FutureChangeTags' )
$futureChangeTags = $this->getMockBuilder( \FutureChangeTags::class )
->allowMockingUnknownTypes()
->setMethods( [ 'addTags' ] )
->getMock();
@ -470,7 +470,7 @@ class RevertedTagUpdateTest extends MediaWikiUnitTestCase {
'notice'
);
$futureChangeTags = $this->getMockBuilder( 'FutureChangeTags' )
$futureChangeTags = $this->getMockBuilder( \FutureChangeTags::class )
->allowMockingUnknownTypes()
->setMethods( [ 'addTags' ] )
->getMock();
@ -513,7 +513,7 @@ class RevertedTagUpdateTest extends MediaWikiUnitTestCase {
'notice'
);
$futureChangeTags = $this->getMockBuilder( 'FutureChangeTags' )
$futureChangeTags = $this->getMockBuilder( \FutureChangeTags::class )
->allowMockingUnknownTypes()
->setMethods( [ 'addTags', 'getTags' ] )
->getMock();
@ -565,7 +565,7 @@ class RevertedTagUpdateTest extends MediaWikiUnitTestCase {
[ 'mw-rollback' ]
);
$futureChangeTags = $this->getMockBuilder( 'FutureChangeTags' )
$futureChangeTags = $this->getMockBuilder( \FutureChangeTags::class )
->allowMockingUnknownTypes()
->setMethods( [ 'addTags', 'getTags' ] )
->getMock();
@ -618,7 +618,7 @@ class RevertedTagUpdateTest extends MediaWikiUnitTestCase {
[ 'mw-undo' ]
);
$futureChangeTags = $this->getMockBuilder( 'FutureChangeTags' )
$futureChangeTags = $this->getMockBuilder( \FutureChangeTags::class )
->allowMockingUnknownTypes()
->setMethods( [ 'addTags', 'getTags' ] )
->getMock();
@ -678,7 +678,7 @@ class RevertedTagUpdateTest extends MediaWikiUnitTestCase {
[ 'mw-undo' ]
);
$futureChangeTags = $this->getMockBuilder( 'FutureChangeTags' )
$futureChangeTags = $this->getMockBuilder( \FutureChangeTags::class )
->allowMockingUnknownTypes()
->setMethods( [ 'addTags', 'getTags' ] )
->getMock();

View file

@ -49,7 +49,7 @@ class KafkaHandlerTest extends \MediaWikiUnitTestCase {
* @dataProvider topicNamingProvider
*/
public function testTopicNaming( $options, $expect ) {
$produce = $this->getMockBuilder( 'Kafka\Produce' )
$produce = $this->getMockBuilder( \Kafka\Produce::class )
->disableOriginalConstructor()
->getMock();
$produce->expects( $this->any() )
@ -86,7 +86,7 @@ class KafkaHandlerTest extends \MediaWikiUnitTestCase {
* @dataProvider swallowsExceptionsWhenRequested
*/
public function testGetAvailablePartitionsException( $options, $expectException ) {
$produce = $this->getMockBuilder( 'Kafka\Produce' )
$produce = $this->getMockBuilder( \Kafka\Produce::class )
->disableOriginalConstructor()
->getMock();
$produce->expects( $this->any() )
@ -117,7 +117,7 @@ class KafkaHandlerTest extends \MediaWikiUnitTestCase {
* @dataProvider swallowsExceptionsWhenRequested
*/
public function testSendException( $options, $expectException ) {
$produce = $this->getMockBuilder( 'Kafka\Produce' )
$produce = $this->getMockBuilder( \Kafka\Produce::class )
->disableOriginalConstructor()
->getMock();
$produce->expects( $this->any() )
@ -145,7 +145,7 @@ class KafkaHandlerTest extends \MediaWikiUnitTestCase {
}
public function testHandlesNullFormatterResult() {
$produce = $this->getMockBuilder( 'Kafka\Produce' )
$produce = $this->getMockBuilder( \Kafka\Produce::class )
->disableOriginalConstructor()
->getMock();
$produce->expects( $this->any() )
@ -179,7 +179,7 @@ class KafkaHandlerTest extends \MediaWikiUnitTestCase {
}
public function testBatchHandlesNullFormatterResult() {
$produce = $this->getMockBuilder( 'Kafka\Produce' )
$produce = $this->getMockBuilder( \Kafka\Produce::class )
->disableOriginalConstructor()
->getMock();
$produce->expects( $this->any() )