[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/lib/simpletestlib/ -> simpletest.php (source)

   1  <?php
   2      /**
   3       *    Global state for SimpleTest and kicker script in future versions.
   4       *    @package    SimpleTest
   5       *    @subpackage    UnitTester
   6       *    @version    $Id$
   7       */
   8  
   9      /**#@+
  10       * include SimpleTest files
  11       */
  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');
  16      }
  17      /**#@-*/
  18  
  19      /**
  20       *    Registry and test context. Includes a few
  21       *    global options that I'm slowly getting rid of.
  22       *      @package    SimpleTest
  23       */
  24      class SimpleTest {
  25  
  26          /**
  27           *    Reads the SimpleTest version from the release file.
  28           *    @return string        Version string.
  29           *    @static
  30           *    @access public
  31           */
  32          function getVersion() {
  33              $content = file(dirname(__FILE__) . '/VERSION');
  34              return trim($content[0]);
  35          }
  36  
  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
  45           */
  46          function ignore($class) {
  47              $registry = &SimpleTest::_getRegistry();
  48              $registry['IgnoreList'][strtolower($class)] = true;
  49          }
  50  
  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
  65           */
  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);
  73                      }
  74                  }
  75              }
  76          }
  77  
  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
  88           */
  89          function isIgnored($class) {
  90              $registry = &SimpleTest::_getRegistry();
  91              return isset($registry['IgnoreList'][strtolower($class)]);
  92          }
  93  
  94          /**
  95           *    @deprecated
  96           */
  97          function setMockBaseClass($mock_base) {
  98              $registry = &SimpleTest::_getRegistry();
  99              $registry['MockBaseClass'] = $mock_base;
 100          }
 101  
 102          /**
 103           *    @deprecated
 104           */
 105          function getMockBaseClass() {
 106              $registry = &SimpleTest::_getRegistry();
 107              return $registry['MockBaseClass'];
 108          }
 109  
 110          /**
 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
 119           */
 120          function useProxy($proxy, $username = false, $password = false) {
 121              $registry = &SimpleTest::_getRegistry();
 122              $registry['DefaultProxy'] = $proxy;
 123              $registry['DefaultProxyUsername'] = $username;
 124              $registry['DefaultProxyPassword'] = $password;
 125          }
 126  
 127          /**
 128           *    Accessor for default proxy host.
 129           *    @return string       Proxy URL.
 130           *    @access public
 131           */
 132          function getDefaultProxy() {
 133              $registry = &SimpleTest::_getRegistry();
 134              return $registry['DefaultProxy'];
 135          }
 136  
 137          /**
 138           *    Accessor for default proxy username.
 139           *    @return string    Proxy username for authentication.
 140           *    @access public
 141           */
 142          function getDefaultProxyUsername() {
 143              $registry = &SimpleTest::_getRegistry();
 144              return $registry['DefaultProxyUsername'];
 145          }
 146  
 147          /**
 148           *    Accessor for default proxy password.
 149           *    @return string    Proxy password for authentication.
 150           *    @access public
 151           */
 152          function getDefaultProxyPassword() {
 153              $registry = &SimpleTest::_getRegistry();
 154              return $registry['DefaultProxyPassword'];
 155          }
 156  
 157          /**
 158           *    Accessor for global registry of options.
 159           *    @return hash           All stored values.
 160           *    @access private
 161           *    @static
 162           */
 163          function &_getRegistry() {
 164              static $registry = false;
 165              if (! $registry) {
 166                  $registry = SimpleTest::_getDefaults();
 167              }
 168              return $registry;
 169          }
 170  
 171          /**
 172           *    Accessor for the context of the current
 173           *    test run.
 174           *    @return SimpleTestContext    Current test run.
 175           *    @access public
 176           *    @static
 177           */
 178          function &getContext() {
 179              static $context = false;
 180              if (! $context) {
 181                  $context = new SimpleTestContext();
 182              }
 183              return $context;
 184          }
 185  
 186          /**
 187           *    Constant default values.
 188           *    @return hash       All registry defaults.
 189           *    @access private
 190           *    @static
 191           */
 192          function _getDefaults() {
 193              return array(
 194                      'StubBaseClass' => 'SimpleStub',
 195                      'MockBaseClass' => 'SimpleMock',
 196                      'IgnoreList' => array(),
 197                      'DefaultProxy' => false,
 198                      'DefaultProxyUsername' => false,
 199                      'DefaultProxyPassword' => false);
 200          }
 201      }
 202  
 203      /**
 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
 210       */
 211      class SimpleTestContext {
 212          var $_test;
 213          var $_reporter;
 214          var $_resources;
 215  
 216          /**
 217           *    Clears down the current context.
 218           *    @access public
 219           */
 220          function clear() {
 221              $this->_resources = array();
 222          }
 223  
 224          /**
 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
 230           */
 231          function setTest(&$test) {
 232              $this->clear();
 233              $this->_test = &$test;
 234          }
 235  
 236          /**
 237           *    Accessor for currently running test case.
 238           *    @return SimpleTestCase    Current test.
 239           *    @acess pubic
 240           */
 241          function &getTest() {
 242              return $this->_test;
 243          }
 244  
 245          /**
 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
 251           */
 252          function setReporter(&$reporter) {
 253              $this->clear();
 254              $this->_reporter = &$reporter;
 255          }
 256  
 257          /**
 258           *    Accessor for current reporter.
 259           *    @return SimpleReporter    Current reporter.
 260           *    @acess pubic
 261           */
 262          function &getReporter() {
 263              return $this->_reporter;
 264          }
 265  
 266          /**
 267           *    Accessor for the Singleton resource.
 268           *    @return object       Global resource.
 269           *    @access public
 270           *    @static
 271           */
 272          function &get($resource) {
 273              if (! isset($this->_resources[$resource])) {
 274                  $this->_resources[$resource] = &new $resource();
 275              }
 276              return $this->_resources[$resource];
 277          }
 278      }
 279  
 280      /**
 281       *    Interrogates the stack trace to recover the
 282       *    failure point.
 283       *      @package SimpleTest
 284       *      @subpackage UnitTester
 285       */
 286      class SimpleStackTrace {
 287          var $_prefixes;
 288  
 289          /**
 290           *    Stashes the list of target prefixes.
 291           *    @param array $prefixes      List of method prefixes
 292           *                                to search for.
 293           */
 294          function SimpleStackTrace($prefixes) {
 295              $this->_prefixes = $prefixes;
 296          }
 297  
 298          /**
 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
 305           */
 306          function traceMethod($stack = false) {
 307              $stack = $stack ? $stack : $this->_captureTrace();
 308              foreach ($stack as $frame) {
 309                  if ($this->_frameLiesWithinSimpleTestFolder($frame)) {
 310                      continue;
 311                  }
 312                  if ($this->_frameMatchesPrefix($frame)) {
 313                      return ' at [' . $frame['file'] . ' line ' . $frame['line'] . ']';
 314                  }
 315              }
 316              return '';
 317          }
 318  
 319          /**
 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
 324           */
 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;
 331                      }
 332                  }
 333              }
 334              return false;
 335          }
 336  
 337          /**
 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
 342           */
 343          function _frameMatchesPrefix($frame) {
 344              foreach ($this->_prefixes as $prefix) {
 345                  if (strncmp($frame['function'], $prefix, strlen($prefix)) == 0) {
 346                      return true;
 347                  }
 348              }
 349              return false;
 350          }
 351  
 352          /**
 353           *    Grabs a current stack trace.
 354           *    @return array        Fulle trace.
 355           *    @access private
 356           */
 357          function _captureTrace() {
 358              if (function_exists('debug_backtrace')) {
 359                  return array_reverse(debug_backtrace());
 360              }
 361              return array();
 362          }
 363      }
 364  
 365      /**
 366       *    @deprecated
 367       */
 368      class SimpleTestOptions extends SimpleTest {
 369  
 370          /**
 371           *    @deprecated
 372           */
 373          function getVersion() {
 374              return Simpletest::getVersion();
 375          }
 376  
 377          /**
 378           *    @deprecated
 379           */
 380          function ignore($class) {
 381              return Simpletest::ignore($class);
 382          }
 383  
 384          /**
 385           *    @deprecated
 386           */
 387          function isIgnored($class) {
 388              return Simpletest::isIgnored($class);
 389          }
 390  
 391          /**
 392           *    @deprecated
 393           */
 394          function setMockBaseClass($mock_base) {
 395              return Simpletest::setMockBaseClass($mock_base);
 396          }
 397  
 398          /**
 399           *    @deprecated
 400           */
 401          function getMockBaseClass() {
 402              return Simpletest::getMockBaseClass();
 403          }
 404  
 405          /**
 406           *    @deprecated
 407           */
 408          function useProxy($proxy, $username = false, $password = false) {
 409              return Simpletest::useProxy($proxy, $username, $password);
 410          }
 411  
 412          /**
 413           *    @deprecated
 414           */
 415          function getDefaultProxy() {
 416              return Simpletest::getDefaultProxy();
 417          }
 418  
 419          /**
 420           *    @deprecated
 421           */
 422          function getDefaultProxyUsername() {
 423              return Simpletest::getDefaultProxyUsername();
 424          }
 425  
 426          /**
 427           *    @deprecated
 428           */
 429          function getDefaultProxyPassword() {
 430              return Simpletest::getDefaultProxyPassword();
 431          }
 432      }
 433  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7