Exemple #1
0
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());
    }
}