<?PHP

/* RDFInt.php - RDF Interfaces for PHP
 * Copyright 2011 netlabs.org
 * Author: Christian Langanke, Adrian Gschwend
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// load library setup
define( 'RDFINT', getenv( 'PHP_LIBROOT_RDFINT'));
require_once( RDFINT.'/rdfa.php');

class UnitTest extends PHPUnit_Framework_TestCase {

  static protected $baseuri;
  private $testcase;
  const BASEURI = 'http://www.w3.org/2000/10/rdf-tests/rdfcore';

  // --------------------------------------------------------

  public static function setUpBeforeClass() {
    $testcase = trim( getenv( 'TESTCASE'));
    echo "Executing RDF testcase: $testcase\n\n";
  }

  // --------------------------------------------------------

  protected function setUp() {
  }

  // --------------------------------------------------------

  private function _reIdentifyBlankNode( $data) {
    return preg_replace( '/_:[^ ]*/', '_:a', $data);
  }

  // --------------------------------------------------------

  private function _writeErrorData( $testcase,
                                    $source, $sourceuri,
                                    $result, $resulturi) {
    if (!is_dir( $testcase))
      mkdir( $testcase, NULL, true);

    $orgsource = file_get_contents( $sourceuri);
    file_put_contents ( "$testcase/org_source.txt" , $orgsource);

    $orgresult = file_get_contents( $resulturi);
    file_put_contents ( "$testcase/org_result.txt" , $orgresult);

    file_put_contents ( "$testcase/parsed_source.txt" , $source);
    file_put_contents ( "$testcase/parsed_result.txt" , $result);
  }

  // --------------------------------------------------------

  private function _getSerializedData( $uri) {

    $rdfaData = new \rdfa\Data();
    $this->assertInstanceOf( '\rdfa\Data', $rdfaData);
    $rdfaData->parse( $uri);
    return  $rdfaData->_serialize( 'n3');

  } // private function _getSerializedData

  // --------------------------------------------------------

  public function test() {

    // get testcase name again in object context
    $testcase = trim( getenv( 'TESTCASE'));
    $this->assertGreaterThan( 0, strlen( $testcase));

    // read source data
    $sourceuri = self::BASEURI."/$testcase.rdf";
    $source = $this->_getSerializedData( $sourceuri);
    $this->assertEquals( ($source !== false), true);

    // read target data
    $resulturi = self::BASEURI."/$testcase.nt";
    $result = $this->_getSerializedData( $resulturi);
    $this->assertEquals( ($result !== false), true);

    // rework blank node identifiers for coparison
    $source = $this->_reIdentifyBlankNode( $source);
    $result = $this->_reIdentifyBlankNode( $result);


    // check if results are equal
    $success = ($source == $result);

    // in case of error, write serialized data to subdirectory
    if (!$success)
      $this->_writeErrorData( $testcase, 
                              $source, $sourceuri, 
                              $result, $resulturi);

    // compare source and target data
    $this->assertEquals( $success, true);


  } // public function test_parse_sparql()

} // class UnitTest


