source: php/trunk/unittest/rdftestcases/unittest.php@ 58

Last change on this file since 58 was 58, checked in by cla, 14 years ago

rdfint

  • reworked handling of RDF testcase results, results are now also ordered before comparing
  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
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
21define( 'RDFINT', getenv( 'PHP_LIBROOT_RDFINT'));
22require_once( RDFINT.'/rdfa.php');
23
24class 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 _refineResult( $data) {
45 // replace all blank nodes by one ID
46 // NOTE: thi sis not accurate, but not much better
47 // possible without access to the triple data
48 $data = preg_replace( '/_:[^ ]*/', '_:a', $data);
49
50 // sort the data
51 $adata = explode( "\n", $data);
52 sort( &$adata);
53 $data = implode( "\n", $adata);
54
55 return $data;
56 }
57
58 // --------------------------------------------------------
59
60 private function _writeErrorData( $testcase,
61 $source, $sourceuri,
62 $result, $resulturi) {
63 if (!is_dir( $testcase))
64 mkdir( $testcase, NULL, true);
65
66 $orgsource = file_get_contents( $sourceuri);
67 file_put_contents ( "$testcase/org_source.txt" , $orgsource);
68
69 $orgresult = file_get_contents( $resulturi);
70 file_put_contents ( "$testcase/org_result.txt" , $orgresult);
71
72 file_put_contents ( "$testcase/parsed_source.txt" , $source);
73 file_put_contents ( "$testcase/parsed_result.txt" , $result);
74 }
75
76 // --------------------------------------------------------
77
78 private function _getSerializedData( $uri) {
79
80 $rdfaData = new \rdfa\Data();
81 $this->assertInstanceOf( '\rdfa\Data', $rdfaData);
82 $rdfaData->parse( $uri);
83 return $rdfaData->_serialize( 'n3');
84
85 } // private function _getSerializedData
86
87 // --------------------------------------------------------
88
89 public function test() {
90
91 // get testcase name again in object context
92 $testcase = trim( getenv( 'TESTCASE'));
93 $this->assertGreaterThan( 0, strlen( $testcase));
94
95 // read source data
96 $sourceuri = self::BASEURI."/$testcase.rdf";
97 $source = $this->_getSerializedData( $sourceuri);
98 $this->assertEquals( ($source !== false), true);
99
100 // read target data
101 $resulturi = self::BASEURI."/$testcase.nt";
102 $result = $this->_getSerializedData( $resulturi);
103 $this->assertEquals( ($result !== false), true);
104
105 // resolve problems with blnak node identifiers
106 // and different sorting of triples
107 $source = $this->_refineResult( $source);
108 $result = $this->_refineResult( $result);
109
110 // check if results are equal
111 $success = ($source == $result);
112
113 // in case of error, write serialized data to subdirectory
114 if (!$success)
115 $this->_writeErrorData( $testcase,
116 $source, $sourceuri,
117 $result, $resulturi);
118
119 // compare source and target data
120 $this->assertEquals( $success, true);
121
122
123 } // public function test_parse_sparql()
124
125} // class UnitTest
126
127
Note: See TracBrowser for help on using the repository browser.