| 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 | define( 'FDEBUG_SESSION_NAME', 'RDF Test Cases Unittest');
 | 
|---|
| 23 | require_once( RDFINT.'/rdfa.php');
 | 
|---|
| 24 | require_once( 'rdftestcases.php');
 | 
|---|
| 25 | 
 | 
|---|
| 26 | class UnitTest extends PHPUnit_Framework_TestCase {
 | 
|---|
| 27 | 
 | 
|---|
| 28 |   static protected $manifest;
 | 
|---|
| 29 | 
 | 
|---|
| 30 |   // --------------------------------------------------------
 | 
|---|
| 31 | 
 | 
|---|
| 32 |   // rune once initializer
 | 
|---|
| 33 |   public static function setUpBeforeClass() {
 | 
|---|
| 34 | 
 | 
|---|
| 35 |     // create manifest data object
 | 
|---|
| 36 |     self::$manifest = new RDFTestCasesManifest;
 | 
|---|
| 37 | 
 | 
|---|
| 38 |   }
 | 
|---|
| 39 | 
 | 
|---|
| 40 |   // --------------------------------------------------------
 | 
|---|
| 41 | 
 | 
|---|
| 42 |   // helper: adjusts serialization results so that comparison can work
 | 
|---|
| 43 |   // - make all blank node identifiers unique
 | 
|---|
| 44 |   // - sort the n-triple lines
 | 
|---|
| 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 |   }
 | 
|---|
| 58 | 
 | 
|---|
| 59 |   // --------------------------------------------------------
 | 
|---|
| 60 |   // helper: write input and output data to new output
 | 
|---|
| 61 |   // directory for manual examination
 | 
|---|
| 62 |   private function _writeErrorData( $testcase, $source, $result) {
 | 
|---|
| 63 |     if (!is_dir( $testcase->name))
 | 
|---|
| 64 |       mkdir( $testcase->name, NULL, true);
 | 
|---|
| 65 | 
 | 
|---|
| 66 |     $orgsource = file_get_contents( $testcase->input);
 | 
|---|
| 67 |     file_put_contents ( $testcase->name."/org_source.txt" , $orgsource);
 | 
|---|
| 68 | 
 | 
|---|
| 69 |     $orgresult = file_get_contents( $testcase->output);
 | 
|---|
| 70 |     file_put_contents ( $testcase->name."/org_result.txt" , $orgresult);
 | 
|---|
| 71 | 
 | 
|---|
| 72 |     file_put_contents ( $testcase->name."/parsed_source.txt" , $source);
 | 
|---|
| 73 |     file_put_contents ( $testcase->name."/parsed_result.txt" , $result);
 | 
|---|
| 74 |   }
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   // --------------------------------------------------------
 | 
|---|
| 77 | 
 | 
|---|
| 78 |   // helper: serialize data from URI to N3
 | 
|---|
| 79 |   private function _getSerializedData( $uri) {
 | 
|---|
| 80 | 
 | 
|---|
| 81 |     $rdfaData = new \rdfa\Data();
 | 
|---|
| 82 |     $this->assertInstanceOf( '\rdfa\Data', $rdfaData);
 | 
|---|
| 83 |     $rdfaData->parse( $uri);
 | 
|---|
| 84 |     return  $rdfaData->_serialize( 'n3');
 | 
|---|
| 85 | 
 | 
|---|
| 86 |   } // private function _getSerializedData
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   // --------------------------------------------------------
 | 
|---|
| 89 | 
 | 
|---|
| 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 | 
 | 
|---|
| 120 |   // --------------------------------------------------------
 | 
|---|
| 121 | 
 | 
|---|
| 122 |   public function test() {
 | 
|---|
| 123 | 
 | 
|---|
| 124 |     $category = trim( getenv( 'CATEGORY'));
 | 
|---|
| 125 |     if ($category == '')
 | 
|---|
| 126 |       $categories = self::$manifest->getCategories();
 | 
|---|
| 127 |     else
 | 
|---|
| 128 |       $categories = array( $type);
 | 
|---|
| 129 | 
 | 
|---|
| 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) {
 | 
|---|
| 136 | 
 | 
|---|
| 137 |         echo "  Executing: " . $testcase->name . "\n";
 | 
|---|
| 138 |         switch ($category) {
 | 
|---|
| 139 |           case 'PositiveParserTest':
 | 
|---|
| 140 |             $result = $this->_executePositiveParserTest( $testcase);
 | 
|---|
| 141 |             break;
 | 
|---|
| 142 |         } // switch ($category)
 | 
|---|
| 143 |       }
 | 
|---|
| 144 |     }
 | 
|---|
| 145 | 
 | 
|---|
| 146 |   } // public function test
 | 
|---|
| 147 | 
 | 
|---|
| 148 |   // --------------------------------------------------------
 | 
|---|
| 149 | 
 | 
|---|
| 150 |   public function disabled_test() {
 | 
|---|
| 151 | 
 | 
|---|
| 152 |     // get testcase name again in object context
 | 
|---|
| 153 |     $testcase = trim( getenv( 'TESTCASE'));
 | 
|---|
| 154 |     $this->assertGreaterThan( 0, strlen( $testcase));
 | 
|---|
| 155 | 
 | 
|---|
| 156 |     // read source data
 | 
|---|
| 157 |     $sourceuri = self::BASEURI."/$testcase.rdf";
 | 
|---|
| 158 |     $source = $this->_getSerializedData( $sourceuri);
 | 
|---|
| 159 |     $this->assertEquals( ($source !== false), true);
 | 
|---|
| 160 | 
 | 
|---|
| 161 |     // read target data
 | 
|---|
| 162 |     $resulturi = self::BASEURI."/$testcase.nt";
 | 
|---|
| 163 |     $result = $this->_getSerializedData( $resulturi);
 | 
|---|
| 164 |     $this->assertEquals( ($result !== false), true);
 | 
|---|
| 165 | 
 | 
|---|
| 166 |     // resolve problems with blnak node identifiers
 | 
|---|
| 167 |     // and different sorting of triples
 | 
|---|
| 168 |     $source = $this->_refineResult( $source);
 | 
|---|
| 169 |     $result = $this->_refineResult( $result);
 | 
|---|
| 170 | 
 | 
|---|
| 171 |     // check if results are equal
 | 
|---|
| 172 |     $success = ($source == $result);
 | 
|---|
| 173 | 
 | 
|---|
| 174 |     // in case of error, write serialized data to subdirectory
 | 
|---|
| 175 |     if (!$success)
 | 
|---|
| 176 |       $this->_writeErrorData( $testcase,
 | 
|---|
| 177 |                               $source, $sourceuri,
 | 
|---|
| 178 |                               $result, $resulturi);
 | 
|---|
| 179 | 
 | 
|---|
| 180 |     // compare source and target data
 | 
|---|
| 181 |     $this->assertEquals( $success, true);
 | 
|---|
| 182 | 
 | 
|---|
| 183 | 
 | 
|---|
| 184 |   } // public function test_parse_sparql()
 | 
|---|
| 185 | 
 | 
|---|
| 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";
 | 
|---|
| 195 |       echo 'Conclusion: ' . $testcase->conclusion . "\n";
 | 
|---|
| 196 |     }
 | 
|---|
| 197 |   }
 | 
|---|
| 198 | 
 | 
|---|
| 199 | 
 | 
|---|
| 200 | } // class UnitTest
 | 
|---|
| 201 | 
 | 
|---|
| 202 | 
 | 
|---|