2011-07-14 06:47:40 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PHP unit tests for JSON-LD.
|
|
|
|
*
|
|
|
|
* @author Dave Longley
|
|
|
|
*
|
2013-02-07 23:19:19 +01:00
|
|
|
* Copyright (c) 2011-2013 Digital Bazaar, Inc. All rights reserved.
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
|
|
|
require_once('jsonld.php');
|
|
|
|
|
2013-02-20 22:55:36 +01:00
|
|
|
// send errors to stdout
|
|
|
|
ini_set('display_errors', 'stdout');
|
|
|
|
|
2011-07-25 23:40:17 +02:00
|
|
|
// determine EOL for output based on command line php or webpage php
|
|
|
|
$isCli = defined('STDIN');
|
2011-09-20 20:28:51 +02:00
|
|
|
$eol = $isCli ? "\n" : '<br/>';
|
2011-07-25 23:40:17 +02:00
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
function error_handler($errno, $errstr, $errfile, $errline) {
|
|
|
|
global $eol;
|
|
|
|
echo "$eol$errstr$eol";
|
|
|
|
array_walk(
|
|
|
|
debug_backtrace(),
|
|
|
|
create_function(
|
|
|
|
'$a,$b',
|
|
|
|
'echo "{$a[\'function\']}()' .
|
|
|
|
'(".basename($a[\'file\']).":{$a[\'line\']}); ' . $eol . '";'));
|
|
|
|
throw new Exception();
|
|
|
|
return false;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
if(!$isCli) {
|
|
|
|
set_error_handler('error_handler');
|
2011-07-25 23:40:17 +02:00
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2013-03-01 21:07:40 +01:00
|
|
|
function deep_compare($expect, $result) {
|
|
|
|
if(is_array($expect)) {
|
|
|
|
if(!is_array($result)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(count($expect) !== count($result)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
foreach($expect as $i => $v) {
|
|
|
|
if(!deep_compare($v, $result[$i])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_object($expect)) {
|
|
|
|
if(!is_object($result)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(count(get_object_vars($expect)) !== count(get_object_vars($result))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
foreach($expect as $k => $v) {
|
|
|
|
if(!property_exists($result, $k) || !deep_compare($v, $result->{$k})) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $expect === $result;
|
|
|
|
}
|
2013-02-15 18:16:26 +01:00
|
|
|
|
|
|
|
function expanded_compare($x, $y, $is_list=false) {
|
|
|
|
if($x === $y) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(gettype($x) !== gettype($y)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(is_array($x)) {
|
|
|
|
if(!is_array($y) || count($x) !== count($y)) {
|
2012-04-23 18:57:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-02-15 18:16:26 +01:00
|
|
|
$rval = true;
|
|
|
|
if($is_list) {
|
|
|
|
// compare in order
|
|
|
|
for($i = 0; $rval && $i < count($x); ++$i) {
|
|
|
|
$rval = expanded_compare($x[$i], $y[$i], false);
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2013-02-15 18:16:26 +01:00
|
|
|
else {
|
|
|
|
// compare in any order
|
|
|
|
$iso = array();
|
|
|
|
for($i = 0; $rval && $i < count($x); ++$i) {
|
|
|
|
$rval = false;
|
|
|
|
for($j = 0; !$rval && $j < count($y); ++$j) {
|
|
|
|
if(!isset($iso[$j])) {
|
|
|
|
if(expanded_compare($x[$i], $y[$j], false)) {
|
|
|
|
$iso[$j] = $i;
|
|
|
|
$rval = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
2013-02-15 18:16:26 +01:00
|
|
|
$rval = $rval && (count($iso) === count($x));
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2013-02-15 18:16:26 +01:00
|
|
|
return $rval;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2013-02-15 18:16:26 +01:00
|
|
|
if(is_object($x)) {
|
|
|
|
$x_keys = array_keys((array)$x);
|
|
|
|
$y_keys = array_keys((array)$y);
|
|
|
|
if(count($x_keys) !== count($y_keys)) {
|
2012-04-23 18:57:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-02-15 18:16:26 +01:00
|
|
|
foreach($x_keys as $key) {
|
|
|
|
if(!property_exists($y, $key)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!expanded_compare($x->{$key}, $y->{$key}, $key === '@list')) {
|
2012-04-23 18:57:06 +02:00
|
|
|
return false;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2012-04-23 20:01:56 +02:00
|
|
|
return true;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2013-02-15 18:16:26 +01:00
|
|
|
return false;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads test JSON files.
|
|
|
|
*
|
2012-04-23 18:57:06 +02:00
|
|
|
* @param string $file the file to read.
|
|
|
|
* @param string $filepath the test filepath.
|
2011-07-14 06:47:40 +02:00
|
|
|
*
|
2012-04-23 18:57:06 +02:00
|
|
|
* @return string the read JSON.
|
2011-07-14 06:47:40 +02:00
|
|
|
*/
|
2012-04-23 18:57:06 +02:00
|
|
|
function read_test_json($file, $filepath) {
|
|
|
|
global $eol;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$file = $filepath . '/' . $file;
|
|
|
|
return json_decode(file_get_contents($file));
|
|
|
|
}
|
|
|
|
catch(Exception $e) {
|
|
|
|
echo "Exception while parsing file: '$file'$eol";
|
|
|
|
throw $e;
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
2012-04-30 23:10:27 +02:00
|
|
|
/**
|
|
|
|
* Reads test N-Quads files.
|
|
|
|
*
|
|
|
|
* @param string $file the file to read.
|
|
|
|
* @param string $filepath the test filepath.
|
|
|
|
*
|
|
|
|
* @return string the read N-Quads.
|
|
|
|
*/
|
|
|
|
function read_test_nquads($file, $filepath) {
|
|
|
|
global $eol;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$file = $filepath . '/' . $file;
|
|
|
|
return file_get_contents($file);
|
|
|
|
}
|
|
|
|
catch(Exception $e) {
|
|
|
|
echo "Exception while parsing file: '$file'$eol";
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-26 22:36:51 +02:00
|
|
|
/**
|
|
|
|
* JSON-encodes the given input (does not escape slashes).
|
|
|
|
*
|
|
|
|
* @param mixed $input the input to encode.
|
|
|
|
*
|
|
|
|
* @return the encoded input.
|
|
|
|
*/
|
2013-03-01 21:07:40 +01:00
|
|
|
function jsonld_encode($input) {
|
|
|
|
// newer PHP has a flag to avoid escaped '/'
|
2012-09-26 22:36:51 +02:00
|
|
|
if(defined('JSON_UNESCAPED_SLASHES')) {
|
|
|
|
$options = JSON_UNESCAPED_SLASHES;
|
|
|
|
if(defined('JSON_PRETTY_PRINT')) {
|
|
|
|
$options |= JSON_PRETTY_PRINT;
|
2013-03-01 21:07:40 +01:00
|
|
|
}
|
|
|
|
$json = json_encode($input, $options);
|
2012-09-26 22:36:51 +02:00
|
|
|
}
|
2013-03-01 21:07:40 +01:00
|
|
|
else {
|
|
|
|
// use a simple string replacement of '\/' to '/'.
|
|
|
|
$json = str_replace('\\/', '/', json_encode($input));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $json;
|
2012-09-26 22:36:51 +02:00
|
|
|
}
|
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
class TestRunner {
|
|
|
|
public function __construct() {
|
|
|
|
// set up groups, add root group
|
|
|
|
$this->groups = array();
|
|
|
|
$this->group('');
|
|
|
|
|
|
|
|
$this->passed = 0;
|
|
|
|
$this->failed = 0;
|
|
|
|
$this->total = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function group($name) {
|
|
|
|
$this->groups[] = (object)array(
|
|
|
|
'name' => $name,
|
|
|
|
'tests' => array(),
|
|
|
|
'count' => 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function ungroup() {
|
|
|
|
array_pop($this->groups);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test($name) {
|
|
|
|
$this->groups[count($this->groups) - 1]->tests[] = $name;
|
|
|
|
$this->total += 1;
|
|
|
|
|
|
|
|
$line = '';
|
|
|
|
foreach($this->groups as $g) {
|
|
|
|
$line .= ($line === '') ? $g->name : ('/' . $g->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
$g = $this->groups[count($this->groups) - 1];
|
|
|
|
if($g->name !== '') {
|
|
|
|
$count = '' . $g->count;
|
|
|
|
$end = 4 - strlen($count);
|
|
|
|
for($i = 0; $i < $end; ++$i) {
|
|
|
|
$count = '0' . $count;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
$line .= ' ' . $count;
|
|
|
|
$g->count += 1;
|
|
|
|
}
|
|
|
|
$line .= '/' . array_pop($g->tests) . '... ';
|
|
|
|
echo $line;
|
|
|
|
}
|
|
|
|
|
2013-02-15 18:16:26 +01:00
|
|
|
public function check($test, $expect, $result, $type) {
|
2012-04-23 18:57:06 +02:00
|
|
|
global $eol;
|
|
|
|
|
2013-02-15 18:16:26 +01:00
|
|
|
$compare_json = !in_array('jld:ToRDFTest', $type);
|
|
|
|
$relabel = in_array('jld:FlattenTest', $type);
|
|
|
|
$expanded = $relabel || in_array('jld:ExpandTest', $type);
|
|
|
|
if($relabel) {
|
|
|
|
$expect = jsonld_relabel_blank_nodes($expect);
|
|
|
|
$result = jsonld_relabel_blank_nodes($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
$pass = false;
|
2013-02-08 06:15:45 +01:00
|
|
|
if($compare_json) {
|
2013-02-15 18:16:26 +01:00
|
|
|
$pass = (json_encode($expect) === json_encode($result));
|
|
|
|
}
|
|
|
|
if(!$pass) {
|
|
|
|
$pass = deep_compare($expect, $result) ||
|
|
|
|
($expanded && expanded_compare($expect, $result, $relabel));
|
2013-02-08 06:15:45 +01:00
|
|
|
}
|
2013-02-15 18:16:26 +01:00
|
|
|
if(!$pass && $relabel) {
|
|
|
|
echo "WARN tried normalization...";
|
|
|
|
$expect_normalized = jsonld_normalize(
|
2013-03-01 21:07:40 +01:00
|
|
|
$expect, array('format' => 'application/nquads'));
|
|
|
|
$result_normalized = jsonld_normalize(
|
2013-02-15 18:16:26 +01:00
|
|
|
$result, array('format' => 'application/nquads'));
|
2013-03-01 21:07:40 +01:00
|
|
|
$pass = ($expect_normalized === $result_normalized);
|
2013-02-15 18:16:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if($pass) {
|
2012-04-23 18:57:06 +02:00
|
|
|
$this->passed += 1;
|
|
|
|
echo "PASS$eol";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->failed += 1;
|
|
|
|
echo "FAIL$eol";
|
2012-09-26 22:36:51 +02:00
|
|
|
echo 'Expect: ' . jsonld_encode($expect) . $eol;
|
|
|
|
echo 'Result: ' . jsonld_encode($result) . $eol;
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function load($filepath) {
|
|
|
|
global $eol;
|
|
|
|
$manifests = array();
|
|
|
|
|
|
|
|
// get full path
|
|
|
|
$filepath = realpath($filepath);
|
|
|
|
echo "Reading manifest files from: '$filepath'$eol";
|
|
|
|
|
|
|
|
// read each test file from the directory
|
|
|
|
$files = array();
|
|
|
|
$handle = opendir($filepath);
|
|
|
|
if($handle) {
|
|
|
|
while(($file = readdir($handle)) !== false) {
|
2012-04-23 20:01:56 +02:00
|
|
|
if($file !== '..' and $file !== '.') {
|
|
|
|
$files[] = $filepath . '/' . $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($handle);
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Exception('Could not open directory.');
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($files as $file) {
|
|
|
|
$info = pathinfo($file);
|
|
|
|
// FIXME: hackish, manifests are now JSON-LD
|
|
|
|
if(strstr($info['basename'], 'manifest') !== false &&
|
|
|
|
$info['extension'] == 'jsonld') {
|
|
|
|
echo "Reading manifest file: '$file'$eol";
|
|
|
|
|
|
|
|
try {
|
|
|
|
$manifest = json_decode(file_get_contents($file));
|
|
|
|
}
|
|
|
|
catch(Exception $e) {
|
|
|
|
echo "Exception while parsing file: '$file'$eol";
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
$manifest->filepath = $filepath;
|
|
|
|
$manifests[] = $manifest;
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
echo count($manifests) . " manifest file(s) read.$eol";
|
|
|
|
return $manifests;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run($manifests) {
|
|
|
|
/* Manifest format: {
|
|
|
|
name: <optional manifest name>,
|
|
|
|
sequence: [{
|
|
|
|
'name': <test name>,
|
|
|
|
'@type': ["test:TestCase", "jld:<type of test>"],
|
|
|
|
'input': <input file for test>,
|
|
|
|
'context': <context file for add context test type>,
|
|
|
|
'frame': <frame file for frame test type>,
|
|
|
|
'expect': <expected result file>,
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
global $eol;
|
|
|
|
foreach($manifests as $manifest) {
|
2012-04-23 22:06:42 +02:00
|
|
|
if(property_exists($manifest, 'name')) {
|
|
|
|
$this->group($manifest->name);
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
$filepath = $manifest->filepath;
|
|
|
|
foreach($manifest->sequence as $test) {
|
|
|
|
// read test input files
|
|
|
|
$type = $test->{'@type'};
|
|
|
|
$options = array(
|
|
|
|
'base' => 'http://json-ld.org/test-suite/tests/' . $test->input);
|
2012-09-26 22:36:51 +02:00
|
|
|
|
|
|
|
try {
|
2013-02-07 23:19:19 +01:00
|
|
|
if(in_array('jld:ApiErrorTest', $type)) {
|
2013-03-01 21:07:40 +01:00
|
|
|
echo "Skipping test \"{$test->name}\" of type: " .
|
2013-02-07 23:19:19 +01:00
|
|
|
json_encode($type) . $eol;
|
2013-03-01 21:07:40 +01:00
|
|
|
continue;
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
|
|
|
else if(in_array('jld:NormalizeTest', $type)) {
|
2012-09-26 22:36:51 +02:00
|
|
|
$this->test($test->name);
|
|
|
|
$input = read_test_json($test->input, $filepath);
|
|
|
|
$test->expect = read_test_nquads($test->expect, $filepath);
|
|
|
|
$options['format'] = 'application/nquads';
|
|
|
|
$result = jsonld_normalize($input, $options);
|
|
|
|
}
|
|
|
|
else if(in_array('jld:ExpandTest', $type)) {
|
|
|
|
$this->test($test->name);
|
|
|
|
$input = read_test_json($test->input, $filepath);
|
|
|
|
$test->expect = read_test_json($test->expect, $filepath);
|
|
|
|
$result = jsonld_expand($input, $options);
|
|
|
|
}
|
|
|
|
else if(in_array('jld:CompactTest', $type)) {
|
|
|
|
$this->test($test->name);
|
|
|
|
$input = read_test_json($test->input, $filepath);
|
|
|
|
$test->context = read_test_json($test->context, $filepath);
|
|
|
|
$test->expect = read_test_json($test->expect, $filepath);
|
|
|
|
$result = jsonld_compact($input, $test->context, $options);
|
|
|
|
}
|
2013-02-07 23:19:19 +01:00
|
|
|
else if(in_array('jld:FlattenTest', $type)) {
|
2013-03-01 21:07:40 +01:00
|
|
|
$this->test($test->name);
|
|
|
|
$input = read_test_json($test->input, $filepath);
|
|
|
|
$test->expect = read_test_json($test->expect, $filepath);
|
|
|
|
$result = jsonld_flatten($input, null, $options);
|
2013-02-07 23:19:19 +01:00
|
|
|
}
|
2012-09-26 22:36:51 +02:00
|
|
|
else if(in_array('jld:FrameTest', $type)) {
|
|
|
|
$this->test($test->name);
|
|
|
|
$input = read_test_json($test->input, $filepath);
|
|
|
|
$test->frame = read_test_json($test->frame, $filepath);
|
|
|
|
$test->expect = read_test_json($test->expect, $filepath);
|
|
|
|
$result = jsonld_frame($input, $test->frame, $options);
|
|
|
|
}
|
|
|
|
else if(in_array('jld:FromRDFTest', $type)) {
|
|
|
|
$this->test($test->name);
|
|
|
|
$input = read_test_nquads($test->input, $filepath);
|
|
|
|
$test->expect = read_test_json($test->expect, $filepath);
|
|
|
|
$result = jsonld_from_rdf($input, $options);
|
|
|
|
}
|
|
|
|
else if(in_array('jld:ToRDFTest', $type)) {
|
|
|
|
$this->test($test->name);
|
|
|
|
$input = read_test_json($test->input, $filepath);
|
|
|
|
$test->expect = read_test_nquads($test->expect, $filepath);
|
|
|
|
$options['format'] = 'application/nquads';
|
|
|
|
$result = jsonld_to_rdf($input, $options);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
echo "Skipping test \"{$test->name}\" of type: " .
|
|
|
|
json_encode($type) . $eol;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-03-01 21:07:40 +01:00
|
|
|
// check results
|
|
|
|
$this->check($test, $test->expect, $result, $type);
|
2012-04-30 23:43:38 +02:00
|
|
|
}
|
2012-09-26 22:36:51 +02:00
|
|
|
catch(JsonLdException $e) {
|
|
|
|
echo $eol . $e;
|
2013-03-01 21:07:40 +01:00
|
|
|
$this->failed += 1;
|
|
|
|
echo "FAIL$eol";
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
2012-04-23 22:06:42 +02:00
|
|
|
if(property_exists($manifest, 'name')) {
|
|
|
|
$this->ungroup();
|
|
|
|
}
|
2012-04-23 18:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2012-04-23 18:57:06 +02:00
|
|
|
// get command line options
|
|
|
|
$options = getopt('d:');
|
|
|
|
if($options === false || !array_key_exists('d', $options)) {
|
|
|
|
$var = 'path to json-ld.org/test-suite/tests';
|
|
|
|
echo "Usage: php jsonld-tests.php -d <$var>$eol";
|
|
|
|
exit(0);
|
2011-07-14 06:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// load and run tests
|
|
|
|
$tr = new TestRunner();
|
|
|
|
$tr->group('JSON-LD');
|
2012-04-23 18:57:06 +02:00
|
|
|
$tr->run($tr->load($options['d']));
|
2011-07-14 06:47:40 +02:00
|
|
|
$tr->ungroup();
|
2012-04-23 22:06:42 +02:00
|
|
|
echo "Done. Total:{$tr->total} Passed:{$tr->passed} Failed:{$tr->failed}$eol";
|
2011-07-14 06:47:40 +02:00
|
|
|
|
2012-04-23 22:06:42 +02:00
|
|
|
/* end of file, omit ?> */
|