From 6f918ccbb731ce1bcc42c3ca60d35f8f372d7688 Mon Sep 17 00:00:00 2001 From: Tobias Oetterer Date: Sat, 13 Jul 2024 16:06:29 +0200 Subject: [PATCH] fixed unit tests * fix unit tests for 1.40+ * delegate handling of suppressed modal from magic word to class BootstrapComponentsService --- src/BootstrapComponentsService.php | 21 +++++ src/Hooks/OutputPageParserOutput.php | 11 ++- src/HooksHandler.php | 12 +-- src/ImageModal.php | 32 ++------ src/ModalBuilder.php | 1 + src/ParserOutputHelper.php | 41 ++++++---- tests/phpunit/Unit/AbstractComponentTest.php | 17 ++-- .../Unit/BootstrapComponentServiceTest.php | 3 +- tests/phpunit/Unit/Components/AlertTest.php | 20 +++-- tests/phpunit/Unit/Components/ButtonTest.php | 34 +++++--- .../Unit/Hooks/OutputPageParserOutputTest.php | 3 +- tests/phpunit/Unit/ImageModalTest.php | 42 +++++----- tests/phpunit/Unit/ImageModalTriggerTest.php | 78 ++++++++++++------- tests/phpunit/Unit/ParserOutputHelperTest.php | 57 ++------------ 14 files changed, 199 insertions(+), 173 deletions(-) diff --git a/src/BootstrapComponentsService.php b/src/BootstrapComponentsService.php index 069ca32..cc11ea9 100644 --- a/src/BootstrapComponentsService.php +++ b/src/BootstrapComponentsService.php @@ -20,6 +20,10 @@ class BootstrapComponentsService */ private Config $mainConfig; + /** + * @var bool + */ + private bool $modalsSuppressedByMagicWord; /** * Holds the name of the skin we use (or false, if there is no skin). @@ -31,6 +35,15 @@ class BootstrapComponentsService public function __construct( Config $mainConfig ) { $this->mainConfig = $mainConfig; $this->activeComponents = []; + $this->modalsSuppressedByMagicWord = false; + } + + + /** + * @return bool + */ + public function areModalsSuppressedByMagicWord(): bool { + return $this->modalsSuppressedByMagicWord; } /** @@ -61,6 +74,14 @@ public function registerComponentAsActive( string $componentName ): void { $this->activeComponents[$componentName] = true; } + /** + * @param bool $modalsSuppressedByMagicWord + * @return void + */ + public function setModalsSuppressedByMagicWord( bool $modalsSuppressedByMagicWord ): void { + $this->modalsSuppressedByMagicWord = $modalsSuppressedByMagicWord; + } + /** * Returns true, if active skin is vector * diff --git a/src/Hooks/OutputPageParserOutput.php b/src/Hooks/OutputPageParserOutput.php index 9a68996..f6e6fb1 100644 --- a/src/Hooks/OutputPageParserOutput.php +++ b/src/Hooks/OutputPageParserOutput.php @@ -30,6 +30,11 @@ use MediaWiki\Extension\BootstrapComponents\BootstrapComponentsService; use OutputPage; use ParserOutput; +/* + * TODO: When dropping support for 1.39, use these: +use MediaWiki\Output\OutputPage; +use MediaWiki\Parser\ParserOutput; +*/ /** * Class OutputPageParserOutput @@ -126,16 +131,14 @@ protected function getBootstrapComponentsService(): BootstrapComponentsService { /** * @return OutputPage */ - protected function getOutputPage(): OutputPage - { + protected function getOutputPage(): OutputPage { return $this->outputPage; } /** * @return ParserOutput */ - protected function getParserOutput(): ParserOutput - { + protected function getParserOutput(): ParserOutput { return $this->parserOutput; } } diff --git a/src/HooksHandler.php b/src/HooksHandler.php index 0b07332..6ead633 100644 --- a/src/HooksHandler.php +++ b/src/HooksHandler.php @@ -14,7 +14,7 @@ use MediaWiki\Hook\ParserFirstCallInitHook; use MediaWiki\Hook\SetupAfterCacheHook; use MediaWiki\MediaWikiServices; -use Parser; +use MediaWiki\Parser\Parser; use SMW\Utils\File; use StripState; @@ -168,14 +168,10 @@ public function onImageBeforeProduceHTML( * @return bool */ public function onInternalParseBeforeLinks( $parser, &$text, $stripState ): bool { - // we do not use our ParserOutputHelper class here, for we would need to reset it in integration tests. - // resetting our factory build classes is unfortunately a little skittish - $parser->getOutput()->setExtensionData( - BootstrapComponents::EXTENSION_DATA_NO_IMAGE_MODAL, - MediaWikiServices::getInstance()->getMagicWordFactory()->get( 'BSC_NO_IMAGE_MODAL' ) - ->matchAndRemove( $text ) + $this->getBootstrapComponentsService()->setModalsSuppressedByMagicWord( + MediaWikiServices::getInstance() + ->getMagicWordFactory()->get( 'BSC_NO_IMAGE_MODAL' )->matchAndRemove( $text ) ); - return true; } diff --git a/src/ImageModal.php b/src/ImageModal.php index e4e5b1d..455f0cb 100644 --- a/src/ImageModal.php +++ b/src/ImageModal.php @@ -1,6 +1,6 @@ file = $file; - $this->dummyLinker = $dummyLinker; $this->title = $title; $this->nestingController = $nestingController; @@ -195,6 +188,7 @@ public function parse( array &$frameParams, array &$handlerParams, &$time, &$res if ( $res === '' ) { // ImageModal::turnParamsIntoModal returns the empty string, when something went wrong + // returning true means, delegating the image rendering back to \MediaWiki\Linker\Linker::makeImageLink return true; } return false; @@ -281,14 +275,6 @@ protected function getBootstrapComponentsService(): BootstrapComponentsService { return $this->bootstrapComponentService; } - /** - * @return DummyLinker - */ - /** @scrutinizer ignore-unused */ - protected function getDummyLinker(): DummyLinker { - return $this->dummyLinker; - } - /** * @return File */ @@ -312,7 +298,6 @@ protected function getParentComponent(): bool|NestableInterface|null { /** * @return ParserOutputHelper - * @deprecated */ protected function getParserOutputHelper(): ParserOutputHelper { return $this->parserOutputHelper; @@ -422,16 +407,13 @@ private function assertImageModalNotSuppressed( array $frameParams ): bool ) { return false; } - /** @see ParserOutputHelper::areImageModalsSuppressed as to why we need to use the global parser! */ - $parser = MediaWikiServices::getInstance()->getParser(); - // the is_null test has to be added because otherwise some unit tests will fail - return is_null( $parser->getOutput() ) - || !$parser->getOutput()->getExtensionData( BootstrapComponents::EXTENSION_DATA_NO_IMAGE_MODAL ); + return !MediaWikiServices::getInstance()->getService( 'BootstrapComponentsService' ) + ->areModalsSuppressedByMagicWord(); } /** * @param MediaTransformOutput $img - * @param array $sanitizedFrameParams + * @param array $sanitizedFrameParams * * @return string */ diff --git a/src/ModalBuilder.php b/src/ModalBuilder.php index 1e89bfe..d1b6f23 100644 --- a/src/ModalBuilder.php +++ b/src/ModalBuilder.php @@ -27,6 +27,7 @@ namespace MediaWiki\Extension\BootstrapComponents; use \Html; +use MediaWiki\MediaWikiServices; /** * Class ModalBase diff --git a/src/ParserOutputHelper.php b/src/ParserOutputHelper.php index 235fac5..9877ff0 100644 --- a/src/ParserOutputHelper.php +++ b/src/ParserOutputHelper.php @@ -26,9 +26,21 @@ namespace MediaWiki\Extension\BootstrapComponents; -use \Html; -use \ParserOutput; -use \Title; +/* + * TODO: When dropping support for MW1.39, use these class imports: +use MediaWiki\Html\Html; +use MediaWiki\Message\Message; +use MediaWiki\Parser\Parser; +use MediaWiki\Parser\ParserOutput; +use MediaWiki\Title\Title; +*/ + +use Html; +use Message; +use Parser; +use ParserOutput; +use Title; + /** * Class ParserOutputHelper @@ -54,7 +66,7 @@ class ParserOutputHelper { private $articleTrackedOnError; /** - * @var \Parser $parser + * @var Parser $parser */ private $parser; @@ -64,7 +76,7 @@ class ParserOutputHelper { * * Do not instantiate directly, but use {@see ApplicationFactory::getParserOutputHelper} instead. * - * @param \Parser $parser + * @param Parser $parser * * @see ApplicationFactory::getParserOutputHelper */ @@ -115,7 +127,7 @@ public function addTrackingCategory() { * Unless I find a solution for the integration test problem, I cannot use an instance of * ParserOutputHelper in ImageModal to ascertain this. In integration tests, "we" use a * different parser than the InternalParseBeforeLinks-Hook. At least, after I added - * Scribunto _unit_ tests. All messes up, I'm afraid. ImageModal better use global parser, and + * Scribunto _unit_ tests. All messed up, I'm afraid. ImageModal better use global parser, and * for the time being this method will be * @deprecated * @@ -157,7 +169,7 @@ public function injectLater( $id, $rawHtml ) { * * @return string */ - public function renderErrorMessage( $errorMessageName ) { + public function renderErrorMessage( string $errorMessageName ): string { if ( !$errorMessageName || !trim( $errorMessageName ) ) { return ''; } @@ -165,14 +177,17 @@ public function renderErrorMessage( $errorMessageName ) { return Html::rawElement( 'span', [ 'class' => 'error' ], - wfMessage( trim( $errorMessageName ) )->inContentLanguage()->title( $this->parser->getTitle() )->parse() + (new Message( trim( $errorMessageName ) ))->inContentLanguage()->page( + $this->getParser()->getPage() + )->parse() ); + } /** - * @return \Parser + * @return Parser */ - protected function getParser() { + protected function getParser(): Parser { return $this->parser; } @@ -181,11 +196,11 @@ protected function getParser() { * * @param String $trackingCategoryMessageName name of the message, containing the tracking category */ - private function placeTrackingCategory( $trackingCategoryMessageName ) { - $categoryMessage = wfMessage( $trackingCategoryMessageName )->inContentLanguage(); + private function placeTrackingCategory( string $trackingCategoryMessageName ): void { + $categoryMessage = (new Message( $trackingCategoryMessageName ))->inContentLanguage(); $parserOutput = $this->parser->getOutput(); if ( !$categoryMessage->isDisabled() && is_a( $parserOutput, ParserOutput::class ) ) { - // Q: when do we expect \Parser->getOutput() no to be a \ParserOutput? A:During tests. + // Q: when do we expect Parser->getOutput() no to be a ParserOutput? A:During tests. $cat = Title::makeTitleSafe( NS_CATEGORY, $categoryMessage->text() ); if ( $cat ) { $sort = (string)$parserOutput->getPageProperty('defaultsort') ?? ''; diff --git a/tests/phpunit/Unit/AbstractComponentTest.php b/tests/phpunit/Unit/AbstractComponentTest.php index 97da2a3..22c3ee4 100644 --- a/tests/phpunit/Unit/AbstractComponentTest.php +++ b/tests/phpunit/Unit/AbstractComponentTest.php @@ -119,14 +119,15 @@ public function testSimpleOutput( string $component ) { $parsedString = reset( $parsedString ); } $this->assertIsString( $parsedString ); - $this->assertRegExp( - '/class="[^"]*test-class"/', - $parsedString - ); - $this->assertRegExp( - '/style="[^"]*color:black"/', - $parsedString - ); + + // TODO when we drop support for MW1.39 + if ( version_compare( $GLOBALS['wgVersion'], '1.40', 'lt' ) ) { + $this->assertRegExp( '/class="[^"]*test-class"/', $parsedString ); + $this->assertRegExp( '/style="[^"]*color:black"/', $parsedString ); + } else { + $this->assertMatchesRegularExpression( '/class="[^"]*test-class"/', $parsedString ); + $this->assertMatchesRegularExpression( '/style="[^"]*color:black"/', $parsedString ); + } } /** diff --git a/tests/phpunit/Unit/BootstrapComponentServiceTest.php b/tests/phpunit/Unit/BootstrapComponentServiceTest.php index fa3d70d..eec6f51 100644 --- a/tests/phpunit/Unit/BootstrapComponentServiceTest.php +++ b/tests/phpunit/Unit/BootstrapComponentServiceTest.php @@ -43,7 +43,7 @@ public function testCanGetNameOfActiveSkin() { ); } - public function testRegisterModules() { + public function testRegisterComponentAsActive() { $instance = new BootstrapComponentsService( $this->getMockBuilder( Config::class )->getMock() ); $instance->registerComponentAsActive( 'Foo' ); @@ -70,6 +70,7 @@ public function testPrivateCanDetectSkinInUse() { $reflection = new ReflectionClass( BootstrapComponentsService::class ); $method = $reflection->getMethod( 'detectSkinInUse' ); + $method->setAccessible( true ); // this is default $this->assertEquals( diff --git a/tests/phpunit/Unit/Components/AlertTest.php b/tests/phpunit/Unit/Components/AlertTest.php index 0293bd1..55d7de8 100644 --- a/tests/phpunit/Unit/Components/AlertTest.php +++ b/tests/phpunit/Unit/Components/AlertTest.php @@ -29,7 +29,7 @@ class AlertTest extends ComponentsTestBase { public function testCanConstruct() { $this->assertInstanceOf( - 'MediaWiki\\Extension\\BootstrapComponents\\Components\\Alert', + Alert::class, new Alert( $this->getComponentLibrary(), $this->getParserOutputHelper(), @@ -55,13 +55,21 @@ public function testCanRender( $input, $arguments, $expectedOutput ) { $parserRequest = $this->buildParserRequest( $input, $arguments ); - /** @noinspection PhpParamsInspection */ $generatedOutput = $instance->parseComponent( $parserRequest ); - $this->assertRegExp( - '~^' . $this->input . '()?$~', - $generatedOutput - ); + // TODO when we drop support for MW1.39 + if ( version_compare( $GLOBALS['wgVersion'], '1.40', 'lt' ) ) { + $this->assertRegExp( + '~^' . $this->input . '()?$~', + $generatedOutput + ); + } else { + $this->assertMatchesRegularExpression( + '~^' . $this->input . '()?$~', + $generatedOutput + ); + } + $this->assertEquals( $expectedOutput, $generatedOutput ); } diff --git a/tests/phpunit/Unit/Components/ButtonTest.php b/tests/phpunit/Unit/Components/ButtonTest.php index dfd6adb..da6e560 100644 --- a/tests/phpunit/Unit/Components/ButtonTest.php +++ b/tests/phpunit/Unit/Components/ButtonTest.php @@ -29,7 +29,7 @@ class ButtonTest extends ComponentsTestBase { public function testCanConstruct() { $this->assertInstanceOf( - 'MediaWiki\\Extension\\BootstrapComponents\\Components\\Button', + Button::class, new Button( $this->getComponentLibrary(), $this->getParserOutputHelper(), @@ -55,16 +55,17 @@ public function testCanRender( $input, $arguments, $expectedOutputPattern ) { $parserRequest = $this->buildParserRequest( $input, $arguments ); - /** @noinspection PhpParamsInspection */ $generatedOutput = $instance->parseComponent( $parserRequest ); if ( is_array( $generatedOutput ) ) { $generatedOutput = $generatedOutput[0]; } - $this->assertRegExp( - $expectedOutputPattern, - $generatedOutput - ); + // TODO when we drop support for MW1.39 + if ( version_compare( $GLOBALS['wgVersion'], '1.40', 'lt' ) ) { + $this->assertRegExp( $expectedOutputPattern, $generatedOutput ); + } else { + $this->assertMatchesRegularExpression( $expectedOutputPattern, $generatedOutput ); + } } /** @@ -87,16 +88,27 @@ public function testCanInjectRawAttributes() { [ 'data-toggle' => 'foo', 'data-target' => '#bar' ] ); - /** @noinspection PhpParamsInspection */ $generatedOutput = $instance->parseComponent( $parserRequest ); if ( is_array( $generatedOutput ) ) { $generatedOutput = $generatedOutput[0]; } - $this->assertRegExp( - '~^' . $this->input . '$~', - $generatedOutput - ); + // TODO when we drop support for MW1.39 + if ( version_compare( $GLOBALS['wgVersion'], '1.40', 'lt' ) ) { + $this->assertRegExp( + '~^' . $this->input . '$~', + $generatedOutput + ); + } else { + $this->assertMatchesRegularExpression( + '~^' . $this->input . '$~', + $generatedOutput + ); + } } /** diff --git a/tests/phpunit/Unit/Hooks/OutputPageParserOutputTest.php b/tests/phpunit/Unit/Hooks/OutputPageParserOutputTest.php index 7b72fd4..e7d97fb 100644 --- a/tests/phpunit/Unit/Hooks/OutputPageParserOutputTest.php +++ b/tests/phpunit/Unit/Hooks/OutputPageParserOutputTest.php @@ -5,7 +5,6 @@ use MediaWiki\Extension\BootstrapComponents\BootstrapComponentsService; use MediaWiki\Extension\BootstrapComponents\Hooks\OutputPageParserOutput; use OutputPage; -use Parser; use ParserOutput; use PHPUnit\Framework\TestCase; @@ -35,7 +34,7 @@ public function testCanConstruct() { ); $this->assertInstanceOf( - 'MediaWiki\\Extension\\BootstrapComponents\\Hooks\\OutputPageParserOutput', + OutputPageParserOutput::class, $instance ); } diff --git a/tests/phpunit/Unit/ImageModalTest.php b/tests/phpunit/Unit/ImageModalTest.php index 9007e01..7a6d49c 100644 --- a/tests/phpunit/Unit/ImageModalTest.php +++ b/tests/phpunit/Unit/ImageModalTest.php @@ -269,11 +269,21 @@ function( $component ) { $resultOfParseCall = $instance->parse( $fp, $hp, $time, $res ); - $this->assertEquals( - $expectedTrigger, - $resultOfParseCall ?: $res, - 'failed trigger with test data:' . $this->generatePhpCodeForManualProviderDataOneCase( $fp, $hp ) - ); + if ( version_compare( $GLOBALS['wgVersion'], '1.40', 'lt' ) ) { + $this->assertRegExp( + $expectedTrigger, + $resultOfParseCall ?: $res, + 'failed with test data:' . $this->generatePhpCodeForManualProviderDataOneCase( $fp, $hp ) + ); + } else { + $this->assertMatchesRegularExpression( + $expectedTrigger, + $resultOfParseCall ?: $res, + 'failed with test data:' . $this->generatePhpCodeForManualProviderDataOneCase( $fp, $hp ) + . '++ ' . $expectedTrigger . PHP_EOL + . '-- ' . ($resultOfParseCall ?: $res) + ); + } $this->assertEquals( $expectedModal, $modalInjection, @@ -289,12 +299,6 @@ public function canParseDataProvider(): array { $globalConfig = MediaWikiServices::getInstance()->getMainConfig(); $scriptPath = $globalConfig->get( 'ScriptPath' ); - # @todo remove this, when dropping support for 1.31 - $injectedAsyncLoading = ( - version_compare( $globalConfig->get( 'Version' ), '1.32', 'gt' ) - ? 'decoding="async" ' - : '' - ); /* * notes on adding tests: * - when using manual thumbnail, inject $scriptPath: [ [], [], - '', + '~~', '' . "\n", ], @@ -320,7 +324,7 @@ public function canParseDataProvider(): array 'valign' => 'text-top', ], [], - '
test_alt
', + '~
test_alt
~', '' . "\n", ], @@ -333,7 +337,7 @@ public function canParseDataProvider(): array 'width' => 200, 'page' => 7, ], - '
', + '~
~', '' . "\n", ], @@ -346,7 +350,7 @@ public function canParseDataProvider(): array 'width' => 200, 'page' => 7, ], - '
', + '~
~', '' . "\n", ], @@ -357,7 +361,7 @@ public function canParseDataProvider(): array 'framed' => false, ], [], - '
', + '~
()?()?
~', '' . "\n", ], @@ -367,7 +371,7 @@ public function canParseDataProvider(): array 'framed' => false, ], [], - '
', + '~
~', '' . "\n", ], @@ -378,7 +382,7 @@ public function canParseDataProvider(): array [ 'width' => 200, ], - '
', + '~
~', '' . "\n", ], @@ -389,7 +393,7 @@ public function canParseDataProvider(): array 'manualthumb' => 'Shuttle.png', ], [], - '
', + '~
()?()?
~', '' . "\n", ], diff --git a/tests/phpunit/Unit/ImageModalTriggerTest.php b/tests/phpunit/Unit/ImageModalTriggerTest.php index decd5c0..f80e549 100644 --- a/tests/phpunit/Unit/ImageModalTriggerTest.php +++ b/tests/phpunit/Unit/ImageModalTriggerTest.php @@ -35,17 +35,15 @@ public function testCanConstruct() { ->disableOriginalConstructor() ->getMock(); - /** @noinspection PhpParamsInspection */ $this->assertInstanceOf( - 'MediaWiki\\Extension\\BootstrapComponents\\ImageModalTrigger', + ImageModalTrigger::class, new ImageModalTrigger( 'id', $localFile ) ); - /** @noinspection PhpParamsInspection */ $this->assertInstanceOf( - 'MediaWiki\\Extension\\BootstrapComponents\\ImageModalTrigger', + ImageModalTrigger::class, new ImageModalTrigger( 'id', $file @@ -58,7 +56,7 @@ public function testCanConstruct() { * @param array $hp * @param string $expectedRegExp * - * @dataProvider canGenerateProvider + * @dataProvider canParseProvider * @throws \ConfigException */ public function testCanParse( $sfp, $hp, $expectedRegExp ) { @@ -104,32 +102,33 @@ function( $params ) { ->method( 'transform' ) ->willReturn( $thumb ); - /** @noinspection PhpParamsInspection */ $instance = new ImageModalTrigger( 'id', $file ); $resultOfParseCall = $instance->generate( $sfp, $hp ); - /** @noinspection PhpParamsInspection */ - $this->assertEquals( - $expectedRegExp, - $resultOfParseCall, - 'failed with test data:' . $this->generatePhpCodeForManualProviderDataOneCase( $sfp, $hp ) - ); + foreach ( $expectedRegExp as $regExp ) { + // TODO when we drop support for MW1.39 + if ( version_compare( $GLOBALS['wgVersion'], '1.40', 'lt' ) ) { + $this->assertRegExp( + $regExp, $resultOfParseCall, + 'failed with test data:' . $this->generatePhpCodeForManualProviderDataOneCase( $sfp, $hp ) + ); + } else { + $this->assertMatchesRegularExpression( + $regExp, $resultOfParseCall, + 'failed with test data:' . $this->generatePhpCodeForManualProviderDataOneCase( $sfp, $hp ) + ); + } + } } /** * @throws \ConfigException cascading {@see \Config::get} * @return array */ - public function canGenerateProvider() { + public function canParseProvider() { $globalConfig = MediaWikiServices::getInstance()->getMainConfig(); $scriptPath = $globalConfig->get( 'ScriptPath' ); - # @todo remove this, when dropping support for 1.31 - $injectedAsyncLoading = ( - version_compare( $globalConfig->get( 'Version' ), '1.32', 'gt' ) - ? 'decoding="async" ' - : '' - ); /* * 1. Parameter: Sanitized frame parameter: @@ -172,7 +171,9 @@ public function canGenerateProvider() { [ 'page' => false, ], - '', + [ + '~~', + ] ], 'frame params w/o thumbnail' => [ [ @@ -190,7 +191,10 @@ public function canGenerateProvider() { [ 'page' => false, ], - '
test_alt
', + [ + '~
~', + '~test_alt~', + ] ], 'manual width, frameless' => [ [ @@ -209,7 +213,10 @@ public function canGenerateProvider() { 'width' => 200, 'page' => 7, ], - '
', + [ + '~
~', + '~~', + ] ], 'thumbnail, manual width' => [ [ @@ -228,7 +235,11 @@ public function canGenerateProvider() { 'width' => 200, 'page' => 7, ], - '
', + [ + '~
~', + '~
~', + '~
~', + ] ], 'manual thumbnail, NOT centered' => [ [ @@ -248,7 +259,11 @@ public function canGenerateProvider() { [ 'page' => false, ], - '
', + [ + '~
~', + '~
~', + '~~', + ] ], 'framed' => [ [ @@ -266,7 +281,10 @@ public function canGenerateProvider() { [ 'page' => false, ], - '
', + [ + '~
~', + '~
~', + ] ], 'centered' => [ [ @@ -285,7 +303,9 @@ public function canGenerateProvider() { 'width' => 200, 'page' => false, ], - '
', + [ + '~
~', + ] ], 'manual thumbnail, upright' => [ [ @@ -305,7 +325,11 @@ public function canGenerateProvider() { [ 'page' => false, ], - '', + [ + '~
~', + '~~', + '~
~', + ] ], ]; } diff --git a/tests/phpunit/Unit/ParserOutputHelperTest.php b/tests/phpunit/Unit/ParserOutputHelperTest.php index c20079c..5fda1a2 100644 --- a/tests/phpunit/Unit/ParserOutputHelperTest.php +++ b/tests/phpunit/Unit/ParserOutputHelperTest.php @@ -5,9 +5,9 @@ use MediaWiki\Extension\BootstrapComponents\ComponentLibrary; use MediaWiki\Extension\BootstrapComponents\ParserOutputHelper; use \MWException; -use \Parser; -use \ParserOutput; -use \PHPUnit_Framework_MockObject_MockObject; +// TODO: when dropping 1.39, switch to MediaWiki\Parser\Parser and MediaWiki\Parser\ParserOutput +use Parser; +use ParserOutput; use PHPUnit\Framework\TestCase; /** @@ -27,7 +27,7 @@ class ParserOutputHelperTest extends TestCase { /** * @var Parser */ - private $parser; + private Parser $parser; /** * @var ParserOutput @@ -39,9 +39,7 @@ public function setUp(): void { $this->parserOutput = new ParserOutput( 'ParserOutputMockText' ); - $this->parser = $this->getMockBuilder( 'Parser' ) - ->disableOriginalConstructor() - ->getMock(); + $this->parser = $this->createMock( 'Parser' ); $this->parser->expects( $this->any() ) ->method( 'getOutput' ) @@ -58,59 +56,23 @@ public function testCanConstruct() { public function testCanAddErrorTrackingCategory() { - $parser = $this->getMockBuilder( 'Parser' ) - ->disableOriginalConstructor() - ->getMock(); + $parser = $this->createMock( 'Parser' ); $parser->expects( $this->once() ) ->method( 'getOutput' ) ->willReturn( false ); - /** @noinspection PhpParamsInspection */ $instance = new ParserOutputHelper( $parser ); $instance->addErrorTrackingCategory(); $instance->addErrorTrackingCategory(); } - /** - * This is so lame to test. Only reason to do this to up test coverage. - */ - public function testCanAddModules() { - $parserOutput = $this->getMockBuilder( 'ParserOutput' ) - ->disableOriginalConstructor() - ->getMock(); - $parserOutput->expects( $this->exactly( 4 ) ) - ->method( 'addModules' ) - ->will( $this->returnArgument( 0 ) ); - $parser = $this->getMockBuilder( 'Parser' ) - ->disableOriginalConstructor() - ->getMock(); - $parser->expects( $this->exactly( 4 ) ) - ->method( 'getOutput' ) - ->willReturn( $parserOutput ); - - /** @noinspection PhpParamsInspection */ - $instance = new ParserOutputHelper( $parser ); - - $instance->addModules( /** @scrutinizer ignore-type */ null ); - - $instance->addModules( [] ); - - /** @noinspection PhpParamsInspection */ - $instance->addModules( /** @scrutinizer ignore-type */ 'module0' ); - - $instance->addModules( [ 'module1', 'module2' ] ); - } - public function testCanAddTrackingCategory() { - $parser = $this->getMockBuilder( 'Parser' ) - ->disableOriginalConstructor() - ->getMock(); + $parser = $this->createMock( 'Parser' ); $parser->expects( $this->once() ) ->method( 'getOutput' ) ->willReturn( false ); - /** @noinspection PhpParamsInspection */ $instance = new ParserOutputHelper( $parser ); $instance->addTrackingCategory(); @@ -123,8 +85,7 @@ public function testCanAddTrackingCategory() { * * @dataProvider errorMessageProvider */ - public function testCanRenderErrorMessage( $messageText, $renderedMessageRegExp ) { - /** @noinspection PhpParamsInspection */ + public function testCanRenderErrorMessage( $messageText, string $renderedMessageRegExp ) { $instance = new ParserOutputHelper( $this->buildFullyEquippedParser( ( $renderedMessageRegExp != '~^$~' ) ) ); @@ -161,8 +122,6 @@ public function componentNameAndClassProvider() { */ public function errorMessageProvider() { return [ - 'null' => [ null, '~^$~' ], - 'false' => [ false, '~^$~' ], 'none' => [ '', '~^$~' ], 'empty' => [ ' ', '~^$~' ], 'word' => [ '__rndErrorMessageTextNotInMessageFiles', '~^[^_]+__rndErrorMessageTextNotInMessageFiles[^<]+$~' ],