Source for file FileRunner.php

Documentation is available at FileRunner.php

  1. <?php
  2. /**
  3. * FIT FileRunner
  4. *
  5. * $Id$
  6. *
  7. * @author Luis A. Floreani <luis.floreani@gmail.com>
  8. * @author gERD Schaufelberger <gerd@php-tools.net>
  9. * @package FIT
  10. * @subpackage FileRunner
  11. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  12. * @copyright Copyright (c) 2002-2005 Cunningham & Cunningham, Inc.
  13. */
  14.  
  15. /**
  16. * load exception class: FileIOException
  17. */
  18. require_once dirname( __FILE__ ) . '/FileIOException.php';
  19.  
  20. /**
  21. * FIT FileRunner
  22. *
  23. * Run fit-tests from tables stored in HTML files.
  24. * FileRunner provides a simple interface to process tests from CLI or
  25. * "remote controlled" from anther application.
  26. *
  27. * @see main()
  28. * @see run()
  29. *
  30. * @version 0.1.0
  31. * @package FIT
  32. * @subpackage FileRunner
  33. */
  34. class FileRunner {
  35.  
  36. /**
  37. * running fixture object
  38. * @var Fixture
  39. */
  40. private $fixture;
  41. /**
  42. * table parse
  43. * @var Parse
  44. */
  45. private $tables;
  46. /**
  47. * @var string
  48. */
  49.  
  50. private $input;
  51.  
  52. /**
  53. * Emulate c-stylish main() function to run applicattion on command line
  54. *
  55. * The most common usage from any script.
  56. * FileRunner::main( $_SERVER['argv'] );
  57. *
  58. * return codes:
  59. * 0 - everything went alright
  60. * 1 - invalid number of arguments
  61. * 2 - file io problem
  62. * 3 - parse exception
  63. * 127 - unexpected exception
  64. *
  65. * @param array argv
  66. * @return int 0 on success, or value greater 1 on error
  67. * @see run()
  68. */
  69. public static function main( $argv ) {
  70. /**
  71. * open stderr for writing
  72. * required for NON CLI misuse of FileRunner::main()
  73. */
  74. if( !defined( 'STDERR' ) ) {
  75. throw new FileIOException( 'STDERR is not defined - use PHP-CLI to call this method' );
  76. }
  77. if( count( $argv ) != 3 ) {
  78. fwrite( STDERR, "Invalid number of arguments. input file and output file expected\n" );
  79. return 1;
  80. }
  81. try {
  82. $fr = new FileRunner();
  83. $fr->run( $argv[1], $argv[2] );
  84. }
  85. catch( FileIOException $e ) {
  86. fwrite( STDERR, $e->getMessage() . ": " . $e->getFilename() . "\n" );
  87. return 2;
  88. }
  89. catch( ParseException $e ) {
  90. fwrite( STDERR, $e->getMessage() . " @ " . $e->getErrorOffset() . "\n" );
  91. return 3;
  92. }
  93. catch( Exception $e ) {
  94. fwrite( STDERR, 'Caught unknown exception: ' . $e->getMessage() . "\n" );
  95. return 127;
  96. }
  97. return 0;
  98. }
  99.  
  100.  
  101. /**
  102. * run test
  103. *
  104. * Process all tables in input file and store result in output file.
  105. *
  106. * Example:
  107. * $fr = new FileRunner();
  108. * $fr->run( 'infilt.html', 'outfile.html' );
  109. *
  110. * @param string $in path to input file
  111. * @param string $out path to output file
  112. * @return bool always true
  113. */
  114. public function run( $in, $out ) {
  115. // check input file
  116. if( !file_exists( $in ) ) {
  117. throw new FileIOException( 'Input file does not exist!', $in );
  118. }
  119. if( !is_readable( $in ) ) {
  120. throw new FileIOException( 'Input file does not exist!', $in );
  121. }
  122. // check output file
  123. if( file_exists( $out ) ) {
  124. if( !is_writable( $out ) ) {
  125. throw new FileIOException( 'Output file is not writable (probably a problem of file permissions)', $in );
  126. }
  127. }
  128. else {
  129. if( !is_writable( dirname( $out ) ) ) {
  130. throw new FileIOException( 'Cannot create output file in given folder. (probably a problem of file permissions)', $in );
  131. }
  132. }
  133. // summary data
  134. $this->fixture->summary["input file"]=$in;
  135. $this->fixture->summary["output file"]=$out;
  136. $this->fixture->summary["input update"] = date("F d Y H:i:s.", filemtime($in));
  137. // load input
  138. $this->input = file_get_contents( $in );
  139. // run tests
  140. $this->process();
  141. // save output
  142. file_put_contents( $out, $this->tables->toString() );
  143. return true;
  144. }
  145.  
  146. /**
  147. * process tables
  148. *
  149. * @return bool always true
  150. */
  151. public function process() {
  152. include_once dirname( __FILE__ ) . '/Fixture.php';
  153. include_once dirname( __FILE__ ) . '/Parse.php';
  154. $this->fixture = new Fixture();
  155. $this->tables = new Parse( $this->input );
  156. $this->fixture->doTables( $this->tables );
  157. return true;
  158. }
  159. }
  160. ?>

Documentation generated on Sun, 02 Apr 2006 16:01:04 +0200 by phpDocumentor 1.3.0RC5