MediaWiki  1.34.0
ParserDiffTest.php
Go to the documentation of this file.
1 <?php
28  public $parsers;
29  public $conf;
30  public $shortOutput = false;
31 
32  public function __construct( $conf ) {
33  if ( !isset( $conf['parsers'] ) ) {
34  throw new MWException( __METHOD__ . ': no parsers specified' );
35  }
36  $this->conf = $conf;
37  }
38 
39  public function init() {
40  if ( !is_null( $this->parsers ) ) {
41  return;
42  }
43  $this->parsers = [];
44 
45  if ( isset( $this->conf['shortOutput'] ) ) {
46  $this->shortOutput = $this->conf['shortOutput'];
47  }
48 
49  foreach ( $this->conf['parsers'] as $i => $parserConf ) {
50  if ( !is_array( $parserConf ) ) {
51  $class = $parserConf;
52  $parserConf = [ 'class' => $parserConf ];
53  } else {
54  $class = $parserConf['class'];
55  }
56  $this->parsers[$i] = new $class( $parserConf );
57  }
58  }
59 
60  public function __call( $name, $args ) {
61  $this->init();
62  $results = [];
63  $mismatch = false;
64  $lastResult = null;
65  foreach ( $this->parsers as $parser ) {
66  $currentResult = $parser->$name( ...$args );
67  if ( count( $results ) > 0 ) {
68  if ( is_object( $lastResult ) ) {
69  if ( $lastResult != $currentResult ) {
70  $mismatch = true;
71  }
72  } else {
73  if ( $lastResult !== $currentResult ) {
74  $mismatch = true;
75  }
76  }
77  }
78  $results[] = $currentResult;
79  $lastResult = $currentResult;
80  }
81  if ( $mismatch ) {
82  if ( count( $results ) == 2 ) {
83  $resultsList = [];
84  foreach ( $this->parsers as $i => $parser ) {
85  $resultsList[] = var_export( $results[$i], true );
86  }
87  $diff = wfDiff( $resultsList[0], $resultsList[1] );
88  } else {
89  $diff = '[too many parsers]';
90  }
91  $msg = "ParserDiffTest: results mismatch on call to $name\n";
92  if ( !$this->shortOutput ) {
93  $msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
94  }
95  $msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
96  "Diff: $diff\n";
97  throw new MWException( $msg );
98  }
99  return $lastResult;
100  }
101 
102  public function formatArray( $array ) {
103  if ( $this->shortOutput ) {
104  foreach ( $array as $key => $value ) {
105  if ( $value instanceof ParserOutput ) {
106  $array[$key] = "ParserOutput: {$value->getText()}";
107  }
108  }
109  }
110  return var_export( $array, true );
111  }
112 
113  public function setFunctionHook( $id, $callback, $flags = 0 ) {
114  $this->init();
115  foreach ( $this->parsers as $parser ) {
116  $parser->setFunctionHook( $id, $callback, $flags );
117  }
118  }
119 }
ParserDiffTest\setFunctionHook
setFunctionHook( $id, $callback, $flags=0)
Definition: ParserDiffTest.php:113
ParserOutput
Definition: ParserOutput.php:25
wfDiff
wfDiff( $before, $after, $params='-u')
Returns unified plain-text diff of two texts.
Definition: GlobalFunctions.php:2308
ParserDiffTest\formatArray
formatArray( $array)
Definition: ParserDiffTest.php:102
ParserDiffTest\$conf
$conf
Definition: ParserDiffTest.php:29
MWException
MediaWiki exception.
Definition: MWException.php:26
ParserDiffTest\$parsers
$parsers
Definition: ParserDiffTest.php:28
$args
if( $line===false) $args
Definition: cdb.php:64
ParserDiffTest\__call
__call( $name, $args)
Definition: ParserDiffTest.php:60
ParserDiffTest
Definition: ParserDiffTest.php:27
ParserDiffTest\$shortOutput
$shortOutput
Definition: ParserDiffTest.php:30
ParserDiffTest\__construct
__construct( $conf)
Definition: ParserDiffTest.php:32
ParserDiffTest\init
init()
Definition: ParserDiffTest.php:39