Special case opus mime detction
[mediawiki.git] / includes / parser / ParserDiffTest.php
blobd0b66f8061fbb4178c8815503806f6305f8e7af7
1 <?php
2 /**
3 * Fake parser that output the difference of two different parsers
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Parser
24 /**
25 * @ingroup Parser
27 class ParserDiffTest
29 public $parsers;
30 public $conf;
31 public $shortOutput = false;
33 public function __construct( $conf ) {
34 if ( !isset( $conf['parsers'] ) ) {
35 throw new MWException( __METHOD__ . ': no parsers specified' );
37 $this->conf = $conf;
40 public function init() {
41 if ( !is_null( $this->parsers ) ) {
42 return;
45 if ( isset( $this->conf['shortOutput'] ) ) {
46 $this->shortOutput = $this->conf['shortOutput'];
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'];
56 $this->parsers[$i] = new $class( $parserConf );
60 public function __call( $name, $args ) {
61 $this->init();
62 $results = [];
63 $mismatch = false;
64 $lastResult = null;
65 $first = true;
66 foreach ( $this->parsers as $i => $parser ) {
67 $currentResult = call_user_func_array( [ &$this->parsers[$i], $name ], $args );
68 if ( $first ) {
69 $first = false;
70 } else {
71 if ( is_object( $lastResult ) ) {
72 if ( $lastResult != $currentResult ) {
73 $mismatch = true;
75 } else {
76 if ( $lastResult !== $currentResult ) {
77 $mismatch = true;
81 $results[$i] = $currentResult;
82 $lastResult = $currentResult;
84 if ( $mismatch ) {
85 if ( count( $results ) == 2 ) {
86 $resultsList = [];
87 foreach ( $this->parsers as $i => $parser ) {
88 $resultsList[] = var_export( $results[$i], true );
90 $diff = wfDiff( $resultsList[0], $resultsList[1] );
91 } else {
92 $diff = '[too many parsers]';
94 $msg = "ParserDiffTest: results mismatch on call to $name\n";
95 if ( !$this->shortOutput ) {
96 $msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
98 $msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
99 "Diff: $diff\n";
100 throw new MWException( $msg );
102 return $lastResult;
105 public function formatArray( $array ) {
106 if ( $this->shortOutput ) {
107 foreach ( $array as $key => $value ) {
108 if ( $value instanceof ParserOutput ) {
109 $array[$key] = "ParserOutput: {$value->getText()}";
113 return var_export( $array, true );
116 public function setFunctionHook( $id, $callback, $flags = 0 ) {
117 $this->init();
118 foreach ( $this->parsers as $parser ) {
119 $parser->setFunctionHook( $id, $callback, $flags );