Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / admin / report / simpletest / ex_simple_test.php
blobc6732e9d4f04c8054640a638fe96cfc20f88ba3a
1 <?php
2 /**
3 * A SimpleTest GroupTest that automatically finds all the
4 * test files in a directory tree according to certain rules.
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
13 if (!defined('MOODLE_INTERNAL')) {
14 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
17 require_once($CFG->libdir . '/simpletestlib/test_case.php');
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
25 class AutoGroupTest extends GroupTest {
27 var $thorough;
28 var $showsearch;
30 function AutoGroupTest($showsearch, $thorough, $test_name = null) {
31 $this->GroupTest($test_name);
32 $this->showsearch = $showsearch;
33 $this->thorough = $thorough;
36 function setLabel($test_name) {
37 //:HACK: there is no GroupTest::setLabel, so access parent::_label.
38 $this->_label = $test_name;
41 function addIgnoreFolder($ignorefolder) {
42 $this->ignorefolders[]=$ignorefolder;
45 function _recurseFolders($path) {
46 if ($this->showsearch) {
47 echo '<li>' . basename(realpath($path)) . '<ul>';
50 $files = scandir($path);
51 static $s_count = 0;
53 foreach ($files as $file) {
54 if ($file == '.' || $file == '..') {
55 continue;
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);
62 } elseif (preg_match('/simpletest(\/|\\\\)test.*\.php$/', $file_path) ||
63 ($this->thorough && preg_match('/simpletest(\/|\\\\)slowtest.*\.php$/', $file_path))) {
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));
69 // addTestFile: Unfortunately this doesn't return fail/success (bool).
70 $this->addTestFile($file_path, true);
74 if ($this->showsearch) {
75 echo '</ul></li>';
77 return $s_count;
80 function findTestFiles($dir) {
81 if ($this->showsearch) {
82 echo '<p>Searching folder: ' . realpath($dir) . '</p><ul>';
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));
91 if ($this->showsearch) {
92 echo '</ul>';
94 return $count;
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>';
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);
108 if(!is_file($file) ){
109 parent::addTestCase(new BadTest($file, 'Not a file or does not exist'));
111 parent::addTestFile($file);
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));
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
128 class BadTest {
130 var $label;
131 var $error;
133 function BadTest($label, $error) {
134 $this->label = $label;
135 $this->error = $error;
138 function getLabel() {
139 return $this->label;
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();
151 * @return int the number of test cases starting.
153 function getSize() {
154 return 0;
159 * An informational notice base-class for when a test suite is being processed.
160 * See class, simple_test.php: BadGroupTest.
161 * @package SimpleTestEx
163 class Notice {
165 var $label;
166 var $status;
168 function Notice($label, $error) {
169 $this->label = $label;
170 $this->status = $error;
173 function getLabel() {
174 return $this->label;
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();
185 function getSize() {
186 return 0;
191 * A failing folder test for when the test-user specifies an invalid directory
192 * (run.php?folder=woops).
193 * @package SimpleTestEx
195 class BadFolderTest extends BadTest { }
198 * A failing auto test for when no unit test files are found.
199 * @package SimpleTestEx
201 class BadAutoGroupTest extends BadTest { }
204 * Auto group test notices - 1. Search complete. 2. A test file has been found.
205 * @package SimpleTestEx
207 class AutoGroupTestNotice extends Notice { }
209 class FindFileNotice extends Notice { }