1 | <?PHP
|
---|
2 |
|
---|
3 | /* RDFInt.php - RDF Interfaces for PHP
|
---|
4 | * Copyright 2011 netlabs.org
|
---|
5 | * Author: Christian Langanke, Adrian Gschwend
|
---|
6 | *
|
---|
7 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
8 | * you may not use this file except in compliance with the License.
|
---|
9 | * You may obtain a copy of the License at
|
---|
10 | *
|
---|
11 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
12 | *
|
---|
13 | * Unless required by applicable law or agreed to in writing, software
|
---|
14 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
16 | * See the License for the specific language governing permissions and
|
---|
17 | * limitations under the License.
|
---|
18 | */
|
---|
19 |
|
---|
20 | // load library setup
|
---|
21 | define( 'RDFINT', getenv( 'PHP_LIBROOT_RDFINT'));
|
---|
22 | require_once( RDFINT.'/rdfa.php');
|
---|
23 |
|
---|
24 | class UnitTest extends PHPUnit_Framework_TestCase {
|
---|
25 |
|
---|
26 | static protected $baseuri;
|
---|
27 | private $testcase;
|
---|
28 | const BASEURI = 'http://www.w3.org/2000/10/rdf-tests/rdfcore';
|
---|
29 |
|
---|
30 | // --------------------------------------------------------
|
---|
31 |
|
---|
32 | public static function setUpBeforeClass() {
|
---|
33 | $testcase = trim( getenv( 'TESTCASE'));
|
---|
34 | echo "Executing RDF testcase: $testcase\n\n";
|
---|
35 | }
|
---|
36 |
|
---|
37 | // --------------------------------------------------------
|
---|
38 |
|
---|
39 | protected function setUp() {
|
---|
40 | }
|
---|
41 |
|
---|
42 | // --------------------------------------------------------
|
---|
43 |
|
---|
44 | private function _reIdentifyBlankNode( $data) {
|
---|
45 | return preg_replace( '/_:[^ ]*/', '_:a', $data);
|
---|
46 | }
|
---|
47 |
|
---|
48 | // --------------------------------------------------------
|
---|
49 |
|
---|
50 | private function _writeErrorData( $testcase,
|
---|
51 | $source, $sourceuri,
|
---|
52 | $result, $resulturi) {
|
---|
53 | if (!is_dir( $testcase))
|
---|
54 | mkdir( $testcase, NULL, true);
|
---|
55 |
|
---|
56 | $orgsource = file_get_contents( $sourceuri);
|
---|
57 | file_put_contents ( "$testcase/org_source.txt" , $orgsource);
|
---|
58 |
|
---|
59 | $orgresult = file_get_contents( $resulturi);
|
---|
60 | file_put_contents ( "$testcase/org_result.txt" , $orgresult);
|
---|
61 |
|
---|
62 | file_put_contents ( "$testcase/parsed_source.txt" , $source);
|
---|
63 | file_put_contents ( "$testcase/parsed_result.txt" , $result);
|
---|
64 | }
|
---|
65 |
|
---|
66 | // --------------------------------------------------------
|
---|
67 |
|
---|
68 | private function _getSerializedData( $uri) {
|
---|
69 |
|
---|
70 | $rdfaData = new \rdfa\Data();
|
---|
71 | $this->assertInstanceOf( '\rdfa\Data', $rdfaData);
|
---|
72 | $rdfaData->parse( $uri);
|
---|
73 | return $rdfaData->_serialize( 'n3');
|
---|
74 |
|
---|
75 | } // private function _getSerializedData
|
---|
76 |
|
---|
77 | // --------------------------------------------------------
|
---|
78 |
|
---|
79 | public function test() {
|
---|
80 |
|
---|
81 | // get testcase name again in object context
|
---|
82 | $testcase = trim( getenv( 'TESTCASE'));
|
---|
83 | $this->assertGreaterThan( 0, strlen( $testcase));
|
---|
84 |
|
---|
85 | // read source data
|
---|
86 | $sourceuri = self::BASEURI."/$testcase.rdf";
|
---|
87 | $source = $this->_getSerializedData( $sourceuri);
|
---|
88 | $this->assertEquals( ($source !== false), true);
|
---|
89 |
|
---|
90 | // read target data
|
---|
91 | $resulturi = self::BASEURI."/$testcase.nt";
|
---|
92 | $result = $this->_getSerializedData( $resulturi);
|
---|
93 | $this->assertEquals( ($result !== false), true);
|
---|
94 |
|
---|
95 | // rework blank node identifiers for coparison
|
---|
96 | $source = $this->_reIdentifyBlankNode( $source);
|
---|
97 | $result = $this->_reIdentifyBlankNode( $result);
|
---|
98 |
|
---|
99 |
|
---|
100 | // check if results are equal
|
---|
101 | $success = ($source == $result);
|
---|
102 |
|
---|
103 | // in case of error, write serialized data to subdirectory
|
---|
104 | if (!$success)
|
---|
105 | $this->_writeErrorData( $testcase,
|
---|
106 | $source, $sourceuri,
|
---|
107 | $result, $resulturi);
|
---|
108 |
|
---|
109 | // compare source and target data
|
---|
110 | $this->assertEquals( $success, true);
|
---|
111 |
|
---|
112 |
|
---|
113 | } // public function test_parse_sparql()
|
---|
114 |
|
---|
115 | } // class UnitTest
|
---|
116 |
|
---|
117 |
|
---|