-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathrun_tests.php
217 lines (195 loc) · 4.67 KB
/
run_tests.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
// This is a simple jig for testing JsonPatch.inc against json-encoded
// test files.
require 'vendor/autoload.php';
use mikemccabe\JsonPatch\JsonPatch;
$verbose = false;
function print_test($test)
{
print "{ ";
$first = true;
foreach(array('comment', 'doc', 'patch', 'expected', 'error') as $key)
{
if (array_key_exists($key, $test))
{
if (!$first)
{
print ",\n ";
}
$first = false;
print("\"$key\": " . json_encode($test[$key]));
}
}
print " }\n";
}
function do_test($test, $simplexml_mode=false)
{
global $verbose;
// Allow 'comment-only' test records
if (!(isset($test['doc']) && isset($test['patch'])))
return true;
try
{
$patched = JsonPatch::patch($test['doc'], $test['patch'], $simplexml_mode);
if (isset($test['error']))
{
print("test failed: expected error didn't occur\n");
print_test($test);
print("found: ");
print json_encode($patched);
print("\n\n");
}
if (!isset($test['expected']))
{
return true;
}
if (!JsonPatch::considered_equal($patched, $test['expected']))
{
print("test failed:\n");
print_test($test);
print("found: " . json_encode($patched) . "\n\n");
return false;
}
else
{
if ($verbose && array_key_exists('comment', $test))
{
print "OK: " . $test['comment'] . "\n\n";
}
return true;
}
}
catch (Exception $ex)
{
if (!isset($test['error']))
{
print("test failed with exception: " . $ex->getMessage() . "\n");
print_test($test);
print("\n");
return false;
}
else
{
if ($verbose)
{
if (array_key_exists('comment', $test))
{
print "OK: " . $test['comment'] . "\n";
}
print("caught: " . $ex->getMessage() . "\n");
print("expected: " . $test['error'] . "\n\n");
}
return true;
}
}
}
// Piggyback on patch tests to test diff as well - use 'doc' and
// 'expected' from testcases. Generate a diff, apply it, and check
// that it matches the target - in both directions.
function diff_test($test)
{
// Skip comment-only or test op tests
if (!(isset($test['doc']) && isset($test['expected'])))
{
return true;
}
$result = true;
try
{
$doc1 = $test['doc']; // copy, in case sort/patch alters
$doc2 = $test['expected'];
$patch = JsonPatch::diff($doc1, $doc2);
$patched = JsonPatch::patch($doc1, $patch);
if (!JsonPatch::considered_equal($patched, $doc2))
{
print("diff test failed:\n");
print_test($test);
print("from: " . json_encode($doc1) . "\n");
print("diff: " . json_encode($patch) . "\n");
print("found: " . json_encode($patched) . "\n");
print("expected: " . json_encode($doc2) . "\n\n");
$result = false;
}
// reverse order
$doc1 = $test['expected']; // copy, in case sort/patch alters
$doc2 = $test['doc'];
$patch = JsonPatch::diff($doc1, $doc2);
$patched = JsonPatch::patch($doc1, $patch);
if (!JsonPatch::considered_equal($patched, $doc2))
{
print("reverse diff test failed:\n");
print_test($test);
print("from: " . json_encode($doc1) . "\n");
print("diff: " . json_encode($patch) . "\n");
print("found: " . json_encode($patched) . "\n");
print("expected: " . json_encode($doc2) . "\n\n");
$result = false;
}
}
catch (Exception $ex)
{
print("caught exception ".$ex->getMessage()."\n");
return false;
}
return $result;
}
function test_file($filename, $simplexml_mode=false)
{
$testfile = file_get_contents($filename);
if (!$testfile)
{
throw new Exception("Couldn't find test file $filename");
return false;
}
$tests = json_decode($testfile, 1);
if (is_null($tests))
{
throw new Exception("Error json-decoding test file $filename");
}
$success = true;
foreach ($tests as $test)
{
if (isset($test['disabled']) && $test['disabled'])
{
continue;
}
if (!do_test($test, $simplexml_mode))
{
$success = false;
}
if (!$simplexml_mode && !diff_test($test))
{
$success = false;
}
}
return $success;
}
function main()
{
$result = true;
$testfiles = array(
'local_tests.json',
'json-patch-tests/tests.json',
'json-patch-tests/spec_tests.json'
);
foreach ($testfiles as $testfile)
{
if (!test_file($testfile))
{
$result = false;
}
}
if (!test_file('simplexml_tests.json', true))
{
$result = false;
}
return $result;
}
if (!main())
{
exit(1);
}
else
{
exit(0);
}