Compare commits
61 commits
Author | SHA1 | Date | |
---|---|---|---|
1f33c8766b | |||
f52f3f322b | |||
8d1d421b1d | |||
43397ed81c | |||
3249d1f595 | |||
dafc68dc1e | |||
5f6ea87b26 | |||
e37882011f | |||
ca3916d10d | |||
f62652abff | |||
ccbd3d6bc4 | |||
7b779b2b53 | |||
a9ac64daf0 | |||
23cd99e8fb | |||
|
56b01d779d | ||
|
38b07bbe59 | ||
|
dc1bd23f0e | ||
|
3433a01a65 | ||
|
956fb8b790 | ||
|
81b072b438 | ||
|
6b5fba05e5 | ||
|
9759c9340d | ||
|
1abb809e8e | ||
|
b16d43aa74 | ||
|
d832d72b09 | ||
|
e98b8f61ba | ||
|
c63a5961fb | ||
|
57b3279718 | ||
|
e08fe87860 | ||
|
51a0635c61 | ||
|
eb4b344049 | ||
|
2927e09639 | ||
|
3330605897 | ||
|
16155eab6a | ||
|
989c394690 | ||
|
f23d0eb51e | ||
|
e599042c7d | ||
|
95a9945fc2 | ||
|
7535f05755 | ||
|
0cbff8c600 | ||
|
aba9709907 | ||
|
0b0442696d | ||
|
2314cfe0d1 | ||
|
906ad1d805 | ||
|
362a8cb387 | ||
|
e06d20062a | ||
|
b22dbbf285 | ||
|
b13749ed03 | ||
|
7be06f2ee2 | ||
|
237a405175 | ||
|
daed05a54b | ||
|
f252a5281c | ||
|
b80e4b80a5 | ||
|
1a9c6bffdd | ||
|
fed40914c8 | ||
|
bf504a1506 | ||
|
deb6afeac5 | ||
|
ea516341be | ||
|
d4ae67899d | ||
|
8fb6a75e67 | ||
|
8ca3291bc0 |
5 changed files with 6567 additions and 6069 deletions
14
.travis.yml
14
.travis.yml
|
@ -1,14 +0,0 @@
|
||||||
language: php
|
|
||||||
php:
|
|
||||||
- 5.5
|
|
||||||
- 5.4
|
|
||||||
- 5.3
|
|
||||||
# download test suite and run tests... submodule? meta testing project with
|
|
||||||
# all of the reference implementations?
|
|
||||||
script:
|
|
||||||
- git clone https://github.com/json-ld/json-ld.org.git spec
|
|
||||||
- phpunit test.php -d spec/test-suite
|
|
||||||
notifications:
|
|
||||||
email:
|
|
||||||
on_success: change
|
|
||||||
on_failure: change
|
|
114
README.md
114
README.md
|
@ -1,27 +1,29 @@
|
||||||
|
php-json-ld
|
||||||
|
===========
|
||||||
|
|
||||||
Introduction
|
Introduction
|
||||||
------------
|
------------
|
||||||
|
|
||||||
JSON, as specified in RFC4627, is a simple language for representing
|
This library is an implementation of the [JSON-LD][] specification in [PHP][].
|
||||||
|
|
||||||
|
JSON, as specified in [RFC7159][], is a simple language for representing
|
||||||
objects on the Web. Linked Data is a way of describing content across
|
objects on the Web. Linked Data is a way of describing content across
|
||||||
different documents or Web sites. Web resources are described using
|
different documents or Web sites. Web resources are described using
|
||||||
IRIs, and typically are dereferencable entities that may be used to find
|
IRIs, and typically are dereferencable entities that may be used to find
|
||||||
more information, creating a "Web of Knowledge". JSON-LD is intended to
|
more information, creating a "Web of Knowledge". [JSON-LD][] is intended
|
||||||
be a simple publishing method for expressing not only Linked Data in
|
to be a simple publishing method for expressing not only Linked Data in
|
||||||
JSON, but for adding semantics to existing JSON.
|
JSON, but for adding semantics to existing JSON.
|
||||||
|
|
||||||
This library is an implementation of the [JSON-LD] specification
|
|
||||||
in [PHP].
|
|
||||||
|
|
||||||
JSON-LD is designed as a light-weight syntax that can be used to express
|
JSON-LD is designed as a light-weight syntax that can be used to express
|
||||||
Linked Data. It is primarily intended to be a way to express Linked Data
|
Linked Data. It is primarily intended to be a way to express Linked Data
|
||||||
in Javascript and other Web-based programming environments. It is also
|
in JavaScript and other Web-based programming environments. It is also
|
||||||
useful when building interoperable Web Services and when storing Linked
|
useful when building interoperable Web Services and when storing Linked
|
||||||
Data in JSON-based document storage engines. It is practical and
|
Data in JSON-based document storage engines. It is practical and
|
||||||
designed to be as simple as possible, utilizing the large number of JSON
|
designed to be as simple as possible, utilizing the large number of JSON
|
||||||
parsers and existing code that is in use today. It is designed to be
|
parsers and existing code that is in use today. It is designed to be
|
||||||
able to express key-value pairs, RDF data, RDFa [RDFA-CORE] data,
|
able to express key-value pairs, RDF data, [RDFa][] data,
|
||||||
Microformats [MICROFORMATS] data, and Microdata [MICRODATA]. That is, it
|
[Microformats][] data, and [Microdata][]. That is, it supports every
|
||||||
supports every major Web-based structured data model in use today.
|
major Web-based structured data model in use today.
|
||||||
|
|
||||||
The syntax does not require many applications to change their JSON, but
|
The syntax does not require many applications to change their JSON, but
|
||||||
easily add meaning by adding context in a way that is either in-band or
|
easily add meaning by adding context in a way that is either in-band or
|
||||||
|
@ -88,40 +90,92 @@ $flattened = jsonld_flatten($doc);
|
||||||
$framed = jsonld_frame($doc, $frame);
|
$framed = jsonld_frame($doc, $frame);
|
||||||
// document transformed into a particular tree structure per the given frame
|
// document transformed into a particular tree structure per the given frame
|
||||||
|
|
||||||
// normalize a document
|
// normalize a document using the RDF Dataset Normalization Algorithm
|
||||||
$normalized = jsonld_normalize($doc, array('format' => 'application/nquads'));
|
// (URDNA2015), see: http://json-ld.github.io/normalization/spec/
|
||||||
|
$normalized = jsonld_normalize(
|
||||||
|
$doc, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
|
||||||
// normalized is a string that is a canonical representation of the document
|
// normalized is a string that is a canonical representation of the document
|
||||||
// that can be used for hashing
|
// that can be used for hashing, comparison, etc.
|
||||||
|
|
||||||
|
// force HTTPS-only context loading:
|
||||||
|
// use built-in secure document loader
|
||||||
|
jsonld_set_document_loader('jsonld_default_secure_document_loader');
|
||||||
|
|
||||||
|
// set a default custom document loader
|
||||||
|
jsonld_set_document_loader('my_custom_doc_loader');
|
||||||
|
|
||||||
|
// a custom loader that demonstrates using a simple in-memory mock for
|
||||||
|
// certain contexts before falling back to the default loader
|
||||||
|
// note: if you want to set this loader as the new default, you'll need to
|
||||||
|
// store the previous default in another variable first and access that inside
|
||||||
|
// the loader
|
||||||
|
global $mocks;
|
||||||
|
$mocks = array('http://example.com/mycontext' => (object)array(
|
||||||
|
'hombre' => 'http://schema.org/name'));
|
||||||
|
function mock_load($url) {
|
||||||
|
global $jsonld_default_load_document, $mocks;
|
||||||
|
if(isset($mocks[$url])) {
|
||||||
|
// return a "RemoteDocument", it has these three properties:
|
||||||
|
return (object)array(
|
||||||
|
'contextUrl' => null,
|
||||||
|
'document' => $mocks[$url],
|
||||||
|
'documentUrl' => $url);
|
||||||
|
}
|
||||||
|
// use default loader
|
||||||
|
return call_user_func($jsonld_default_load_document, $url);
|
||||||
|
}
|
||||||
|
|
||||||
|
// use the mock loader for just this call, witout modifying the default one
|
||||||
|
$compacted = jsonld_compact($foo, 'http://example.com/mycontext', array(
|
||||||
|
'documentLoader' => 'mock_load'));
|
||||||
|
|
||||||
|
// a custom loader that uses a simplistic in-memory cache (no invalidation)
|
||||||
|
global $cache;
|
||||||
|
$cache = array();
|
||||||
|
function cache_load($url) {
|
||||||
|
global $jsonld_default_load_document, $cache;
|
||||||
|
if(isset($cache[$url])) {
|
||||||
|
return $cache[$url];
|
||||||
|
}
|
||||||
|
// use default loader
|
||||||
|
$doc = call_user_func($jsonld_default_load_document, $url);
|
||||||
|
$cache[$url] = $doc;
|
||||||
|
return $doc;
|
||||||
|
}
|
||||||
|
|
||||||
|
// use the cache loader for just this call, witout modifying the default one
|
||||||
|
$compacted = jsonld_compact($foo, 'http://schema.org', array(
|
||||||
|
'documentLoader' => 'cache_load'));
|
||||||
```
|
```
|
||||||
|
|
||||||
Commercial Support
|
|
||||||
------------------
|
|
||||||
|
|
||||||
Commercial support for this library is available upon request from
|
|
||||||
Digital Bazaar: support@digitalbazaar.com
|
|
||||||
|
|
||||||
Source
|
Source
|
||||||
------
|
------
|
||||||
|
|
||||||
The source code for the PHP implementation of the JSON-LD API
|
The source code for the PHP implementation of the JSON-LD API is available at:
|
||||||
is available at:
|
|
||||||
|
|
||||||
http://github.com/digitalbazaar/php-json-ld
|
https://git.friendi.ca/friendica/php-json-ld
|
||||||
|
|
||||||
|
Tests
|
||||||
|
-----
|
||||||
|
|
||||||
This library includes a sample testing utility which may be used to verify
|
This library includes a sample testing utility which may be used to verify
|
||||||
that changes to the processor maintain the correct output.
|
that changes to the processor maintain the correct output.
|
||||||
|
|
||||||
To run the sample tests you will need to get the test suite files by cloning
|
To run the sample tests you will need to get the test suite files by cloning
|
||||||
the [json-ld.org repository][json-ld.org] hosted on GitHub.
|
the `json-ld.org` and `normalization` repositories hosted on GitHub:
|
||||||
|
|
||||||
https://github.com/json-ld/json-ld.org
|
- https://github.com/json-ld/json-ld.org
|
||||||
|
- https://github.com/json-ld/normalization
|
||||||
|
|
||||||
Then run the PHPUnit test.php application and point it at the directory
|
Then run the PHPUnit test.php application and point it at the directories
|
||||||
containing the tests.
|
containing the tests:
|
||||||
|
|
||||||
phpunit test.php -d {PATH_TO_JSON_LD_ORG/test-suite}
|
phpunit --group json-ld.org test.php -d {PATH_TO_JSON_LD_ORG/test-suite}
|
||||||
|
phpunit --group normalization test.php -d {PATH_TO_NORMALIZATION/tests}
|
||||||
|
|
||||||
[PHP]: http://php.net
|
|
||||||
[JSON-LD]: http://json-ld.org/
|
[JSON-LD]: http://json-ld.org/
|
||||||
[json-ld.org]: https://github.com/json-ld/json-ld.org
|
[Microdata]: http://www.w3.org/TR/microdata/
|
||||||
|
[Microformats]: http://microformats.org/
|
||||||
|
[PHP]: http://php.net
|
||||||
|
[RDFa]: http://www.w3.org/TR/rdfa-core/
|
||||||
|
[RFC7159]: http://tools.ietf.org/html/rfc7159
|
||||||
|
|
33
composer.json
Normal file
33
composer.json
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
{
|
||||||
|
"name": "friendica/json-ld",
|
||||||
|
"type": "library",
|
||||||
|
"description": "A JSON-LD Processor and API implementation in PHP.",
|
||||||
|
"keywords": [
|
||||||
|
"JSON",
|
||||||
|
"Linked Data",
|
||||||
|
"JSON-LD",
|
||||||
|
"RDF",
|
||||||
|
"Semantic Web",
|
||||||
|
"jsonld"
|
||||||
|
],
|
||||||
|
"homepage": "https://git.friendi.ca/friendica/php-json-ld",
|
||||||
|
"license": "BSD-3-Clause",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Digital Bazaar, Inc.",
|
||||||
|
"email": "support@digitalbazaar.com",
|
||||||
|
"homepage": "http://digitalbazaar.com/"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Friendica Team",
|
||||||
|
"homepage": "https://friendi.ca/"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"require": {
|
||||||
|
"php": ">=5.4.0",
|
||||||
|
"ext-json": "*"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"files": ["jsonld.php"]
|
||||||
|
}
|
||||||
|
}
|
1942
jsonld.php
1942
jsonld.php
File diff suppressed because it is too large
Load diff
457
test.php
457
test.php
|
@ -1,4 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PHP unit tests for JSON-LD.
|
* PHP unit tests for JSON-LD.
|
||||||
*
|
*
|
||||||
|
@ -8,14 +9,16 @@
|
||||||
*/
|
*/
|
||||||
require_once('jsonld.php');
|
require_once('jsonld.php');
|
||||||
|
|
||||||
class JsonLdTestCase extends PHPUnit_Framework_TestCase {
|
class JsonLdTestCase extends PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
/**
|
/**
|
||||||
* Runs this test case. Overridden to attach to EARL report w/o need for
|
* Runs this test case. Overridden to attach to EARL report w/o need for
|
||||||
* an external XML configuration file.
|
* an external XML configuration file.
|
||||||
*
|
*
|
||||||
* @param PHPUnit_Framework_TestResult $result the test result.
|
* @param PHPUnit_Framework_TestResult $result the test result.
|
||||||
*/
|
*/
|
||||||
public function run(PHPUnit_Framework_TestResult $result = NULL) {
|
public function run(PHPUnit_Framework_TestResult $result = NULL)
|
||||||
|
{
|
||||||
global $EARL;
|
global $EARL;
|
||||||
$EARL->attach($result);
|
$EARL->attach($result);
|
||||||
$this->result = $result;
|
$this->result = $result;
|
||||||
|
@ -28,13 +31,15 @@ class JsonLdTestCase extends PHPUnit_Framework_TestCase {
|
||||||
* @param JsonLdTest $test the test to run.
|
* @param JsonLdTest $test the test to run.
|
||||||
*
|
*
|
||||||
* @group expand
|
* @group expand
|
||||||
|
* @group json-ld.org
|
||||||
* @dataProvider expandProvider
|
* @dataProvider expandProvider
|
||||||
*/
|
*/
|
||||||
public function testExpand($test) {
|
public function testExpand($test)
|
||||||
|
{
|
||||||
$this->test = $test;
|
$this->test = $test;
|
||||||
$input = $test->readUrl('input');
|
$input = $test->readUrl('input');
|
||||||
$options = $test->createOptions();
|
$options = $test->createOptions();
|
||||||
$test->run('jsonld_expand', array($input, $options));
|
$test->run('jsonld_expand', [$input, $options]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -43,14 +48,16 @@ class JsonLdTestCase extends PHPUnit_Framework_TestCase {
|
||||||
* @param JsonLdTest $test the test to run.
|
* @param JsonLdTest $test the test to run.
|
||||||
*
|
*
|
||||||
* @group compact
|
* @group compact
|
||||||
|
* @group json-ld.org
|
||||||
* @dataProvider compactProvider
|
* @dataProvider compactProvider
|
||||||
*/
|
*/
|
||||||
public function testCompact($test) {
|
public function testCompact($test)
|
||||||
|
{
|
||||||
$this->test = $test;
|
$this->test = $test;
|
||||||
$input = $test->readUrl('input');
|
$input = $test->readUrl('input');
|
||||||
$context = $test->readProperty('context');
|
$context = $test->readProperty('context');
|
||||||
$options = $test->createOptions();
|
$options = $test->createOptions();
|
||||||
$test->run('jsonld_compact', array($input, $context, $options));
|
$test->run('jsonld_compact', [$input, $context, $options]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -59,14 +66,16 @@ class JsonLdTestCase extends PHPUnit_Framework_TestCase {
|
||||||
* @param JsonLdTest $test the test to run.
|
* @param JsonLdTest $test the test to run.
|
||||||
*
|
*
|
||||||
* @group flatten
|
* @group flatten
|
||||||
|
* @group json-ld.org
|
||||||
* @dataProvider flattenProvider
|
* @dataProvider flattenProvider
|
||||||
*/
|
*/
|
||||||
public function testFlatten($test) {
|
public function testFlatten($test)
|
||||||
|
{
|
||||||
$this->test = $test;
|
$this->test = $test;
|
||||||
$input = $test->readUrl('input');
|
$input = $test->readUrl('input');
|
||||||
$context = $test->readProperty('context');
|
$context = $test->readProperty('context');
|
||||||
$options = $test->createOptions();
|
$options = $test->createOptions();
|
||||||
$test->run('jsonld_flatten', array($input, $context, $options));
|
$test->run('jsonld_flatten', [$input, $context, $options]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -75,13 +84,15 @@ class JsonLdTestCase extends PHPUnit_Framework_TestCase {
|
||||||
* @param JsonLdTest $test the test to run.
|
* @param JsonLdTest $test the test to run.
|
||||||
*
|
*
|
||||||
* @group toRdf
|
* @group toRdf
|
||||||
|
* @group json-ld.org
|
||||||
* @dataProvider toRdfProvider
|
* @dataProvider toRdfProvider
|
||||||
*/
|
*/
|
||||||
public function testToRdf($test) {
|
public function testToRdf($test)
|
||||||
|
{
|
||||||
$this->test = $test;
|
$this->test = $test;
|
||||||
$input = $test->readUrl('input');
|
$input = $test->readUrl('input');
|
||||||
$options = $test->createOptions(array('format' => 'application/nquads'));
|
$options = $test->createOptions(['format' => 'application/nquads']);
|
||||||
$test->run('jsonld_to_rdf', array($input, $options));
|
$test->run('jsonld_to_rdf', [$input, $options]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -90,13 +101,15 @@ class JsonLdTestCase extends PHPUnit_Framework_TestCase {
|
||||||
* @param JsonLdTest $test the test to run.
|
* @param JsonLdTest $test the test to run.
|
||||||
*
|
*
|
||||||
* @group fromRdf
|
* @group fromRdf
|
||||||
|
* @group json-ld.org
|
||||||
* @dataProvider fromRdfProvider
|
* @dataProvider fromRdfProvider
|
||||||
*/
|
*/
|
||||||
public function testFromRdf($test) {
|
public function testFromRdf($test)
|
||||||
|
{
|
||||||
$this->test = $test;
|
$this->test = $test;
|
||||||
$input = $test->readProperty('input');
|
$input = $test->readProperty('input');
|
||||||
$options = $test->createOptions(array('format' => 'application/nquads'));
|
$options = $test->createOptions(['format' => 'application/nquads']);
|
||||||
$test->run('jsonld_from_rdf', array($input, $options));
|
$test->run('jsonld_from_rdf', [$input, $options]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -105,14 +118,16 @@ class JsonLdTestCase extends PHPUnit_Framework_TestCase {
|
||||||
* @param JsonLdTest $test the test to run.
|
* @param JsonLdTest $test the test to run.
|
||||||
*
|
*
|
||||||
* @group frame
|
* @group frame
|
||||||
|
* @group json-ld.org
|
||||||
* @dataProvider frameProvider
|
* @dataProvider frameProvider
|
||||||
*/
|
*/
|
||||||
public function testFrame($test) {
|
public function testFrame($test)
|
||||||
|
{
|
||||||
$this->test = $test;
|
$this->test = $test;
|
||||||
$input = $test->readUrl('input');
|
$input = $test->readUrl('input');
|
||||||
$frame = $test->readProperty('frame');
|
$frame = $test->readProperty('frame');
|
||||||
$options = $test->createOptions();
|
$options = $test->createOptions();
|
||||||
$test->run('jsonld_frame', array($input, $frame, $options));
|
$test->run('jsonld_frame', [$input, $frame, $options]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -121,73 +136,142 @@ class JsonLdTestCase extends PHPUnit_Framework_TestCase {
|
||||||
* @param JsonLdTest $test the test to run.
|
* @param JsonLdTest $test the test to run.
|
||||||
*
|
*
|
||||||
* @group normalize
|
* @group normalize
|
||||||
|
* @group json-ld.org
|
||||||
* @dataProvider normalizeProvider
|
* @dataProvider normalizeProvider
|
||||||
*/
|
*/
|
||||||
public function testNormalize($test) {
|
public function testNormalize($test)
|
||||||
|
{
|
||||||
$this->test = $test;
|
$this->test = $test;
|
||||||
$input = $test->readUrl('input');
|
$input = $test->readUrl('input');
|
||||||
$options = $test->createOptions(array('format' => 'application/nquads'));
|
$options = $test->createOptions(['format' => 'application/nquads']);
|
||||||
$test->run('jsonld_normalize', array($input, $options));
|
$test->run('jsonld_normalize', [$input, $options]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function expandProvider() {
|
/**
|
||||||
|
* Tests URGNA2012 normalization.
|
||||||
|
*
|
||||||
|
* @param JsonLdTest $test the test to run.
|
||||||
|
*
|
||||||
|
* @group normalize
|
||||||
|
* @group normalization
|
||||||
|
* @dataProvider urgna2012Provider
|
||||||
|
*/
|
||||||
|
public function testUrgna2012($test)
|
||||||
|
{
|
||||||
|
$this->test = $test;
|
||||||
|
$input = $test->readProperty('action');
|
||||||
|
$options = $test->createOptions([
|
||||||
|
'algorithm' => 'URGNA2012',
|
||||||
|
'inputFormat' => 'application/nquads',
|
||||||
|
'format' => 'application/nquads']);
|
||||||
|
$test->run('jsonld_normalize', [$input, $options]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests URDNA2015 normalization.
|
||||||
|
*
|
||||||
|
* @param JsonLdTest $test the test to run.
|
||||||
|
*
|
||||||
|
* @group normalize
|
||||||
|
* @group normalization
|
||||||
|
* @dataProvider urdna2015Provider
|
||||||
|
*/
|
||||||
|
public function testUrdna2015($test)
|
||||||
|
{
|
||||||
|
$this->test = $test;
|
||||||
|
$input = $test->readProperty('action');
|
||||||
|
$options = $test->createOptions([
|
||||||
|
'algorithm' => 'URDNA2015',
|
||||||
|
'inputFormat' => 'application/nquads',
|
||||||
|
'format' => 'application/nquads']);
|
||||||
|
$test->run('jsonld_normalize', [$input, $options]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function expandProvider()
|
||||||
|
{
|
||||||
return new JsonLdTestIterator('jld:ExpandTest');
|
return new JsonLdTestIterator('jld:ExpandTest');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function compactProvider() {
|
public function compactProvider()
|
||||||
|
{
|
||||||
return new JsonLdTestIterator('jld:CompactTest');
|
return new JsonLdTestIterator('jld:CompactTest');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function flattenProvider() {
|
public function flattenProvider()
|
||||||
|
{
|
||||||
return new JsonLdTestIterator('jld:FlattenTest');
|
return new JsonLdTestIterator('jld:FlattenTest');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toRdfProvider() {
|
public function toRdfProvider()
|
||||||
|
{
|
||||||
return new JsonLdTestIterator('jld:ToRDFTest');
|
return new JsonLdTestIterator('jld:ToRDFTest');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fromRdfProvider() {
|
public function fromRdfProvider()
|
||||||
|
{
|
||||||
return new JsonLdTestIterator('jld:FromRDFTest');
|
return new JsonLdTestIterator('jld:FromRDFTest');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function normalizeProvider() {
|
public function normalizeProvider()
|
||||||
|
{
|
||||||
return new JsonLdTestIterator('jld:NormalizeTest');
|
return new JsonLdTestIterator('jld:NormalizeTest');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function frameProvider() {
|
public function frameProvider()
|
||||||
|
{
|
||||||
return new JsonLdTestIterator('jld:FrameTest');
|
return new JsonLdTestIterator('jld:FrameTest');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function urgna2012Provider()
|
||||||
|
{
|
||||||
|
return new JsonLdTestIterator('rdfn:Urgna2012EvalTest');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function urdna2015Provider()
|
||||||
|
{
|
||||||
|
return new JsonLdTestIterator('rdfn:Urdna2015EvalTest');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class JsonLdManifest {
|
class JsonLdManifest
|
||||||
public function __construct($data, $filename) {
|
{
|
||||||
|
public function __construct($data, $filename)
|
||||||
|
{
|
||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
$this->filename = $filename;
|
$this->filename = $filename;
|
||||||
$this->dirname = dirname($filename);
|
$this->dirname = dirname($filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function load(&$tests) {
|
public function load(&$tests)
|
||||||
$sequence = JsonLdProcessor::getValues($this->data, 'sequence');
|
{
|
||||||
foreach($sequence as $entry) {
|
$entries = array_merge(
|
||||||
|
JsonLdProcessor::getValues($this->data, 'sequence'), JsonLdProcessor::getValues($this->data, 'entries'));
|
||||||
|
$includes = JsonLdProcessor::getValues($this->data, 'include');
|
||||||
|
foreach ($includes as $include) {
|
||||||
|
array_push($entries, $include . '.jsonld');
|
||||||
|
}
|
||||||
|
foreach ($entries as $entry) {
|
||||||
if (is_string($entry)) {
|
if (is_string($entry)) {
|
||||||
$filename = join(
|
$filename = join(
|
||||||
DIRECTORY_SEPARATOR, array($this->dirname, $entry));
|
DIRECTORY_SEPARATOR, [$this->dirname, $entry]);
|
||||||
$entry = Util::readJson($filename);
|
$entry = Util::readJson($filename);
|
||||||
} else {
|
} else {
|
||||||
$filename = $this->filename;
|
$filename = $this->filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(JsonLdProcessor::hasValue($entry, '@type', 'mf:Manifest')) {
|
if (JsonLdProcessor::hasValue($entry, '@type', 'mf:Manifest') ||
|
||||||
|
JsonLdProcessor::hasValue($entry, 'type', 'mf:Manifest')) {
|
||||||
// entry is another manifest
|
// entry is another manifest
|
||||||
$manifest = new JsonLdManifest($entry, $filename);
|
$manifest = new JsonLdManifest($entry, $filename);
|
||||||
$manifest->load($tests);
|
$manifest->load($tests);
|
||||||
} else {
|
} else {
|
||||||
// assume entry is a test
|
// assume entry is a test
|
||||||
$test = new JsonLdTest($this, $entry, $filename);
|
$test = new JsonLdTest($this, $entry, $filename);
|
||||||
$types = JsonLdProcessor::getValues($test->data, '@type');
|
$types = array_merge(
|
||||||
|
JsonLdProcessor::getValues($test->data, '@type'), JsonLdProcessor::getValues($test->data, 'type'));
|
||||||
foreach ($types as $type) {
|
foreach ($types as $type) {
|
||||||
if (!isset($tests[$type])) {
|
if (!isset($tests[$type])) {
|
||||||
$tests[$type] = array();
|
$tests[$type] = [];
|
||||||
}
|
}
|
||||||
$tests[$type][] = $test;
|
$tests[$type][] = $test;
|
||||||
}
|
}
|
||||||
|
@ -196,33 +280,72 @@ class JsonLdManifest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class JsonLdTest {
|
class JsonLdTest
|
||||||
public function __construct($manifest, $data, $filename) {
|
{
|
||||||
|
public function __construct($manifest, $data, $filename)
|
||||||
|
{
|
||||||
$this->manifest = $manifest;
|
$this->manifest = $manifest;
|
||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
$this->filename = $filename;
|
$this->filename = $filename;
|
||||||
$this->dirname = dirname($filename);
|
$this->dirname = dirname($filename);
|
||||||
$this->isPositive = JsonLdProcessor::hasValue(
|
$this->isPositive = JsonLdProcessor::hasValue(
|
||||||
$data, '@type', 'jld:PositiveEvaluationTest');
|
$data, '@type', 'jld:PositiveEvaluationTest') ||
|
||||||
|
JsonLdProcessor::hasValue(
|
||||||
|
$data, 'type', 'jld:PositiveEvaluationTest');
|
||||||
$this->isNegative = JsonLdProcessor::hasValue(
|
$this->isNegative = JsonLdProcessor::hasValue(
|
||||||
$data, '@type', 'jld:NegativeEvaluationTest');
|
$data, '@type', 'jld:NegativeEvaluationTest') ||
|
||||||
|
JsonLdProcessor::hasValue(
|
||||||
|
$data, 'type', 'jld:NegativeEvaluationTest');
|
||||||
|
|
||||||
// generate test name
|
// generate test name
|
||||||
$this->name = $manifest->data->name . ' ' . substr($data->{'@id'}, 2) .
|
if (isset($manifest->data->name)) {
|
||||||
' - ' . $this->data->name;
|
$manifestLabel = $manifest->data->name;
|
||||||
|
} else if (isset($manifest->data->label)) {
|
||||||
|
$manifestLabel = $manifest->data->label;
|
||||||
|
} else {
|
||||||
|
$manifestLabel = 'UNNAMED';
|
||||||
|
}
|
||||||
|
if (isset($this->data->id)) {
|
||||||
|
$testId = $this->data->id;
|
||||||
|
} else {
|
||||||
|
$testId = $this->data->{'@id'};
|
||||||
|
}
|
||||||
|
if (isset($this->data->name)) {
|
||||||
|
$testLabel = $this->data->name;
|
||||||
|
} else if (isset($this->data->label)) {
|
||||||
|
$testLabel = $this->data->label;
|
||||||
|
} else {
|
||||||
|
$testLabel = 'UNNAMED';
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->name = $manifestLabel . ' ' . $testId . ' - ' . $testLabel;
|
||||||
|
|
||||||
// expand @id and input base
|
// expand @id and input base
|
||||||
|
if (isset($manifest->data->baseIri)) {
|
||||||
$data->{'@id'} = ($manifest->data->baseIri .
|
$data->{'@id'} = ($manifest->data->baseIri .
|
||||||
basename($manifest->filename) . $data->{'@id'});
|
basename($manifest->filename) . $data->{'@id'});
|
||||||
$this->base = $manifest->data->baseIri . $data->input;
|
$this->base = $manifest->data->baseIri . $data->input;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function run($fn, $params) {
|
private function _getResultProperty()
|
||||||
|
{
|
||||||
|
if (isset($this->data->expect)) {
|
||||||
|
return 'expect';
|
||||||
|
} else if (isset($this->data->result)) {
|
||||||
|
return 'result';
|
||||||
|
} else {
|
||||||
|
throw new Exception('No test result property found.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run($fn, $params)
|
||||||
|
{
|
||||||
// read expected data
|
// read expected data
|
||||||
if ($this->isNegative) {
|
if ($this->isNegative) {
|
||||||
$this->expected = $this->data->expect;
|
$this->expected = $this->data->expect;
|
||||||
} else {
|
} else {
|
||||||
$this->expected = $this->readProperty('expect');
|
$this->expected = $this->readProperty($this->_getResultProperty());
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -232,70 +355,81 @@ class JsonLdTest {
|
||||||
}
|
}
|
||||||
PHPUnit_Framework_TestCase::assertEquals($this->expected, $this->actual);
|
PHPUnit_Framework_TestCase::assertEquals($this->expected, $this->actual);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
if($this->isPositive) {
|
// assume positive test
|
||||||
|
if ($this->isNegative) {
|
||||||
|
$this->actual = $this->getJsonLdErrorCode($e);
|
||||||
|
PHPUnit_Framework_TestCase::assertEquals(
|
||||||
|
$this->expected, $this->actual);
|
||||||
|
} else {
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
$this->actual = $this->getJsonLdErrorCode($e);
|
|
||||||
PHPUnit_Framework_TestCase::assertEquals($this->expected, $this->actual);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function readUrl($property) {
|
public function readUrl($property)
|
||||||
|
{
|
||||||
if (!property_exists($this->data, $property)) {
|
if (!property_exists($this->data, $property)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return $this->manifest->data->baseIri . $this->data->{$property};
|
return $this->manifest->data->baseIri . $this->data->{$property};
|
||||||
}
|
}
|
||||||
|
|
||||||
public function readProperty($property) {
|
public function readProperty($property)
|
||||||
|
{
|
||||||
$data = $this->data;
|
$data = $this->data;
|
||||||
if (!property_exists($data, $property)) {
|
if (!property_exists($data, $property)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$filename = join(
|
$filename = join(
|
||||||
DIRECTORY_SEPARATOR, array($this->dirname, $data->{$property}));
|
DIRECTORY_SEPARATOR, [$this->dirname, $data->{$property}]);
|
||||||
$extension = pathinfo($filename, PATHINFO_EXTENSION);
|
$extension = pathinfo($filename, PATHINFO_EXTENSION);
|
||||||
if ($extension === 'jsonld') {
|
if ($extension === 'jsonld') {
|
||||||
return Util::readJson($filename);
|
return Util::readJson($filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Util::readFile($filename);
|
return Util::readFile($filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createOptions($opts=array()) {
|
public function createOptions($opts = [])
|
||||||
$http_options = array(
|
{
|
||||||
'contentType', 'httpLink', 'httpStatus', 'redirectTo');
|
$http_options = [
|
||||||
|
'contentType', 'httpLink', 'httpStatus', 'redirectTo'];
|
||||||
$test_options = (property_exists($this->data, 'option') ?
|
$test_options = (property_exists($this->data, 'option') ?
|
||||||
$this->data->option : array());
|
$this->data->option : []);
|
||||||
$options = array();
|
$options = [];
|
||||||
foreach ($test_options as $k => $v) {
|
foreach ($test_options as $k => $v) {
|
||||||
if (!in_array($k, $http_options)) {
|
if (!in_array($k, $http_options)) {
|
||||||
$options[$k] = $v;
|
$options[$k] = $v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$options['documentLoader'] = $this->createDocumentLoader();
|
$options['documentLoader'] = $this->createDocumentLoader();
|
||||||
$options = array_merge($options, $opts);
|
$options = array_merge($options, $opts);
|
||||||
if (isset($options['expandContext'])) {
|
if (isset($options['expandContext'])) {
|
||||||
$filename = join(
|
$filename = join(
|
||||||
DIRECTORY_SEPARATOR, array($this->dirname, $options['expandContext']));
|
DIRECTORY_SEPARATOR, [$this->dirname, $options['expandContext']]);
|
||||||
$options['expandContext'] = Util::readJson($filename);
|
$options['expandContext'] = Util::readJson($filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $options;
|
return $options;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createDocumentLoader() {
|
public function createDocumentLoader()
|
||||||
|
{
|
||||||
$base = 'http://json-ld.org/test-suite';
|
$base = 'http://json-ld.org/test-suite';
|
||||||
$test = $this;
|
$test = $this;
|
||||||
|
|
||||||
$load_locally = function($url) use ($test, $base) {
|
$load_locally = function($url) use ($test, $base) {
|
||||||
$doc = (object)array(
|
$doc = (object) [
|
||||||
'contextUrl' => null, 'documentUrl' => $url, 'document' => null);
|
'contextUrl' => null, 'documentUrl' => $url, 'document' => null];
|
||||||
$options = (property_exists($test->data, 'option') ?
|
$options = (property_exists($test->data, 'option') ?
|
||||||
$test->data->option : null);
|
$test->data->option : null);
|
||||||
if ($options and $url === $test->base) {
|
if ($options and $url === $test->base) {
|
||||||
if (property_exists($options, 'redirectTo') &&
|
if (property_exists($options, 'redirectTo') &&
|
||||||
property_exists($options, 'httpStatus') &&
|
property_exists($options, 'httpStatus') &&
|
||||||
$options->httpStatus >= '300') {
|
$options->httpStatus >= '300'
|
||||||
$doc->documentUrl = ($test->manifest->data->{'baseIri'} .
|
) {
|
||||||
|
$doc->documentUrl = ($test->manifest->data->baseIri .
|
||||||
$options->redirectTo);
|
$options->redirectTo);
|
||||||
} else if (property_exists($options, 'httpLink')) {
|
} else if (property_exists($options, 'httpLink')) {
|
||||||
$content_type = (property_exists($options, 'contentType') ?
|
$content_type = (property_exists($options, 'contentType') ?
|
||||||
|
@ -304,38 +438,51 @@ class JsonLdTest {
|
||||||
if (!$content_type && $extension === 'jsonld') {
|
if (!$content_type && $extension === 'jsonld') {
|
||||||
$content_type = 'application/ld+json';
|
$content_type = 'application/ld+json';
|
||||||
}
|
}
|
||||||
|
|
||||||
$link_header = $options->httpLink;
|
$link_header = $options->httpLink;
|
||||||
if (is_array($link_header)) {
|
if (is_array($link_header)) {
|
||||||
$link_header = join(',', $link_header);
|
$link_header = join(',', $link_header);
|
||||||
}
|
}
|
||||||
|
|
||||||
$link_header = jsonld_parse_link_header($link_header);
|
$link_header = jsonld_parse_link_header($link_header);
|
||||||
if (isset($link_header['http://www.w3.org/ns/json-ld#context'])) {
|
if (isset($link_header['http://www.w3.org/ns/json-ld#context'])) {
|
||||||
$link_header = $link_header['http://www.w3.org/ns/json-ld#context'];
|
$link_header = $link_header['http://www.w3.org/ns/json-ld#context'];
|
||||||
} else {
|
} else {
|
||||||
$link_header = null;
|
$link_header = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($link_header && $content_type !== 'application/ld+json') {
|
if ($link_header && $content_type !== 'application/ld+json') {
|
||||||
if (is_array($link_header)) {
|
if (is_array($link_header)) {
|
||||||
throw new Exception('multiple context link headers');
|
throw new Exception('multiple context link headers');
|
||||||
}
|
}
|
||||||
$doc->{'contextUrl'} = $link_header->target;
|
$doc->contextUrl = $link_header->target;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
global $ROOT_MANIFEST_DIR;
|
global $ROOT_MANIFEST_DIR;
|
||||||
$filename = $ROOT_MANIFEST_DIR .
|
if (strpos($doc->documentUrl, ':') === false) {
|
||||||
substr($doc->{'documentUrl'}, strlen($base));
|
$filename = join(
|
||||||
|
DIRECTORY_SEPARATOR, [
|
||||||
|
$ROOT_MANIFEST_DIR, $doc->documentUrl]);
|
||||||
|
$doc->documentUrl = 'file://' . $filename;
|
||||||
|
} else {
|
||||||
|
$filename = join(
|
||||||
|
DIRECTORY_SEPARATOR, [
|
||||||
|
$ROOT_MANIFEST_DIR, substr($doc->documentUrl, strlen($base))]);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$doc->{'document'} = Util::readJson($filename);
|
$doc->document = Util::readJson($filename);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
throw new Exception('loading document failed');
|
throw new Exception('loading document failed');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $doc;
|
return $doc;
|
||||||
};
|
};
|
||||||
|
|
||||||
$local_loader = function($url) use ($test, $base, $load_locally) {
|
$local_loader = function($url) use ($test, $base, $load_locally) {
|
||||||
// always load remote-doc and non-base tests remotely
|
// always load remote-doc and non-base tests remotely
|
||||||
if(strpos($url, $base) !== 0 ||
|
if ((strpos($url, $base) !== 0 && strpos($url, ':') !== false) ||
|
||||||
$test->manifest->data->name === 'Remote document') {
|
$test->manifest->data->name === 'Remote document') {
|
||||||
return call_user_func('jsonld_default_document_loader', $url);
|
return call_user_func('jsonld_default_document_loader', $url);
|
||||||
}
|
}
|
||||||
|
@ -347,20 +494,25 @@ class JsonLdTest {
|
||||||
return $local_loader;
|
return $local_loader;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getJsonLdErrorCode($err) {
|
public function getJsonLdErrorCode($err)
|
||||||
|
{
|
||||||
if ($err instanceof JsonLdException) {
|
if ($err instanceof JsonLdException) {
|
||||||
if ($err->getCode()) {
|
if ($err->getCode()) {
|
||||||
return $err->getCode();
|
return $err->getCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($err->cause) {
|
if ($err->cause) {
|
||||||
return $this->getJsonLdErrorCode($err->cause);
|
return $this->getJsonLdErrorCode($err->cause);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $err->getMessage();
|
return $err->getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class JsonLdTestIterator implements Iterator {
|
class JsonLdTestIterator implements Iterator
|
||||||
|
{
|
||||||
/**
|
/**
|
||||||
* The current test index.
|
* The current test index.
|
||||||
*/
|
*/
|
||||||
|
@ -376,13 +528,15 @@ class JsonLdTestIterator implements Iterator {
|
||||||
*
|
*
|
||||||
* @param string $type the type of tests to iterate over.
|
* @param string $type the type of tests to iterate over.
|
||||||
*/
|
*/
|
||||||
public function __construct($type) {
|
public function __construct($type)
|
||||||
|
{
|
||||||
global $TESTS;
|
global $TESTS;
|
||||||
if (isset($TESTS[$type])) {
|
if (isset($TESTS[$type])) {
|
||||||
$this->tests = $TESTS[$type];
|
$this->tests = $TESTS[$type];
|
||||||
} else {
|
} else {
|
||||||
$this->tests = array();
|
$this->tests = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->count = count($this->tests);
|
$this->count = count($this->tests);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -391,8 +545,9 @@ class JsonLdTestIterator implements Iterator {
|
||||||
*
|
*
|
||||||
* @return assoc the parameters for the next test.
|
* @return assoc the parameters for the next test.
|
||||||
*/
|
*/
|
||||||
public function current() {
|
public function current()
|
||||||
return array('test' => $this->tests[$this->index]);
|
{
|
||||||
|
return ['test' => $this->tests[$this->index]];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -400,21 +555,24 @@ class JsonLdTestIterator implements Iterator {
|
||||||
*
|
*
|
||||||
* @return int the current test number.
|
* @return int the current test number.
|
||||||
*/
|
*/
|
||||||
public function key() {
|
public function key()
|
||||||
|
{
|
||||||
return $this->index;
|
return $this->index;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Proceeds to the next test.
|
* Proceeds to the next test.
|
||||||
*/
|
*/
|
||||||
public function next() {
|
public function next()
|
||||||
|
{
|
||||||
$this->index += 1;
|
$this->index += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rewinds to the first test.
|
* Rewinds to the first test.
|
||||||
*/
|
*/
|
||||||
public function rewind() {
|
public function rewind()
|
||||||
|
{
|
||||||
$this->index = 0;
|
$this->index = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -423,36 +581,38 @@ class JsonLdTestIterator implements Iterator {
|
||||||
*
|
*
|
||||||
* @return bool true if there are more tests to be run.
|
* @return bool true if there are more tests to be run.
|
||||||
*/
|
*/
|
||||||
public function valid() {
|
public function valid()
|
||||||
|
{
|
||||||
return $this->index < $this->count;
|
return $this->index < $this->count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class EarlReport extends PHPUnit_Util_Printer
|
class EarlReport extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
|
||||||
implements PHPUnit_Framework_TestListener {
|
{
|
||||||
public function __construct() {
|
public function __construct()
|
||||||
|
{
|
||||||
$this->filename = null;
|
$this->filename = null;
|
||||||
$this->attached = false;
|
$this->attached = false;
|
||||||
$this->report = (object)array(
|
$this->report = (object) [
|
||||||
'@context' => (object)array(
|
'@context' => (object) [
|
||||||
'doap' => 'http://usefulinc.com/ns/doap#',
|
'doap' => 'http://usefulinc.com/ns/doap#',
|
||||||
'foaf' => 'http://xmlns.com/foaf/0.1/',
|
'foaf' => 'http://xmlns.com/foaf/0.1/',
|
||||||
'dc' => 'http://purl.org/dc/terms/',
|
'dc' => 'http://purl.org/dc/terms/',
|
||||||
'earl' => 'http://www.w3.org/ns/earl#',
|
'earl' => 'http://www.w3.org/ns/earl#',
|
||||||
'xsd' => 'http://www.w3.org/2001/XMLSchema#',
|
'xsd' => 'http://www.w3.org/2001/XMLSchema#',
|
||||||
'doap:homepage' => (object)array('@type' => '@id'),
|
'doap:homepage' => (object) ['@type' => '@id'],
|
||||||
'doap:license' => (object)array('@type' => '@id'),
|
'doap:license' => (object) ['@type' => '@id'],
|
||||||
'dc:creator' => (object)array('@type' => '@id'),
|
'dc:creator' => (object) ['@type' => '@id'],
|
||||||
'foaf:homepage' => (object)array('@type' => '@id'),
|
'foaf:homepage' => (object) ['@type' => '@id'],
|
||||||
'subjectOf' => (object)array('@reverse' => 'earl:subject'),
|
'subjectOf' => (object) ['@reverse' => 'earl:subject'],
|
||||||
'earl:assertedBy' => (object)array('@type' => '@id'),
|
'earl:assertedBy' => (object) ['@type' => '@id'],
|
||||||
'earl:mode' => (object)array('@type' => '@id'),
|
'earl:mode' => (object) ['@type' => '@id'],
|
||||||
'earl:test' => (object)array('@type' => '@id'),
|
'earl:test' => (object) ['@type' => '@id'],
|
||||||
'earl:outcome' => (object)array('@type' => '@id'),
|
'earl:outcome' => (object) ['@type' => '@id'],
|
||||||
'dc:date' => (object)array('@type' => 'xsd:date')
|
'dc:date' => (object) ['@type' => 'xsd:date']
|
||||||
),
|
],
|
||||||
'@id' => 'https://github.com/digitalbazaar/php-json-ld',
|
'@id' => 'https://github.com/digitalbazaar/php-json-ld',
|
||||||
'@type' => array('doap:Project', 'earl:TestSubject', 'earl:Software'),
|
'@type' => ['doap:Project', 'earl:TestSubject', 'earl:Software'],
|
||||||
'doap:name' => 'php-json-ld',
|
'doap:name' => 'php-json-ld',
|
||||||
'dc:title' => 'php-json-ld',
|
'dc:title' => 'php-json-ld',
|
||||||
'doap:homepage' => 'https://github.com/digitalbazaar/php-json-ld',
|
'doap:homepage' => 'https://github.com/digitalbazaar/php-json-ld',
|
||||||
|
@ -460,18 +620,18 @@ class EarlReport extends PHPUnit_Util_Printer
|
||||||
'doap:description' => 'A JSON-LD processor for PHP',
|
'doap:description' => 'A JSON-LD processor for PHP',
|
||||||
'doap:programming-language' => 'PHP',
|
'doap:programming-language' => 'PHP',
|
||||||
'dc:creator' => 'https://github.com/dlongley',
|
'dc:creator' => 'https://github.com/dlongley',
|
||||||
'doap:developer' => (object)array(
|
'doap:developer' => (object) [
|
||||||
'@id' => 'https://github.com/dlongley',
|
'@id' => 'https://github.com/dlongley',
|
||||||
'@type' => array('foaf:Person', 'earl:Assertor'),
|
'@type' => ['foaf:Person', 'earl:Assertor'],
|
||||||
'foaf:name' => 'Dave Longley',
|
'foaf:name' => 'Dave Longley',
|
||||||
'foaf:homepage' => 'https://github.com/dlongley'
|
'foaf:homepage' => 'https://github.com/dlongley'
|
||||||
),
|
],
|
||||||
'dc:date' => array(
|
'dc:date' => [
|
||||||
'@value' => gmdate('Y-m-d'),
|
'@value' => gmdate('Y-m-d'),
|
||||||
'@type' => 'xsd:date'
|
'@type' => 'xsd:date'
|
||||||
),
|
],
|
||||||
'subjectOf' => array()
|
'subjectOf' => []
|
||||||
);
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -479,7 +639,8 @@ class EarlReport extends PHPUnit_Util_Printer
|
||||||
*
|
*
|
||||||
* @param PHPUnit_Framework_Test $result the result to attach to.
|
* @param PHPUnit_Framework_Test $result the result to attach to.
|
||||||
*/
|
*/
|
||||||
public function attach(PHPUnit_Framework_TestResult $result) {
|
public function attach(PHPUnit_Framework_TestResult $result)
|
||||||
|
{
|
||||||
if (!$this->attached && $this->filename) {
|
if (!$this->attached && $this->filename) {
|
||||||
$this->attached = true;
|
$this->attached = true;
|
||||||
$result->addListener($this);
|
$result->addListener($this);
|
||||||
|
@ -492,25 +653,28 @@ class EarlReport extends PHPUnit_Util_Printer
|
||||||
* @param JsonLdTest $test the JsonLdTest for the assertion is for.
|
* @param JsonLdTest $test the JsonLdTest for the assertion is for.
|
||||||
* @param bool $passed whether or not the test passed.
|
* @param bool $passed whether or not the test passed.
|
||||||
*/
|
*/
|
||||||
public function addAssertion($test, $passed) {
|
public function addAssertion($test, $passed)
|
||||||
$this->report->{'subjectOf'}[] = (object)array(
|
{
|
||||||
|
$this->report->{'subjectOf'}[] = (object) [
|
||||||
'@type' => 'earl:Assertion',
|
'@type' => 'earl:Assertion',
|
||||||
'earl:assertedBy' => $this->report->{'doap:developer'}->{'@id'},
|
'earl:assertedBy' => $this->report->{'doap:developer'}->{'@id'},
|
||||||
'earl:mode' => 'earl:automatic',
|
'earl:mode' => 'earl:automatic',
|
||||||
'earl:test' => $test->data->{'@id'},
|
'earl:test' => $test->data->{'@id'},
|
||||||
'earl:result' => (object)array(
|
'earl:result' => (object) [
|
||||||
'@type' => 'earl:TestResult',
|
'@type' => 'earl:TestResult',
|
||||||
'dc:date' => gmdate(DateTime::ISO8601),
|
'dc:date' => gmdate(DateTime::ISO8601),
|
||||||
'earl:outcome' => $passed ? 'earl:passed' : 'earl:failed'
|
'earl:outcome' => $passed ? 'earl:passed' : 'earl:failed'
|
||||||
)
|
]
|
||||||
);
|
];
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes this EARL report to a file.
|
* Writes this EARL report to a file.
|
||||||
*/
|
*/
|
||||||
public function flush() {
|
public function flush()
|
||||||
|
{
|
||||||
if ($this->filename) {
|
if ($this->filename) {
|
||||||
printf("\nWriting EARL report to: %s\n", $this->filename);
|
printf("\nWriting EARL report to: %s\n", $this->filename);
|
||||||
$fd = fopen($this->filename, 'w');
|
$fd = fopen($this->filename, 'w');
|
||||||
|
@ -519,22 +683,31 @@ class EarlReport extends PHPUnit_Util_Printer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function endTest(PHPUnit_Framework_Test $test, $time) {
|
public function endTest(PHPUnit_Framework_Test $test, $time)
|
||||||
|
{
|
||||||
$this->addAssertion($test->test, true);
|
$this->addAssertion($test->test, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addError(
|
public function addError(
|
||||||
PHPUnit_Framework_Test $test, Exception $e, $time) {
|
PHPUnit_Framework_Test $test, Exception $e, $time)
|
||||||
|
{
|
||||||
$this->addAssertion($test->test, false);
|
$this->addAssertion($test->test, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addFailure(
|
public function addFailure(
|
||||||
PHPUnit_Framework_Test $test,
|
PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
|
||||||
PHPUnit_Framework_AssertionFailedError $e, $time) {
|
{
|
||||||
$this->addAssertion($test->test, false);
|
$this->addAssertion($test->test, false);
|
||||||
if ($test->result->shouldStop()) {
|
if ($test->result->shouldStop()) {
|
||||||
|
if (isset($test->test->name)) {
|
||||||
|
$name = $test->test->name;
|
||||||
|
} else if (isset($test->test->label)) {
|
||||||
|
$name = $test->test->label;
|
||||||
|
} else {
|
||||||
|
$name = 'UNNAMED';
|
||||||
|
}
|
||||||
printf("\n\nFAILED\n");
|
printf("\n\nFAILED\n");
|
||||||
printf("Test: %s\n", $test->test->name);
|
printf("Test: %s\n", $name);
|
||||||
printf("Purpose: %s\n", $test->test->data->purpose);
|
printf("Purpose: %s\n", $test->test->data->purpose);
|
||||||
printf("EXPECTED: %s\n", Util::jsonldEncode($test->test->expected));
|
printf("EXPECTED: %s\n", Util::jsonldEncode($test->test->expected));
|
||||||
printf("ACTUAL: %s\n", Util::jsonldEncode($test->test->actual));
|
printf("ACTUAL: %s\n", Util::jsonldEncode($test->test->actual));
|
||||||
|
@ -542,41 +715,68 @@ class EarlReport extends PHPUnit_Util_Printer
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addIncompleteTest(
|
public function addIncompleteTest(
|
||||||
PHPUnit_Framework_Test $test, Exception $e, $time) {
|
PHPUnit_Framework_Test $test, Exception $e, $time)
|
||||||
|
{
|
||||||
$this->addAssertion($test->test, false);
|
$this->addAssertion($test->test, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addRiskyTest(
|
public function addRiskyTest(
|
||||||
PHPUnit_Framework_Test $test, Exception $e, $time) {}
|
PHPUnit_Framework_Test $test, Exception $e, $time)
|
||||||
public function addSkippedTest(
|
{
|
||||||
PHPUnit_Framework_Test $test, Exception $e, $time) {}
|
|
||||||
public function startTest(PHPUnit_Framework_Test $test) {}
|
|
||||||
public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {}
|
|
||||||
public function endTestSuite(PHPUnit_Framework_TestSuite $suite) {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Util {
|
public function addSkippedTest(
|
||||||
public static function readFile($filename) {
|
PHPUnit_Framework_Test $test, Exception $e, $time)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function startTest(PHPUnit_Framework_Test $test)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Util
|
||||||
|
{
|
||||||
|
public static function readFile($filename)
|
||||||
|
{
|
||||||
$rval = @file_get_contents($filename);
|
$rval = @file_get_contents($filename);
|
||||||
if ($rval === false) {
|
if ($rval === false) {
|
||||||
throw new Exception('File read error: ' . $filename);
|
throw new Exception('File read error: ' . $filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $rval;
|
return $rval;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function readJson($filename) {
|
public static function readJson($filename)
|
||||||
|
{
|
||||||
$rval = json_decode(self::readFile($filename));
|
$rval = json_decode(self::readFile($filename));
|
||||||
if ($rval === null) {
|
if ($rval === null) {
|
||||||
throw new Exception('JSON parse error');
|
throw new Exception('JSON parse error');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $rval;
|
return $rval;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function readNQuads($filename) {
|
public static function readNQuads($filename)
|
||||||
|
{
|
||||||
return self::readFile($filename);
|
return self::readFile($filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function jsonldEncode($input) {
|
public static function jsonldEncode($input)
|
||||||
|
{
|
||||||
// newer PHP has a flag to avoid escaped '/'
|
// newer PHP has a flag to avoid escaped '/'
|
||||||
if (defined('JSON_UNESCAPED_SLASHES')) {
|
if (defined('JSON_UNESCAPED_SLASHES')) {
|
||||||
$options = JSON_UNESCAPED_SLASHES;
|
$options = JSON_UNESCAPED_SLASHES;
|
||||||
|
@ -588,21 +788,22 @@ class Util {
|
||||||
// use a simple string replacement of '\/' to '/'.
|
// use a simple string replacement of '\/' to '/'.
|
||||||
$json = str_replace('\\/', '/', json_encode($input));
|
$json = str_replace('\\/', '/', json_encode($input));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $json;
|
return $json;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// tests to skip
|
// tests to skip
|
||||||
$SKIP_TESTS = array();
|
$SKIP_TESTS = [];
|
||||||
|
|
||||||
// root manifest directory
|
// root manifest directory
|
||||||
$ROOT_MANIFEST_DIR;
|
$ROOT_MANIFEST_DIR;
|
||||||
|
|
||||||
// parsed tests; keyed by type
|
// parsed tests; keyed by type
|
||||||
$TESTS = array();
|
$TESTS = [];
|
||||||
|
|
||||||
// parsed command line options
|
// parsed command line options
|
||||||
$OPTIONS = array();
|
$OPTIONS = [];
|
||||||
|
|
||||||
// parse command line options
|
// parse command line options
|
||||||
global $argv;
|
global $argv;
|
||||||
|
@ -623,11 +824,13 @@ for($i = 0; $i < $total; ++$i) {
|
||||||
$i += 1;
|
$i += 1;
|
||||||
$OPTIONS[$arg] = $args[$i];
|
$OPTIONS[$arg] = $args[$i];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($OPTIONS['-d'])) {
|
if (!isset($OPTIONS['-d'])) {
|
||||||
$dvar = 'path to json-ld.org/test-suite';
|
$dvar = 'path to json-ld.org/test-suite';
|
||||||
$evar = 'file to write EARL report to';
|
$evar = 'file to write EARL report to';
|
||||||
echo "php-json-ld Tests\n";
|
echo "php-json-ld Tests\n";
|
||||||
echo "Usage: phpunit test.php -d <$dvar> [-e <$evar>]\n\n";
|
echo "Usage: phpunit test.php -d <$dvar> [-e <$evar>]\n\n";
|
||||||
|
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -640,7 +843,7 @@ if(isset($OPTIONS['-e'])) {
|
||||||
// load root manifest
|
// load root manifest
|
||||||
$ROOT_MANIFEST_DIR = realpath($OPTIONS['-d']);
|
$ROOT_MANIFEST_DIR = realpath($OPTIONS['-d']);
|
||||||
$filename = join(
|
$filename = join(
|
||||||
DIRECTORY_SEPARATOR, array($ROOT_MANIFEST_DIR, 'manifest.jsonld'));
|
DIRECTORY_SEPARATOR, [$ROOT_MANIFEST_DIR, 'manifest.jsonld']);
|
||||||
$root_manifest = Util::readJson($filename);
|
$root_manifest = Util::readJson($filename);
|
||||||
$manifest = new JsonLdManifest($root_manifest, $filename);
|
$manifest = new JsonLdManifest($root_manifest, $filename);
|
||||||
$manifest->load($TESTS);
|
$manifest->load($TESTS);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue