Replace all Language and Parser mocks with no-op mocks

Both Language and Parser are extremely complex classes with hundreds
of public methods. We really want to make sure we are not depending
on anything unexpected from these classes. If calls are made into
these classes we want to know exactly what is called.

Doing this also showed that some mocked methods are not even needed.

Change-Id: Icdfff6c07be78a47bf7cadb1813a72581a51272a
This commit is contained in:
thiemowmde 2023-07-27 11:39:46 +02:00 committed by Thiemo Kreuz (WMDE)
parent df5484ea25
commit 2aa421a021
9 changed files with 37 additions and 38 deletions

View file

@ -57,8 +57,8 @@ class CiteIntegrationTest extends \MediaWikiIntegrationTestCase {
$spy->referencesFormatter = $referencesFormatter; $spy->referencesFormatter = $referencesFormatter;
$spy->isSectionPreview = $isSectionPreview; $spy->isSectionPreview = $isSectionPreview;
$output = $cite->checkRefsNoReferences( $parser = $this->createNoOpMock( Parser::class );
$this->createMock( Parser::class ), $isSectionPreview ); $output = $cite->checkRefsNoReferences( $parser, $isSectionPreview );
$this->assertSame( $expectedOutput, $output ); $this->assertSame( $expectedOutput, $output );
} }
@ -92,15 +92,15 @@ class CiteIntegrationTest extends \MediaWikiIntegrationTestCase {
} }
private function newCite(): Cite { private function newCite(): Cite {
$language = $this->createNoOpMock( Language::class );
$mockOptions = $this->createMock( ParserOptions::class ); $mockOptions = $this->createMock( ParserOptions::class );
$mockOptions->method( 'getIsPreview' )->willReturn( false ); $mockOptions->method( 'getIsPreview' )->willReturn( false );
$mockOptions->method( 'getIsSectionPreview' )->willReturn( false ); $mockOptions->method( 'getIsSectionPreview' )->willReturn( false );
$mockOptions->method( 'getUserLangObj' )->willReturn(
$this->createMock( Language::class ) ); $mockParser = $this->createNoOpMock( Parser::class, [ 'getOptions', 'getContentLanguage' ] );
$mockParser = $this->createMock( Parser::class );
$mockParser->method( 'getOptions' )->willReturn( $mockOptions ); $mockParser->method( 'getOptions' )->willReturn( $mockOptions );
$mockParser->method( 'getContentLanguage' )->willReturn( $mockParser->method( 'getContentLanguage' )->willReturn( $language );
$this->createMock( Language::class ) );
return new Cite( $mockParser ); return new Cite( $mockParser );
} }

View file

