summaryrefslogtreecommitdiff
path: root/tests/test_tools/simpletest/options.php
blob: 23008ff92e375e8bb5476f44a5ba5530d220e361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
    /**
     *	base include file for SimpleTest
     *	@package	SimpleTest
     *	@version	$Id: options.php 1532 2006-12-01 12:28:55Z xue $
     */

    /**
     *    Static global directives and options.
     *	  @package	SimpleTest
     */
    class SimpleTestOptions {

        /**
         *    Reads the SimpleTest version from the release file.
         *    @return string        Version string.
         *    @static
         *    @access public
         */
        static function getVersion() {
            $content = file(dirname(__FILE__) . '/VERSION');
            return trim($content[0]);
        }

        /**
         *    Sets the name of a test case to ignore, usually
         *    because the class is an abstract case that should
         *    not be run.
         *    @param string $class        Add a class to ignore.
         *    @static
         *    @access public
         */
        static function ignore($class) {
            $registry =SimpleTestOptions::_getRegistry();
            $registry['IgnoreList'][] = strtolower($class);
        }

        /**
         *    Test to see if a test case is in the ignore
         *    list.
         *    @param string $class        Class name to test.
         *    @return boolean             True if should not be run.
         *    @access public
         *    @static
         */
        static function isIgnored($class) {
            $registry =SimpleTestOptions::_getRegistry();
            return in_array(strtolower($class), $registry['IgnoreList']);
        }

        /**
         *    The base class name is settable here. This is the
         *    class that a new stub will inherited from.
         *    To modify the generated stubs simply extend the
         *    SimpleStub class and set it's name
         *    with this method before any stubs are generated.
         *    @param string $stub_base     Server stub class to use.
         *    @static
         *    @access public
         */
        static function setStubBaseClass($stub_base) {
            $registry =SimpleTestOptions::_getRegistry();
            $registry['StubBaseClass'] = $stub_base;
        }

        /**
         *    Accessor for the currently set stub base class.
         *    @return string        Class name to inherit from.
         *    @static
         *    @access public
         */
        static function getStubBaseClass() {
            $registry =SimpleTestOptions::_getRegistry();
            return $registry['StubBaseClass'];
        }

        /**
         *    The base class name is settable here. This is the
         *    class that a new mock will inherited from.
         *    To modify the generated mocks simply extend the
         *    SimpleMock class and set it's name
         *    with this method before any mocks are generated.
         *    @param string $mock_base        Mock base class to use.
         *    @static
         *    @access public
         */
        static function setMockBaseClass($mock_base) {
            $registry =SimpleTestOptions::_getRegistry();
            $registry['MockBaseClass'] = $mock_base;
        }

        /**
         *    Accessor for the currently set mock base class.
         *    @return string           Class name to inherit from.
         *    @static
         *    @access public
         */
        static function getMockBaseClass() {
            $registry =SimpleTestOptions::_getRegistry();
            return $registry['MockBaseClass'];
        }

        /**
         *    Adds additional mock code.
         *    @param string $code    Extra code that can be added
         *                           to the partial mocks for
         *                           extra functionality. Useful
         *                           when a test tool has overridden
         *                           the mock base classes.
         *    @access public
         */
        static function addPartialMockCode($code = '') {
            $registry =SimpleTestOptions::_getRegistry();
            $registry['AdditionalPartialMockCode'] = $code;
        }

        /**
         *    Accessor for additional partial mock code.
         *    @return string       Extra code.
         *    @access public
         */
        function getPartialMockCode() {
            $registry =SimpleTestOptions::_getRegistry();
            return $registry['AdditionalPartialMockCode'];
        }

        /**
         *    Sets proxy to use on all requests for when
         *    testing from behind a firewall. Set host
         *    to false to disable. This will take effect
         *    if there are no other proxy settings.
         *    @param string $proxy     Proxy host as URL.
         *    @param string $username  Proxy username for authentication.
         *    @param string $password  Proxy password for authentication.
         *    @access public
         */
        static function useProxy($proxy, $username = false, $password = false) {
            $registry =SimpleTestOptions::_getRegistry();
            $registry['DefaultProxy'] = $proxy;
            $registry['DefaultProxyUsername'] = $username;
            $registry['DefaultProxyPassword'] = $password;
        }

        /**
         *    Accessor for default proxy host.
         *    @return string       Proxy URL.
         *    @access public
         */
        function getDefaultProxy() {
            $registry =SimpleTestOptions::_getRegistry();
            return $registry['DefaultProxy'];
        }

        /**
         *    Accessor for default proxy username.
         *    @return string    Proxy username for authentication.
         *    @access public
         */
        function getDefaultProxyUsername() {
            $registry =SimpleTestOptions::_getRegistry();
            return $registry['DefaultProxyUsername'];
        }

        /**
         *    Accessor for default proxy password.
         *    @return string    Proxy password for authentication.
         *    @access public
         */
        function getDefaultProxyPassword() {
            $registry =SimpleTestOptions::_getRegistry();
            return $registry['DefaultProxyPassword'];
        }

        /**
         *    Accessor for global registry of options.
         *    @return hash           All stored values.
         *    @access private
         *    @static
         */
        static function _getRegistry() {
            static $registry = false;
            if (! $registry) {
                $registry = SimpleTestOptions::_getDefaults();
            }
            return $registry;
        }

        /**
         *    Constant default values.
         *    @return hash       All registry defaults.
         *    @access private
         *    @static
         */
        static function _getDefaults() {
            return array(
                    'StubBaseClass' => 'SimpleStub',
                    'MockBaseClass' => 'SimpleMock',
                    'IgnoreList' => array(),
                    'AdditionalPartialMockCode' => '',
                    'DefaultProxy' => false,
                    'DefaultProxyUsername' => false,
                    'DefaultProxyPassword' => false);
        }
    }

    /**
     *  Static methods for compatibility between different
     *  PHP versions.
     *  @package	SimpleTest
     */
    class SimpleTestCompatibility {

        /**
         *    Identity test. Drops back to equality + types for PHP5
         *    objects as the === operator counts as the
         *    stronger reference constraint.
         *    @param mixed $first    Test subject.
         *    @param mixed $second   Comparison object.
         *    @access public
         *    @static
         */
        static function isIdentical($first, $second) {
            if ($first != $second) {
                return false;
            }
            if (version_compare(phpversion(), '5') >= 0) {
                return SimpleTestCompatibility::_isIdenticalType($first, $second);
            }
            return ($first === $second);
        }

        /**
         *    Recursive type test.
         *    @param mixed $first    Test subject.
         *    @param mixed $second   Comparison object.
         *    @access private
         *    @static
         */
        static function _isIdenticalType($first, $second) {
            if (gettype($first) != gettype($second)) {
                return false;
            }
            if (is_object($first) && is_object($second)) {
                if (get_class($first) != get_class($second)) {
                    return false;
                }
                return SimpleTestCompatibility::_isArrayOfIdenticalTypes(
                        get_object_vars($first),
                        get_object_vars($second));
            }
            if (is_array($first) && is_array($second)) {
                return SimpleTestCompatibility::_isArrayOfIdenticalTypes($first, $second);
            }
            return true;
        }

        /**
         *    Recursive type test for each element of an array.
         *    @param mixed $first    Test subject.
         *    @param mixed $second   Comparison object.
         *    @access private
         *    @static
         */
        static function _isArrayOfIdenticalTypes($first, $second) {
            if (array_keys($first) != array_keys($second)) {
                return false;
            }
            foreach (array_keys($first) as $key) {
                $is_identical = SimpleTestCompatibility::_isIdenticalType(
                        $first[$key],
                        $second[$key]);
                if (! $is_identical) {
                    return false;
                }
            }
            return true;
        }

        /**
         *    Test for two variables being aliases.
         *    @param mixed $first    Test subject.
         *    @param mixed $second   Comparison object.
         *    @access public
         *    @static
         */
        static function isReference($first, $second) {
            if (version_compare(phpversion(), '5', '>=')
	    	    && is_object($first)) {
	    	    return ($first === $second);
	        }
	        $temp = $first;
            $first = uniqid("test");
            $is_ref = ($first === $second);
            $first = $temp;
            return $is_ref;
        }

        /**
         *    Test to see if an object is a member of a
         *    class hiearchy.
         *    @param object $object    Object to test.
         *    @param string $class     Root name of hiearchy.
         *    @access public
         *    @static
         */
        static function isA($object, $class) {
            if (version_compare(phpversion(), '5') >= 0) {
                if (! class_exists($class, false)) {
                    return false;
                }
                eval("\$is_a = \$object instanceof $class;");
                return $is_a;
            }
            if (function_exists('is_a')) {
                return is_a($object, $class);
            }
            return ((strtolower($class) == get_class($object))
                    or (is_subclass_of($object, $class)));
        }

        /**
         *    Autoload safe version of class_exists().
         *    @param string $class        Name of class to look for.
         *    @return boolean             True if class is defined.
         *    @access public
         *    @static
         */
        static function classExists($class) {
            if (version_compare(phpversion(), '5') >= 0) {
                return class_exists($class, false);
            } else {
                return class_exists($class);
            }
        }

        /**
         *    Sets a socket timeout for each chunk.
         *    @param resource $handle    Socket handle.
         *    @param integer $timeout    Limit in seconds.
         *    @access public
         *    @static
         */
        static function setTimeout($handle, $timeout) {
            if (function_exists('stream_set_timeout')) {
                stream_set_timeout($handle, $timeout, 0);
            } elseif (function_exists('socket_set_timeout')) {
                socket_set_timeout($handle, $timeout, 0);
            } elseif (function_exists('set_socket_timeout')) {
                set_socket_timeout($handle, $timeout, 0);
            }
        }

        /**
         *    Gets the current stack trace topmost first.
         *    @return array        List of stack frames.
         *    @access public
         *    @static
         */
        static function getStackTrace() {
            if (function_exists('debug_backtrace')) {
                return array_reverse(debug_backtrace());
            }
            return array();
        }
    }