| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use JSParseHelper; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class JSParseHelperTest extends MaintenanceBaseTestCase { |
| 12 |
public function getMaintenanceClass() { |
| 13 |
return JSParseHelper::class; |
| 14 |
} |
| 15 |
|
| 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(); |
| 22 |
} |
| 23 |
|
| 24 |
private function getTestJSFile( string $content ): string { |
| 25 |
$testFilename = $this->getNewTempFile(); |
| 26 |
$testFile = fopen( $testFilename, 'w' ); |
| 27 |
fwrite( $testFile, $content ); |
| 28 |
fclose( $testFile ); |
| 29 |
return $testFilename; |
| 30 |
} |
| 31 |
|
| 32 |
public function testExecuteForInvalidJS() { |
| 33 |
|
| 34 |
$testFilename = $this->getTestJSFile( "const a =;" ); |
| 35 |
|
| 36 |
$this->expectCallToFatalError(); |
| 37 |
$this->expectOutputRegex( '/' . preg_quote( $testFilename, '/' ) . ' ERROR/' ); |
| 38 |
$this->maintenance->setArg( 'file(s)', $testFilename ); |
| 39 |
$this->maintenance->execute(); |
| 40 |
} |
| 41 |
|
| 42 |
public function testExecuteForValidJS() { |
| 43 |
|
| 44 |
$testFilename = $this->getTestJSFile( "const a = 1;" ); |
| 45 |
|
| 46 |
$this->expectOutputRegex( '/' . preg_quote( $testFilename, '/' ) . ' OK/' ); |
| 47 |
$this->maintenance->setArg( 'file(s)', $testFilename ); |
| 48 |
$this->maintenance->execute(); |
| 49 |
} |
| 50 |
} |