first reimport from platal
[platal.git] / htdocs / TESTS / simpletest / options.php
1 <?php
2 /**
3 * base include file for SimpleTest
4 * @package SimpleTest
5 * @version $Id: options.php,v 1.28 2004/08/18 16:24:00 lastcraft Exp $
6 */
7
8 /**
9 * Static global directives and options.
10 * @package SimpleTest
11 */
12 class SimpleTestOptions {
13
14 /**
15 * Reads the SimpleTest version from the release file.
16 * @return string Version string.
17 * @static
18 * @access public
19 */
20 function getVersion() {
21 $content = file(dirname(__FILE__) . '/VERSION');
22 return trim($content[0]);
23 }
24
25 /**
26 * Sets the name of a test case to ignore, usually
27 * because the class is an abstract case that should
28 * not be run.
29 * @param string $class Add a class to ignore.
30 * @static
31 * @access public
32 */
33 function ignore($class) {
34 $registry = &SimpleTestOptions::_getRegistry();
35 $registry['IgnoreList'][] = strtolower($class);
36 }
37
38 /**
39 * Test to see if a test case is in the ignore
40 * list.
41 * @param string $class Class name to test.
42 * @return boolean True if should not be run.
43 * @access public
44 * @static
45 */
46 function isIgnored($class) {
47 $registry = &SimpleTestOptions::_getRegistry();
48 return in_array(strtolower($class), $registry['IgnoreList']);
49 }
50
51 /**
52 * The base class name is settable here. This is the
53 * class that a new stub will inherited from.
54 * To modify the generated stubs simply extend the
55 * SimpleStub class and set it's name
56 * with this method before any stubs are generated.
57 * @param string $stub_base Server stub class to use.
58 * @static
59 * @access public
60 */
61 function setStubBaseClass($stub_base) {
62 $registry = &SimpleTestOptions::_getRegistry();
63 $registry['StubBaseClass'] = $stub_base;
64 }
65
66 /**
67 * Accessor for the currently set stub base class.
68 * @return string Class name to inherit from.
69 * @static
70 * @access public
71 */
72 function getStubBaseClass() {
73 $registry = &SimpleTestOptions::_getRegistry();
74 return $registry['StubBaseClass'];
75 }
76
77 /**
78 * The base class name is settable here. This is the
79 * class that a new mock will inherited from.
80 * To modify the generated mocks simply extend the
81 * SimpleMock class and set it's name
82 * with this method before any mocks are generated.
83 * @param string $mock_base Mock base class to use.
84 * @static
85 * @access public
86 */
87 function setMockBaseClass($mock_base) {
88 $registry = &SimpleTestOptions::_getRegistry();
89 $registry['MockBaseClass'] = $mock_base;
90 }
91
92 /**
93 * Accessor for the currently set mock base class.
94 * @return string Class name to inherit from.
95 * @static
96 * @access public
97 */
98 function getMockBaseClass() {
99 $registry = &SimpleTestOptions::_getRegistry();
100 return $registry['MockBaseClass'];
101 }
102
103 /**
104 * Adds additional mock code.
105 * @param string $code Extra code that can be added
106 * to the partial mocks for
107 * extra functionality. Useful
108 * when a test tool has overridden
109 * the mock base classes.
110 * @access public
111 */
112 function addPartialMockCode($code = '') {
113 $registry = &SimpleTestOptions::_getRegistry();
114 $registry['AdditionalPartialMockCode'] = $code;
115 }
116
117 /**
118 * Accessor for additional partial mock code.
119 * @return string Extra code.
120 * @access public
121 */
122 function getPartialMockCode() {
123 $registry = &SimpleTestOptions::_getRegistry();
124 return $registry['AdditionalPartialMockCode'];
125 }
126
127 /**
128 * Sets proxy to use on all requests for when
129 * testing from behind a firewall. Set host
130 * to false to disable. This will take effect
131 * if there are no other proxy settings.
132 * @param string $proxy Proxy host as URL.
133 * @param string $username Proxy username for authentication.
134 * @param string $password Proxy password for authentication.
135 * @access public
136 */
137 function useProxy($proxy, $username = false, $password = false) {
138 $registry = &SimpleTestOptions::_getRegistry();
139 $registry['DefaultProxy'] = $proxy;
140 $registry['DefaultProxyUsername'] = $username;
141 $registry['DefaultProxyPassword'] = $password;
142 }
143
144 /**
145 * Accessor for default proxy host.
146 * @return string Proxy URL.
147 * @access public
148 */
149 function getDefaultProxy() {
150 $registry = &SimpleTestOptions::_getRegistry();
151 return $registry['DefaultProxy'];
152 }
153
154 /**
155 * Accessor for default proxy username.
156 * @return string Proxy username for authentication.
157 * @access public
158 */
159 function getDefaultProxyUsername() {
160 $registry = &SimpleTestOptions::_getRegistry();
161 return $registry['DefaultProxyUsername'];
162 }
163
164 /**
165 * Accessor for default proxy password.
166 * @return string Proxy password for authentication.
167 * @access public
168 */
169 function getDefaultProxyPassword() {
170 $registry = &SimpleTestOptions::_getRegistry();
171 return $registry['DefaultProxyPassword'];
172 }
173
174 /**
175 * Accessor for global registry of options.
176 * @return hash All stored values.
177 * @access private
178 * @static
179 */
180 function &_getRegistry() {
181 static $registry = false;
182 if (! $registry) {
183 $registry = SimpleTestOptions::_getDefaults();
184 }
185 return $registry;
186 }
187
188 /**
189 * Constant default values.
190 * @return hash All registry defaults.
191 * @access private
192 * @static
193 */
194 function _getDefaults() {
195 return array(
196 'StubBaseClass' => 'SimpleStub',
197 'MockBaseClass' => 'SimpleMock',
198 'IgnoreList' => array(),
199 'AdditionalPartialMockCode' => '',
200 'DefaultProxy' => false,
201 'DefaultProxyUsername' => false,
202 'DefaultProxyPassword' => false);
203 }
204 }
205
206 /**
207 * Static methods for compatibility between different
208 * PHP versions.
209 * @package SimpleTest
210 */
211 class SimpleTestCompatibility {
212
213 /**
214 * Identity test. Drops back to equality for PHP5
215 * objects as the === operator counts as the
216 * stronger reference constraint.
217 * @param mixed $first Test subject.
218 * @param mixed $second Comparison object.
219 * @access public
220 * @static
221 */
222 function isIdentical($first, $second) {
223 if (version_compare(phpversion(), '5') >= 0) {
224 if (gettype($first) != gettype($second)) {
225 return false;
226 }
227 if ($first != $second) {
228 return false;
229 }
230 if (is_object($first) && is_object($second)) {
231 return (get_class($first) == get_class($second));
232 }
233 if (is_array($first) && is_array($second)) {
234 if (array_keys($first) != array_keys($second)) {
235 return false;
236 }
237 foreach (array_keys($first) as $key) {
238 if (! SimpleTestCompatibility::isIdentical($first[$key], $second[$key])) {
239 return false;
240 }
241 }
242 }
243 return true;
244 }
245 return ($first === $second);
246 }
247
248 /**
249 * Test to see if an object is a member of a
250 * class hiearchy.
251 * @param object $object Object to test.
252 * @param string $class Root name of hiearchy.
253 * @access public
254 * @static
255 */
256 function isA($object, $class) {
257 if (version_compare(phpversion(), '5') >= 0) {
258 if (! class_exists($class)) {
259 return false;
260 }
261 eval("\$is_a = \$object instanceof $class;");
262 return $is_a;
263 }
264 if (function_exists('is_a')) {
265 return is_a($object, $class);
266 }
267 return ((strtolower($class) == get_class($object))
268 or (is_subclass_of($object, $class)));
269 }
270
271 /**
272 * Sets a socket timeout for each chunk.
273 * @param resource $handle Socket handle.
274 * @param integer $timeout Limit in seconds.
275 * @access public
276 * @static
277 */
278 function setTimeout($handle, $timeout) {
279 stream_set_timeout($handle, $timeout, 0);
280 }
281
282 /**
283 * Gets the current stack trace topmost first.
284 * @return array List of stack frames.
285 * @access public
286 * @static
287 */
288 function getStackTrace() {
289 if (function_exists('debug_backtrace')) {
290 return array_reverse(debug_backtrace());
291 }
292 return array();
293 }
294 }
295 ?>