mediawiki-extensions-Cite/tests/phpunit/unit/ErrorReporterTest.php
thiemowmde 4377f0923d More simple and consistent @covers and @license tags
Same arguments as in Iafa2412. The one reason to use more detailled
per-method @covers annotations is to avoid "accidental coverage"
where code is marked as being covered by tests that don't assert
anything that would be meaningful for this code. This is especially a
problem with older, bigger classes with lots of side effects.

But all the new classes we introduced over the years are small, with
predictable, local effects.

That's also why we keep the more detailled @covers annotations for
the original Cite class.

Bug: T353227
Bug: T353269
Change-Id: I69850f4d740d8ad5a7c2368b9068dc91e47cc797
2023-12-15 12:12:16 +01:00

104 lines
3.4 KiB
PHP

<?php
namespace Cite\Tests\Unit;
use Cite\ErrorReporter;
use Cite\ReferenceMessageLocalizer;
use Language;
use Message;
use Parser;
use ParserOptions;
/**
* @covers \Cite\ErrorReporter
* @license GPL-2.0-or-later
*/
class ErrorReporterTest extends \MediaWikiUnitTestCase {
/**
* @dataProvider provideErrors
*/
public function testPlain(
string $key,
string $expectedHtml,
array $expectedCategories
) {
$language = $this->createLanguage();
$reporter = $this->createReporter( $language );
$mockParser = $this->createParser( $language, $expectedCategories );
$this->assertSame(
$expectedHtml,
$reporter->plain( $mockParser, $key, 'first param' ) );
}
public function testHalfParsed() {
$language = $this->createLanguage();
$reporter = $this->createReporter( $language );
$mockParser = $this->createParser( $language, [] );
$this->assertSame(
'<span class="warning mw-ext-cite-warning mw-ext-cite-warning-example" lang="qqx" ' .
'dir="rtl">[(cite_warning|(cite_warning_example|first param))]</span>',
$reporter->halfParsed( $mockParser, 'cite_warning_example', 'first param' ) );
}
public static function provideErrors() {
return [
'Example error' => [
'cite_error_example',
'<span class="error mw-ext-cite-error" lang="qqx" dir="rtl">' .
'(cite_error|(cite_error_example|first param))</span>',
[ 'cite-tracking-category-cite-error' ]
],
'Warning error' => [
'cite_warning_example',
'<span class="warning mw-ext-cite-warning mw-ext-cite-warning-example" lang="qqx" ' .
'dir="rtl">(cite_warning|(cite_warning_example|first param))</span>',
[]
],
];
}
private function createLanguage(): Language {
$language = $this->createNoOpMock( Language::class, [ 'getDir', 'getHtmlCode' ] );
$language->method( 'getDir' )->willReturn( 'rtl' );
$language->method( 'getHtmlCode' )->willReturn( 'qqx' );
return $language;
}
private function createReporter( Language $language ): ErrorReporter {
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
function ( ...$args ) use ( $language ) {
$message = $this->createMock( Message::class );
$message->method( 'getKey' )->willReturn( $args[0] );
$message->method( 'plain' )->willReturn( '(' . implode( '|', $args ) . ')' );
$message->method( 'inLanguage' )->with( $language )->willReturnSelf();
$message->method( 'getLanguage' )->willReturn( $language );
return $message;
}
);
return new ErrorReporter( $mockMessageLocalizer );
}
private function createParser( Language $language, array $expectedCategories ): Parser {
$parserOptions = $this->createMock( ParserOptions::class );
$parserOptions->method( 'getUserLangObj' )->willReturn( $language );
$parser = $this->createNoOpMock( Parser::class, [ 'addTrackingCategory', 'getOptions', 'recursiveTagParse' ] );
$parser->expects( $this->exactly( count( $expectedCategories ) ) )
->method( 'addTrackingCategory' )
->willReturnCallback( function ( $cat ) use ( &$expectedCategories ) {
$catIdx = array_search( $cat, $expectedCategories, true );
$this->assertNotFalse( $catIdx, "Unexpected category: $cat" );
unset( $expectedCategories[$catIdx] );
} );
$parser->method( 'getOptions' )->willReturn( $parserOptions );
$parser->method( 'recursiveTagParse' )->willReturnCallback(
static fn ( $content ) => '[' . $content . ']'
);
return $parser;
}
}