Documentation is available at Fixture.php
- <?php
- /**
- * FIT Fixture
- *
- * $Id$
- *
- * @author Luis A. Floreani <luis.floreani@gmail.com>
- * @author gERD Schaufelberger <gerd@php-tools.net>
- * @package FIT
- * @subpackage Fixture
- * @license LGPL http://www.gnu.org/copyleft/lesser.html
- * @copyright Copyright (c) 2002-2005 Cunningham & Cunningham, Inc.
- */
- /**
- * path to FIT implementation
- */
- if( !defined( 'PHPFIT_INCLUDE_DIR' ) ) {
- define( 'PHPFIT_INCLUDE_DIR', dirname( __FILE__ ) );
- }
- /**
- * path to user's fixtures
- */
- if( !defined( 'PHPFIT_FIXTURE_DIR' ) ) {
- define( 'PHPFIT_FIXTURE_DIR', realpath( PHPFIT_INCLUDE_DIR . '/..' ) );
- }
- /**
- * load counts class
- */
- include_once PHPFIT_INCLUDE_DIR . '/Counts.php';
- /**
- * load scientfic double class
- */
- include_once PHPFIT_INCLUDE_DIR . '/ScientificDouble.php';
- /**
- * load timer class
- */
- include_once PHPFIT_INCLUDE_DIR . '/RunTime.php';
- /**
- * FIT Fixture
- *
- * @version 0.1.0
- * @package FIT
- * @subpackage Fixture
- */
- class Fixture {
- /**
- * make the include folder available for user's fixtures
- * @var array
- */
- const INCLUDE_DIR = PHPFIT_INCLUDE_DIR;
- protected $green = "#cfffcf";
- protected $red = "#ffcfcf";
- protected $yellow = "#ffffcf";
- protected $gray = "#efefef";
- /**
- * collecting information of this fixture
- * @var array
- */
- public $summary = array();
- /**
- * count what?
- * @var object
- */
- public $counts;
- /**
- * construtor
- *
- * instanciate counter
- */
- function __construct() {
- $this->counts = new Counts();
- }
- /**
- * Traverse all tables
- *
- * Tables are packed in Parse-objects
- *
- * @param Parse $tables
- */
- public function doTables( $tables ) {
- $this->summary['run date'] = date( 'F d Y H:i:s.' );
- $this->summary['run elapsed time'] = new RunTime();
- // no tables left
- if( $tables == null ) {
- return;
- }
- // fixture name
- $fixtureName = $this->fixtureName( $tables );
- if( $fixtureName == null ) {
- return;
- }
- try {
- $fixture = $this->getLinkedFixtureWithArgs($tables);
- $fixture->interpretTables($tables);
- }
- catch( Exception $e ) {
- $this->exception($fixtureName, $e);
- $this->interpretFollowingTables($tables);
- }
- }
- /**
- * CamelCaseString
- *
- * @todo This looks quite fragile - consider using preg_replace
- * @param string $string
- * @return string
- */
- public static function camel( $string ) {
- while( ( $pos = stripos($string, " " ) ) !== false ) {
- $characterUpper = strtoupper( $string[$pos+1] );
- $string[$pos+1] = $characterUpper;
- $string[$pos] = "&";
- }
- $string = str_replace('&', '', $string);
- return $string;
- }
- /**
- * Work on tables
- *
- * @param Parse $tables
- */
- protected function interpretTables($tables) {
- try {
- $this->doTable($tables);
- }
- catch( Exception $e ) {
- $this->exception($this->fixtureName($tables), $e);
- }
- $this->interpretFollowingTables($tables);
- }
- /**
- * @param Parse tables
- */
- private function interpretFollowingTables($tables) {
- $tables = $tables->more;
- while ($tables != null) {
- $fixtureName = $this->fixtureName($tables);
- if ($fixtureName != null ) {
- try {
- $fixture = $this->getLinkedFixtureWithArgs($tables);
- $fixture->doTable($tables);
- } catch (Exception $e) {
- $this->exception($fixtureName, $e);
- }
- }
- $tables = $tables->more;
- }
- }
- /**
- * @param Parse table
- */
- public function doTable( $table ) {
- $this->doRows( $table->parts->more );
- }
- /**
- * @param Parse rows
- */
- public function doRows( $rows ) {
- $i = 1;
- while( $rows != null ) {
- $more = $rows->more;
- $this->doRow( $rows );
- $rows = $more;
- }
- }
- /**
- * @param Parse row
- */
- public function doRow( $row ) {
- $this->doCells( $row->parts );
- }
- /**
- * process cells
- *
- * Generic processing of all upcoming cells. Actually, this method
- * just iterates through them and delegates to doCell()
- *
- * This method may be overwritten by a subclass (ActionFixture)
- *
- * @param object $cells A parse object
- * @return void
- * @see doCell()
- */
- public function doCells( $cells ) {
- $i = 0;
- while( $cells ) {
- try {
- $this->doCell( $cells, $i++ );
- }
- catch( Exception $e ) {
- $this->exception( $cells, $e );
- }
- $cells = $cells->more;
- }
- }
- /**
- * process a single cell
- *
- * Generic processing of a table cell. Well, this function
- * just ignores cells.
- *
- * This method may be overwritten by a subclass (ColumnFixture)
- *
- * @param object $cell A parse object
- * @return void
- */
- public function doCell( $cell ) {
- $this->ignore( $cell );
- }
- /**
- * @param Parse tables
- * @return Parse
- */
- public function fixtureName($tables) {
- return $tables->at(0, 0, 0);
- }
- /**
- * @param Parse tables
- * @return Fixture
- */
- protected function getLinkedFixtureWithArgs($tables) {
- $header = $tables->at(0, 0, 0);
- $fixture = $this->loadFixture($header->text());
- $fixture->counts = $this->counts;
- $fixture->summary = $this->summary;
- return $fixture;
- }
- /**
- * load a fixture by java-stylish name (dot-sparated)
- *
- * A fixture name might be something like: eg.net.Simulator. This will
- * load eg/net/Simulator.php and intanciates the clss Simulator. The path name
- * is realtive to the basic fixture di
- *
- * @param string fixtureName
- * @return object Fixture
- */
- public function loadFixture( $fixtureName ) {
- $srcFile = str_replace( '.', '/', $fixtureName );
- $class = basename( $srcFile );
- // load class
- if( !include_once PHPFIT_FIXTURE_DIR . '/' . $srcFile . '.php' ) {
- throw new Exception( 'Could not load Fixture ' . $fixtureName . 'from ' . $srcFile );
- }
- // instanciate
- $instance = new $class();
- return $instance;
- }
- /**
- * @param Parse cell
- * @param TypeAdapter a
- */
- public function check($cell, $a) {
- $text = $cell->text();
- if ($text == "") {
- try {
- $this->info($cell, $a->toString($a->get()));
- } catch (Exception $e) {
- $this->info($cell, "error");
- }
- } else if ($a == null) {
- $this->ignore($cell);
- } else if (strcmp($text, "error") == 0) {
- try {
- $result = $a->invoke();
- $this->wrong($cell, $a->toString());
- } catch (Exception $e) {
- $this->right($cell);
- }
- } else {
- try {
- $result = $a->get(); // the value of the attribute or the return value of the method
- if ($a->equals($a->parse($text), $result)) {
- $this->right($cell);
- } else {
- $result = $this->fixBoolToString($result);
- $this->wrong($cell, $a->toString($result));
- }
- } catch (Exception $e) {
- $this->exception($cell, $e);
- }
- }
- }
- public function fixBoolToString($result) {
- if (!is_bool($result))
- return $result;
- if ($result)
- return "true";
- else
- return "false";
- }
- /**
- * transform an exception to a cell error
- *
- * @param object $cell Parse object
- * @param object $e Exception
- * @see error()
- */
- public function exception( $cell, $e ) {
- $this->error( $cell, $e->getMessage() );
- }
- /**
- * place an error text into a cell
- *
- * @param object $cell Parse object
- * @param string $message
- */
- public function error( $cell, $message ) {
- $cell->body = $cell->text() . ': '. $this->escape( $message );
- $cell->addToTag( ' bgcolor=" '. $this->yellow . '\"' );
- $this->counts->exceptions++;
- }
- /**
- * @param string s
- * @param string type
- * @return mixed (object or string)
- */
- public function parse($s, $type) {
- if ($type == "ScientificDouble") {
- //echo "PARSE TYPE : " . $type . "<br>";
- return ScientificDouble::valueOf($s);
- }
- return $s;
- //echo "<br>type:" . $type;
- //return new Exception("can't yet parse " . get_class($s));;
- }
- // Annotation ///////////////////////////////
- /**
- * @param Parse cell
- */
- public function right($cell) {
- $cell->addToTag(" bgcolor=\"" . $this->green . "\"");
- $this->counts->right++;
- }
- /**
- * @param Parse cell
- * @param string actual
- */
- public function wrong($cell, $actual=false) {
- $cell->addToTag(" bgcolor=\"" . $this->red . "\"");
- $cell->body = $this->escape($cell->text());
- $this->counts->wrong++;
- if ($actual !== false)
- $cell->addToBody($this->label("expected") . "<hr>" . $this->escape($actual) . $this->label("actual"));
- }
- /**
- * @param Parse cell
- * @param string message
- */
- public function info($cell, $message) {
- $str = $this->infoS($message);
- $cell->addToBody($str);
- }
- /**
- * @param string message
- * @return string
- */
- public function infoS($message) {
- return " <font color=\"#808080\">" . $this->escape($message) . "</font>";
- }
- /**
- * @param Parse cell
- */
- public function ignore ($cell) {
- $cell->addToTag(" bgcolor=\"" . $this->gray . "\"");
- $this->counts->ignores++;
- }
- /**
- * @param string string
- * @return string
- */
- public function label($string) {
- return " <font size=-1 color=\"#c08080\"><i>" . $string . "</i></font>";
- }
- /**
- * @param string string
- * @return string
- */
- public function escape($string) {
- $string = str_replace('&', '&', $string);
- $string = str_replace('<', '<', $string);
- $string = str_replace(' ', ' ', $string);
- $string = str_replace('\r\n', '<br />', $string);
- $string = str_replace('\r', '<br />', $string);
- $string = str_replace('\n', '<br />', $string);
- return $string;
- }
- /**
- * receive member variable's type specification
- *
- * Use the helper property typeDict to figure out what type
- * a variable is.
- *
- * Type is one of:
- * - integer
- * - string
- * - array
- * - object
- * - object:CLASSNAME
- * - callable
- *
- * @todo As PHP does automatica type conversation, I reckon this can be spared
- * @return string $type
- */
- public function getType( $name ) {
- if( !property_exists( $this, $name ) ) {
- throw new Exception( 'Property does not exist! ' . __CLASS__ . '->' . $name );
- return null;
- }
- if( !isset( $this->typeDict[$name] ) ) {
- throw new Exception( 'Property has no definition in $typeDict! ' . __CLASS__ . '->' . $name );
- return null;
- }
- return $this->typeDict[$name];
- }
- }
- ?>
Documentation generated on Sun, 02 Apr 2006 16:01:04 +0200 by phpDocumentor 1.3.0RC5