forked from mvdriel/php-crud-api2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.php
129 lines (123 loc) · 3.92 KB
/
test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
use Tqdev\PhpCrudApi\Api;
use Tqdev\PhpCrudApi\Config;
use Tqdev\PhpCrudApi\Database\GenericDB;
use Tqdev\PhpCrudApi\Request;
spl_autoload_register(function ($class) {
include str_replace('\\', '/', "src\\$class.php");
});
function runDir(Api $api, String $dir, String $match): array
{
$success = 0;
$total = 0;
$entries = scandir($dir);
foreach ($entries as $entry) {
if ($entry === '.' || $entry === '..') {
continue;
}
$file = "$dir/$entry";
if (is_file($file)) {
if (substr($entry, -4) != '.log') {
continue;
}
if ($match != '') {
if (!preg_match('/' . preg_quote($match) . '/', $entry)) {
continue;
}
}
$success += runTest($api, $file);
$total += 1;
}
}
$failed = $total - $success;
return compact('total', 'success', 'failed');
}
function runTest(Api $api, String $file): int
{
$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));
$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;
}
$in = $parts[$i];
$exp = $parts[$i + 1];
$out = $api->handle(Request::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 ($dirty) {
file_put_contents($file, implode("===\n", $parts));
}
return $success;
}
function loadFixture(String $dir, Config $config)
{
$driver = $config->getDriver();
$filename = "$dir/fixtures/blog_$driver.sql";
$file = file_get_contents($filename);
$db = new GenericDB(
$config->getDriver(),
$config->getAddress(),
$config->getPort(),
$config->getDatabase(),
$config->getUsername(),
$config->getPassword()
);
$pdo = $db->pdo();
$file = preg_replace('/--.*$/m', '', $file);
if ($driver == 'sqlsrv') {
$statements = preg_split('/\n\s*GO\s*\n/s', $file);
} else {
$statements = preg_split('/(?<=;)\n/s', $file);
}
foreach ($statements as $i => $statement) {
$statement = trim($statement);
if ($statement) {
try {
$pdo->exec($statement);
} catch (\PDOException $e) {
$error = print_r($pdo->errorInfo(), true);
$statement = var_export($statement, true);
echo "Loading '$filename' failed on statemement #$i:\n$statement\nwith error:\n$error\n";
exit(1);
}
}
}
}
function run(array $drivers, String $dir, String $match)
{
foreach ($drivers as $driver) {
if (!extension_loaded("pdo_$driver")) {
echo sprintf("%s: skipped, driver not loaded\n", $driver);
continue;
}
$start = microtime(true);
$ini = parse_ini_file(sprintf("$dir/config/config_%s.ini", $driver));
$config = new Config($ini);
loadFixture($dir, $config);
$api = new Api($config);
$stats = runDir($api, "$dir/functional", $match);
$end = microtime(true);
$time = ($end - $start) * 1000;
$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'], __DIR__ . '/tests', isset($argv[1]) ? $argv[1] : '');