| 1 |
<?php |
| 2 |
|
| 3 |
use PHPUnit\Framework\TestSuite; |
| 4 |
use Wikimedia\ScopedCallback; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class ParserTestTopLevelSuite extends TestSuite { |
| 16 |
use SuiteEventsTrait; |
| 17 |
|
| 18 |
|
| 19 |
private $ptRecorder; |
| 20 |
|
| 21 |
|
| 22 |
private $ptRunner; |
| 23 |
|
| 24 |
|
| 25 |
private $ptTeardownScope; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public const CORE_ONLY = 1; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public const NO_CORE = 2; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public const WITH_ALL = self::CORE_ONLY | self::NO_CORE; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static function suite( $flags = self::CORE_ONLY ) { |
| 74 |
return new self( $flags ); |
| 75 |
} |
| 76 |
|
| 77 |
public function __construct( $flags, ?array $parserTestFlags = null ) { |
| 78 |
parent::__construct(); |
| 79 |
|
| 80 |
$this->ptRecorder = new PhpunitTestRecorder; |
| 81 |
$runnerOpts = $parserTestFlags ?? json_decode( getenv( "PARSERTEST_FLAGS" ) ?: "[]", true ); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
$runnerOpts['numchanges'] = 0; |
| 88 |
$this->ptRunner = new ParserTestRunner( |
| 89 |
$this->ptRecorder, $runnerOpts |
| 90 |
); |
| 91 |
|
| 92 |
if ( is_string( $flags ) ) { |
| 93 |
$flags = self::CORE_ONLY; |
| 94 |
} |
| 95 |
global $IP; |
| 96 |
|
| 97 |
$mwTestDir = $IP . '/tests/'; |
| 98 |
|
| 99 |
|
| 100 |
$wantsCore = ( $flags & self::CORE_ONLY ); |
| 101 |
$wantsRest = ( $flags & self::NO_CORE ); |
| 102 |
|
| 103 |
|
| 104 |
$filesToTest = []; |
| 105 |
|
| 106 |
|
| 107 |
$files = ParserTestRunner::getParserTestFiles(); |
| 108 |
foreach ( $files as $extName => $parserTestFile ) { |
| 109 |
$isCore = str_starts_with( $parserTestFile, $mwTestDir ); |
| 110 |
|
| 111 |
if ( $isCore && $wantsCore ) { |
| 112 |
self::debug( "included core parser tests: $parserTestFile" ); |
| 113 |
$filesToTest[$extName] = $parserTestFile; |
| 114 |
} elseif ( !$isCore && $wantsRest ) { |
| 115 |
self::debug( "included non core parser tests: $parserTestFile" ); |
| 116 |
$filesToTest[$extName] = $parserTestFile; |
| 117 |
} else { |
| 118 |
self::debug( "skipped parser tests: $parserTestFile" ); |
| 119 |
} |
| 120 |
} |
| 121 |
self::debug( 'parser tests files: ' |
| 122 |
. implode( ' ', $filesToTest ) ); |
| 123 |
|
| 124 |
$testList = []; |
| 125 |
$counter = 0; |
| 126 |
foreach ( $filesToTest as $extensionName => $fileName ) { |
| 127 |
$isCore = str_starts_with( $fileName, $mwTestDir ); |
| 128 |
if ( is_int( $extensionName ) ) { |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
$extensionName = basename( dirname( $fileName ) ); |
| 133 |
} |
| 134 |
$testsName = $extensionName . '__' . basename( $fileName, '.txt' ); |
| 135 |
$parserTestClassName = ucfirst( $testsName ); |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
$parserTestClassName = 'ParserTest_' . |
| 140 |
preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', $parserTestClassName ); |
| 141 |
|
| 142 |
$originalClassName = $parserTestClassName; |
| 143 |
while ( isset( $testList[$parserTestClassName] ) ) { |
| 144 |
|
| 145 |
$counter++; |
| 146 |
$parserTestClassName = $originalClassName . '_' . $counter; |
| 147 |
} |
| 148 |
$testList[$parserTestClassName] = true; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
self::debug( "Adding test class $parserTestClassName" ); |
| 154 |
|
| 155 |
$this->addTest( new ParserTestFileSuite( |
| 156 |
$this->ptRunner, "Legacy$parserTestClassName", $fileName ) ); |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
if ( !$isCore ) { |
| 161 |
$this->addTest( new ParsoidTestFileSuite( |
| 162 |
$this->ptRunner, "Parsoid$parserTestClassName", $fileName |
| 163 |
) ); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
protected function setUp(): void { |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
MediaWikiIntegrationTestCase::teardownTestDB(); |
| 173 |
|
| 174 |
TestUserRegistry::clear(); |
| 175 |
|
| 176 |
$teardown = $this->ptRunner->setupDatabase( null ); |
| 177 |
$teardown = $this->ptRunner->staticSetup( $teardown ); |
| 178 |
$teardown = $this->ptRunner->setupUploads( $teardown ); |
| 179 |
$this->ptTeardownScope = $teardown; |
| 180 |
} |
| 181 |
|
| 182 |
protected function tearDown(): void { |
| 183 |
if ( $this->ptTeardownScope ) { |
| 184 |
ScopedCallback::consume( $this->ptTeardownScope ); |
| 185 |
} |
| 186 |
TestUserRegistry::clear(); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
protected static function debug( $msg ) { |
| 194 |
wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg ); |
| 195 |
} |
| 196 |
} |