mediawiki-extensions-Discus.../tests/phpunit/unit/CommentUtilsTest.php
Bartosz Dziewoński 361283a332 Ship HTML test files for JS using 'packageFiles' instead of 'templates'
We originally used 'templates' because it seemed like an obvious
choice for HTML files, and because 'packageFiles' requires extra code
to include anything that isn't a .js or .json file.

However, the templates are expected to be HTML fragments rather than
whole documents, and they are parsed in a particular way that takes a
lot of code to clean up (which we needed to do, because we use the
same test files for testing PHP code).

I tried doing it in the 'packageFiles' way, and the extra code doesn't
seem that bad in comparison after all. Moreover, the 'templates'
mechanism (when used the intended way) feels vaguely deprecated in
favor of Vue.js, and I'd rather move away from it.

This makes the tests faster too (probably mostly thanks to the removal
of the clean up code) – on my machine they go from 1800ms to 1500ms.

(Simplify linearWalk tests, as we no longer need to do weird things
with document fragments to get consistent outputs in PHP and JS.)

Change-Id: I39f9b994ce5636d70fea2e935a7c87c7d56dcb26
2022-10-12 22:45:41 +00:00

52 lines
1.6 KiB
PHP

<?php
namespace MediaWiki\Extension\DiscussionTools\Tests\Unit;
use MediaWiki\Extension\DiscussionTools\CommentUtils;
use MediaWiki\Extension\DiscussionTools\Tests\TestUtils;
use MediaWikiUnitTestCase;
/**
* @group DiscussionTools
* @covers \MediaWiki\Extension\DiscussionTools\CommentUtils
*/
class CommentUtilsTest extends MediaWikiUnitTestCase {
use TestUtils;
/**
* @dataProvider provideLinearWalk
*/
public function testLinearWalk( string $name, string $htmlPath, string $expectedPath ) {
$html = static::getHtml( $htmlPath );
$doc = static::createDocument( $html );
$expected = static::getJson( $expectedPath );
$actual = [];
CommentUtils::linearWalk( $doc, static function ( $event, $node ) use ( &$actual ) {
$actual[] = "$event {$node->nodeName}({$node->nodeType})";
} );
$actualBackwards = [];
CommentUtils::linearWalkBackwards( $doc, static function ( $event, $node ) use ( &$actualBackwards ) {
$actualBackwards[] = "$event {$node->nodeName}({$node->nodeType})";
} );
// Optionally write updated content to the JSON files
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
static::overwriteJsonFile( $expectedPath, $actual );
}
static::assertEquals( $expected, $actual, $name );
$expectedBackwards = array_map( static function ( $a ) {
return ( substr( $a, 0, 5 ) === 'enter' ? 'leave' : 'enter' ) . substr( $a, 5 );
}, array_reverse( $expected ) );
static::assertEquals( $expectedBackwards, $actualBackwards, $name . ' (backwards)' );
}
public function provideLinearWalk(): array {
return static::getJson( '../cases/linearWalk.json' );
}
}