Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / cli / runtests.php
1 <?php
2
3 chdir( '..' );
4 $xdebugSupported = (function_exists('xdebug_start_code_coverage'));
5
6 if ($xdebugSupported) xdebug_start_code_coverage( XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE );
7 require 'testcontainer/rb.php';
8
9 //load core classes
10 require 'RedUNIT.php';
11 error_reporting( E_ALL );
12
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 );
18 } else {
19         die( 'Cant find configuration file.' );
20 }
21
22 echo '*** RedUNIT ***'.PHP_EOL;
23 echo 'Welcome to RedUNIT Unit testing framework for RedBeanPHP.'.PHP_EOL;
24 echo PHP_EOL;
25
26 /**
27  * Define some globals.
28  */
29 global $pdo;
30 global $currentDriver;
31
32 //Load basic functions and classes
33 require_once( 'helpers/functions.php' );
34 require_once( 'helpers/classes.php' );
35
36 //Load main classes
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' );
42
43 require_once( 'RedUNIT/Pretest.php' );
44
45 $extraTestsFromHook = array();
46 $hookPath = '';
47
48 $colorMap = array(
49                  'mysql'  => '0;31',
50                  'pgsql'  => '0;32',
51                  'sqlite' => '0;34',
52 );
53
54 @include 'cli/test_hook.php';
55
56 //Configure the databases
57 if ( isset( $ini['mysql'] ) ) {
58         $dsn = "mysql:host={$ini['mysql']['host']};dbname={$ini['mysql']['schema']}";
59
60         R::addDatabase( 'mysql', $dsn, $ini['mysql']['user'], $ini['mysql']['pass'], FALSE );
61
62         R::selectDatabase( 'mysql' );
63
64         R::exec( ' SET GLOBAL sql_mode="" ' );
65 }
66
67 //HHVM on Travis does not yet support PostgreSQL.
68 if ( defined( 'HHVM_VERSION' ) ) {
69         unset( $ini['pgsql'] );
70 } else {
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 );
74         }
75 }
76
77 if ( isset( $ini['sqlite'] ) ) {
78         R::addDatabase( 'sqlite', 'sqlite:' . $ini['sqlite']['file'], NULL, NULL, FALSE );
79 }
80
81 R::selectDatabase( 'sqlite' );
82
83 // Function to activate a driver
84 function activate_driver( $d )
85 {
86         R::selectDatabase( $d );
87 }
88
89 $arguments = $_SERVER['argc'];
90
91 $mode = 'all';
92 if ( $arguments > 1 ) {
93         $mode = $_SERVER['argv'][1];
94 }
95
96 $classSpec = null;
97 if ( $arguments > 2 ) {
98         $classSpec = $_SERVER['argv'][2];
99 }
100
101 $covFilter = null;
102 if ( $arguments > 3 ) {
103         $covFilter = $_SERVER['argv'][3];
104 }
105
106 $path = 'RedUNIT/';
107
108 // Possible Selections
109 $packList = array();
110
111 $allPacks = array(
112         'Blackhole/Version',
113         'Blackhole/Toolbox',
114         'Blackhole/Fusebox',
115         'Blackhole/Labels',
116         'Blackhole/Tainted',
117         'Blackhole/Meta',
118         'Blackhole/Import',
119         'Blackhole/Export',
120         'Blackhole/Glue',
121         'Blackhole/Plugins',
122         'Blackhole/Debug',
123         'Base/Dispense',
124         'Base/Logging',
125         'Base/Cursors',
126         'Base/Indexes',
127         'Base/Issue408',
128         'Base/Threeway',
129         'Base/Chill',
130         'Base/Arrays',
131         'Base/Boxing',
132         'Base/Typechecking',
133         'Base/Observers',
134         'Base/Database',
135         'Base/Foreignkeys',
136         'Base/Namedparams',
137         'Base/Prefixes',
138         'Base/Copy',
139         'Base/Dup',
140         'Base/Update',
141         'Base/Utf8',
142         'Base/Batch',
143         'Base/Bean',
144         'Base/Writecache',
145         'Base/Relations',
146         'Base/Association',
147         'Base/Aliasing',
148         'Base/Cross',
149         'Base/Finding',
150         'Base/Facade',
151         'Base/Frozen',
152         'Base/Fuse',
153         'Base/Tags',
154         'Base/Xnull',
155         'Base/Largenum',
156         'Base/Issue90',
157         'Base/Issue259',
158         'Base/Issue303',
159         'Base/Nuke',
160         'Base/Keywords',
161         'Base/Count',
162         'Base/With',
163         'Base/Joins',
164         'Base/Traverse',
165         'Base/Misc',
166         'Base/Via',
167         'Base/PullRequest530',
168         'Mysql/Preexist',
169         'Mysql/Double',
170         'Mysql/Writer',
171         'Mysql/Freeze',
172         'Mysql/Setget',
173         'Mysql/Foreignkeys',
174         'Mysql/Parambind',
175         'Mysql/Uuid',
176         'Mysql/Bigint',
177         'Mysql/Issue411',
178         'Postgres/Setget',
179         'Postgres/Foreignkeys',
180         'Postgres/Parambind',
181         'Postgres/Writer',
182         'Postgres/Uuid',
183         'Postgres/Bigint',
184         'Sqlite/Setget',
185         'Sqlite/Foreignkeys',
186         'Sqlite/Parambind',
187         'Sqlite/Writer',
188         'Sqlite/Rebuild',
189 );
190
191 $suffix = array(
192         'Blackhole/Misc',
193         'Base/Close'
194 );
195
196 // Default (mode == all)
197 if ( $mode == 'all' ) {
198         $packList = $allPacks;
199 } else {
200         foreach ( $allPacks as $pack ) {
201                 if ( strpos( $pack, $mode ) === 0 ) $packList[] = $pack;
202         }
203         //Add plugin pack to list.
204         if ( count($packList) === 0 && count($extraTestsFromHook) === 0 ) {
205                 $packList[] = $mode;
206         }
207 }
208
209 // Always include the last ones.
210 $packList = array_unique(array_merge( $packList, $suffix, $extraTestsFromHook ));
211 $j = 0;
212 foreach ( $packList as $testPack ) {
213         $j++;
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;
219         } else {
220                 $testClassName = str_replace( ' ', '\\', ( str_replace( '/', ' ', $testPack ) ) );
221                 $testClass     = '\\RedUNIT\\' . ucfirst( $testClassName );
222         }
223         $test          = new $testClass();
224         $drivers       = $test->getTargetDrivers();
225         maintestpack( str_replace( '_', ' ', get_class( $test ) ) );
226         $round = 0;
227         $test->setRound( $round );
228         if ( $drivers && is_array( $drivers ) ) {
229                 foreach ( $drivers as $driver ) {
230                         if ( !isset( $ini[$driver] ) ) continue;
231                         echo PHP_EOL;
232                         echo '===== DRIVER : (' . $driver . ') =====';
233                         echo PHP_EOL;
234                         echo PHP_EOL;
235                         if ( isset( $colorMap[$driver] ) ) {
236                                 echo "\033[{$colorMap[$driver]}m";
237                         }
238                         activate_driver( $driver );
239                         $currentDriver = $driver;
240                         $test->setCurrentDriver( $driver );
241                         $test->prepare();
242                         $test->run();
243                         $test->cleanUp();
244                         if ( isset ( $colorMap[$driver] ) ) {
245                                 echo "\033[0m";
246                         }
247                         echo PHP_EOL;
248                         $test->setRound( ++$round );
249                 }
250         } else {
251                 $test->prepare();
252                 $test->run();
253                 $test->cleanUp();
254         }
255 }
256
257 if (!$xdebugSupported) {
258         echo 'Done. No report - XDEBUG not installed.';
259         exit(0);
260 }
261
262 $report = xdebug_get_code_coverage();
263 $misses = 0;
264 $hits = 0;
265
266 $covLines = array();
267 foreach( $report as $file => $lines ) {
268
269         $pi = pathinfo( $file );
270
271         if ( $covFilter !== null ) {
272                 if ( strpos( $file, $covFilter ) === false ) continue;
273         } else {
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;
278                 }
279         }
280         $covLines[] = '***** File:'.$file.' ******';
281         $fileData = file_get_contents( $file );
282         $fileLines = explode( "\n", $fileData );
283         $i = 1;
284         foreach( $fileLines as $covLine ) {
285                 if ( isset( $lines [$i] ) ) {
286                         if ( $lines[$i] === 1 ) {
287                                 $covLines[] = '[ OK      ] '.$covLine;
288                                 $hits ++;
289                         } else if ( $lines[$i] === -1 ){
290                                 $covLines[] = '[ MISSED! ] '.$covLine;
291                                 $misses ++;
292                         } else {
293                                 $covLines[] = '[ -       ] '.$covLine;
294                         }
295                 } else {
296                         $covLines[] = '[ -       ] '.$covLine;
297                 }
298                 $i ++;
299         }
300 }
301 $covFile = implode( "\n", $covLines );
302 @file_put_contents( 'cli/coverage_log.txt', $covFile );
303 if ( $hits > 0 ) {
304         $perc = ( $hits / ( $hits + $misses ) ) * 100;
305 } else {
306         $perc = 0;
307 }
308 echo 'Code Coverage: '.PHP_EOL;
309 echo 'Hits: '.$hits.PHP_EOL;
310 echo 'Misses: '.$misses.PHP_EOL;
311 echo 'Percentage: '.$perc.' %'.PHP_EOL;
312 exit( 0 );