Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/tool/unittest/ex_simple_test.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00028 if (!defined('MOODLE_INTERNAL')) {
00029     die('Direct access to this script is forbidden.');    
00030 }
00031 
00032 require_once($CFG->libdir . '/simpletestlib/test_case.php');
00033 
00040 class AutoGroupTest extends TestSuite {
00041 
00042     var $showsearch;
00043 
00044     function AutoGroupTest($showsearch, $test_name = null) {
00045         $this->TestSuite($test_name);
00046         $this->showsearch = $showsearch;
00047     }
00048 
00049     function run(&$reporter) {
00050         global $UNITTEST;
00051 
00052         $UNITTEST->running = true;
00053         $return = parent::run($reporter);
00054         unset($UNITTEST->running);
00055         return $return;
00056     }
00057 
00058     function setLabel($test_name) {
00059         //:HACK: there is no GroupTest::setLabel, so access parent::_label.
00060         $this->_label = $test_name;
00061     }
00062 
00063     function addIgnoreFolder($ignorefolder) {
00064         $this->ignorefolders[]=$ignorefolder;
00065     }
00066 
00067     function _recurseFolders($path) {
00068         if ($this->showsearch) {
00069             echo '<li>' . basename(realpath($path)) . '<ul>';
00070         }
00071 
00072         $files = scandir($path);
00073         static $s_count = 0;
00074 
00075         foreach ($files as $file) {
00076             if ($file == '.' || $file == '..') {
00077                 continue;
00078             }
00079             $file_path = $path . '/' . $file;
00080             if (is_dir($file_path)) {
00081                 if ($file != 'CVS' && $file != '.git' && !in_array($file_path, $this->ignorefolders)) {
00082                     $this->_recurseFolders($file_path);
00083                 }
00084             } elseif (preg_match('/simpletest(\/|\\\\)test.*\.php$/', $file_path)) {
00085 
00086                 $s_count++;
00087                 // OK, found: this shows as a 'Notice' for any 'simpletest/test*.php' file.
00088                 $this->addTestCase(new FindFileNotice($file_path, 'Found unit test file, '. $s_count));
00089 
00090                 // addTestFile: Unfortunately this doesn't return fail/success (bool).
00091                 $this->addTestFile($file_path, true);
00092             }
00093         }
00094 
00095         if ($this->showsearch) {
00096             echo '</ul></li>';
00097         }
00098         return $s_count;
00099     }
00100 
00101     function findTestFiles($dir) {
00102         if ($this->showsearch) {
00103             echo '<p>Searching folder: ' . realpath($dir) . '</p><ul>';
00104         }
00105         $path = $dir;
00106         $count = $this->_recurseFolders($path);
00107         if ($count <= 0) {
00108             $this->addTestCase(new BadAutoGroupTest($path, 'Search complete. No unit test files found'));
00109         } else {
00110             $this->addTestCase(new AutoGroupTestNotice($path, 'Search complete. Total unit test files found: '. $count));
00111         }
00112         if ($this->showsearch) {
00113                 echo '</ul>';
00114         }
00115         return $count;
00116     }
00117 
00118     function addTestFile($file, $internalcall = false) {
00119         if ($this->showsearch) {
00120             if ($internalcall) {
00121                 echo '<li><b>' . basename($file) . '</b></li>';
00122             } else {
00123                 echo '<p>Adding test file: ' . realpath($file) . '</p>';
00124             }
00125             // Make sure that syntax errors show up suring the search, otherwise you often
00126             // get blank screens because evil people turn down error_reporting elsewhere.
00127             error_reporting(E_ALL);
00128         }
00129         if(!is_file($file) ){
00130             parent::addTestCase(new BadTest($file, 'Not a file or does not exist'));
00131         }
00132         parent::addTestFile($file);
00133     }
00134 }
00135 
00136 
00137 /* ======================================================================= */
00138 // get_class_ex: Insert spaces to prettify the class-name.
00139 function get_class_ex($object) {
00140     return preg_replace('/(.?)([A-Z])/', '${1} ${2}', get_class($object));
00141 }
00142 
00143 
00149 class BadTest {
00150 
00151     var $label;
00152     var $error;
00153 
00154     function BadTest($label, $error) {
00155         $this->label = $label;
00156         $this->error = $error;
00157     }
00158 
00159     function getLabel() {
00160         return $this->label;
00161     }
00162 
00163     function run(&$reporter) {
00164         $reporter->paintGroupStart(basename(__FILE__), $this->getSize());
00165         $reporter->paintFail(get_class_ex($this) .' [' . $this->getLabel() .
00166                 '] with error [' . $this->error . ']');
00167         $reporter->paintGroupEnd($this->getLabel());
00168         return $reporter->getStatus();
00169     }
00170 
00174     function getSize() {
00175         return 0;
00176   }
00177 }
00178 
00184 class Notice {
00185 
00186     var $label;
00187     var $status;
00188 
00189     function Notice($label, $error) {
00190         $this->label = $label;
00191         $this->status = $error;
00192     }
00193 
00194     function getLabel() {
00195         return $this->label;
00196     }
00197 
00198     function run(&$reporter) {
00199         $reporter->paintGroupStart(basename(__FILE__), $this->getSize());
00200         $reporter->paintNotice(get_class_ex($this) .
00201                 ' ['. $this->getLabel() .'] with status [' . $this->status . ']');
00202         $reporter->paintGroupEnd($this->getLabel());
00203         return $reporter->getStatus();
00204     }
00205 
00206     function getSize() {
00207         return 0;
00208     }
00209 }
00210 
00216 class BadFolderTest extends BadTest { }
00217 
00222 class BadAutoGroupTest extends BadTest { }
00223 
00228 class AutoGroupTestNotice extends Notice { }
00229 
00230 class FindFileNotice extends Notice { }
 All Data Structures Namespaces Files Functions Variables Enumerations