Source for file Column.php

Documentation is available at Column.php

  1. <?php
  2. /**
  3. * FIT Fixture ColumnFixture
  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 Fixture
  11. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  12. * @copyright Copyright (c) 2002-2005 Cunningham & Cunningham, Inc.
  13. */
  14.  
  15. /**
  16. * load class TypeAdapter
  17. */
  18. include_once 'PHPFIT/TypeAdapter.php';
  19.  
  20. /**
  21. * FIT Fixture: ColumnFixture
  22. *
  23. * A ColumnFixture maps columns in the test data to fields or methods of its
  24. * subclasses. SimpleExample and CalculatorExample use column fixtures.
  25. *
  26. * @version 0.1.0
  27. * @package FIT
  28. * @subpackage Fixture
  29. */
  30. class PHPFIT_Fixture_Column extends PHPFIT_Fixture {
  31.  
  32. /**
  33. * TypeAdapter
  34. * @var object
  35. */
  36. protected $columnBindings;
  37. /**
  38. * Excecution state
  39. * @var bool
  40. */
  41. protected $hasExecuted = false;
  42. /**
  43. * Process a table's row
  44. *
  45. * @param Parce rows
  46. */
  47. public function doRows( $rows )
  48. {
  49. $this->bind( $rows->parts );
  50. parent::doRows( $rows->more );
  51. }
  52. /**
  53. * Process a table's row
  54. *
  55. * @param Parce rows
  56. */
  57. public function doRow( $row )
  58. {
  59. $this->hasExecuted = false;
  60. try {
  61. $this->reset();
  62. parent::doRow( $row );
  63. if( !$this->hasExecuted ) {
  64. $this->execute();
  65. }
  66. }
  67. catch( Exception $e ) {
  68. $this->exception($row->leaf(), $e);
  69. }
  70. }
  71. /**
  72. * process a single cell
  73. *
  74. * Generic processing of a table cell. Well, this function
  75. * just ignores cells.
  76. *
  77. * This method may be overwritten by a subclass (ColumnFixture)
  78. *
  79. * @param object $cell A parse object
  80. * @return void
  81. */
  82. public function doCell( $cell )
  83. {
  84. $adapter = null;
  85. if( isset( $this->columnBindings[$cell->count] ) ) {
  86. $adapter = $this->columnBindings[$cell->count];
  87. }
  88. try {
  89. $text = $cell->text();
  90. if( $text === '' ) {
  91. $this->checkCell( $cell, $adapter );
  92. return;
  93. }
  94. // skip the rest if there is no adapter
  95. if( $adapter == null ) {
  96. $this->ignore( $cell );
  97. return;
  98. }
  99. // a column can be a value
  100. if( $adapter->field != null ) {
  101. $adapter->set( $adapter->parse( $text ) );
  102. return;
  103. }
  104. // or a column can be method
  105. if( $adapter->method != null ) {
  106. $this->checkCell( $cell, $adapter );
  107. return;
  108. }
  109. }
  110. catch( Exception $e ) {
  111. $this->exception( $cell, $e );
  112. }
  113. }
  114.  
  115. /**
  116. * check whether the value of a cell matches
  117. *
  118. * @param Parse $cell,
  119. * @param TypeAdapter $a
  120. * @return bool true on success, false otherwise
  121. */
  122. public function checkCell( $cell, $a )
  123. {
  124. if( !$this->hasExecuted ) {
  125. try {
  126. $this->execute();
  127. }
  128. catch(Exception $e) {
  129. $this->exception ($cell, $e);
  130. }
  131. $this->hasExecuted = true;
  132. }
  133. parent::checkCell( $cell, $a );
  134. }
  135. /**
  136. * bind columns of table header to functions and properties
  137. *
  138. * @param Parse $head
  139. */
  140. protected function bind( $heads )
  141. {
  142. $this->columnBindings = array( $heads->size() );
  143. for( $i=0; $heads != null; $heads = $heads->more ) {
  144. //echo "<br>".$heads->text();
  145. $name = $heads->text();
  146. $suffix = "()";
  147. try {
  148. if ($name == "") {
  149. $this->columnBindings[$i] = null;
  150. } else if (strstr($name, $suffix) !== false) {
  151. $this->columnBindings[$i] = $this->bindMethod(substr($name, 0, strlen($name)-strlen($suffix)));
  152. } else {
  153. $this->columnBindings[$i] = $this->bindField($name);
  154. }
  155. } catch (Exception $e) {
  156. $this->exception($heads, $e);
  157. }
  158. $i=$i+1;
  159. }
  160. }
  161. /**
  162. * @param String name
  163. * @return TypeAdapter
  164. */
  165. protected function bindMethod($name) {
  166. return PHPFIT_TypeAdapter::onMethod($this, $name);
  167. }
  168.  
  169. /**
  170. * @param String name
  171. * @return TypeAdapter
  172. */
  173. protected function bindField($name) {
  174. return PHPFIT_TypeAdapter::onField($this, $name);
  175. }
  176. public function reset() {
  177. }
  178. public function execute() {
  179. }
  180.  
  181. }
  182. ?>

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