3 * Global state for SimpleTest and kicker script in future versions.
5 * @subpackage UnitTester
10 * include SimpleTest files
12 if (version_compare(phpversion(), '5') >= 0) {
13 require_once(dirname(__FILE__
) . '/reflection_php5.php');
15 require_once(dirname(__FILE__
) . '/reflection_php4.php');
20 * Registry and test context. Includes a few
21 * global options that I'm slowly getting rid of.
27 * Reads the SimpleTest version from the release file.
28 * @return string Version string.
32 function getVersion() {
33 $content = file(dirname(__FILE__
) . '/VERSION');
34 return trim($content[0]);
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.
46 function ignore($class) {
47 $registry = &SimpleTest
::_getRegistry();
48 $registry['IgnoreList'][strtolower($class)] = true;
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.
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);
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
84 * @param string $class Class name to test.
85 * @return boolean True if should not be run.
89 function isIgnored($class) {
90 $registry = &SimpleTest
::_getRegistry();
91 return isset($registry['IgnoreList'][strtolower($class)]);
97 function setMockBaseClass($mock_base) {
98 $registry = &SimpleTest
::_getRegistry();
99 $registry['MockBaseClass'] = $mock_base;
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.
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.
132 function getDefaultProxy() {
133 $registry = &SimpleTest
::_getRegistry();
134 return $registry['DefaultProxy'];
138 * Accessor for default proxy username.
139 * @return string Proxy username for authentication.
142 function getDefaultProxyUsername() {
143 $registry = &SimpleTest
::_getRegistry();
144 return $registry['DefaultProxyUsername'];
148 * Accessor for default proxy password.
149 * @return string Proxy password for authentication.
152 function getDefaultProxyPassword() {
153 $registry = &SimpleTest
::_getRegistry();
154 return $registry['DefaultProxyPassword'];
158 * Accessor for global registry of options.
159 * @return hash All stored values.
163 function &_getRegistry() {
164 static $registry = false;
166 $registry = SimpleTest
::_getDefaults();
172 * Accessor for the context of the current
174 * @return SimpleTestContext Current test run.
178 function &getContext() {
179 static $context = false;
181 $context = new SimpleTestContext();
187 * Constant default values.
188 * @return hash All registry defaults.
192 function _getDefaults() {
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
209 * @package SimpleTest
211 class SimpleTestContext
{
217 * Clears down the current context.
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.
231 function setTest(&$test) {
233 $this->_test
= &$test;
237 * Accessor for currently running test case.
238 * @return SimpleTestCase Current test.
241 function &getTest() {
246 * Sets the current reporter. This
247 * global instance can be used by the mock objects
249 * @param SimpleReporter $reporter Reporter to register.
252 function setReporter(&$reporter) {
254 $this->_reporter
= &$reporter;
258 * Accessor for current reporter.
259 * @return SimpleReporter Current reporter.
262 function &getReporter() {
263 return $this->_reporter
;
267 * Accessor for the Singleton resource.
268 * @return object Global resource.
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
283 * @package SimpleTest
284 * @subpackage UnitTester
286 class SimpleStackTrace
{
290 * Stashes the list of target prefixes.
291 * @param array $prefixes List of method prefixes
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
306 function traceMethod($stack = false) {
307 $stack = $stack ?
$stack : $this->_captureTrace();
308 foreach ($stack as $frame) {
309 if ($this->_frameLiesWithinSimpleTestFolder($frame)) {
312 if ($this->_frameMatchesPrefix($frame)) {
313 return ' at [' . $frame['file'] . ' line ' . $frame['line'] . ']';
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.
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) {
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.
343 function _frameMatchesPrefix($frame) {
344 foreach ($this->_prefixes
as $prefix) {
345 if (strncmp($frame['function'], $prefix, strlen($prefix)) == 0) {
353 * Grabs a current stack trace.
354 * @return array Fulle trace.
357 function _captureTrace() {
358 if (function_exists('debug_backtrace')) {
359 return array_reverse(debug_backtrace());
368 class SimpleTestOptions
extends SimpleTest
{
373 function getVersion() {
374 return Simpletest
::getVersion();
380 function ignore($class) {
381 return Simpletest
::ignore($class);
387 function isIgnored($class) {
388 return Simpletest
::isIgnored($class);
394 function setMockBaseClass($mock_base) {
395 return Simpletest
::setMockBaseClass($mock_base);
401 function getMockBaseClass() {
402 return Simpletest
::getMockBaseClass();
408 function useProxy($proxy, $username = false, $password = false) {
409 return Simpletest
::useProxy($proxy, $username, $password);
415 function getDefaultProxy() {
416 return Simpletest
::getDefaultProxy();
422 function getDefaultProxyUsername() {
423 return Simpletest
::getDefaultProxyUsername();
429 function getDefaultProxyPassword() {
430 return Simpletest
::getDefaultProxyPassword();