4 $xdebugSupported = (function_exists('xdebug_start_code_coverage'));
6 if ($xdebugSupported) xdebug_start_code_coverage( XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE );
7 require 'testcontainer/rb.php';
10 require 'RedUNIT.php';
11 error_reporting( E_ALL );
13 //Load configuration file
14 if ( file_exists( 'config/test.ini' ) ) {
15 $ini = parse_ini_file( "config/test.ini", TRUE );
16 } elseif ( file_exists( 'config/test-travis.ini' ) ) {
17 $ini = parse_ini_file( "config/test-travis.ini", TRUE );
19 die( 'Cant find configuration file.' );
22 echo '*** RedUNIT ***'.PHP_EOL;
23 echo 'Welcome to RedUNIT Unit testing framework for RedBeanPHP.'.PHP_EOL;
27 * Define some globals.
30 global $currentDriver;
32 //Load basic functions and classes
33 require_once( 'helpers/functions.php' );
34 require_once( 'helpers/classes.php' );
37 require_once( 'RedUNIT/Base.php' );
38 require_once( 'RedUNIT/Blackhole.php' );
39 require_once( 'RedUNIT/Mysql.php' );
40 require_once( 'RedUNIT/Postgres.php' );
41 require_once( 'RedUNIT/Sqlite.php' );
43 require_once( 'RedUNIT/Pretest.php' );
45 $extraTestsFromHook = array();
54 @include 'cli/test_hook.php';
56 //Configure the databases
57 if ( isset( $ini['mysql'] ) ) {
58 $dsn = "mysql:host={$ini['mysql']['host']};dbname={$ini['mysql']['schema']}";
60 R::addDatabase( 'mysql', $dsn, $ini['mysql']['user'], $ini['mysql']['pass'], FALSE );
62 R::selectDatabase( 'mysql' );
64 R::exec( ' SET GLOBAL sql_mode="" ' );
67 //HHVM on Travis does not yet support PostgreSQL.
68 if ( defined( 'HHVM_VERSION' ) ) {
69 unset( $ini['pgsql'] );
71 if ( isset( $ini['pgsql'] ) ) {
72 $dsn = "pgsql:host={$ini['pgsql']['host']};dbname={$ini['pgsql']['schema']}";
73 R::addDatabase( 'pgsql', $dsn, $ini['pgsql']['user'], $ini['pgsql']['pass'], FALSE );
77 if ( isset( $ini['sqlite'] ) ) {
78 R::addDatabase( 'sqlite', 'sqlite:' . $ini['sqlite']['file'], NULL, NULL, FALSE );
81 R::selectDatabase( 'sqlite' );
83 // Function to activate a driver
84 function activate_driver( $d )
86 R::selectDatabase( $d );
89 $arguments = $_SERVER['argc'];
92 if ( $arguments > 1 ) {
93 $mode = $_SERVER['argv'][1];
97 if ( $arguments > 2 ) {
98 $classSpec = $_SERVER['argv'][2];
102 if ( $arguments > 3 ) {
103 $covFilter = $_SERVER['argv'][3];
108 // Possible Selections
167 'Base/PullRequest530',
179 'Postgres/Foreignkeys',
180 'Postgres/Parambind',
185 'Sqlite/Foreignkeys',
196 // Default (mode == all)
197 if ( $mode == 'all' ) {
198 $packList = $allPacks;
200 foreach ( $allPacks as $pack ) {
201 if ( strpos( $pack, $mode ) === 0 ) $packList[] = $pack;
203 //Add plugin pack to list.
204 if ( count($packList) === 0 && count($extraTestsFromHook) === 0 ) {
209 // Always include the last ones.
210 $packList = array_unique(array_merge( $packList, $suffix, $extraTestsFromHook ));
212 foreach ( $packList as $testPack ) {
214 if ( file_exists( $path . $testPack . '.php' ) ) require( $path . $testPack . '.php' );
215 elseif ( file_exists( $hookPath . $testPack . '.php') ) require( $hookPath . $testPack . '.php' );
216 $testPack = str_replace( '../', '', $testPack );
217 if ($j === 1 && $classSpec) {
218 $testClass = $classSpec;
220 $testClassName = str_replace( ' ', '\\', ( str_replace( '/', ' ', $testPack ) ) );
221 $testClass = '\\RedUNIT\\' . ucfirst( $testClassName );
223 $test = new $testClass();
224 $drivers = $test->getTargetDrivers();
225 maintestpack( str_replace( '_', ' ', get_class( $test ) ) );
227 $test->setRound( $round );
228 if ( $drivers && is_array( $drivers ) ) {
229 foreach ( $drivers as $driver ) {
230 if ( !isset( $ini[$driver] ) ) continue;
232 echo '===== DRIVER : (' . $driver . ') =====';
235 if ( isset( $colorMap[$driver] ) ) {
236 echo "\033[{$colorMap[$driver]}m";
238 activate_driver( $driver );
239 $currentDriver = $driver;
240 $test->setCurrentDriver( $driver );
244 if ( isset ( $colorMap[$driver] ) ) {
248 $test->setRound( ++$round );
257 if (!$xdebugSupported) {
258 echo 'Done. No report - XDEBUG not installed.';
262 $report = xdebug_get_code_coverage();
267 foreach( $report as $file => $lines ) {
269 $pi = pathinfo( $file );
271 if ( $covFilter !== null ) {
272 if ( strpos( $file, $covFilter ) === false ) continue;
274 if ( strpos( $file, '/rb.php' ) === false) {
275 if ( strpos( $file, 'phar/' ) === false ) continue;
276 if ( strpos( $file, '.php' ) === false ) continue;
277 if ( strpos( $file, 'RedBeanPHP' ) === false ) continue;
280 $covLines[] = '***** File:'.$file.' ******';
281 $fileData = file_get_contents( $file );
282 $fileLines = explode( "\n", $fileData );
284 foreach( $fileLines as $covLine ) {
285 if ( isset( $lines [$i] ) ) {
286 if ( $lines[$i] === 1 ) {
287 $covLines[] = '[ OK ] '.$covLine;
289 } else if ( $lines[$i] === -1 ){
290 $covLines[] = '[ MISSED! ] '.$covLine;
293 $covLines[] = '[ - ] '.$covLine;
296 $covLines[] = '[ - ] '.$covLine;
301 $covFile = implode( "\n", $covLines );
302 @file_put_contents( 'cli/coverage_log.txt', $covFile );
304 $perc = ( $hits / ( $hits + $misses ) ) * 100;
308 echo 'Code Coverage: '.PHP_EOL;
309 echo 'Hits: '.$hits.PHP_EOL;
310 echo 'Misses: '.$misses.PHP_EOL;
311 echo 'Percentage: '.$perc.' %'.PHP_EOL;