LimeOutputArray.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /*
  3. * This file is part of the Lime test framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * Formats test results as multidimensional array.
  13. *
  14. * @package Lime
  15. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  16. * @version SVN: $Id: LimeOutputArray.php 23701 2009-11-08 21:23:40Z bschussek $
  17. */
  18. class LimeOutputArray implements LimeOutputInterface
  19. {
  20. protected
  21. $serialize = false,
  22. $results = array(),
  23. $currentResults = null;
  24. /**
  25. * Constructor.
  26. *
  27. * @param boolean $serialize Whether the array should be serialized before printing
  28. */
  29. public function __construct($serialize = false)
  30. {
  31. $this->serialize = $serialize;
  32. }
  33. /**
  34. * (non-PHPdoc)
  35. * @see output/LimeOutputInterface#supportsThreading()
  36. */
  37. public function supportsThreading()
  38. {
  39. return true;
  40. }
  41. /**
  42. * (non-PHPdoc)
  43. * @see output/LimeOutputInterface#focus($file)
  44. */
  45. public function focus($file)
  46. {
  47. $this->currentResults =& $this->getResults($file);
  48. }
  49. /**
  50. * (non-PHPdoc)
  51. * @see output/LimeOutputInterface#close()
  52. */
  53. public function close()
  54. {
  55. }
  56. /**
  57. * (non-PHPdoc)
  58. * @see output/LimeOutputInterface#plan($amount)
  59. */
  60. public function plan($amount)
  61. {
  62. $this->currentResults['stats']['plan'] = $amount;
  63. }
  64. /**
  65. * (non-PHPdoc)
  66. * @see output/LimeOutputInterface#pass($message, $file, $line)
  67. */
  68. public function pass($message, $file, $line)
  69. {
  70. $this->currentResults['stats']['total']++;
  71. $this->currentResults['stats']['passed'][] = $this->addTest(true, $line, $file, $message);
  72. }
  73. /**
  74. * (non-PHPdoc)
  75. * @see output/LimeOutputInterface#fail($message, $file, $line, $error)
  76. */
  77. public function fail($message, $file, $line, $error = null)
  78. {
  79. $index = $this->addTest(false, $line, $file, $message);
  80. $this->currentResults['stats']['total']++;
  81. $this->currentResults['stats']['failed'][] = $index;
  82. if (!is_null($error))
  83. {
  84. $this->currentResults['tests'][$index]['error'] = $error;
  85. }
  86. }
  87. /**
  88. * (non-PHPdoc)
  89. * @see output/LimeOutputInterface#skip($message, $file, $line)
  90. */
  91. public function skip($message, $file, $line)
  92. {
  93. $this->currentResults['stats']['total']++;
  94. $this->currentResults['stats']['skipped'][] = $this->addTest(true, $line, $file, $message);
  95. }
  96. /**
  97. * (non-PHPdoc)
  98. * @see output/LimeOutputInterface#todo($message, $file, $line)
  99. */
  100. public function todo($message, $file, $line)
  101. {
  102. }
  103. /**
  104. * (non-PHPdoc)
  105. * @see output/LimeOutputInterface#warning($message, $file, $line)
  106. */
  107. public function warning($message, $file, $line)
  108. {
  109. }
  110. /**
  111. * (non-PHPdoc)
  112. * @see output/LimeOutputInterface#error($error)
  113. */
  114. public function error(LimeError $error)
  115. {
  116. }
  117. /**
  118. * (non-PHPdoc)
  119. * @see output/LimeOutputInterface#comment($message)
  120. */
  121. public function comment($message)
  122. {
  123. }
  124. /**
  125. * (non-PHPdoc)
  126. * @see output/LimeOutputInterface#flush()
  127. */
  128. public function flush()
  129. {
  130. if ($this->serialize)
  131. {
  132. print serialize($this->results);
  133. }
  134. else
  135. {
  136. var_export($this->results);
  137. }
  138. }
  139. /**
  140. * Returns the results as array.
  141. *
  142. * @return array
  143. */
  144. public function toArray()
  145. {
  146. return $this->results;
  147. }
  148. /**
  149. * Returns the result array of the given test file.
  150. *
  151. * @param string $file
  152. * @return array
  153. */
  154. protected function &getResults($file)
  155. {
  156. foreach ($this->results as $key => &$fileResults)
  157. {
  158. if ($fileResults['file'] == $file)
  159. {
  160. return $fileResults;
  161. }
  162. }
  163. $newResults = array(
  164. 'file' => $file,
  165. 'tests' => array(),
  166. 'stats' => array(
  167. 'plan' => 0,
  168. 'total' => 0,
  169. 'failed' => array(),
  170. 'passed' => array(),
  171. 'skipped' => array(),
  172. ),
  173. );
  174. $this->results[] =& $newResults;
  175. return $newResults;
  176. }
  177. /**
  178. * Addsthe given test to the test results.
  179. *
  180. * @param boolean $status
  181. * @param integer $line
  182. * @param string $file
  183. * @param string $message
  184. * @return integer
  185. */
  186. protected function addTest($status, $line, $file, $message)
  187. {
  188. $index = count($this->currentResults['tests']) + 1;
  189. $this->currentResults['tests'][$index] = array(
  190. 'line' => $line,
  191. 'file' => $file,
  192. 'message' => $message,
  193. 'status' => $status,
  194. );
  195. return $index;
  196. }
  197. }