3 * base include file for SimpleTest
5 * @version $Id: options.php,v 1.28 2004/08/18 16:24:00 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 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 (version_compare(phpversion(), '5') >= 0) {
224 if (gettype($first) != gettype($second)) {
227 if ($first != $second) {
230 if (is_object($first) && is_object($second)) {
231 return (get_class($first) == get_class($second));
233 if (is_array($first) && is_array($second)) {
234 if (array_keys($first) != array_keys($second)) {
237 foreach (array_keys($first) as $key) {
238 if (! SimpleTestCompatibility
::isIdentical($first[$key], $second[$key])) {
245 return ($first === $second);
249 * Test to see if an object is a member of a
251 * @param object $object Object to test.
252 * @param string $class Root name of hiearchy.
256 function isA($object, $class) {
257 if (version_compare(phpversion(), '5') >= 0) {
258 if (! class_exists($class)) {
261 eval("\$is_a = \$object instanceof $class;");
264 if (function_exists('is_a')) {
265 return is_a($object, $class);
267 return ((strtolower($class) == get_class($object))
268 or (is_subclass_of($object, $class)));
272 * Sets a socket timeout for each chunk.
273 * @param resource $handle Socket handle.
274 * @param integer $timeout Limit in seconds.
278 function setTimeout($handle, $timeout) {
279 stream_set_timeout($handle, $timeout, 0);
283 * Gets the current stack trace topmost first.
284 * @return array List of stack frames.
288 function getStackTrace() {
289 if (function_exists('debug_backtrace')) {
290 return array_reverse(debug_backtrace());