[ Index ]

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

title

Body

[close]

/admin/report/simpletest/ -> ex_simple_test.php (source)

   1  <?php
   2  /**
   3   * A SimpleTest GroupTest that automatically finds all the
   4   * test files in a directory tree according to certain rules.
   5   *
   6   * @copyright &copy; 2006 The Open University
   7   * @author N.D.Freear@open.ac.uk, T.J.Hunt@open.ac.uk
   8   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
   9   * @version $Id$
  10   * @package SimpleTestEx
  11   */
  12  
  13  if (!defined('MOODLE_INTERNAL')) {
  14      die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
  15  }
  16  
  17  require_once($CFG->libdir . '/simpletestlib/test_case.php');
  18  
  19  /**
  20   * This is a composite test class for finding test cases and
  21   * other RunnableTest classes in a directory tree and combining
  22   * them into a group test.
  23   * @package SimpleTestEx
  24   */
  25  class AutoGroupTest extends GroupTest {
  26  
  27      var $thorough;
  28      var $showsearch;
  29  
  30      function AutoGroupTest($showsearch, $thorough, $test_name = null) {
  31          $this->GroupTest($test_name);
  32          $this->showsearch = $showsearch;
  33          $this->thorough = $thorough;
  34      }
  35  
  36      function setLabel($test_name) {
  37          //:HACK: there is no GroupTest::setLabel, so access parent::_label.
  38          $this->_label = $test_name;
  39      }
  40  
  41      function addIgnoreFolder($ignorefolder) {
  42          $this->ignorefolders[]=$ignorefolder;
  43      }
  44  
  45      function _recurseFolders($path) {
  46          if ($this->showsearch) {
  47              echo '<li>' . basename(realpath($path)) . '<ul>';
  48          }
  49  
  50          $files = scandir($path);
  51          static $s_count = 0;
  52  
  53          foreach ($files as $file) {
  54              if ($file == '.' || $file == '..') {
  55                  continue;
  56              }
  57              $file_path = $path . '/' . $file;
  58              if (is_dir($file_path)) {
  59                  if ($file != 'CVS' && !in_array($file_path, $this->ignorefolders)) {
  60                      $this->_recurseFolders($file_path);
  61                  }
  62              } elseif (preg_match('/simpletest(\/|\\\\)test.*\.php$/', $file_path) ||
  63                      ($this->thorough && preg_match('/simpletest(\/|\\\\)slowtest.*\.php$/', $file_path))) {
  64  
  65                  $s_count++;
  66                  // OK, found: this shows as a 'Notice' for any 'simpletest/test*.php' file.
  67                  $this->addTestCase(new FindFileNotice($file_path, 'Found unit test file, '. $s_count));
  68  
  69                  // addTestFile: Unfortunately this doesn't return fail/success (bool).
  70                  $this->addTestFile($file_path, true);
  71              }
  72          }
  73  
  74          if ($this->showsearch) {
  75              echo '</ul></li>';
  76          }
  77          return $s_count;
  78      }
  79  
  80      function findTestFiles($dir) {
  81          if ($this->showsearch) {
  82              echo '<p>Searching folder: ' . realpath($dir) . '</p><ul>';
  83          }
  84          $path = $dir;
  85          $count = $this->_recurseFolders($path);
  86          if ($count <= 0) {
  87              $this->addTestCase(new BadAutoGroupTest($path, 'Search complete. No unit test files found'));
  88          } else {
  89              $this->addTestCase(new AutoGroupTestNotice($path, 'Search complete. Total unit test files found: '. $count));
  90          }
  91          if ($this->showsearch) {
  92                  echo '</ul>';
  93          }
  94          return $count;
  95      }
  96  
  97      function addTestFile($file, $internalcall = false) {
  98          if ($this->showsearch) {
  99              if ($internalcall) {
 100                  echo '<li><b>' . basename($file) . '</b></li>';
 101              } else {
 102                  echo '<p>Adding test file: ' . realpath($file) . '</p>';
 103              }
 104              // Make sure that syntax errors show up suring the search, otherwise you often
 105              // get blank screens because evil people turn down error_reporting elsewhere.
 106              error_reporting(E_ALL);
 107          }
 108          if(!is_file($file) ){
 109              parent::addTestCase(new BadTest($file, 'Not a file or does not exist'));
 110          }
 111          parent::addTestFile($file);
 112      }
 113  }
 114  
 115  
 116  /* ======================================================================= */
 117  // get_class_ex: Insert spaces to prettify the class-name.
 118  function get_class_ex($object) {
 119      return preg_replace('/(.?)([A-Z])/', '$1} $2}', get_class($object));
 120  }
 121  
 122  
 123  /**
 124   * A failing test base-class for when a test suite has NOT loaded properly.
 125   * See class, simple_test.php: BadGroupTest.
 126   * @package SimpleTestEx
 127   */
 128  class BadTest {
 129  
 130      var $label;
 131      var $error;
 132  
 133      function BadTest($label, $error) {
 134          $this->label = $label;
 135          $this->error = $error;
 136      }
 137  
 138      function getLabel() {
 139          return $this->label;
 140      }
 141  
 142      function run(&$reporter) {
 143          $reporter->paintGroupStart(basename(__FILE__), $this->getSize());
 144          $reporter->paintFail(get_class_ex($this) .' [' . $this->getLabel() .
 145                  '] with error [' . $this->error . ']');
 146          $reporter->paintGroupEnd($this->getLabel());
 147          return $reporter->getStatus();
 148      }
 149  
 150      /**
 151       * @return int the number of test cases starting.
 152       */
 153      function getSize() {
 154          return 0;
 155    }
 156  }
 157  
 158  /**
 159   * An informational notice base-class for when a test suite is being processed.
 160   * See class, simple_test.php: BadGroupTest.
 161   * @package SimpleTestEx
 162   */
 163  class Notice {
 164  
 165      var $label;
 166      var $status;
 167  
 168      function Notice($label, $error) {
 169          $this->label = $label;
 170          $this->status = $error;
 171      }
 172  
 173      function getLabel() {
 174          return $this->label;
 175      }
 176  
 177      function run(&$reporter) {
 178          $reporter->paintGroupStart(basename(__FILE__), $this->getSize());
 179          $reporter->paintNotice(get_class_ex($this) .
 180                  ' ['. $this->getLabel() .'] with status [' . $this->status . ']');
 181          $reporter->paintGroupEnd($this->getLabel());
 182          return $reporter->getStatus();
 183      }
 184  
 185      function getSize() {
 186          return 0;
 187      }
 188  }
 189  
 190  /**
 191   * A failing folder test for when the test-user specifies an invalid directory
 192   * (run.php?folder=woops).
 193   * @package SimpleTestEx
 194   */
 195  class BadFolderTest extends BadTest { }
 196  
 197  /**
 198   * A failing auto test for when no unit test files are found.
 199   * @package SimpleTestEx
 200   */
 201  class BadAutoGroupTest extends BadTest { }
 202  
 203  /**
 204   * Auto group test notices - 1. Search complete. 2. A test file has been found.
 205   * @package SimpleTestEx
 206   */
 207  class AutoGroupTestNotice extends Notice { }
 208  
 209  class FindFileNotice extends Notice { }
 210  ?>


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