Avoid "No transaction is active" saveFileDependencies() tests errors
[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 ParserTest|NewParserTest $parserTest
51 * @return bool
52 * @throws MWException
53 */
54 public function unleash( &$parserTest ) {
55 if ( !( $parserTest instanceof ParserTest || $parserTest instanceof NewParserTest ) ) {
56 throw new MWException( __METHOD__ . " must be passed an instance of ParserTest or "
57 . "NewParserTest classes\n" );
58 }
59
60 # Trigger delayed hooks. Any failure will make us abort
61 foreach ( $this->hooks as $hook ) {
62 $ret = $parserTest->requireHook( $hook );
63 if ( !$ret ) {
64 return false;
65 }
66 }
67
68 # Trigger delayed function hooks. Any failure will make us abort
69 foreach ( $this->fnHooks as $fnHook ) {
70 $ret = $parserTest->requireFunctionHook( $fnHook );
71 if ( !$ret ) {
72 return false;
73 }
74 }
75
76 # Trigger delayed transparent hooks. Any failure will make us abort
77 foreach ( $this->transparentHooks as $hook ) {
78 $ret = $parserTest->requireTransparentHook( $hook );
79 if ( !$ret ) {
80 return false;
81 }
82 }
83
84 # Delayed execution was successful.
85 return true;
86 }
87
88 /**
89 * Similar to ParserTest object but does not run anything
90 * Use unleash() to really execute the hook
91 * @param string $hook
92 */
93 public function requireHook( $hook ) {
94 $this->hooks[] = $hook;
95 }
96
97 /**
98 * Similar to ParserTest object but does not run anything
99 * Use unleash() to really execute the hook function
100 * @param string $fnHook
101 */
102 public function requireFunctionHook( $fnHook ) {
103 $this->fnHooks[] = $fnHook;
104 }
105
106 /**
107 * Similar to ParserTest object but does not run anything
108 * Use unleash() to really execute the hook function
109 * @param string $hook
110 */
111 public function requireTransparentHook( $hook ) {
112 $this->transparentHooks[] = $hook;
113 }
114
115 }
116