function recursiveEqual($a, $b) { if (is_object($a)) { if (!is_object($b)) { return FALSE; } foreach ($a as $key => $value) { if (!isset($b->{$key})) { return FALSE; } if (!recursiveEqual($value, $b->{$key})) { return FALSE; } } foreach ($b as $key => $value) { if (!isset($a->{$key})) { return FALSE; } } return TRUE; } if (is_array($a)) { if (!is_array($b)) { return FALSE; } foreach ($a as $key => $value) { if (!isset($b[$key])) { return FALSE; } if (!recursiveEqual($value, $b[$key])) { return FALSE; } } foreach ($b as $key => $value) { if (!isset($a[$key])) { return FALSE; } } return TRUE; } return $a === $b; }
function runJsonTest($key, $test) { global $totalTestCount; global $failedTests; $totalTestCount++; try { if ($test->method == "validate") { $result = Jsv4::validate($test->data, $test->schema); } else { if ($test->method == "isValid") { $result = Jsv4::isValid($test->data, $test->schema); } else { if ($test->method == "coerce") { $result = Jsv4::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()); } }
"somewhereElse": { "id": "http://somewhere-else.com/test-schema" } } }'); $store->add($url, $schema); if (!recursiveEqual($store->get($url . "#foo"), $schema->properties->foo)) { throw new Exception("#foo not found"); } if (!recursiveEqual($store->get($url . "?baz=1"), $schema->properties->baz)) { throw new Exception("?baz=1 not found"); } if (!recursiveEqual($store->get($url . "/foobar"), $schema->properties->foobar)) { throw new Exception("/foobar not found"); } if (!recursiveEqual($store->get($url . "/foo#bar"), $schema->properties->nestedSchema->nested)) { throw new Exception("/foo#bar not found"); } if ($store->get($urlBase . "bar")) { throw new Exception("/bar should not be indexed, as it should not be trusted"); } if ($store->get($url . "-foo")) { throw new Exception("/test-schema-foo should not be indexed, as it should not be trusted"); } if ($store->get("http://somewhere-else.com/test-schema")) { throw new Exception("http://somewhere-else.com/test-schema should not be indexed, as it should not be trusted"); } $store->add($url, $schema, TRUE); if (!recursiveEqual($store->get($urlBase . "bar"), $schema->properties->bar)) { throw new Exception("/bar not found"); }
} }'); $store->add($url, $schema); $schema = $store->get($url); if ($schema->properties->foo != $schema->definitions->foo) { throw new Exception('$ref was not resolved'); } // Add external $ref, and don't resolve it // While we're at it, use an array, not an object $schema = array("title" => "Test schema 2", "properties" => array("foo" => array('$ref' => "somewhere-else"))); $store->add($urlBase . "test-schema-2", $schema); $schema = $store->get($urlBase . "test-schema-2"); if (!$schema->properties->foo->{'$ref'}) { throw new Exception('$ref should still exist'); } if (!recursiveEqual($store->missing(), array($urlBase . "somewhere-else"))) { throw new Exception('$store->missing() is not correct: ' . json_encode($store->missing()) . ' is not ' . json_encode(array($urlBase . "somewhere-else"))); } $otherSchema = json_decode('{ "title": "Somewhere else", "items": [ {"$ref": "' . $urlBase . "test-schema-2" . '"} ] }'); $store->add($urlBase . "somewhere-else", $otherSchema); $fooSchema = $schema->properties->foo; if (property_exists($fooSchema, '$ref')) { throw new Exception('$ref should have been resolved'); } if ($fooSchema->title != "Somewhere else") { throw new Exception('$ref does not point to correct place');
<?php $store = new SchemaStore(); $url = "http://example.com/test-schema"; $schema = json_decode('{ "title": "Test schema" }'); $store->add($url, $schema); if (!recursiveEqual($store->get($url), $schema)) { throw new Exception("Not equal"); } if (!recursiveEqual($store->get($url . "#/title"), $schema->title)) { throw new Exception("Not equal"); }