LimeOutputConsoleSummary.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. * Colorizes test results and summarizes them in the console.
  13. *
  14. * For each test file, one line is printed in the console with a few optional
  15. * lines in case the file contains errors or failed tests.
  16. *
  17. * @package Lime
  18. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  19. * @version SVN: $Id: LimeOutputConsoleSummary.php 25932 2009-12-27 19:55:32Z bschussek $
  20. */
  21. class LimeOutputConsoleSummary implements LimeOutputInterface
  22. {
  23. protected
  24. $printer = null,
  25. $options = array(),
  26. $startTime = 0,
  27. $file = null,
  28. $results = array(),
  29. $actualFiles = 0,
  30. $failedFiles = 0,
  31. $actualTests = 0,
  32. $expectedTests = 0,
  33. $failedTests = 0;
  34. /**
  35. * Constructor.
  36. *
  37. * @param LimePrinter $printer The printer for printing text to the console
  38. * @param array $options The options of this output
  39. */
  40. public function __construct(LimePrinter $printer, array $options = array())
  41. {
  42. $this->printer = $printer;
  43. $this->startTime = time();
  44. $this->options = array_merge(array(
  45. 'base_dir' => null,
  46. 'processes' => 1,
  47. 'verbose' => false,
  48. ), $options);
  49. }
  50. public function supportsThreading()
  51. {
  52. return true;
  53. }
  54. public function focus($file)
  55. {
  56. if (!array_key_exists($file, $this->results))
  57. {
  58. $this->results[$file] = new LimeOutputResult();
  59. }
  60. $this->file = $file;
  61. }
  62. public function close()
  63. {
  64. if (!is_null($this->file))
  65. {
  66. $result = $this->results[$this->file];
  67. $this->actualFiles++;
  68. $this->actualTests += $result->getNbActual();
  69. $this->expectedTests += $result->getNbExpected();
  70. $this->failedTests += $result->getNbFailures();
  71. $path = $this->truncate($this->file);
  72. if (strlen($path) > 71)
  73. {
  74. $path = substr($path, -71);
  75. }
  76. $this->printer->printText(str_pad($path, 73, '.'));
  77. if ($result->hasErrors() || $result->hasFailures() || $result->isIncomplete())
  78. {
  79. $this->failedFiles++;
  80. $this->printer->printLine("not ok", LimePrinter::NOT_OK);
  81. }
  82. else if ($result->hasWarnings())
  83. {
  84. $this->printer->printLine("warning", LimePrinter::WARNING);
  85. }
  86. else
  87. {
  88. $this->printer->printLine("ok", LimePrinter::OK);
  89. }
  90. if ($result->isIncomplete())
  91. {
  92. $this->printer->printLine(' Plan Mismatch:', LimePrinter::COMMENT);
  93. if ($result->getNbActual() > $result->getNbExpected())
  94. {
  95. $this->printer->printLine(sprintf(' Looks like you only planned %s tests but ran %s.', $result->getNbExpected(), $result->getNbActual()));
  96. }
  97. else
  98. {
  99. $this->printer->printLine(sprintf(' Looks like you planned %s tests but only ran %s.', $result->getNbExpected(), $result->getNbActual()));
  100. }
  101. }
  102. if ($result->hasFailures())
  103. {
  104. $this->printer->printLine(' Failed Tests:', LimePrinter::COMMENT);
  105. $i = 0;
  106. foreach ($result->getFailures() as $number => $failed)
  107. {
  108. if (!$this->options['verbose'] && $i > 2)
  109. {
  110. $this->printer->printLine(sprintf(' ... and %s more', $result->getNbFailures()-$i));
  111. break;
  112. }
  113. ++$i;
  114. $this->printer->printLine(' not ok '.$number.' - '.$failed[0]);
  115. }
  116. }
  117. if ($result->hasWarnings())
  118. {
  119. $this->printer->printLine(' Warnings:', LimePrinter::COMMENT);
  120. foreach ($result->getWarnings() as $i => $warning)
  121. {
  122. if (!$this->options['verbose'] && $i > 2)
  123. {
  124. $this->printer->printLine(sprintf(' ... and %s more', $result->getNbWarnings()-$i));
  125. break;
  126. }
  127. $this->printer->printLine(' '.$warning[0]);
  128. if ($this->options['verbose'])
  129. {
  130. $this->printer->printText(' (in ');
  131. $this->printer->printText($this->truncate($warning[1]), LimePrinter::TRACE);
  132. $this->printer->printText(' on line ');
  133. $this->printer->printText($warning[2], LimePrinter::TRACE);
  134. $this->printer->printLine(')');
  135. }
  136. }
  137. }
  138. if ($result->hasErrors())
  139. {
  140. $this->printer->printLine(' Errors:', LimePrinter::COMMENT);
  141. foreach ($result->getErrors() as $i => $error)
  142. {
  143. if (!$this->options['verbose'] && $i > 2)
  144. {
  145. $this->printer->printLine(sprintf(' ... and %s more', $result->getNbErrors()-$i));
  146. break;
  147. }
  148. $this->printer->printLine(' '.$error->getMessage());
  149. if ($this->options['verbose'])
  150. {
  151. $this->printer->printText(' (in ');
  152. $this->printer->printText($this->truncate($error->getFile()), LimePrinter::TRACE);
  153. $this->printer->printText(' on line ');
  154. $this->printer->printText($error->getLine(), LimePrinter::TRACE);
  155. $this->printer->printLine(')');
  156. }
  157. }
  158. }
  159. if ($result->hasTodos())
  160. {
  161. $this->printer->printLine(' TODOs:', LimePrinter::COMMENT);
  162. foreach ($result->getTodos() as $i => $todo)
  163. {
  164. if (!$this->options['verbose'] && $i > 2)
  165. {
  166. $this->printer->printLine(sprintf(' ... and %s more', $result->getNbTodos()-$i));
  167. break;
  168. }
  169. $this->printer->printLine(' '.$todo);
  170. }
  171. }
  172. }
  173. }
  174. public function plan($amount)
  175. {
  176. $this->results[$this->file]->addPlan($amount);
  177. }
  178. public function pass($message, $file, $line)
  179. {
  180. $this->results[$this->file]->addPassed();
  181. }
  182. public function fail($message, $file, $line, $error = null)
  183. {
  184. $this->results[$this->file]->addFailure(array($message, $file, $line, $error));
  185. }
  186. public function skip($message, $file, $line)
  187. {
  188. $this->results[$this->file]->addSkipped();
  189. }
  190. public function todo($message, $file, $line)
  191. {
  192. $this->results[$this->file]->addTodo($message);
  193. }
  194. public function warning($message, $file, $line)
  195. {
  196. $this->results[$this->file]->addWarning(array($message, $file, $line));
  197. }
  198. public function error(LimeError $error)
  199. {
  200. $this->results[$this->file]->addError($error);
  201. }
  202. public function comment($message) {}
  203. public function flush()
  204. {
  205. if ($this->failedFiles > 0)
  206. {
  207. $stats = sprintf(' Failed %d/%d test scripts, %.2f%% okay. %d/%d subtests failed, %.2f%% okay.',
  208. $this->failedFiles, $this->actualFiles, 100 - 100*$this->failedFiles/max(1,$this->actualFiles),
  209. $this->failedTests, $this->expectedTests, 100 - 100*$this->failedTests/max(1,$this->expectedTests));
  210. $this->printer->printBox($stats, LimePrinter::NOT_OK);
  211. }
  212. else
  213. {
  214. $time = max(1, time() - $this->startTime);
  215. $stats = sprintf(' Files=%d, Tests=%d, Time=%02d:%02d, Processes=%d',
  216. $this->actualFiles, $this->actualTests, floor($time/60), $time%60, $this->options['processes']);
  217. $this->printer->printBox(' All tests successful.', LimePrinter::HAPPY);
  218. $this->printer->printBox($stats, LimePrinter::HAPPY);
  219. }
  220. }
  221. protected function truncate($file)
  222. {
  223. $extension = pathinfo($file, PATHINFO_EXTENSION);
  224. $file = substr($file, 0, strlen($file)-strlen($extension));
  225. if (!is_null($this->options['base_dir']))
  226. {
  227. return str_replace($this->options['base_dir'], '', $file);
  228. }
  229. else
  230. {
  231. return $file;
  232. }
  233. }
  234. }