3 namespace MediaWiki\Tests\Maintenance
;
8 * @covers \JSParseHelper
11 class JSParseHelperTest
extends MaintenanceBaseTestCase
{
12 public function getMaintenanceClass() {
13 return JSParseHelper
::class;
16 public function testExecuteForMissingFilename() {
17 $this->expectCallToFatalError();
18 $nonExistingFile = $this->getNewTempDirectory() . 'non-existing-test-file.js';
19 $this->expectOutputRegex( '/' . preg_quote( $nonExistingFile, '/' ) . " ERROR: could not read file\n/" );
20 $this->maintenance
->setArg( 'files(s)', $nonExistingFile );
21 $this->maintenance
->execute();
24 private function getTestJSFile( string $content ): string {
25 $testFilename = $this->getNewTempFile();
26 $testFile = fopen( $testFilename, 'w' );
27 fwrite( $testFile, $content );
32 public function testExecuteForInvalidJS() {
33 // Get a file which has invalid JS
34 $testFilename = $this->getTestJSFile( "const a =;" );
35 // Run the maintenance script on this file, and expect it to be parsed as invalid.
36 $this->expectCallToFatalError();
37 $this->expectOutputRegex( '/' . preg_quote( $testFilename, '/' ) . ' ERROR/' );
38 $this->maintenance
->setArg( 'file(s)', $testFilename );
39 $this->maintenance
->execute();
42 public function testExecuteForValidJS() {
43 // Get a file which has valid JS
44 $testFilename = $this->getTestJSFile( "const a = 1;" );
45 // Run the maintenance script on this file, and expect it to parse as okay.
46 $this->expectOutputRegex( '/' . preg_quote( $testFilename, '/' ) . ' OK/' );
47 $this->maintenance
->setArg( 'file(s)', $testFilename );
48 $this->maintenance
->execute();