3 * base include file for SimpleTest
5 * @version $Id: options.php,v 1.35 2005/02/04 03:48:52 lastcraft Exp $
9 * Static global directives and options.
12 class SimpleTestOptions
{
15 * Reads the SimpleTest version from the release file.
16 * @return string Version string.
20 function getVersion() {
21 $content = file(dirname(__FILE__
) . '/VERSION');
22 return trim($content[0]);
26 * Sets the name of a test case to ignore, usually
27 * because the class is an abstract case that should
29 * @param string $class Add a class to ignore.
33 function ignore($class) {
34 $registry = &SimpleTestOptions
::_getRegistry();
35 $registry['IgnoreList'][] = strtolower($class);
39 * Test to see if a test case is in the ignore
41 * @param string $class Class name to test.
42 * @return boolean True if should not be run.
46 function isIgnored($class) {
47 $registry = &SimpleTestOptions
::_getRegistry();
48 return in_array(strtolower($class), $registry['IgnoreList']);
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.
61 function setStubBaseClass($stub_base) {
62 $registry = &SimpleTestOptions
::_getRegistry();
63 $registry['StubBaseClass'] = $stub_base;
67 * Accessor for the currently set stub base class.
68 * @return string Class name to inherit from.
72 function getStubBaseClass() {
73 $registry = &SimpleTestOptions
::_getRegistry();
74 return $registry['StubBaseClass'];
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.
87 function setMockBaseClass($mock_base) {
88 $registry = &SimpleTestOptions
::_getRegistry();
89 $registry['MockBaseClass'] = $mock_base;
93 * Accessor for the currently set mock base class.
94 * @return string Class name to inherit from.
98 function getMockBaseClass() {
99 $registry = &SimpleTestOptions
::_getRegistry();
100 return $registry['MockBaseClass'];
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.
112 function addPartialMockCode($code = '') {
113 $registry = &SimpleTestOptions
::_getRegistry();
114 $registry['AdditionalPartialMockCode'] = $code;
118 * Accessor for additional partial mock code.
119 * @return string Extra code.
122 function getPartialMockCode() {
123 $registry = &SimpleTestOptions
::_getRegistry();
124 return $registry['AdditionalPartialMockCode'];
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.
137 function useProxy($proxy, $username = false, $password = false) {
138 $registry = &SimpleTestOptions
::_getRegistry();
139 $registry['DefaultProxy'] = $proxy;
140 $registry['DefaultProxyUsername'] = $username;
141 $registry['DefaultProxyPassword'] = $password;
145 * Accessor for default proxy host.
146 * @return string Proxy URL.
149 function getDefaultProxy() {
150 $registry = &SimpleTestOptions
::_getRegistry();
151 return $registry['DefaultProxy'];
155 * Accessor for default proxy username.
156 * @return string Proxy username for authentication.
159 function getDefaultProxyUsername() {
160 $registry = &SimpleTestOptions
::_getRegistry();
161 return $registry['DefaultProxyUsername'];
165 * Accessor for default proxy password.
166 * @return string Proxy password for authentication.
169 function getDefaultProxyPassword() {
170 $registry = &SimpleTestOptions
::_getRegistry();
171 return $registry['DefaultProxyPassword'];
175 * Accessor for global registry of options.
176 * @return hash All stored values.
180 function &_getRegistry() {
181 static $registry = false;
183 $registry = SimpleTestOptions
::_getDefaults();
189 * Constant default values.
190 * @return hash All registry defaults.
194 function _getDefaults() {
196 'StubBaseClass' => 'SimpleStub',
197 'MockBaseClass' => 'SimpleMock',
198 'IgnoreList' => array(),
199 'AdditionalPartialMockCode' => '',
200 'DefaultProxy' => false,
201 'DefaultProxyUsername' => false,
202 'DefaultProxyPassword' => false);
207 * Static methods for compatibility between different
209 * @package SimpleTest
211 class SimpleTestCompatibility
{
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.
222 function isIdentical($first, $second) {
223 if ($first != $second) {
226 if (version_compare(phpversion(), '5') >= 0) {
227 return SimpleTestCompatibility
::_isIdenticalType($first, $second);
229 return ($first === $second);
233 * Recursive type test.
234 * @param mixed $first Test subject.
235 * @param mixed $second Comparison object.
239 function _isIdenticalType($first, $second) {
240 if (gettype($first) != gettype($second)) {
243 if (is_object($first) && is_object($second)) {
244 if (get_class($first) != get_class($second)) {
247 return SimpleTestCompatibility
::_isArrayOfIdenticalTypes(
248 get_object_vars($first),
249 get_object_vars($second));
251 if (is_array($first) && is_array($second)) {
252 return SimpleTestCompatibility
::_isArrayOfIdenticalTypes($first, $second);
258 * Recursive type test for each element of an array.
259 * @param mixed $first Test subject.
260 * @param mixed $second Comparison object.
264 function _isArrayOfIdenticalTypes($first, $second) {
265 if (array_keys($first) != array_keys($second)) {
268 foreach (array_keys($first) as $key) {
269 $is_identical = SimpleTestCompatibility
::_isIdenticalType(
272 if (! $is_identical) {
280 * Test for two variables being aliases.
281 * @param mixed $first Test subject.
282 * @param mixed $second Comparison object.
286 function isReference(&$first, &$second) {
287 if (version_compare(phpversion(), '5', '>=')
288 && is_object($first)) {
289 return ($first === $second);
292 $first = uniqid("test");
293 $is_ref = ($first === $second);
299 * Test to see if an object is a member of a
301 * @param object $object Object to test.
302 * @param string $class Root name of hiearchy.
306 function isA($object, $class) {
307 if (version_compare(phpversion(), '5') >= 0) {
308 if (! class_exists($class, false)) {
311 eval("\$is_a = \$object instanceof $class;");
314 if (function_exists('is_a')) {
315 return is_a($object, $class);
317 return ((strtolower($class) == get_class($object))
318 or (is_subclass_of($object, $class)));
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.
328 function classExists($class) {
329 if (version_compare(phpversion(), '5') >= 0) {
330 return class_exists($class, false);
332 return class_exists($class);
337 * Sets a socket timeout for each chunk.
338 * @param resource $handle Socket handle.
339 * @param integer $timeout Limit in seconds.
343 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);
354 * Gets the current stack trace topmost first.
355 * @return array List of stack frames.
359 function getStackTrace() {
360 if (function_exists('debug_backtrace')) {
361 return array_reverse(debug_backtrace());