wiki.techinc.nl/tests/phpunit/includes/content/TextContentHandlerIntegrationTest.php
Timo Tijhof 64734f61ee content: Widen @covers tags in phpunit tests
https://gerrit.wikimedia.org/r/q/owner:Krinkle+is:merged+message:Widen

> Given all called methods are de-facto and liberally claimed, and
> that we keep the coverage limited to the subject class, it maintains
> the spirit and intent by listing the class explicitly instead.
>
> PHPUnit offers a more precise tool when you need it (i.e. when testing
> legacy monster/god classes), but for well-written code, the
> class-wide tag is exactly what you want.
>
> We lose useful coverage and waste valuable time on keeping tags
> accurate through refactors (or worse, forget to do so).
> Tracking tiny per-method details wastes time in realizing (and
> fixing) when people inevitably don't keep them in sync, and time
> lost in finding uncovered code to write tests to realize it was
> already covered but "not yet claimed".

While at it, also fix PHPUnit warnings in CssContentHandlerIntegrationTest
and JavaScriptContentHandlerIntegrationTest about not having any
`@covers` annotations.

Change-Id: I5afd9fe0bca0fa86cc096f6e5e79f2ba1cfbfa77
2024-07-21 21:03:10 +00:00

63 lines
1.9 KiB
PHP

<?php
use MediaWiki\Title\Title;
use Wikimedia\Parsoid\ParserTests\TestUtils;
/**
* @group ContentHandler
* @group Database
* ^--- needed, because we do need the database to test link updates
* @covers \MediaWiki\Content\TextContentHandler
*/
class TextContentHandlerIntegrationTest extends MediaWikiLangTestCase {
public static function provideGetParserOutput() {
yield 'Basic render' => [
'title' => 'TextContentTest_testGetParserOutput',
'model' => CONTENT_MODEL_TEXT,
'text' => "hello ''world'' & [[stuff]]\n",
'expectedHtml' => "<pre>hello ''world'' &amp; [[stuff]]\n</pre>",
'expectedFields' => [ 'Links' => [] ]
];
yield 'Multi line render' => [
'title' => 'TextContentTest_testGetParserOutput',
'model' => CONTENT_MODEL_TEXT,
'text' => "Test 1\nTest 2\n\nTest 3\n",
'expectedHtml' => "<pre>Test 1\nTest 2\n\nTest 3\n</pre>",
'expectedFields' => [ 'Links' => [] ]
];
}
/**
* @dataProvider provideGetParserOutput
*/
public function testGetParserOutput( $title, $model, $text, $expectedHtml,
$expectedFields = null, $parserOptions = null
) {
$title = Title::newFromText( $title );
$content = ContentHandler::makeContent( $text, $title, $model );
$contentRenderer = $this->getServiceContainer()->getContentRenderer();
$po = $contentRenderer->getParserOutput( $content, $title, null, $parserOptions );
$html = $po->getText();
$html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
$html = TestUtils::stripParsoidIds( $html );
if ( $expectedHtml !== null ) {
$this->assertEquals( TestUtils::stripParsoidIds( $expectedHtml ), trim( $html ) );
}
if ( $expectedFields ) {
foreach ( $expectedFields as $field => $exp ) {
$getter = 'get' . ucfirst( $field );
$v = $po->$getter();
if ( is_array( $exp ) ) {
$this->assertArrayEquals( $exp, $v );
} else {
$this->assertEquals( $exp, $v );
}
}
}
}
}