| [ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 /** 4 * \defgroup Test 5 * 6 * Unit testing in LifeType is implemented based on PHPUnit and provides a mechanism for users 7 * who write customizations or work with the LifeType core to make sure that their changes 8 * do not break the behaviour of the code being modified. 9 * 10 * Please see http://wiki.lifetype.net/index.php/Unit_Testing_in_LifeType for more information on 11 * how unit testing works with LifeType and how to create your own test cases. If you're only interested 12 * in running the included test cases, please use the runtest.php which you can get from Subversion (it 13 * is not included in final releases as it is only meaningful for developers) 14 */ 15 16 lt_include( PLOG_CLASS_PATH."class/misc/glob.class.php" ); 17 lt_include( PLOG_CLASS_PATH."class/file/file.class.php" ); 18 lt_include( PLOG_CLASS_PATH."class/test/PHPUnit.php" ); 19 20 /** 21 * base folder where all test cases are located 22 */ 23 define( "TEST_CLASS_FOLDER", PLOG_CLASS_PATH."class/test/tests" ); 24 25 /** 26 * pattern for test class names 27 */ 28 define( "TEST_CLASS_NAME_PATTERN", "*_test.class.php" ); 29 30 /** 31 * Exclude folders, seperated by comma 32 **/ 33 define( "EXCLUDE_FOLDERS_LIST", ".svn" ); 34 35 36 /** 37 * \ingroup Test 38 * 39 * This class takes care of running all unit tests in LifeType, as many as there are. This class 40 * process the contents of the class/test/tests/ folder and loads all the classes whose name 41 * ends with "_test.class.php" and uses PHPUnit to process them. 42 * 43 * The correct way to use this class is as follows: 44 * 45 * <pre> 46 * $r = new TestRunner(); 47 * $result = $r->run(); 48 * print( $result->toHTML()); 49 * </pre> 50 * 51 * In order to add new test cases, please reproduce the class/ structure in the class/test/tests folder 52 * (so that it is easier to tell which class each one of the tests is taking care of) and call your class 53 * ClassThatIsBeingTested_test.class.php. The TestRunner class will load them automatically and execute any 54 * methods whose name starts with "test". Please the PHPUnit documentation for more details on how to 55 * implement test cases. 56 */ 57 class TestRunner 58 { 59 var $folder; 60 var $pattern; 61 var $files; 62 var $suite; 63 var $excludeFolders; 64 var $listener; 65 66 /** 67 * Constructor. 68 * 69 * @param folder Where test suites are stored. Defaults to class/test/tests (relative 70 * to PLOG_CLASS_PATH) 71 * @param pattern Pattern that will be used to check whether a file in folder $folder 72 * this this work. The default value is 'false' 73 * is a test suite. Defaults to "*_test.class.php" 74 */ 75 function TestRunner( $folders = TEST_CLASS_FOLDER, $pattern = TEST_CLASS_NAME_PATTERN ) 76 { 77 if( !is_array( $folders )) 78 $folders = Array( $folders ); 79 80 $this->folders = $folders; 81 $this->pattern = $pattern; 82 $this->excludeFolders = explode( ",", EXCLUDE_FOLDERS_LIST ); 83 84 $this->files = $this->_findClasses( $this->folders, $this->pattern ); 85 86 $this->listener = NULL; 87 } 88 89 /** 90 * Adds a test listener 91 * 92 * @see PHPUnit_TestResult::addListener 93 */ 94 function addListener( &$listener ) 95 { 96 $this->listener = $listener; 97 } 98 99 /** 100 * Runs a test suite, or all of them if no test suite name is given 101 * 102 * @param suite The suite name, "all" or empty to run all suites 103 * @return Returns a PHPUnit_TestResult object containing information about which 104 * test suites were run and their results. Please use the HtmlReporter class to obtain 105 * a nicer report. 106 */ 107 function run( $suite = Array( "all" )) 108 { 109 // process all the classes and add them to the test suite 110 $this->suite = new PHPUnit_TestSuite(); 111 foreach( $this->files as $file ) { 112 // build the class name 113 $className = str_replace( ".class.php", "", basename( $file )); 114 // load the class file 115 //if( $suite == "all" || $suite == $className || $suite == str_replace( "_test", "", $className )) { 116 if( in_array( "all", $suite ) || in_array( $className, $suite ) || in_array( str_replace( "_test", "", $className ), $suite )) { 117 // add the current suite only if we're either loading them all or if 118 // the current one is the one we want to load 119 lt_include( $file ); 120 // and create an instance of it 121 $this->suite->addTestSuite( $className ); 122 } 123 } 124 125 // after adding all the tests, run the suite and return the result 126 $result = new PHPUnit_TestResult(); 127 if( $this->listener !== NULL ) { 128 $result->addListener( $this->listener ); 129 } 130 $this->suite->run( $result ); 131 132 return( $result ); 133 } 134 135 /** 136 * Returns an associative array with the names of all test suites available. The 137 * name of a test suite consists of its class name minus the "_test" suffix, although 138 * The TestRunner::run() method can take both naming schemes (with and without suffix) 139 * 140 * @return An associative array with the name of all suites 141 */ 142 function getTestSuites() 143 { 144 $suites = Array(); 145 foreach( $this->files as $file ) { 146 //$suites[] = str_replace( "_test.class.php", "", basename( $file )); 147 148 // load the class file and create a new instance 149 lt_include( $file ); 150 $className = str_replace( ".class.php", "", basename( $file )); 151 $instance = new $className(); 152 153 $suites[] = $instance; 154 } 155 156 return( $suites ); 157 } 158 159 /** 160 * @private 161 * 162 * Returns a list of all the files in the given folder that match the given pattern. In this case 163 * it is used to easily find all the test classes. Later on these classes will be loaded, instantiated 164 * and a test suite will be automatically created. 165 */ 166 function _findClasses( $folders, $pattern = "*" ) 167 { 168 $list = Array(); 169 170 // load all test cases included in core code 171 foreach( $folders as $folder ) { 172 $files = Glob::myGlob( $folder , "*" ); 173 foreach( $files as $file ) { 174 // recursive call 175 if( File::isDir( $file )) { 176 if( array_search( basename( $file ), $this->excludeFolders ) === false ) 177 { 178 $res = $this->_findClasses( Array( $file ), $pattern ); 179 foreach( $res as $f ) { 180 $list[] = $f; 181 } 182 } 183 } 184 else { 185 if ( File::isReadable( $file )) { 186 if( Glob::fnmatch( $pattern, $file )) { 187 // add the file only if it matched our pattern 188 $list[] = $file; 189 } 190 } 191 } 192 } 193 } 194 195 return( $list ); 196 } 197 } 198 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
|