| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* base include file for SimpleTest
|
|
|
4 |
* @package SimpleTest
|
|
|
5 |
* @version $Id: options.php 1532 2006-12-01 12:28:55Z xue $
|
|
|
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 |
static 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 |
static 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 |
static 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 |
static 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 |
static 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 |
static 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 |
static 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 |
static 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 |
static 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 |
static 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 |
static 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 + types 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 |
static function isIdentical($first, $second) {
|
|
|
223 |
if ($first != $second) {
|
|
|
224 |
return false;
|
|
|
225 |
}
|
|
|
226 |
if (version_compare(phpversion(), '5') >= 0) {
|
|
|
227 |
return SimpleTestCompatibility::_isIdenticalType($first, $second);
|
|
|
228 |
}
|
|
|
229 |
return ($first === $second);
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
/**
|
|
|
233 |
* Recursive type test.
|
|
|
234 |
* @param mixed $first Test subject.
|
|
|
235 |
* @param mixed $second Comparison object.
|
|
|
236 |
* @access private
|
|
|
237 |
* @static
|
|
|
238 |
*/
|
|
|
239 |
static function _isIdenticalType($first, $second) {
|
|
|
240 |
if (gettype($first) != gettype($second)) {
|
|
|
241 |
return false;
|
|
|
242 |
}
|
|
|
243 |
if (is_object($first) && is_object($second)) {
|
|
|
244 |
if (get_class($first) != get_class($second)) {
|
|
|
245 |
return false;
|
|
|
246 |
}
|
|
|
247 |
return SimpleTestCompatibility::_isArrayOfIdenticalTypes(
|
|
|
248 |
get_object_vars($first),
|
|
|
249 |
get_object_vars($second));
|
|
|
250 |
}
|
|
|
251 |
if (is_array($first) && is_array($second)) {
|
|
|
252 |
return SimpleTestCompatibility::_isArrayOfIdenticalTypes($first, $second);
|
|
|
253 |
}
|
|
|
254 |
return true;
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
/**
|
|
|
258 |
* Recursive type test for each element of an array.
|
|
|
259 |
* @param mixed $first Test subject.
|
|
|
260 |
* @param mixed $second Comparison object.
|
|
|
261 |
* @access private
|
|
|
262 |
* @static
|
|
|
263 |
*/
|
|
|
264 |
static function _isArrayOfIdenticalTypes($first, $second) {
|
|
|
265 |
if (array_keys($first) != array_keys($second)) {
|
|
|
266 |
return false;
|
|
|
267 |
}
|
|
|
268 |
foreach (array_keys($first) as $key) {
|
|
|
269 |
$is_identical = SimpleTestCompatibility::_isIdenticalType(
|
|
|
270 |
$first[$key],
|
|
|
271 |
$second[$key]);
|
|
|
272 |
if (! $is_identical) {
|
|
|
273 |
return false;
|
|
|
274 |
}
|
|
|
275 |
}
|
|
|
276 |
return true;
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
/**
|
|
|
280 |
* Test for two variables being aliases.
|
|
|
281 |
* @param mixed $first Test subject.
|
|
|
282 |
* @param mixed $second Comparison object.
|
|
|
283 |
* @access public
|
|
|
284 |
* @static
|
|
|
285 |
*/
|
|
|
286 |
static function isReference($first, $second) {
|
|
|
287 |
if (version_compare(phpversion(), '5', '>=')
|
|
|
288 |
&& is_object($first)) {
|
|
|
289 |
return ($first === $second);
|
|
|
290 |
}
|
|
|
291 |
$temp = $first;
|
|
|
292 |
$first = uniqid("test");
|
|
|
293 |
$is_ref = ($first === $second);
|
|
|
294 |
$first = $temp;
|
|
|
295 |
return $is_ref;
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
/**
|
|
|
299 |
* Test to see if an object is a member of a
|
|
|
300 |
* class hiearchy.
|
|
|
301 |
* @param object $object Object to test.
|
|
|
302 |
* @param string $class Root name of hiearchy.
|
|
|
303 |
* @access public
|
|
|
304 |
* @static
|
|
|
305 |
*/
|
|
|
306 |
static function isA($object, $class) {
|
|
|
307 |
if (version_compare(phpversion(), '5') >= 0) {
|
|
|
308 |
if (! class_exists($class, false)) {
|
|
|
309 |
return false;
|
|
|
310 |
}
|
|
|
311 |
eval("\$is_a = \$object instanceof $class;");
|
|
|
312 |
return $is_a;
|
|
|
313 |
}
|
|
|
314 |
if (function_exists('is_a')) {
|
|
|
315 |
return is_a($object, $class);
|
|
|
316 |
}
|
|
|
317 |
return ((strtolower($class) == get_class($object))
|
|
|
318 |
or (is_subclass_of($object, $class)));
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
/**
|
|
|
322 |
* Autoload safe version of class_exists().
|
|
|
323 |
* @param string $class Name of class to look for.
|
|
|
324 |
* @return boolean True if class is defined.
|
|
|
325 |
* @access public
|
|
|
326 |
* @static
|
|
|
327 |
*/
|
|
|
328 |
static function classExists($class) {
|
|
|
329 |
if (version_compare(phpversion(), '5') >= 0) {
|
|
|
330 |
return class_exists($class, false);
|
|
|
331 |
} else {
|
|
|
332 |
return class_exists($class);
|
|
|
333 |
}
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
/**
|
|
|
337 |
* Sets a socket timeout for each chunk.
|
|
|
338 |
* @param resource $handle Socket handle.
|
|
|
339 |
* @param integer $timeout Limit in seconds.
|
|
|
340 |
* @access public
|
|
|
341 |
* @static
|
|
|
342 |
*/
|
|
|
343 |
static function setTimeout($handle, $timeout) {
|
|
|
344 |
if (function_exists('stream_set_timeout')) {
|
|
|
345 |
stream_set_timeout($handle, $timeout, 0);
|
|
|
346 |
} elseif (function_exists('socket_set_timeout')) {
|
|
|
347 |
socket_set_timeout($handle, $timeout, 0);
|
|
|
348 |
} elseif (function_exists('set_socket_timeout')) {
|
|
|
349 |
set_socket_timeout($handle, $timeout, 0);
|
|
|
350 |
}
|
|
|
351 |
}
|
|
|
352 |
|
|
|
353 |
/**
|
|
|
354 |
* Gets the current stack trace topmost first.
|
|
|
355 |
* @return array List of stack frames.
|
|
|
356 |
* @access public
|
|
|
357 |
* @static
|
|
|
358 |
*/
|
|
|
359 |
static function getStackTrace() {
|
|
|
360 |
if (function_exists('debug_backtrace')) {
|
|
|
361 |
return array_reverse(debug_backtrace());
|
|
|
362 |
}
|
|
|
363 |
return array();
|
|
|
364 |
}
|
|
|
365 |
}
|
|
|
366 |
?>
|