added static php-crud-api

This commit is contained in:
zino
2021-01-20 13:13:22 +01:00
parent ea9085b4a4
commit fda9de0fa5
175 changed files with 770 additions and 15545 deletions

View File

@@ -11,8 +11,7 @@ require 'vendor/autoload.php';
function runDir(Config $config, string $dir, array $matches, string $category): array
{
$success = 0;
$skipped = 0;
$failed = 0;
$total = 0;
$entries = scandir($dir);
foreach ($entries as $entry) {
if ($entry === '.' || $entry === '..') {
@@ -28,77 +27,54 @@ function runDir(Config $config, string $dir, array $matches, string $category):
if (substr($entry, -4) != '.log') {
continue;
}
$statistics = runTest($config, $file, $category);
$success += $statistics['success'];
$skipped += $statistics['skipped'];
$failed += $statistics['failed'];
$success += runTest($config, $file, $category);
$total += 1;
} elseif (is_dir($file)) {
$statistics = runDir($config, $file, array_slice($matches, 1), "$category/$entry");
$total += $statistics['total'];
$success += $statistics['success'];
$skipped += $statistics['skipped'];
$failed += $statistics['failed'];
}
}
return compact('success', 'skipped', 'failed');
$failed = $total - $success;
return compact('total', 'success', 'failed');
}
function runTest(Config $config, string $file, string $category): array
function runTest(Config $config, string $file, string $category): int
{
$success = 1;
$skipped = 0;
$failed = 0;
$title = ucwords(str_replace('_', ' ', $category)) . '/';
$title .= ucwords(str_replace('_', ' ', substr(basename($file), 0, -4)));
$line1 = "=====[$title]=====";
$len = strlen($line1);
$line2 = str_repeat("=", $len);
$parts = preg_split('/^[=]+([\r\n]+|$)/m', file_get_contents($file));
$headers = explode("\n", $parts[0]);
$driver = $config->getDriver();
foreach ($headers as $header) {
if (!strpos($header, ':')) {
continue;
$dirty = false;
$success = 1;
for ($i = 0; $i < count($parts); $i += 2) {
$recording = false;
if (empty($parts[$i + 1])) {
if (substr($parts[$i], -1) != "\n") {
$parts[$i] .= "\n";
}
$parts[$i + 1] = '';
$recording = true;
$dirty = true;
}
list($key, $value) = explode(':', strtolower($header));
if ($key == "skip-for-$driver") {
$skipped = 1;
$success = 0;
}
if ($key == "skip-always") {
$skipped = 1;
$in = $parts[$i];
$exp = $parts[$i + 1];
$api = new Api($config);
$_SERVER['REMOTE_ADDR'] = 'TEST_IP';
$out = ResponseUtils::toString($api->handle(RequestFactory::fromString($in)));
if ($recording) {
$parts[$i + 1] = $out;
} else if ($out != $exp) {
echo "$line1\n$exp\n$line2\n$out\n$line2\n";
$success = 0;
}
}
if (!$skipped) {
$dirty = false;
for ($i = 1; $i < count($parts); $i += 2) {
$recording = false;
if (empty($parts[$i + 1])) {
if (substr($parts[$i], -1) != "\n") {
$parts[$i] .= "\n";
}
$parts[$i + 1] = '';
$recording = true;
$dirty = true;
}
$in = $parts[$i];
$exp = $parts[$i + 1];
$api = new Api($config);
$_SERVER['REMOTE_ADDR'] = 'TEST_IP';
$out = ResponseUtils::toString($api->handle(RequestFactory::fromString($in)));
if ($recording) {
$parts[$i + 1] = $out;
} else if ($out != $exp) {
echo "$line1\n$exp\n$line2\n$out\n$line2\n";
$failed = 1;
$success = 0;
}
}
if ($dirty) {
file_put_contents($file, implode("===\n", $parts));
}
if ($dirty) {
file_put_contents($file, implode("===\n", $parts));
}
return compact('success', 'skipped', 'failed');
return $success;
}
function getDatabase(Config $config)
@@ -188,15 +164,13 @@ function run(array $drivers, string $dir, array $matches)
$config = new Config($settings);
loadFixture($dir, $config);
$start = microtime(true);
$statistics = runDir($config, "$dir/functional", array_slice($matches, 1), '');
$stats = runDir($config, "$dir/functional", array_slice($matches, 1), '');
$end = microtime(true);
$time = ($end - $start) * 1000;
$success = $statistics['success'];
$skipped = $statistics['skipped'];
$failed = $statistics['failed'];
$total = $success + $skipped + $failed;
echo sprintf("%s: %d tests ran in %d ms, %d skipped, %d failed\n", $driver, $total, $time, $skipped, $failed);
$total = $stats['total'];
$failed = $stats['failed'];
echo sprintf("%s: %d tests ran in %d ms, %d failed\n", $driver, $total, $time, $failed);
}
}
run(['mysql', 'pgsql', 'sqlsrv', 'sqlite'], __DIR__ . '/tests', array_slice($argv, 1));
run(['mysql', 'pgsql', 'sqlsrv'], __DIR__ . '/tests', array_slice($argv, 1));