More options to run-all.php

Gotta document these some day
This commit is contained in:
Marcin Kurczewski 2014-05-05 17:13:26 +02:00
parent 9ad1507b53
commit 878f09ad0d

View file

@ -1,16 +1,22 @@
<?php <?php
$configPath = __DIR__ . '/test.ini'; $configPath = __DIR__ . '/test.ini';
if (isset($_SERVER['argv'])) $options = getopt('cf:', ['clean', 'filter:']);
$args = $_SERVER['argv'];
$cleanDatabase = (isset($options['c']) or isset($options['clean']));
if (isset($options['f']))
$filter = $options['f'];
elseif (isset($options['filter']))
$filter = $options['filter'];
else else
$args = []; $filter = null;
try try
{ {
$dbPath = __DIR__ . '/db.sqlite'; $dbPath = __DIR__ . '/db.sqlite';
if (file_exists($dbPath) and in_array('-c', $args)) if (file_exists($dbPath) and $cleanDatabase)
unlink($dbPath); unlink($dbPath);
$configIni = $configIni =
@ -34,17 +40,25 @@ try
upgradeDatabase(); upgradeDatabase();
runAll(); runAll($filter);
} }
finally finally
{ {
unlink($configPath); unlink($configPath);
} }
function getTestMethods() function getTestMethods($filter)
{ {
//get all test methods
$testClasses = \Chibi\Util\Reflection::loadClasses(glob(__DIR__ . '/*Test.php')); $testClasses = \Chibi\Util\Reflection::loadClasses(glob(__DIR__ . '/*Test.php'));
if ($filter !== null)
{
$testClasses = array_filter($testClasses, function($className) use ($filter)
{
return stripos($className, $filter) !== false;
});
}
$testMethods = []; $testMethods = [];
foreach ($testClasses as $class) foreach ($testClasses as $class)
@ -62,10 +76,10 @@ function getTestMethods()
return $testMethods; return $testMethods;
} }
function runAll() function runAll($filter)
{ {
$startTime = microtime(true); $startTime = microtime(true);
$testMethods = getTestMethods(); $testMethods = getTestMethods($filter);
echo 'Starting tests' . PHP_EOL; echo 'Starting tests' . PHP_EOL;
@ -75,7 +89,7 @@ function runAll()
$labels[$key] = $method->class . '::' . $method->name; $labels[$key] = $method->class . '::' . $method->name;
//ensure every label has the same length //ensure every label has the same length
$maxLabelLength = max(array_map('strlen', $labels)); $maxLabelLength = count($testMethods) > 0 ? max(array_map('strlen', $labels)) : 0;
foreach ($labels as &$label) foreach ($labels as &$label)
$label = str_pad($label, $maxLabelLength + 1, ' '); $label = str_pad($label, $maxLabelLength + 1, ' ');