ParserDiffTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Fake parser that output the difference of two different parsers
  4. *
  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.
  9. *
  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.
  14. *
  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
  19. *
  20. * @file
  21. * @ingroup Parser
  22. */
  23. /**
  24. * @ingroup Parser
  25. */
  26. class ParserDiffTest {
  27. public $parsers;
  28. public $conf;
  29. public $shortOutput = false;
  30. public function __construct( $conf ) {
  31. if ( !isset( $conf['parsers'] ) ) {
  32. throw new MWException( __METHOD__ . ': no parsers specified' );
  33. }
  34. $this->conf = $conf;
  35. }
  36. public function init() {
  37. if ( !is_null( $this->parsers ) ) {
  38. return;
  39. }
  40. if ( isset( $this->conf['shortOutput'] ) ) {
  41. $this->shortOutput = $this->conf['shortOutput'];
  42. }
  43. foreach ( $this->conf['parsers'] as $i => $parserConf ) {
  44. if ( !is_array( $parserConf ) ) {
  45. $class = $parserConf;
  46. $parserConf = [ 'class' => $parserConf ];
  47. } else {
  48. $class = $parserConf['class'];
  49. }
  50. $this->parsers[$i] = new $class( $parserConf );
  51. }
  52. }
  53. public function __call( $name, $args ) {
  54. $this->init();
  55. $results = [];
  56. $mismatch = false;
  57. $lastResult = null;
  58. $first = true;
  59. foreach ( $this->parsers as $i => $parser ) {
  60. $currentResult = call_user_func_array( [ &$this->parsers[$i], $name ], $args );
  61. if ( $first ) {
  62. $first = false;
  63. } else {
  64. if ( is_object( $lastResult ) ) {
  65. if ( $lastResult != $currentResult ) {
  66. $mismatch = true;
  67. }
  68. } else {
  69. if ( $lastResult !== $currentResult ) {
  70. $mismatch = true;
  71. }
  72. }
  73. }
  74. $results[$i] = $currentResult;
  75. $lastResult = $currentResult;
  76. }
  77. if ( $mismatch ) {
  78. if ( count( $results ) == 2 ) {
  79. $resultsList = [];
  80. foreach ( $this->parsers as $i => $parser ) {
  81. $resultsList[] = var_export( $results[$i], true );
  82. }
  83. $diff = wfDiff( $resultsList[0], $resultsList[1] );
  84. } else {
  85. $diff = '[too many parsers]';
  86. }
  87. $msg = "ParserDiffTest: results mismatch on call to $name\n";
  88. if ( !$this->shortOutput ) {
  89. $msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
  90. }
  91. $msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
  92. "Diff: $diff\n";
  93. throw new MWException( $msg );
  94. }
  95. return $lastResult;
  96. }
  97. public function formatArray( $array ) {
  98. if ( $this->shortOutput ) {
  99. foreach ( $array as $key => $value ) {
  100. if ( $value instanceof ParserOutput ) {
  101. $array[$key] = "ParserOutput: {$value->getText()}";
  102. }
  103. }
  104. }
  105. return var_export( $array, true );
  106. }
  107. public function setFunctionHook( $id, $callback, $flags = 0 ) {
  108. $this->init();
  109. foreach ( $this->parsers as $parser ) {
  110. $parser->setFunctionHook( $id, $callback, $flags );
  111. }
  112. }
  113. }