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); }