mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/DiscussionTools
synced 2024-11-17 13:12:26 +00:00
1c3fada1fb
Documentation: https://www.mediawiki.org/wiki/Manual:PHP_unit_testing/Writing_unit_tests_for_extensions#Two_types_of_tests We can do this because the tested methods do not depend on any globals or on MediaWiki being installed. In addition to being the new hotness, MediaWikiUnitTestCase allows the test classes that use it instead of MediaWikiTestCase to start up much faster. In my testing, running this test case individually now takes 0.35s, compared to 1.1s before. Try: * With new code: time php tests/phpunit/phpunit.php extensions/DiscussionTools/tests/phpunit/unit/CommentUtilsTest.php * With old code: time php tests/phpunit/phpunit.php extensions/DiscussionTools/tests/phpunit/CommentUtilsTest.php Change-Id: I771b1f3d101a394ee869e42547d9ae7839397752
48 lines
1.3 KiB
PHP
48 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Tests\Unit;
|
|
|
|
use DOMDocument;
|
|
use MediaWiki\Extension\DiscussionTools\CommentUtils;
|
|
use MediaWiki\Extension\DiscussionTools\Tests\TestUtils;
|
|
use MediaWikiUnitTestCase;
|
|
|
|
/**
|
|
* @coversDefaultClass \MediaWiki\Extension\DiscussionTools\CommentUtils
|
|
*
|
|
* @group DiscussionTools
|
|
*/
|
|
class CommentUtilsTest extends MediaWikiUnitTestCase {
|
|
|
|
use TestUtils;
|
|
|
|
/**
|
|
* @dataProvider provideLinearWalk
|
|
* @covers ::linearWalk
|
|
*/
|
|
public function testLinearWalk( string $name, string $htmlPath, string $expectedPath ) {
|
|
$html = self::getHtml( $htmlPath );
|
|
// Slightly awkward to get the same output as in the JS version
|
|
$fragment = ( new DOMDocument() )->createDocumentFragment();
|
|
$fragment->appendXML( trim( $html ) );
|
|
$expected = self::getJson( $expectedPath );
|
|
|
|
$actual = [];
|
|
CommentUtils::linearWalk( $fragment, function ( $event, $node ) use ( &$actual ) {
|
|
$actual[] = "$event {$node->nodeName}({$node->nodeType})";
|
|
} );
|
|
|
|
// Optionally write updated content to the JSON files
|
|
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
|
|
self::overwriteJsonFile( $expectedPath, $actual );
|
|
}
|
|
|
|
self::assertEquals( $expected, $actual, $name );
|
|
}
|
|
|
|
public function provideLinearWalk() : array {
|
|
return self::getJson( '../cases/linearWalk.json' );
|
|
}
|
|
|
|
}
|