Debian Codegraph @

tests/phpunit/maintenance/JSParseHelperTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Maintenance;
4
5 use JSParseHelper;
6
7 /**
8 * @covers \JSParseHelper
9 * @author Dreamy Jazz
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 // 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();
40 }
41
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();
49 }
50 }