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

Documentation generated on Sun, 02 Apr 2006 23:21:38 +0000 by phpDocumentor 1.3.0RC5