[7] | 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 |
|
---|
[27] | 20 | // load library setup
|
---|
| 21 | define( 'RDFINT', getenv( 'PHP_LIBROOT_RDFINT'));
|
---|
[64] | 22 | define( 'FDEBUG_SESSION_NAME', 'RDF Test Cases Unittest');
|
---|
[27] | 23 | require_once( RDFINT.'/rdfa.php');
|
---|
[64] | 24 | require_once( 'rdftestcases.php');
|
---|
[7] | 25 |
|
---|
| 26 | class UnitTest extends PHPUnit_Framework_TestCase {
|
---|
| 27 |
|
---|
[64] | 28 | static protected $manifest;
|
---|
[7] | 29 |
|
---|
| 30 | // --------------------------------------------------------
|
---|
| 31 |
|
---|
[64] | 32 | // rune once initializer
|
---|
[7] | 33 | public static function setUpBeforeClass() {
|
---|
[65] | 34 |
|
---|
| 35 | // create manifest data object
|
---|
[64] | 36 | self::$manifest = new RDFTestCasesManifest;
|
---|
[65] | 37 |
|
---|
[7] | 38 | }
|
---|
| 39 |
|
---|
| 40 | // --------------------------------------------------------
|
---|
| 41 |
|
---|
[64] | 42 | // helper: adjusts serialization results so that comparison can work
|
---|
| 43 | // - make all blank node identifiers unique
|
---|
| 44 | // - sort the n-triple lines
|
---|
[58] | 45 | private function _refineResult( $data) {
|
---|
| 46 | // replace all blank nodes by one ID
|
---|
| 47 | // NOTE: thi sis not accurate, but not much better
|
---|
| 48 | // possible without access to the triple data
|
---|
| 49 | $data = preg_replace( '/_:[^ ]*/', '_:a', $data);
|
---|
| 50 |
|
---|
| 51 | // sort the data
|
---|
| 52 | $adata = explode( "\n", $data);
|
---|
| 53 | sort( &$adata);
|
---|
| 54 | $data = implode( "\n", $adata);
|
---|
| 55 |
|
---|
| 56 | return $data;
|
---|
[57] | 57 | }
|
---|
| 58 |
|
---|
| 59 | // --------------------------------------------------------
|
---|
[64] | 60 | // helper: write input and output data to new output
|
---|
| 61 | // directory for manual examination
|
---|
[65] | 62 | private function _writeErrorData( $testcase, $source, $result) {
|
---|
| 63 | if (!is_dir( $testcase->name))
|
---|
| 64 | mkdir( $testcase->name, NULL, true);
|
---|
[56] | 65 |
|
---|
[65] | 66 | $orgsource = file_get_contents( $testcase->input);
|
---|
| 67 | file_put_contents ( $testcase->name."/org_source.txt" , $orgsource);
|
---|
[56] | 68 |
|
---|
[65] | 69 | $orgresult = file_get_contents( $testcase->output);
|
---|
| 70 | file_put_contents ( $testcase->name."/org_result.txt" , $orgresult);
|
---|
[56] | 71 |
|
---|
[65] | 72 | file_put_contents ( $testcase->name."/parsed_source.txt" , $source);
|
---|
| 73 | file_put_contents ( $testcase->name."/parsed_result.txt" , $result);
|
---|
[56] | 74 | }
|
---|
| 75 |
|
---|
| 76 | // --------------------------------------------------------
|
---|
| 77 |
|
---|
[64] | 78 | // helper: serialize data from URI to N3
|
---|
[7] | 79 | private function _getSerializedData( $uri) {
|
---|
| 80 |
|
---|
| 81 | $rdfaData = new \rdfa\Data();
|
---|
| 82 | $this->assertInstanceOf( '\rdfa\Data', $rdfaData);
|
---|
| 83 | $rdfaData->parse( $uri);
|
---|
[17] | 84 | return $rdfaData->_serialize( 'n3');
|
---|
[7] | 85 |
|
---|
| 86 | } // private function _getSerializedData
|
---|
| 87 |
|
---|
[65] | 88 | // --------------------------------------------------------
|
---|
[64] | 89 |
|
---|
[65] | 90 | public function _executePositiveParserTest( $testcase) {
|
---|
| 91 |
|
---|
| 92 | // read source data
|
---|
| 93 | $source = $this->_getSerializedData( $testcase->input);
|
---|
| 94 | $this->assertEquals( ($source !== false), true);
|
---|
| 95 |
|
---|
| 96 | // read target data
|
---|
| 97 | $result = $this->_getSerializedData( $testcase->output);
|
---|
| 98 | $this->assertEquals( ($result !== false), true);
|
---|
| 99 |
|
---|
| 100 | // resolve problems with blnak node identifiers
|
---|
| 101 | // and different sorting of triples
|
---|
| 102 | $source = $this->_refineResult( $source);
|
---|
| 103 | $result = $this->_refineResult( $result);
|
---|
| 104 |
|
---|
| 105 | // check if results are equal
|
---|
| 106 | $success = ($source == $result);
|
---|
| 107 |
|
---|
| 108 | // in case of error, write serialized data to subdirectory
|
---|
| 109 | if (!$success)
|
---|
| 110 | $this->_writeErrorData( $testcase,
|
---|
| 111 | $source,
|
---|
| 112 | $result);
|
---|
| 113 |
|
---|
| 114 | // compare source and target data
|
---|
| 115 | $this->assertEquals( $success, true);
|
---|
| 116 |
|
---|
| 117 | } // public function test_parse_sparql()
|
---|
| 118 |
|
---|
| 119 |
|
---|
[7] | 120 | // --------------------------------------------------------
|
---|
| 121 |
|
---|
| 122 | public function test() {
|
---|
| 123 |
|
---|
[65] | 124 | $category = trim( getenv( 'CATEGORY'));
|
---|
| 125 | if ($category == '')
|
---|
| 126 | $categories = self::$manifest->getCategories();
|
---|
[64] | 127 | else
|
---|
[65] | 128 | $categories = array( $type);
|
---|
[64] | 129 |
|
---|
[65] | 130 | // run test
|
---|
| 131 | // NOTE: all assertions are made in the specific test routine
|
---|
| 132 | foreach ($categories as $category) {
|
---|
| 133 | echo "\nExecuting RDF Test Cases of category: $category\n\n";
|
---|
| 134 | $testcases = self::$manifest->getParserTests( $category);
|
---|
| 135 | foreach ($testcases as $testcase) {
|
---|
[64] | 136 |
|
---|
[65] | 137 | echo " Executing: " . $testcase->name . "\n";
|
---|
| 138 | switch ($category) {
|
---|
| 139 | case 'PositiveParserTest':
|
---|
| 140 | $result = $this->_executePositiveParserTest( $testcase);
|
---|
| 141 | break;
|
---|
| 142 | } // switch ($category)
|
---|
[64] | 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | } // public function test
|
---|
| 147 |
|
---|
| 148 | // --------------------------------------------------------
|
---|
| 149 |
|
---|
| 150 | public function disabled_test() {
|
---|
| 151 |
|
---|
[7] | 152 | // get testcase name again in object context
|
---|
| 153 | $testcase = trim( getenv( 'TESTCASE'));
|
---|
| 154 | $this->assertGreaterThan( 0, strlen( $testcase));
|
---|
| 155 |
|
---|
| 156 | // read source data
|
---|
[56] | 157 | $sourceuri = self::BASEURI."/$testcase.rdf";
|
---|
| 158 | $source = $this->_getSerializedData( $sourceuri);
|
---|
[7] | 159 | $this->assertEquals( ($source !== false), true);
|
---|
| 160 |
|
---|
| 161 | // read target data
|
---|
[56] | 162 | $resulturi = self::BASEURI."/$testcase.nt";
|
---|
| 163 | $result = $this->_getSerializedData( $resulturi);
|
---|
[7] | 164 | $this->assertEquals( ($result !== false), true);
|
---|
| 165 |
|
---|
[58] | 166 | // resolve problems with blnak node identifiers
|
---|
| 167 | // and different sorting of triples
|
---|
| 168 | $source = $this->_refineResult( $source);
|
---|
| 169 | $result = $this->_refineResult( $result);
|
---|
[57] | 170 |
|
---|
[56] | 171 | // check if results are equal
|
---|
| 172 | $success = ($source == $result);
|
---|
| 173 |
|
---|
| 174 | // in case of error, write serialized data to subdirectory
|
---|
| 175 | if (!$success)
|
---|
[64] | 176 | $this->_writeErrorData( $testcase,
|
---|
| 177 | $source, $sourceuri,
|
---|
[56] | 178 | $result, $resulturi);
|
---|
| 179 |
|
---|
[7] | 180 | // compare source and target data
|
---|
[56] | 181 | $this->assertEquals( $success, true);
|
---|
[7] | 182 |
|
---|
| 183 |
|
---|
| 184 | } // public function test_parse_sparql()
|
---|
| 185 |
|
---|
[64] | 186 |
|
---|
| 187 | // --------------------------------------------------------
|
---|
| 188 |
|
---|
| 189 | public function _dumpTestcaseInfo( $testcases) {
|
---|
| 190 | foreach ($testcases as $testcase) {
|
---|
| 191 | echo "=====================================================================\n";
|
---|
| 192 | echo 'URI: ' . $testcase->getSubject() . "\n";
|
---|
| 193 | echo 'Input: ' . $testcase->input . "\n";
|
---|
| 194 | echo 'Output: ' . $testcase->output . "\n";
|
---|
[65] | 195 | echo 'Conclusion: ' . $testcase->conclusion . "\n";
|
---|
[64] | 196 | }
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 |
|
---|
[7] | 200 | } // class UnitTest
|
---|
| 201 |
|
---|
| 202 |
|
---|