-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtest.php
137 lines (120 loc) · 3.62 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
130
131
132
133
134
135
136
137
<?php
use Jsv4\Validator;
require './vendor/autoload.php';
//require_once 'src/Jsv4/Validator.php';
//require_once 'src//Jsv4/ValidationException.php';
//require_once 'src//Jsv4/SchemaStore.php';
require_once 'test-utils.php';
$totalTestCount = 0;
$failedTests = array();
function runJsonTest($key, $test)
{
global $totalTestCount;
global $failedTests;
$totalTestCount++;
try {
if ($test->method == "validate") {
$result = Validator::validate($test->data, $test->schema);
} else if ($test->method == "isValid") {
$result = Validator::isValid($test->data, $test->schema);
} else if ($test->method == "coerce") {
$result = Validator::coerce($test->data, $test->schema);
} else {
$failedTests[$key][] = ("Unknown method: {$test->method}");
return;
}
if (is_object($test->result)) {
foreach ($test->result as $path => $expectedValue) {
$actualValue = pointerGet($result, $path, TRUE);
if (!recursiveEqual($actualValue, $expectedValue)) {
$failedTests[$key][] = "$path does not match - should be:\n " . json_encode($expectedValue) . "\nwas:\n " . json_encode($actualValue);
}
}
} else {
if (!recursiveEqual($test->result, $result)) {
$failedTests[$key][] = "$path does not match - should be:\n " . json_encode($test->result) . "\nwas:\n " . json_encode($result);
}
}
} catch (\Exception $e) {
$failedTests[$key][] = $e->getMessage();
$failedTests[$key][] .= " " . str_replace("\n", "\n ", $e->getTraceAsString());
}
}
function runPhpTest($key, $filename)
{
global $totalTestCount;
global $failedTests;
$totalTestCount++;
try {
include_once $filename;
} catch (\Exception $e) {
$failedTests[$key][] = $e->getMessage();
$failedTests[$key][] .= " " . str_replace("\n", "\n ", $e->getTraceAsString());
}
}
function runTests($directory, $indent = "")
{
global $failedTests;
if ($directory[strlen($directory) - 1] != "/") {
$directory .= "/";
}
$baseName = basename($directory);
$testCount = 0;
$testFileCount = 0;
$entries = scandir($directory);
foreach ($entries as $entry) {
$filename = $directory . $entry;
if (stripos($entry, '.php') && is_file($filename)) {
$key = substr($filename, 0, strlen($filename) - 4);
runPhpTest($key, $filename);
} else if (stripos($entry, '.json') && is_file($filename)) {
$testFileCount++;
$tests = json_decode(file_get_contents($filename));
if ($tests == NULL) {
$testCount++;
$failedTests[$filename] = "Error parsing JSON";
continue;
}
if (!is_array($tests)) {
$tests = array($tests);
}
foreach ($tests as $index => $test) {
$key = substr($filename, 0, strlen($filename) - 5);
if (isset($test->title)) {
$key .= ": {$test->title}";
} else {
$key .= ": #{$index}";
}
runJsonTest($key, $test);
$testCount++;
}
}
}
if ($testCount) {
echo "{$indent}{$baseName}/ \t({$testCount} tests in {$testFileCount} files)\n";
} else {
echo "{$indent}{$baseName}/\n";
}
foreach ($entries as $entry) {
$filename = $directory . $entry;
if (strpos($entry, '.') === FALSE && is_dir($filename)) {
runTests($filename, $indent . str_repeat(" ", strlen($baseName) + 1));
}
}
}
runTests("tests/");
echo "\n\n";
if (count($failedTests) == 0) {
echo "Passed all {$totalTestCount} tests\n";
} else {
echo "Failed " . count($failedTests) . "/{$totalTestCount} tests\n";
foreach ($failedTests as $key => $failedTest) {
if (is_array($failedTest)) {
$failedTest = implode("\n", $failedTest);
}
echo "\n";
echo "FAILED $key:\n";
echo str_repeat("-", strlen($key) + 10) . "\n";
echo " | " . str_replace("\n", "\n | ", $failedTest) . "\n";
}
}