premier commit
[bazdig.git] / test / simpletest / simpletest.php
blob5c7cffe08ea87dfafa6f64b096a61f5695c87af7
1 <?php
2 /**
3 * Global state for SimpleTest and kicker script in future versions.
4 * @package SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: simpletest.php,v 1.13 2006/11/20 19:17:06 lastcraft Exp $
7 */
9 /**#@+
10 * include SimpleTest files
12 if (version_compare(phpversion(), '5') >= 0) {
13 require_once(dirname(__FILE__) . '/reflection_php5.php');
14 } else {
15 require_once(dirname(__FILE__) . '/reflection_php4.php');
17 /**#@-*/
19 /**
20 * Registry and test context. Includes a few
21 * global options that I'm slowly getting rid of.
22 * @package SimpleTest
24 class SimpleTest {
26 /**
27 * Reads the SimpleTest version from the release file.
28 * @return string Version string.
29 * @static
30 * @access public
32 function getVersion() {
33 $content = file(dirname(__FILE__) . '/VERSION');
34 return trim($content[0]);
37 /**
38 * Sets the name of a test case to ignore, usually
39 * because the class is an abstract case that should
40 * not be run. Once PHP4 is dropped this will disappear
41 * as a public method and "abstract" will rule.
42 * @param string $class Add a class to ignore.
43 * @static
44 * @access public
46 function ignore($class) {
47 $registry = &SimpleTest::_getRegistry();
48 $registry['IgnoreList'][strtolower($class)] = true;
51 /**
52 * Scans the now complete ignore list, and adds
53 * all parent classes to the list. If a class
54 * is not a runnable test case, then it's parents
55 * wouldn't be either. This is syntactic sugar
56 * to cut down on ommissions of ignore()'s or
57 * missing abstract declarations. This cannot
58 * be done whilst loading classes wiithout forcing
59 * a particular order on the class declarations and
60 * the ignore() calls. It's just nice to have the ignore()
61 * calls at the top of the file before the actual declarations.
62 * @param array $classes Class names of interest.
63 * @static
64 * @access public
66 function ignoreParentsIfIgnored($classes) {
67 $registry = &SimpleTest::_getRegistry();
68 foreach ($classes as $class) {
69 if (SimpleTest::isIgnored($class)) {
70 $reflection = new SimpleReflection($class);
71 if ($parent = $reflection->getParent()) {
72 SimpleTest::ignore($parent);
78 /**
79 * Test to see if a test case is in the ignore
80 * list. Quite obviously the ignore list should
81 * be a separate object and will be one day.
82 * This method is internal to SimpleTest. Don't
83 * use it.
84 * @param string $class Class name to test.
85 * @return boolean True if should not be run.
86 * @access public
87 * @static
89 function isIgnored($class) {
90 $registry = &SimpleTest::_getRegistry();
91 return isset($registry['IgnoreList'][strtolower($class)]);
94 /**
95 * @deprecated
97 function setMockBaseClass($mock_base) {
98 $registry = &SimpleTest::_getRegistry();
99 $registry['MockBaseClass'] = $mock_base;
103 * @deprecated
105 function getMockBaseClass() {
106 $registry = &SimpleTest::_getRegistry();
107 return $registry['MockBaseClass'];
111 * Sets proxy to use on all requests for when
112 * testing from behind a firewall. Set host
113 * to false to disable. This will take effect
114 * if there are no other proxy settings.
115 * @param string $proxy Proxy host as URL.
116 * @param string $username Proxy username for authentication.
117 * @param string $password Proxy password for authentication.
118 * @access public
120 function useProxy($proxy, $username = false, $password = false) {
121 $registry = &SimpleTest::_getRegistry();
122 $registry['DefaultProxy'] = $proxy;
123 $registry['DefaultProxyUsername'] = $username;
124 $registry['DefaultProxyPassword'] = $password;
128 * Accessor for default proxy host.
129 * @return string Proxy URL.
130 * @access public
132 function getDefaultProxy() {
133 $registry = &SimpleTest::_getRegistry();
134 return $registry['DefaultProxy'];
138 * Accessor for default proxy username.
139 * @return string Proxy username for authentication.
140 * @access public
142 function getDefaultProxyUsername() {
143 $registry = &SimpleTest::_getRegistry();
144 return $registry['DefaultProxyUsername'];
148 * Accessor for default proxy password.
149 * @return string Proxy password for authentication.
150 * @access public
152 function getDefaultProxyPassword() {
153 $registry = &SimpleTest::_getRegistry();
154 return $registry['DefaultProxyPassword'];
158 * Accessor for global registry of options.
159 * @return hash All stored values.
160 * @access private
161 * @static
163 function &_getRegistry() {
164 static $registry = false;
165 if (! $registry) {
166 $registry = SimpleTest::_getDefaults();
168 return $registry;
172 * Accessor for the context of the current
173 * test run.
174 * @return SimpleTestContext Current test run.
175 * @access public
176 * @static
178 function &getContext() {
179 static $context = false;
180 if (! $context) {
181 $context = new SimpleTestContext();
183 return $context;
187 * Constant default values.
188 * @return hash All registry defaults.
189 * @access private
190 * @static
192 function _getDefaults() {
193 return array(
194 'StubBaseClass' => 'SimpleStub',
195 'MockBaseClass' => 'SimpleMock',
196 'IgnoreList' => array(),
197 'DefaultProxy' => false,
198 'DefaultProxyUsername' => false,
199 'DefaultProxyPassword' => false);
204 * Container for all components for a specific
205 * test run. Makes things like error queues
206 * available to PHP event handlers, and also
207 * gets around some nasty reference issues in
208 * the mocks.
209 * @package SimpleTest
211 class SimpleTestContext {
212 var $_test;
213 var $_reporter;
214 var $_resources;
217 * Clears down the current context.
218 * @access public
220 function clear() {
221 $this->_resources = array();
225 * Sets the current test case instance. This
226 * global instance can be used by the mock objects
227 * to send message to the test cases.
228 * @param SimpleTestCase $test Test case to register.
229 * @access public
231 function setTest(&$test) {
232 $this->clear();
233 $this->_test = &$test;
237 * Accessor for currently running test case.
238 * @return SimpleTestCase Current test.
239 * @acess pubic
241 function &getTest() {
242 return $this->_test;
246 * Sets the current reporter. This
247 * global instance can be used by the mock objects
248 * to send messages.
249 * @param SimpleReporter $reporter Reporter to register.
250 * @access public
252 function setReporter(&$reporter) {
253 $this->clear();
254 $this->_reporter = &$reporter;
258 * Accessor for current reporter.
259 * @return SimpleReporter Current reporter.
260 * @acess pubic
262 function &getReporter() {
263 return $this->_reporter;
267 * Accessor for the Singleton resource.
268 * @return object Global resource.
269 * @access public
270 * @static
272 function &get($resource) {
273 if (! isset($this->_resources[$resource])) {
274 $this->_resources[$resource] = &new $resource();
276 return $this->_resources[$resource];
281 * Interrogates the stack trace to recover the
282 * failure point.
283 * @package SimpleTest
284 * @subpackage UnitTester
286 class SimpleStackTrace {
287 var $_prefixes;
290 * Stashes the list of target prefixes.
291 * @param array $prefixes List of method prefixes
292 * to search for.
294 function SimpleStackTrace($prefixes) {
295 $this->_prefixes = $prefixes;
299 * Extracts the last method name that was not within
300 * Simpletest itself. Captures a stack trace if none given.
301 * @param array $stack List of stack frames.
302 * @return string Snippet of test report with line
303 * number and file.
304 * @access public
306 function traceMethod($stack = false) {
307 $stack = $stack ? $stack : $this->_captureTrace();
308 foreach ($stack as $frame) {
309 if ($this->_frameLiesWithinSimpleTestFolder($frame)) {
310 continue;
312 if ($this->_frameMatchesPrefix($frame)) {
313 return ' at [' . $frame['file'] . ' line ' . $frame['line'] . ']';
316 return '';
320 * Test to see if error is generated by SimpleTest itself.
321 * @param array $frame PHP stack frame.
322 * @return boolean True if a SimpleTest file.
323 * @access private
325 function _frameLiesWithinSimpleTestFolder($frame) {
326 if (isset($frame['file'])) {
327 $path = substr(SIMPLE_TEST, 0, -1);
328 if (strpos($frame['file'], $path) === 0) {
329 if (dirname($frame['file']) == $path) {
330 return true;
334 return false;
338 * Tries to determine if the method call is an assert, etc.
339 * @param array $frame PHP stack frame.
340 * @return boolean True if matches a target.
341 * @access private
343 function _frameMatchesPrefix($frame) {
344 foreach ($this->_prefixes as $prefix) {
345 if (strncmp($frame['function'], $prefix, strlen($prefix)) == 0) {
346 return true;
349 return false;
353 * Grabs a current stack trace.
354 * @return array Fulle trace.
355 * @access private
357 function _captureTrace() {
358 if (function_exists('debug_backtrace')) {
359 return array_reverse(debug_backtrace());
361 return array();
366 * @deprecated
368 class SimpleTestOptions extends SimpleTest {
371 * @deprecated
373 function getVersion() {
374 return Simpletest::getVersion();
378 * @deprecated
380 function ignore($class) {
381 return Simpletest::ignore($class);
385 * @deprecated
387 function isIgnored($class) {
388 return Simpletest::isIgnored($class);
392 * @deprecated
394 function setMockBaseClass($mock_base) {
395 return Simpletest::setMockBaseClass($mock_base);
399 * @deprecated
401 function getMockBaseClass() {
402 return Simpletest::getMockBaseClass();
406 * @deprecated
408 function useProxy($proxy, $username = false, $password = false) {
409 return Simpletest::useProxy($proxy, $username, $password);
413 * @deprecated
415 function getDefaultProxy() {
416 return Simpletest::getDefaultProxy();
420 * @deprecated
422 function getDefaultProxyUsername() {
423 return Simpletest::getDefaultProxyUsername();
427 * @deprecated
429 function getDefaultProxyPassword() {
430 return Simpletest::getDefaultProxyPassword();