Debian Codegraph @

extensions/Math/tests/phpunit/unit/WikiTexVC/MMLTestUtil.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util;
4
5 use DOMDocument;
6 use InvalidArgumentException;
7 use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmath;
8 use MediaWiki\Extension\Math\WikiTexVC\Nodes\TexNode;
9
10 /**
11 * This Utility class has some methods for running
12 * tests for the Tex to MathML converters in WikiTexVC.
13 * @author Johannes Stegmüller
14 */
15 class MMLTestUtil {
16 /**
17 * @return mixed
18 */
19 public static function getJSON( string $filePath ) {
20 if ( !file_exists( $filePath ) ) {
21 throw new InvalidArgumentException( "No testfile found at specified path: " . $filePath );
22 }
23 return json_decode( file_get_contents( $filePath ) );
24 }
25
26 public static function createJSONstartEnd( bool $start, string $file ) {
27 file_put_contents( $file, $start ? "[\n" : "\n]", FILE_APPEND );
28 }
29
30 public static function appendToJSONFile( array $dataArray, string $file ) {
31 $jsonData = json_encode( $dataArray, JSON_PRETTY_PRINT ) . ",";
32 file_put_contents( $file, $jsonData, FILE_APPEND );
33 }
34
35 public static function deleteFile( string $file ): void {
36 if ( file_exists( $file ) ) {
37 unlink( $file );
38 }
39 }
40
41 public static function prettifyXML( string $xml, bool $replaceHeader = true ): string {
42 $dom = new DOMDocument();
43 // Initial block (must before load xml string)
44 $dom->preserveWhiteSpace = false;
45 $dom->formatOutput = true;
46 // End initial block
47 $dom->loadXML( $xml );
48 $out = $dom->saveXML();
49 if ( $replaceHeader ) {
50 // replacing the xml header in a hacky way
51 return substr_replace( $out, "", 0, 22 );
52 }
53 return $out;
54 }
55
56 public static function getMMLwrapped( TexNode $input ): string {
57 $math = new MMLmath();
58 $mml = $math->encapsulateRaw( $input->renderMML() );
59 return self::prettifyXML( $mml );
60 }
61 }