@ -384,7 +384,7 @@ class CiteTest extends \MediaWikiIntegrationTestCase {
global $wgCiteResponsiveReferences; global $wgCiteResponsiveReferences;
$wgCiteResponsiveReferences = false; $wgCiteResponsiveReferences = false;
$parser = $this->createMock( Parser::class ); $parser = $this->createNoOpMock( Parser::class, [ 'recursiveTagParse' ] );
$cite = $this->newCite(); $cite = $this->newCite();
/** @var Cite $spy */ /** @var Cite $spy */
@ -479,7 +479,7 @@ class CiteTest extends \MediaWikiIntegrationTestCase {
array $expectedRefs, array $expectedRefs,
bool $isSectionPreview = false bool $isSectionPreview = false
) { ) {
$mockParser = $this->createMock( Parser::class ); $mockParser = $this->createNoOpMock( Parser::class, [ 'getStripState' ] );
$mockParser->method( 'getStripState' ) $mockParser->method( 'getStripState' )
->willReturn( $this->createMock( StripState::class ) ); ->willReturn( $this->createMock( StripState::class ) );
@ -658,10 +658,8 @@ class CiteTest extends \MediaWikiIntegrationTestCase {
->method( 'setPageProperty' ) ->method( 'setPageProperty' )
->with( Cite::BOOK_REF_PROPERTY, '' ); ->with( Cite::BOOK_REF_PROPERTY, '' );
$mockParser = $this->createMock( Parser::class ); $mockParser = $this->createNoOpMock( Parser::class, [ 'getOutput' ] );
$mockParser->method( 'getOutput' )->willReturn( $mockOutput ); $mockParser->method( 'getOutput' )->willReturn( $mockOutput );
$mockParser->method( 'getStripState' )
->willReturn( $this->createMock( StripState::class ) );
$cite = $this->newCite(); $cite = $this->newCite();
/** @var Cite $spy */ /** @var Cite $spy */
@ -675,12 +673,14 @@ class CiteTest extends \MediaWikiIntegrationTestCase {
* @coversNothing * @coversNothing
*/ */
public function testReferencesSectionPreview() { public function testReferencesSectionPreview() {
$language = $this->createNoOpMock( Language::class );
$parserOptions = $this->createMock( ParserOptions::class ); $parserOptions = $this->createMock( ParserOptions::class );
$parserOptions->method( 'getIsSectionPreview' )->willReturn( true ); $parserOptions->method( 'getIsSectionPreview' )->willReturn( true );
$parser = $this->createMock( Parser::class ); $parser = $this->createNoOpMock( Parser::class, [ 'getOptions', 'getContentLanguage' ] );
$parser->method( 'getOptions' )->willReturn( $parserOptions ); $parser->method( 'getOptions' )->willReturn( $parserOptions );
$parser->method( 'getContentLanguage' )->willReturn( $this->createMock( Language::class ) ); $parser->method( 'getContentLanguage' )->willReturn( $language );
/** @var Cite $cite */ /** @var Cite $cite */
$cite = TestingAccessWrapper::newFromObject( new Cite( $parser ) ); $cite = TestingAccessWrapper::newFromObject( new Cite( $parser ) );
@ -703,15 +703,14 @@ class CiteTest extends \MediaWikiIntegrationTestCase {
} }
private function newCite( bool $isSectionPreview = false ): Cite { private function newCite( bool $isSectionPreview = false ): Cite {
$language = $this->createNoOpMock( Language::class, [ '__debugInfo' ] );
$mockOptions = $this->createMock( ParserOptions::class ); $mockOptions = $this->createMock( ParserOptions::class );
$mockOptions->method( 'getIsPreview' )->willReturn( false );
$mockOptions->method( 'getIsSectionPreview' )->willReturn( $isSectionPreview ); $mockOptions->method( 'getIsSectionPreview' )->willReturn( $isSectionPreview );
$mockOptions->method( 'getUserLangObj' )->willReturn(
$this->createMock( Language::class ) ); $mockParser = $this->createNoOpMock( Parser::class, [ 'getOptions', 'getContentLanguage' ] );
$mockParser = $this->createMock( Parser::class );
$mockParser->method( 'getOptions' )->willReturn( $mockOptions ); $mockParser->method( 'getOptions' )->willReturn( $mockOptions );
$mockParser->method( 'getContentLanguage' )->willReturn( $mockParser->method( 'getContentLanguage' )->willReturn( $language );
$this->createMock( Language::class ) );
return new Cite( $mockParser ); return new Cite( $mockParser );
} }

View file

@ -49,7 +49,7 @@ class FootnoteMarkFormatterTest extends \MediaWikiIntegrationTestCase {
return $msg; return $msg;
} }
); );
$mockParser = $this->createMock( Parser::class ); $mockParser = $this->createNoOpMock( Parser::class, [ 'recursiveTagParse' ] );
$mockParser->method( 'recursiveTagParse' )->willReturnArgument( 0 ); $mockParser->method( 'recursiveTagParse' )->willReturnArgument( 0 );
$formatter = new FootnoteMarkFormatter( $formatter = new FootnoteMarkFormatter(
$mockErrorReporter, $mockErrorReporter,
@ -167,8 +167,8 @@ class FootnoteMarkFormatterTest extends \MediaWikiIntegrationTestCase {
$mockMessageLocalizer $mockMessageLocalizer
) ); ) );
$output = $formatter->getLinkLabel( $parser = $this->createNoOpMock( Parser::class );
$this->createMock( Parser::class ), $group, $offset ); $output = $formatter->getLinkLabel( $parser, $group, $offset );
$this->assertSame( $expectedLabel, $output ); $this->assertSame( $expectedLabel, $output );
} }

View file

