aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/suites/ParserTestFileSuite.php
blob: 00f9e2844a523f3f3e3cccf42429bf18358ae8bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php

use PHPUnit\Framework\TestSuite;
use Wikimedia\Parsoid\ParserTests\TestFileReader;
use Wikimedia\Parsoid\ParserTests\TestMode as ParserTestMode;
use Wikimedia\ScopedCallback;

/**
 * This is the suite class for running tests with the legacy parser
 * within a single .txt source file.
 * It is not invoked directly. Use --filter to select files, or
 * use parserTests.php.
 */
class ParserTestFileSuite extends TestSuite {
	use SuiteEventsTrait;

	private ParserTestRunner $ptRunner;
	private TestFileReader $ptFileInfo;

	/** @var ScopedCallback */
	private $ptTeardownScope;

	public function __construct( ParserTestRunner $runner, string $name, string $fileName ) {
		parent::__construct( $name );
		$this->ptRunner = $runner;
		try {
			$this->ptFileInfo = TestFileReader::read( $fileName, static function ( $msg ) {
				wfDeprecatedMsg( $msg, '1.35', false, false );
			} );
		} catch ( \Exception $e ) {
			// Friendlier wrapping for any syntax errors that might occur.
			throw new RuntimeException(
				$fileName . ': ' . $e->getMessage()
			);
		}

		$skipMessage = $this->ptRunner->getFileSkipMessage(
			true, /* legacy parser */
			$this->ptFileInfo->fileOptions,
			$fileName
		);
		// Don't bother doing anything else if a skip message is set.
		if ( $skipMessage !== null ) {
			return;
		}

		$mode = new ParserTestMode( 'legacy' );
		foreach ( $this->ptFileInfo->testCases as $test ) {
			$this->addTest( new ParserIntegrationTest(
				$this->ptRunner, $fileName, $test, $mode, $skipMessage ),
				[ 'Database', 'Parser', 'ParserTests' ] );
		}
	}

	protected function setUp(): void {
		$this->ptTeardownScope = $this->ptRunner->addArticles(
			$this->ptFileInfo->articles
		);
	}

	protected function tearDown(): void {
		if ( $this->ptTeardownScope ) {
			ScopedCallback::consume( $this->ptTeardownScope );
		}
	}
}