Renames preparatory to parser tests refactor
[lhc/web/wiklou.git] / tests / parser / DelayedParserTest.php
1 <?php
2
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 * @ingroup Testing
21 */
22
23 /**
24 * A class to delay execution of a parser test hooks.
25 */
26 class DelayedParserTest {
27
28 /** Initialized on construction */
29 private $hooks;
30 private $fnHooks;
31 private $transparentHooks;
32
33 public function __construct() {
34 $this->reset();
35 }
36
37 /**
38 * Init/reset or forgot about the current delayed test.
39 * Call to this will erase any hooks function that were pending.
40 */
41 public function reset() {
42 $this->hooks = [];
43 $this->fnHooks = [];
44 $this->transparentHooks = [];
45 }
46
47 /**
48 * Called whenever we actually want to run the hook.
49 * Should be the case if we found the parserTest is not disabled
50 * @param ParserTestRunner|ParserIntegrationTest $parserTest
51 * @return bool
52 * @throws MWException
53 */
54 public function unleash( &$parserTest ) {
55 if ( !( $parserTest instanceof ParserTestRunner
56 || $parserTest instanceof ParserIntegrationTest )
57 ) {
58 throw new MWException( __METHOD__ . " must be passed an instance of " .
59 "ParserTestRunner or ParserIntegrationTest classes\n" );
60 }
61
62 # Trigger delayed hooks. Any failure will make us abort
63 foreach ( $this->hooks as $hook ) {
64 $ret = $parserTest->requireHook( $hook );
65 if ( !$ret ) {
66 return false;
67 }
68 }
69
70 # Trigger delayed function hooks. Any failure will make us abort
71 foreach ( $this->fnHooks as $fnHook ) {
72 $ret = $parserTest->requireFunctionHook( $fnHook );
73 if ( !$ret ) {
74 return false;
75 }
76 }
77
78 # Trigger delayed transparent hooks. Any failure will make us abort
79 foreach ( $this->transparentHooks as $hook ) {
80 $ret = $parserTest->requireTransparentHook( $hook );
81 if ( !$ret ) {
82 return false;
83 }
84 }
85
86 # Delayed execution was successful.
87 return true;
88 }
89
90 /**
91 * Similar to ParserTestRunner object but does not run anything
92 * Use unleash() to really execute the hook
93 * @param string $hook
94 */
95 public function requireHook( $hook ) {
96 $this->hooks[] = $hook;
97 }
98
99 /**
100 * Similar to ParserTestRunner object but does not run anything
101 * Use unleash() to really execute the hook function
102 * @param string $fnHook
103 */
104 public function requireFunctionHook( $fnHook ) {
105 $this->fnHooks[] = $fnHook;
106 }
107
108 /**
109 * Similar to ParserTestRunner object but does not run anything
110 * Use unleash() to really execute the hook function
111 * @param string $hook
112 */
113 public function requireTransparentHook( $hook ) {
114 $this->transparentHooks[] = $hook;
115 }
116
117 }
118