aboutsummaryrefslogblamecommitdiffstats
path: root/library/jsonld/test.php
blob: 11b72ac09039a1b4ff030c707e4e0397f7a0e6ef (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765




























































































































































































































































































































































































































































































































































































































































































































































































                                                                                           
<?php
/**
 * PHP unit tests for JSON-LD.
 *
 * @author Dave Longley
 *
 * Copyright (c) 2013-2014 Digital Bazaar, Inc. All rights reserved.
 */
require_once('jsonld.php');

class JsonLdTestCase extends PHPUnit_Framework_TestCase {
  /**
   * Runs this test case. Overridden to attach to EARL report w/o need for
   * an external XML configuration file.
   *
   * @param PHPUnit_Framework_TestResult $result the test result.
   */
  public function run(PHPUnit_Framework_TestResult $result = NULL) {
    global $EARL;
    $EARL->attach($result);
    $this->result = $result;
    parent::run($result);
  }

  /**
   * Tests expansion.
   *
   * @param JsonLdTest $test the test to run.
   *
   * @group expand
   * @group json-ld.org
   * @dataProvider expandProvider
   */
  public function testExpand($test) {
    $this->test = $test;
    $input = $test->readUrl('input');
    $options = $test->createOptions();
    $test->run('jsonld_expand', array($input, $options));
  }

  /**
   * Tests compaction.
   *
   * @param JsonLdTest $test the test to run.
   *
   * @group compact
   * @group json-ld.org
   * @dataProvider compactProvider
   */
  public function testCompact($test) {
    $this->test = $test;
    $input = $test->readUrl('input');
    $context = $test->readProperty('context');
    $options = $test->createOptions();
    $test->run('jsonld_compact', array($input, $context, $options));
  }

  /**
   * Tests flatten.
   *
   * @param JsonLdTest $test the test to run.
   *
   * @group flatten
   * @group json-ld.org
   * @dataProvider flattenProvider
   */
  public function testFlatten($test) {
    $this->test = $test;
    $input = $test->readUrl('input');
    $context = $test->readProperty('context');
    $options = $test->createOptions();
    $test->run('jsonld_flatten', array($input, $context, $options));
  }

  /**
   * Tests serialization to RDF.
   *
   * @param JsonLdTest $test the test to run.
   *
   * @group toRdf
   * @group json-ld.org
   * @dataProvider toRdfProvider
   */
  public function testToRdf($test) {
    $this->test = $test;
    $input = $test->readUrl('input');
    $options = $test->createOptions(array('format' => 'application/nquads'));
    $test->run('jsonld_to_rdf', array($input, $options));
  }

  /**
   * Tests deserialization from RDF.
   *
   * @param JsonLdTest $test the test to run.
   *
   * @group fromRdf
   * @group json-ld.org
   * @dataProvider fromRdfProvider
   */
  public function testFromRdf($test) {
    $this->test = $test;
    $input = $test->readProperty('input');
    $options = $test->createOptions(array('format' => 'application/nquads'));
    $test->run('jsonld_from_rdf', array($input, $options));
  }

  /**
   * Tests framing.
   *
   * @param JsonLdTest $test the test to run.
   *
   * @group frame
   * @group json-ld.org
   * @dataProvider frameProvider
   */
  public function testFrame($test) {
    $this->test = $test;
    $input = $test->readUrl('input');
    $frame = $test->readProperty('frame');
    $options = $test->createOptions();
    $test->run('jsonld_frame', array($input, $frame, $options));
  }

  /**
   * Tests normalization.
   *
   * @param JsonLdTest $test the test to run.
   *
   * @group normalize
   * @group json-ld.org
   * @dataProvider normalizeProvider
   */
  public function testNormalize($test) {
    $this->test = $test;
    $input = $test->readUrl('input');
    $options = $test->createOptions(array('format' => 'application/nquads'));
    $test->run('jsonld_normalize', array($input, $options));
  }

  /**
   * 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(array(
      'algorithm' => 'URGNA2012',
      'inputFormat' => 'application/nquads',
      'format' => 'application/nquads'));
    $test->run('jsonld_normalize', array($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(array(
      'algorithm' => 'URDNA2015',
      'inputFormat' => 'application/nquads',
      'format' => 'application/nquads'));
    $test->run('jsonld_normalize', array($input, $options));
  }

  public function expandProvider() {
    return new JsonLdTestIterator('jld:ExpandTest');
  }

  public function compactProvider() {
    return new JsonLdTestIterator('jld:CompactTest');
  }

  public function flattenProvider() {
    return new JsonLdTestIterator('jld:FlattenTest');
  }

  public function toRdfProvider() {
    return new JsonLdTestIterator('jld:ToRDFTest');
  }

  public function fromRdfProvider() {
    return new JsonLdTestIterator('jld:FromRDFTest');
  }

  public function normalizeProvider() {
    return new JsonLdTestIterator('jld:NormalizeTest');
  }

  public function frameProvider() {
    return new JsonLdTestIterator('jld:FrameTest');
  }

  public function urgna2012Provider() {
    return new JsonLdTestIterator('rdfn:Urgna2012EvalTest');
  }

  public function urdna2015Provider() {
    return new JsonLdTestIterator('rdfn:Urdna2015EvalTest');
  }
}

class JsonLdManifest {
  public function __construct($data, $filename) {
    $this->data = $data;
    $this->filename = $filename;
    $this->dirname = dirname($filename);
  }

  public function load(&$tests) {
    $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)) {
        $filename = join(
          DIRECTORY_SEPARATOR, array($this->dirname, $entry));
        $entry = Util::readJson($filename);
      } else {
        $filename = $this->filename;
      }

      if(JsonLdProcessor::hasValue($entry, '@type', 'mf:Manifest') ||
        JsonLdProcessor::hasValue($entry, 'type', 'mf:Manifest')) {
        // entry is another manifest
        $manifest = new JsonLdManifest($entry, $filename);
        $manifest->load($tests);
      } else {
        // assume entry is a test
        $test = new JsonLdTest($this, $entry, $filename);
        $types = array_merge(
           JsonLdProcessor::getValues($test->data, '@type'),
           JsonLdProcessor::getValues($test->data, 'type'));
        foreach($types as $type) {
          if(!isset($tests[$type])) {
            $tests[$type] = array();
          }
          $tests[$type][] = $test;
        }
      }
    }
  }
}

class JsonLdTest {
  public function __construct($manifest, $data, $filename) {
    $this->manifest = $manifest;
    $this->data = $data;
    $this->filename = $filename;
    $this->dirname = dirname($filename);
    $this->isPositive =
      JsonLdProcessor::hasValue(
        $data, '@type', 'jld:PositiveEvaluationTest') ||
      JsonLdProcessor::hasValue(
        $data, 'type', 'jld:PositiveEvaluationTest');
    $this->isNegative =
      JsonLdProcessor::hasValue(
        $data, '@type', 'jld:NegativeEvaluationTest') ||
      JsonLdProcessor::hasValue(
        $data, 'type', 'jld:NegativeEvaluationTest');

    // generate test name
    if(isset($manifest->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
    if(isset($manifest->data->baseIri)) {
      $data->{'@id'} = ($manifest->data->baseIri .
        basename($manifest->filename) . $data->{'@id'});
      $this->base = $manifest->data->baseIri . $data->input;
    }
  }

  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
    if($this->isNegative) {
      $this->expected = $this->data->expect;
    } else {
      $this->expected = $this->readProperty($this->_getResultProperty());
    }

    try {
      $this->actual = call_user_func_array($fn, $params);
      if($this->isNegative) {
        throw new Exception('Expected an error; one was not raised.');
      }
      PHPUnit_Framework_TestCase::assertEquals($this->expected, $this->actual);
    } catch(Exception $e) {
     // assume positive test
      if($this->isNegative) {
        $this->actual = $this->getJsonLdErrorCode($e);
        PHPUnit_Framework_TestCase::assertEquals(
          $this->expected, $this->actual);
      } else {
        throw $e;
      }
    }
  }

  public function readUrl($property) {
    if(!property_exists($this->data, $property)) {
      return null;
    }
    return $this->manifest->data->baseIri . $this->data->{$property};
  }

  public function readProperty($property) {
    $data = $this->data;
    if(!property_exists($data, $property)) {
      return null;
    }
    $filename = join(
      DIRECTORY_SEPARATOR, array($this->dirname, $data->{$property}));
    $extension = pathinfo($filename, PATHINFO_EXTENSION);
    if($extension === 'jsonld') {
      return Util::readJson($filename);
    }
    return Util::readFile($filename);
  }

  public function createOptions($opts=array()) {
    $http_options = array(
      'contentType', 'httpLink', 'httpStatus', 'redirectTo');
    $test_options = (property_exists($this->data, 'option') ?
      $this->data->option : array());
    $options = array();
    foreach($test_options as $k => $v) {
      if(!in_array($k, $http_options)) {
        $options[$k] = $v;
      }
    }
    $options['documentLoader'] = $this->createDocumentLoader();
    $options = array_merge($options, $opts);
    if(isset($options['expandContext'])) {
      $filename = join(
        DIRECTORY_SEPARATOR, array($this->dirname, $options['expandContext']));
      $options['expandContext'] = Util::readJson($filename);
    }
    return $options;
  }

  public function createDocumentLoader() {
    $base = 'http://json-ld.org/test-suite';
    $test = $this;

    $load_locally = function($url) use ($test, $base) {
      $doc = (object)array(
        'contextUrl' => null, 'documentUrl' => $url, 'document' => null);
      $options = (property_exists($test->data, 'option') ?
        $test->data->option : null);
      if($options and $url === $test->base) {
        if(property_exists($options, 'redirectTo') &&
          property_exists($options, 'httpStatus') &&
          $options->httpStatus >= '300') {
          $doc->documentUrl = ($test->manifest->data->baseIri .
            $options->redirectTo);
        } else if(property_exists($options, 'httpLink')) {
          $content_type = (property_exists($options, 'contentType') ?
            $options->contentType : null);
          $extension = pathinfo($url, PATHINFO_EXTENSION);
          if(!$content_type && $extension === 'jsonld') {
            $content_type = 'application/ld+json';
          }
          $link_header = $options->httpLink;
          if(is_array($link_header)) {
            $link_header = join(',', $link_header);
          }
          $link_header = jsonld_parse_link_header($link_header);
          if(isset($link_header['http://www.w3.org/ns/json-ld#context'])) {
            $link_header = $link_header['http://www.w3.org/ns/json-ld#context'];
          } else {
            $link_header = null;
          }
          if($link_header && $content_type !== 'application/ld+json') {
            if(is_array($link_header)) {
              throw new Exception('multiple context link headers');
            }
            $doc->contextUrl = $link_header->target;
          }
        }
      }
      global $ROOT_MANIFEST_DIR;
      if(strpos($doc->documentUrl, ':') === false) {
        $filename = join(
          DIRECTORY_SEPARATOR, array(
            $ROOT_MANIFEST_DIR, $doc->documentUrl));
        $doc->documentUrl = 'file://' . $filename;
      } else {
        $filename = join(
          DIRECTORY_SEPARATOR, array(
            $ROOT_MANIFEST_DIR, substr($doc->documentUrl, strlen($base))));
      }
      try {
        $doc->document = Util::readJson($filename);
      } catch(Exception $e) {
        throw new Exception('loading document failed');
      }
      return $doc;
    };

    $local_loader = function($url) use ($test, $base, $load_locally) {
      // always load remote-doc and non-base tests remotely
      if((strpos($url, $base) !== 0 && strpos($url, ':') !== false) ||
        $test->manifest->data->name === 'Remote document') {
        return call_user_func('jsonld_default_document_loader', $url);
      }

      // attempt to load locally
      return call_user_func($load_locally, $url);
    };

    return $local_loader;
  }

  public function getJsonLdErrorCode($err) {
    if($err instanceof JsonLdException) {
      if($err->getCode()) {
        return $err->getCode();
      }
      if($err->cause) {
        return $this->getJsonLdErrorCode($err->cause);
      }
    }
    return $err->getMessage();
  }
}

class JsonLdTestIterator implements Iterator {
  /**
   * The current test index.
   */
  protected $index = 0;

  /**
   * The total number of tests.
   */
  protected $count = 0;

  /**
   * Creates a TestIterator.
   *
   * @param string $type the type of tests to iterate over.
   */
  public function __construct($type) {
    global $TESTS;
    if(isset($TESTS[$type])) {
      $this->tests = $TESTS[$type];
    } else {
      $this->tests = array();
    }
    $this->count = count($this->tests);
  }

  /**
   * Gets the parameters for the next test.
   *
   * @return assoc the parameters for the next test.
   */
  public function current() {
    return array('test' => $this->tests[$this->index]);
  }

  /**
   * Gets the current test number.
   *
   * @return int the current test number.
   */
  public function key() {
    return $this->index;
  }

  /**
   * Proceeds to the next test.
   */
  public function next() {
    $this->index += 1;
  }

  /**
   * Rewinds to the first test.
   */
  public function rewind() {
    $this->index = 0;
  }

  /**
   * Returns true if there are more tests to be run.
   *
   * @return bool true if there are more tests to be run.
   */
  public function valid() {
    return $this->index < $this->count;
  }
}

class EarlReport extends PHPUnit_Util_Printer
  implements PHPUnit_Framework_TestListener {
  public function __construct() {
    $this->filename = null;
    $this->attached = false;
    $this->report = (object)array(
      '@context' => (object)array(
        'doap' => 'http://usefulinc.com/ns/doap#',
        'foaf' => 'http://xmlns.com/foaf/0.1/',
        'dc' => 'http://purl.org/dc/terms/',
        'earl' => 'http://www.w3.org/ns/earl#',
        'xsd' => 'http://www.w3.org/2001/XMLSchema#',
        'doap:homepage' => (object)array('@type' => '@id'),
        'doap:license' => (object)array('@type' => '@id'),
        'dc:creator' => (object)array('@type' => '@id'),
        'foaf:homepage' => (object)array('@type' => '@id'),
        'subjectOf' => (object)array('@reverse' => 'earl:subject'),
        'earl:assertedBy' => (object)array('@type' => '@id'),
        'earl:mode' => (object)array('@type' => '@id'),
        'earl:test' => (object)array('@type' => '@id'),
        'earl:outcome' => (object)array('@type' => '@id'),
        'dc:date' => (object)array('@type' => 'xsd:date')
      ),
      '@id' => 'https://github.com/digitalbazaar/php-json-ld',
      '@type' => array('doap:Project', 'earl:TestSubject', 'earl:Software'),
      'doap:name' => 'php-json-ld',
      'dc:title' => 'php-json-ld',
      'doap:homepage' => 'https://github.com/digitalbazaar/php-json-ld',
      'doap:license' => 'https://github.com/digitalbazaar/php-json-ld/blob/master/LICENSE',
      'doap:description' => 'A JSON-LD processor for PHP',
      'doap:programming-language' => 'PHP',
      'dc:creator' => 'https://github.com/dlongley',
      'doap:developer' => (object)array(
        '@id' => 'https://github.com/dlongley',
        '@type' => array('foaf:Person', 'earl:Assertor'),
        'foaf:name' => 'Dave Longley',
        'foaf:homepage' => 'https://github.com/dlongley'
      ),
      'dc:date' => array(
        '@value' => gmdate('Y-m-d'),
        '@type' => 'xsd:date'
      ),
      'subjectOf' => array()
    );
  }

  /**
   * Attaches to the given test result, if not yet attached.
   *
   * @param PHPUnit_Framework_Test $result the result to attach to.
   */
  public function attach(PHPUnit_Framework_TestResult $result) {
    if(!$this->attached && $this->filename) {
      $this->attached = true;
      $result->addListener($this);
    }
  }

  /**
   * Adds an assertion to this EARL report.
   *
   * @param JsonLdTest $test the JsonLdTest for the assertion is for.
   * @param bool $passed whether or not the test passed.
   */
  public function addAssertion($test, $passed) {
    $this->report->{'subjectOf'}[] = (object)array(
      '@type' => 'earl:Assertion',
      'earl:assertedBy' => $this->report->{'doap:developer'}->{'@id'},
      'earl:mode' => 'earl:automatic',
      'earl:test' => $test->data->{'@id'},
      'earl:result' => (object)array(
        '@type' => 'earl:TestResult',
        'dc:date' => gmdate(DateTime::ISO8601),
        'earl:outcome' => $passed ? 'earl:passed' : 'earl:failed'
      )
    );
    return $this;
  }

  /**
   * Writes this EARL report to a file.
   */
  public function flush() {
    if($this->filename) {
      printf("\nWriting EARL report to: %s\n", $this->filename);
      $fd = fopen($this->filename, 'w');
      fwrite($fd, Util::jsonldEncode($this->report));
      fclose($fd);
    }
  }

  public function endTest(PHPUnit_Framework_Test $test, $time) {
    $this->addAssertion($test->test, true);
  }

  public function addError(
    PHPUnit_Framework_Test $test, Exception $e, $time) {
    $this->addAssertion($test->test, false);
  }

  public function addFailure(
    PHPUnit_Framework_Test $test,
    PHPUnit_Framework_AssertionFailedError $e, $time) {
    $this->addAssertion($test->test, false);
    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("Test: %s\n", $name);
      printf("Purpose: %s\n", $test->test->data->purpose);
      printf("EXPECTED: %s\n", Util::jsonldEncode($test->test->expected));
      printf("ACTUAL: %s\n", Util::jsonldEncode($test->test->actual));
    }
  }

  public function addIncompleteTest(
    PHPUnit_Framework_Test $test, Exception $e, $time) {
    $this->addAssertion($test->test, false);
  }

  public function addRiskyTest(
    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 static function readFile($filename) {
    $rval = @file_get_contents($filename);
    if($rval === false) {
      throw new Exception('File read error: ' . $filename);
    }
    return $rval;
  }

  public static function readJson($filename) {
    $rval = json_decode(self::readFile($filename));
    if($rval === null) {
      throw new Exception('JSON parse error');
    }
    return $rval;
  }

  public static function readNQuads($filename) {
    return self::readFile($filename);
  }

  public static function jsonldEncode($input) {
    // newer PHP has a flag to avoid escaped '/'
    if(defined('JSON_UNESCAPED_SLASHES')) {
      $options = JSON_UNESCAPED_SLASHES;
      if(defined('JSON_PRETTY_PRINT')) {
        $options |= JSON_PRETTY_PRINT;
      }
      $json = json_encode($input, $options);
    } else {
      // use a simple string replacement of '\/' to '/'.
      $json = str_replace('\\/', '/', json_encode($input));
    }
    return $json;
  }
}

// tests to skip
$SKIP_TESTS = array();

// root manifest directory
$ROOT_MANIFEST_DIR;

// parsed tests; keyed by type
$TESTS = array();

// parsed command line options
$OPTIONS = array();

// parse command line options
global $argv;
$args = $argv;
$total = count($args);
$start = false;
for($i = 0; $i < $total; ++$i) {
  $arg = $args[$i];
  if(!$start) {
    if(realpath($arg) === realpath(__FILE__)) {
      $start = true;
    }
    continue;
  }
  if($arg[0] !== '-') {
    break;
  }
  $i += 1;
  $OPTIONS[$arg] = $args[$i];
}
if(!isset($OPTIONS['-d'])) {
  $dvar = 'path to json-ld.org/test-suite';
  $evar = 'file to write EARL report to';
  echo "php-json-ld Tests\n";
  echo "Usage: phpunit test.php -d <$dvar> [-e <$evar>]\n\n";
  exit(0);
}

// EARL Report
$EARL = new EarlReport();
if(isset($OPTIONS['-e'])) {
  $EARL->filename = $OPTIONS['-e'];
}

// load root manifest
$ROOT_MANIFEST_DIR = realpath($OPTIONS['-d']);
$filename = join(
  DIRECTORY_SEPARATOR, array($ROOT_MANIFEST_DIR, 'manifest.jsonld'));
$root_manifest = Util::readJson($filename);
$manifest = new JsonLdManifest($root_manifest, $filename);
$manifest->load($TESTS);

/* end of file, omit ?> */