Location: PHPKode > scripts > iCalcnv > iCalcnv-3.2/iCalcnvTest.php
<?php // iCalcvnTest.php
error_reporting( E_ERROR | E_WARNING | E_PARSE | E_NOTICE ); // due to Pear Spreadsheet/Excel/Writer PHP Deprecated/Strict standards violation
$startTime = microtime( TRUE );
$encoded   = FALSE;
$_log      = TRUE;
/**/
if( !isset( $_REQUEST['testcase'] )) $_REQUEST['testcase'] = 11;
$bUrl = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$pos  = strrpos( $bUrl, '/' ) + 1;
$bUrl = 'http://'.substr( $bUrl, 0, $pos );
/**/
if( $encoded ) {
  require_once 'encoded/iCalcreator.class.php';
  require_once 'encoded/iCalcnv.class.php';
  require_once 'encoded/eClog.class.php';
}
else {
  require_once './iCalcreator.class.php';
  require_once './iCalcnv.class.php';
  require_once './eClog.class.php';
}
require_once './eClog.PEAR_LOG.levels.php'; // required by Pear Spreadsheet/Excel/Writer
/**/
if( $_log ) {
  require_once './eClog.inc.php'; // trap Spreadsheet/Excel/Writer PHP errors
  /* define default loglevel */
  $loglevels = array( 'LOG_EMERG'   => 0    // System is unusable
                    , 'LOG_ALERT'   => 1    // Immediate action required
                    , 'LOG_CRIT'    => 2    // Critical conditions
                    , 'LOG_ERR'     => 3    // Error conditions
                    , 'LOG_WARNING' => 4    // Warning conditions
                    , 'LOG_NOTICE'  => 5    // Normal but significant
                    , 'LOG_INFO'    => 6    // Informational
                    , 'LOG_DEBUG'   => 7    // Debug-level messages
                    , 'NO_LOG'      => 8 ); // no logging at all
  $logConf = array( 'lineFormat' => '%1$s %3$s %2$s %4$s' ); // Timestamp, priority, specText, msg
//                    'append'     => TRUE,
//                    'locking'    => TRUE,
//                    'lineFormat' => '%1$s %3$s %8$s.%7$s:%6$s %4$s', // Timestamp, priority, class, function, line number, msg
//                    'lineFormat' => '%1$s %4$s', 'timeFormat' => '%X'), // only time + msg
//                    'timeFormat' => '%Y%m%d %X' );
  $logfile = 'iCalcnv.log'; // _'.date('Ymd').'.log'; // create a new logfile every day
/*
  require_once 'Log.php';
  class iCalLog extends Log { function __destruct() { $this->flush(); } }
  $_log = iCalLog::singleton( 'file', $logfile, ICALCNVVERSION, $logConf, $loglevels['LOG_NOTICE'] );
*/
  $_log = eClog::singleton( 'file', $logfile, ICALCNVVERSION, $logConf, $loglevels['LOG_ERR'] );
  $eClog = & $_log; // used in 'eClog.inc.php'
  $_log->log( '******************** start '.ICALCNVVERSION.' (test) ********************', LOG_INFO );
}
/**/
$iCalcnv = new iCalcnv( $_log );
/**/
switch( $_REQUEST['testcase'] ) {
  case 11: // iCal2xls
    if( $_log )
      $_log->log( str_repeat( '* ', 5 ).' start iCal2xls 11, input file, output save to file (+backup)', LOG_ERR ); // log testing entry
    if( FALSE !== $iCalcnv->setConfig( array( 'inputfilename'   => 'testFile.ics'
                                            , 'inputdirectory'  => 'calendars'
                                            , 'outputfilename'  => 'testFile.xls'
                                            , 'outputdirectory' => 'csvxlsStorage'
                                            , 'save'            => TRUE
                                            , 'backup'          => TRUE
                                            , 'unique_id'       => 'kigkonsult.se'
                                            , 'UID'             => 'eventId'
                                            , 'DTSTART'         => 'Start'
                                            , 'SUMMARY'         => 'Subject'
                                            , 2                 => 'DESCRIPTION'
                                            , 6                 => 'UID'
                                            , 8                 => 'DTSTART' )))
      $iCalcnv->iCal2xls();
  break;
/**/
  case 12: // iCal2xls
    if( $_log )
      $_log->log( str_repeat( '* ', 5 ).' start iCal2xls 12, input url, output save to file (write over)', LOG_ERR ); // log testing entry
    $filenameICS = $bUrl.'calendars/testFile.ics';
    if( FALSE !== $iCalcnv->setConfig( array( 'inputurl'        => $filenameICS
                                            , 'outputfilename'  => 'testFile.xls'
                                            , 'outputdirectory' => 'csvxlsStorage'
                                            , 'save'            => TRUE
                                            , 'unique_id'       => 'kigkonsult.se'
                                            , 'UID'             => 'eventId'
                                            , 'DTSTART'         => 'Start'
                                            , 'SUMMARY'         => 'Subject'
                                            , 2                 => 'DESCRIPTION'
                                            , 6                 => 'UID'
                                            , 8                 => 'DTSTART' )))
      $iCalcnv->iCal2xls();
  break;
/**/
  case 13: // iCal2xls
    if( $_log )
      $_log->log( str_repeat( '* ', 5 ).' start iCal2xls 13, input object, output redirect to browser', LOG_ERR ); // log testing entry
    $calendar = new vcalendar( array( 'unique_id' => 'kigkonsult.se', 'directory' => 'calendars', 'filename' => 'testFile.ics' ));
    $calendar->parse();
    if( FALSE !== $iCalcnv->setConfig( array( 'outputfilename'  => 'testFile.xls'
                                            , 'UID'             => 'eventId'
                                            , 'DTSTART'         => 'Start'
                                            , 'SUMMARY'         => 'Subject'
                                            , 2                 => 'DESCRIPTION'
                                            , 6                 => 'UID'
                                            , 8                 => 'DTSTART'
                                            , 'skip'            => array( 'CREATED', 'COMMENT' ))))
      $iCalcnv->iCal2xls( $calendar );
  break;
/**/
/**/
  case 21: // iCal2csv
    if( $_log )
      $_log->log( str_repeat( '* ', 5 ).' start iCal2csv 21, input file, output save to file (backup)', LOG_ERR ); // log testing entry
    if( FALSE !== $iCalcnv->setConfig( array( 'inputfilename'   => 'testFile.ics'
                                            , 'inputdirectory'  => 'calendars'
                                            , 'outputfilename'  => 'testFile.csv'
                                            , 'outputdirectory' => 'csvxlsStorage'
                                            , 'save'            => TRUE
                                            , 'backup'          => TRUE
                                            , 'unique_id'       => 'kigkonsult.se'
                                            , 'UID'             => 'eventId'
                                            , 'DTSTART'         => 'Start'
                                            , 'SUMMARY'         => 'Subject'
                                            , 2                 => 'DESCRIPTION'
                                            , 6                 => 'UID'
                                            , 8                 => 'DTSTART'
                                            , 'skip'            => array( 'CREATED', 'COMMENT' ))));
      $iCalcnv->iCal2csv();
  break;
/**/
  case 22: // iCal2csv
    if( $_log )
      $_log->log( str_repeat( '* ', 5 ).' start iCal2csv 22, input url, output save to file (write over)', LOG_ERR ); // log testing entry
    $filenameICS = $bUrl.'calendars/testFile.ics';
    if( FALSE !== $iCalcnv->setConfig( array( 'inputurl'        => $filenameICS
                                            , 'outputfilename'  => 'testFile.csv'
                                            , 'outputdirectory' => 'csvxlsStorage'
                                            , 'save'            => TRUE
                                            , 'unique_id'       => 'kigkonsult.se'
                                            , 'UID'             => 'eventId'
                                            , 'DTSTART'         => 'Start'
                                            , 'SUMMARY'         => 'Subject'
                                            , 2                 => 'DESCRIPTION'
                                            , 6                 => 'UID'
                                            , 8                 => 'DTSTART' )))
      $iCalcnv->iCal2csv();
  break;
/**/
  case 23: // iCal2csv
    if( $_log )
      $_log->log( str_repeat( '* ', 5 ).' start iCal2csv 23, input object, output redirect to browser', LOG_ERR ); // log testing entry
    $calendar = new vcalendar( array( 'unique_id' => 'kigkonsult.se', 'directory' => 'calendars', 'filename' => 'testFile.ics' ));
    $calendar->parse();
    if( FALSE !== $iCalcnv->setConfig( array( 'UID'             => 'eventId'
                                            , 'DTSTART'         => 'Start'
                                            , 'SUMMARY'         => 'Subject'
                                            , 2                 => 'DESCRIPTION'
                                            , 6                 => 'UID'
                                            , 8                 => 'DTSTART'
                                            , 'skip'            => array( 'CREATED', 'COMMENT' ))))
      $iCalcnv->iCal2csv( $calendar );
  break;
/**/
/**/
  case 31: // csv2iCal
    if( $_log )
      $_log->log( str_repeat( '* ', 5 ).' start csv2iCal 31, input file, output object (+redirect)', LOG_ERR ); // log testing entry
    if( FALSE !== $iCalcnv->setConfig( array( 'inputfilename'   => 'testFile.csv'
                                            , 'inputdirectory'  => 'csvxlsStorage'
                                            , 'outputobj'       => TRUE
                                            , 'unique_id'       => 'kigkonsult.se'
                                            , 'DTSTART'         => 'Start'
                                            , 'UID'             => 'eventId'
                                            , 'SUMMARY'         => 'Subject' ))) {
      $calendar = $iCalcnv->csv2iCal();
      if( FALSE !== $calendar )
        $calendar->returnCalendar();
    }
    break;
/**/
  case 32: // csv2iCal
    if( $_log )
      $_log->log( str_repeat( '* ', 5 ).' start csv2iCal 32, input url, output file (save+backup)', LOG_ERR ); // log testing entry
    $filenameCSV = $bUrl.'csvxlsStorage/testFile.csv';
    copy( 'calendars/testFile.ics', 'calendars/testFile.org.ics' );
    if( FALSE !== $iCalcnv->setConfig( array( 'inputurl'        => $filenameCSV
                                            , 'outputfilename'  => 'testFile.ics'
                                            , 'outputdirectory' => 'calendars'
                                            , 'save'            => TRUE
                                            , 'backup'          => TRUE
                                            , 'unique_id'       => 'kigkonsult.se'
                                            , 'DTSTART'         => 'Start'
                                            , 'UID'             => 'eventId'
                                            , 'SUMMARY'         => 'Subject' ))) {
      $iCalcnv->csv2iCal();
    }
    // compare and display diff result
/*
    $cmd = 'diff calendars/testFile.org.ics calendars/testFile.ics';
    $a   = array();
    exec( $cmd, $a );
    echo '<pre>'.$cmd.chr(10).implode( chr(10), $a )."</pre>\n";
*/
    $cmd = 'diff -b -H --side-by-side -W 160 calendars/testFile.org.ics calendars/testFile.ics';
    $a   = array();
    exec( $cmd, $a );
    echo '<pre>'.chr(10).$cmd.chr(10).implode( chr(10), $a )."</pre>\n";
    unlink( 'calendars/testFile.org.ics' );
    break;
/**/
  case 33: // csv2iCal
    if( $_log )
      $_log->log( str_repeat( '* ', 5 ).' start csv2iCal 33, input file, output redirect', LOG_ERR ); // log testing entry
    if( FALSE !== $iCalcnv->setConfig( array( 'inputfilename'   => 'testFile.csv'
                                            , 'inputdirectory'  => 'csvxlsStorage'
                                            , 'unique_id'       => 'kigkonsult.se'
                                            , 'DTSTART'         => 'Start'
                                            , 'UID'             => 'eventId'
                                            , 'SUMMARY'         => 'Subject' ))) {
      $iCalcnv->csv2iCal();
    }
    break;
/**/
  default:
    if( $_log )
      $_log->log( 'unvalid testcase number', LOG_ERR ); // log testing entry
    break;
}
/**/
if( $_log ) {
  $_log->log( 'total time:'.number_format(( microtime( TRUE ) - $startTime ),  5 ).' sec', LOG_ERR ); // log testing entry
  $_log->flush();
}
?>
Return current item: iCalcnv