@ -20,7 +20,7 @@ class CiteParserHooksTest extends \MediaWikiUnitTestCase {
* @covers ::onParserFirstCallInit * @covers ::onParserFirstCallInit
*/ */
public function testOnParserFirstCallInit() { public function testOnParserFirstCallInit() {
$parser = $this->createMock( Parser::class ); $parser = $this->createNoOpMock( Parser::class, [ 'setHook' ] );
$parser->expects( $this->exactly( 2 ) ) $parser->expects( $this->exactly( 2 ) )
->method( 'setHook' ) ->method( 'setHook' )
->withConsecutive( ->withConsecutive(

View file

@ -19,7 +19,7 @@ class CiteParserTagHooksTest extends \MediaWikiUnitTestCase {
* @covers ::register * @covers ::register
*/ */
public function testRegister() { public function testRegister() {
$parser = $this->createMock( Parser::class ); $parser = $this->createNoOpMock( Parser::class, [ 'setHook' ] );
$parser->expects( $this->exactly( 2 ) ) $parser->expects( $this->exactly( 2 ) )
->method( 'setHook' ) ->method( 'setHook' )
->withConsecutive( ->withConsecutive(

View file

@ -60,7 +60,7 @@ class ErrorReporterTest extends \MediaWikiUnitTestCase {
} }
private function createLanguage(): Language { private function createLanguage(): Language {
$language = $this->createMock( Language::class ); $language = $this->createNoOpMock( Language::class, [ 'getDir', 'getHtmlCode' ] );
$language->method( 'getDir' )->willReturn( 'rtl' ); $language->method( 'getDir' )->willReturn( 'rtl' );
$language->method( 'getHtmlCode' )->willReturn( 'qqx' ); $language->method( 'getHtmlCode' )->willReturn( 'qqx' );
return $language; return $language;
@ -86,7 +86,7 @@ class ErrorReporterTest extends \MediaWikiUnitTestCase {
$parserOptions = $this->createMock( ParserOptions::class ); $parserOptions = $this->createMock( ParserOptions::class );
$parserOptions->method( 'getUserLangObj' )->willReturn( $language ); $parserOptions->method( 'getUserLangObj' )->willReturn( $language );
$parser = $this->createMock( Parser::class ); $parser = $this->createNoOpMock( Parser::class, [ 'addTrackingCategory', 'getOptions', 'recursiveTagParse' ] );
$parser->expects( $this->exactly( count( $expectedCategories ) ) ) $parser->expects( $this->exactly( count( $expectedCategories ) ) )
->method( 'addTrackingCategory' ) ->method( 'addTrackingCategory' )
->withConsecutive( $expectedCategories ); ->withConsecutive( $expectedCategories );

View file

@ -15,7 +15,7 @@ class ReferenceMessageLocalizerUnitTest extends \MediaWikiUnitTestCase {
* @covers ::__construct * @covers ::__construct
*/ */
public function testLocalizeSeparators() { public function testLocalizeSeparators() {
$mockLanguage = $this->createMock( Language::class ); $mockLanguage = $this->createNoOpMock( Language::class, [ 'separatorTransformTable' ] );
$mockLanguage->method( 'separatorTransformTable' )->willReturn( [ '.' => ',', '0' => '' ] ); $mockLanguage->method( 'separatorTransformTable' )->willReturn( [ '.' => ',', '0' => '' ] );
$messageLocalizer = new ReferenceMessageLocalizer( $mockLanguage ); $messageLocalizer = new ReferenceMessageLocalizer( $mockLanguage );
$this->assertSame( '10,0', $messageLocalizer->localizeSeparators( '10.0' ) ); $this->assertSame( '10,0', $messageLocalizer->localizeSeparators( '10.0' ) );
@ -25,7 +25,7 @@ class ReferenceMessageLocalizerUnitTest extends \MediaWikiUnitTestCase {
* @covers ::localizeDigits * @covers ::localizeDigits
*/ */
public function testLocalizeDigits() { public function testLocalizeDigits() {
$mockLanguage = $this->createMock( Language::class ); $mockLanguage = $this->createNoOpMock( Language::class, [ 'formatNumNoSeparators' ] );
$mockLanguage->method( 'formatNumNoSeparators' )->willReturn( 'ה' ); $mockLanguage->method( 'formatNumNoSeparators' )->willReturn( 'ה' );
$messageLocalizer = new ReferenceMessageLocalizer( $mockLanguage ); $messageLocalizer = new ReferenceMessageLocalizer( $mockLanguage );
$this->assertSame( 'ה', $messageLocalizer->localizeDigits( '5' ) ); $this->assertSame( 'ה', $messageLocalizer->localizeDigits( '5' ) );

View file

@ -44,7 +44,7 @@ class ReferenceStackTest extends \MediaWikiUnitTestCase {
for ( $i = 0; $i < count( $refs ); $i++ ) { for ( $i = 0; $i < count( $refs ); $i++ ) {
$result = $stack->pushRef( $result = $stack->pushRef(
$this->createMock( Parser::class ), $this->createNoOpMock( Parser::class ),
$mockStripState, $mockStripState,
...$refs[$i] ...$refs[$i]
); );
@ -991,7 +991,7 @@ class ReferenceStackTest extends \MediaWikiUnitTestCase {
$mockStripState = $this->createMock( StripState::class ); $mockStripState = $this->createMock( StripState::class );
$mockStripState->method( 'unstripBoth' )->willReturnArgument( 0 ); $mockStripState->method( 'unstripBoth' )->willReturnArgument( 0 );
$stack->pushRef( $stack->pushRef(
$this->createMock( Parser::class ), $this->createNoOpMock( Parser::class ),
$mockStripState, $mockStripState,
'text', [], 'text', [],
'foo', null, 'a', null, 'rtl' 'foo', null, 'a', null, 'rtl'

View file

@ -24,7 +24,7 @@ class ReferencesFormatterTest extends \MediaWikiUnitTestCase {
* @dataProvider provideFormatReferences * @dataProvider provideFormatReferences
*/ */
public function testFormatReferences( array $refs, string $expectedOutput ) { public function testFormatReferences( array $refs, string $expectedOutput ) {
$mockParser = $this->createMock( Parser::class ); $mockParser = $this->createNoOpMock( Parser::class, [ 'recursiveTagParse' ] );
$mockParser->method( 'recursiveTagParse' )->willReturnArgument( 0 ); $mockParser->method( 'recursiveTagParse' )->willReturnArgument( 0 );
$mockErrorReporter = $this->createMock( ErrorReporter::class ); $mockErrorReporter = $this->createMock( ErrorReporter::class );
@ -229,8 +229,8 @@ class ReferencesFormatterTest extends \MediaWikiUnitTestCase {
$mockMessageLocalizer $mockMessageLocalizer
) ); ) );
$output = $formatter->formatListItem( $parser = $this->createNoOpMock( Parser::class );
$this->createMock( Parser::class ), $key, $val, false ); $output = $formatter->formatListItem( $parser, $key, $val, false );
$this->assertSame( $expectedOutput, $output ); $this->assertSame( $expectedOutput, $output );
} }
@ -321,8 +321,8 @@ class ReferencesFormatterTest extends \MediaWikiUnitTestCase {
$this->createMock( ReferenceMessageLocalizer::class ) $this->createMock( ReferenceMessageLocalizer::class )
) ); ) );
$output = $formatter->referenceText( $parser = $this->createNoOpMock( Parser::class );
$this->createMock( Parser::class ), $key, $text, $isSectionPreview ); $output = $formatter->referenceText( $parser, $key, $text, $isSectionPreview );
$this->assertSame( $expectedOutput, $output ); $this->assertSame( $expectedOutput, $output );
} }
@ -384,8 +384,8 @@ class ReferencesFormatterTest extends \MediaWikiUnitTestCase {
$mockMessageLocalizer $mockMessageLocalizer
) ); ) );
$label = $formatter->referencesFormatEntryAlternateBacklinkLabel( $parser = $this->createNoOpMock( Parser::class );
$this->createMock( Parser::class ), $offset ); $label = $formatter->referencesFormatEntryAlternateBacklinkLabel( $parser, $offset );
if ( $expectedLabel !== null ) { if ( $expectedLabel !== null ) {
$this->assertSame( $expectedLabel, $label ); $this->assertSame( $expectedLabel, $label );
} }