szurubooru/tests/run-all.php

151 lines
3.1 KiB
PHP
Raw Normal View History

2014-05-04 21:23:12 +02:00
<?php
2014-05-06 11:18:04 +02:00
require_once __DIR__ . '/../src/core.php';
require_once __DIR__ . '/../src/upgrade.php';
\Chibi\Autoloader::registerFileSystem(__DIR__);
$options = getopt('cf:', ['clean', 'filter:']);
$cleanDatabase = (isset($options['c']) or isset($options['clean']));
if (isset($options['f']))
$filter = $options['f'];
elseif (isset($options['filter']))
$filter = $options['filter'];
2014-05-04 21:23:12 +02:00
else
$filter = null;
2014-05-04 21:23:12 +02:00
2014-05-06 12:01:29 +02:00
$dbPath = __DIR__ . '/db.sqlite';
if (file_exists($dbPath) and $cleanDatabase)
unlink($dbPath);
2014-05-04 21:23:12 +02:00
try
{
2014-05-06 12:01:29 +02:00
resetEnvironment();
2014-05-04 21:23:12 +02:00
upgradeDatabase();
runAll($filter);
2014-05-04 21:23:12 +02:00
}
finally
{
2014-05-06 12:01:29 +02:00
}
function resetEnvironment()
{
global $dbPath;
prepareConfig(true);
getConfig()->main->dbDriver = 'sqlite';
getConfig()->main->dbLocation = $dbPath;
prepareEnvironment(true);
2014-05-04 21:23:12 +02:00
}
function getTestMethods($filter)
2014-05-04 21:23:12 +02:00
{
2014-05-06 11:18:04 +02:00
$testFiles = [];
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__)) as $fileName)
{
$path = $fileName->getPathname();
if (preg_match('/.*Test.php$/', $path))
$testFiles []= $path;
}
$testClasses = \Chibi\Util\Reflection::loadClasses($testFiles);
if ($filter !== null)
{
$testClasses = array_filter($testClasses, function($className) use ($filter)
{
return stripos($className, $filter) !== false;
});
}
2014-05-04 21:23:12 +02:00
$testMethods = [];
foreach ($testClasses as $class)
{
$reflectionClass = new ReflectionClass($class);
foreach ($reflectionClass->getMethods() as $method)
{
2014-05-06 13:07:24 +02:00
if (preg_match('/test/i', $method->name) and $method->isPublic())
2014-05-04 21:23:12 +02:00
{
$testMethods []= $method;
}
}
}
return $testMethods;
}
function runAll($filter)
2014-05-04 21:23:12 +02:00
{
$startTime = microtime(true);
$testMethods = getTestMethods($filter);
2014-05-04 21:23:12 +02:00
echo 'Starting tests' . PHP_EOL;
//get display names of the methods
$labels = [];
foreach ($testMethods as $key => $method)
$labels[$key] = $method->class . '::' . $method->name;
//ensure every label has the same length
$maxLabelLength = count($testMethods) > 0 ? max(array_map('strlen', $labels)) : 0;
2014-05-04 21:23:12 +02:00
foreach ($labels as &$label)
$label = str_pad($label, $maxLabelLength + 1, ' ');
2014-05-06 13:07:11 +02:00
$pad = count($testMethods) ? ceil(log10(1 + count($testMethods))) : 0;
2014-05-04 21:23:12 +02:00
//run all the methods
$success = true;
foreach ($testMethods as $key => $method)
{
$instance = new $method->class();
$testStartTime = microtime(true);
echo str_pad($key + 1, $pad, ' ', STR_PAD_LEFT) . ' ';
echo $labels[$key] . '... ';
unset($e);
try
{
runSingle(function() use ($method, $instance)
{
$method->invoke($instance);
});
echo 'OK';
}
catch (Exception $e)
{
$success = false;
echo 'FAIL';
}
printf(' [%.03fs]' . PHP_EOL, microtime(true) - $testStartTime);
if (isset($e))
{
echo '---' . PHP_EOL;
echo $e->getMessage() . PHP_EOL;
echo $e->getTraceAsString() . PHP_EOL;
echo '---' . PHP_EOL . PHP_EOL;
}
}
printf('%s %s... %s [%.03fs]' . PHP_EOL,
str_pad('', $pad, ' '),
str_pad('All tests', $maxLabelLength + 1, ' '),
$success ? 'OK' : 'FAIL',
microtime(true) - $startTime);
return $success;
}
function runSingle($callback)
{
resetEnvironment();
2014-05-06 12:01:29 +02:00
resetEnvironment(true);
2014-05-04 21:23:12 +02:00
\Chibi\Database::rollback(function() use ($callback)
{
$callback();
});
}