/test-suite/lib/Sweety/Reporter/CliReporter.php

https://github.com/h-kanjisan/swiftmailer · PHP · 203 lines · 111 code · 26 blank · 66 comment · 6 complexity · f560dfad40fe329b466a88d1d2dedb96 MD5 · raw file

  1. <?php
  2. require_once 'Sweety/Reporter.php';
  3. require_once 'Sweety/Reporter/CliTestCaseReporter.php';
  4. /**
  5. * The reporter used in command line reporting.
  6. * @package Sweety
  7. * @author Chris Corbyn
  8. */
  9. class Sweety_Reporter_CliReporter implements Sweety_Reporter
  10. {
  11. /**
  12. * True if this repoter is running.
  13. * @var boolean
  14. * @access private
  15. */
  16. private $_started = false;
  17. /**
  18. * The name to show this report as.
  19. * @var string
  20. * @access private
  21. */
  22. private $_name;
  23. /**
  24. * Aggregate scores from tests run.
  25. * @var int[]
  26. */
  27. private $_aggregates = array();
  28. /**
  29. * Creates a new CliReporter.
  30. */
  31. public function __construct($name)
  32. {
  33. $this->_name = $name;
  34. $this->_aggregates = array(
  35. 'cases' => 0,
  36. 'run' => 0,
  37. 'passes' => 0,
  38. 'fails' => 0,
  39. 'exceptions' => 0
  40. );
  41. }
  42. /**
  43. * Used so test case reporters can notify this reporter when they've completed.
  44. * @param string $testCase
  45. */
  46. public function notifyEnded($testCase)
  47. {
  48. $this->_aggregates['run']++;
  49. }
  50. /**
  51. * Get the reporter used to report on this specific test case.
  52. * @param string $testCase
  53. * @return Sweety_Reporter
  54. */
  55. public function getReporterFor($testCase)
  56. {
  57. $this->_aggregates['cases']++;
  58. $reporter = new Sweety_Reporter_CliTestCaseReporter($testCase, $this);
  59. return $reporter;
  60. }
  61. /**
  62. * Returns true if start() has been invoked.
  63. * @return boolean
  64. */
  65. public function isStarted()
  66. {
  67. return $this->_started;
  68. }
  69. /**
  70. * Start reporting.
  71. */
  72. public function start()
  73. {
  74. $this->_started = true;
  75. echo $this->_name . PHP_EOL;
  76. }
  77. /**
  78. * Report a skipped test case.
  79. * @param string $message
  80. * @param string $path
  81. */
  82. public function reportSkip($message, $path)
  83. {
  84. echo " \033[34m\033[1m\033[4mSkip\033[0m:";
  85. $messageLines = explode(PHP_EOL, wordwrap($message, 74, PHP_EOL));
  86. foreach ($messageLines as $line)
  87. {
  88. echo ' ' . $line . PHP_EOL;
  89. }
  90. echo ' in: ' . $path . PHP_EOL;
  91. }
  92. /**
  93. * Report a passing assertion.
  94. * @param string $message
  95. * @param string $path
  96. */
  97. public function reportPass($message, $path)
  98. {
  99. $this->_aggregates['passes']++;
  100. }
  101. /**
  102. * Report a failing assertion.
  103. * @param string $message
  104. * @param string $path
  105. */
  106. public function reportFail($message, $path)
  107. {
  108. $this->_aggregates['fails']++;
  109. echo "\033[31m" . $this->_aggregates['fails'] . ') ';
  110. echo $message . "\033[0m" . PHP_EOL;
  111. echo ' in: ' . $path . PHP_EOL;
  112. }
  113. /**
  114. * Report an unexpected exception.
  115. * @param string $message
  116. * @param string $path
  117. */
  118. public function reportException($message, $path)
  119. {
  120. $this->_aggregates['exceptions']++;
  121. echo "\033[31m\033[1mException" . $this->_aggregates['exceptions'] . "\033[0m!" . PHP_EOL;
  122. echo "\033[1m" . $message . "\033[0m" . PHP_EOL;
  123. echo ' in ' . $path . PHP_EOL;
  124. }
  125. /**
  126. * Report output from something like a dump().
  127. * @param string $output
  128. * @param string $path
  129. */
  130. public function reportOutput($output, $path)
  131. {
  132. if (preg_match('/^\{image @ (.*?)\}$/D', $output, $matches))
  133. {
  134. echo " \033[33mSmoke Test\033[0m" . PHP_EOL;
  135. echo ' Compare email sent with image @ ' . $matches[1] . PHP_EOL;
  136. }
  137. else
  138. {
  139. echo '--------------------' . PHP_EOL;
  140. echo $output . PHP_EOL;
  141. echo '--------------------' . PHP_EOL;
  142. }
  143. }
  144. /**
  145. * End reporting.
  146. */
  147. public function finish()
  148. {
  149. $this->_started = false;
  150. $incomplete = $this->_aggregates['cases'] - $this->_aggregates['run'];
  151. if ($incomplete)
  152. {
  153. echo '**********************' . PHP_EOL;
  154. echo $incomplete . ' test case(s) did not complete.' . PHP_EOL .
  155. 'This may be because invalid XML was output during the test run' . PHP_EOL .
  156. 'and/or because an error occured.' . PHP_EOL .
  157. 'Try running the tests separately for more detail.' . PHP_EOL;
  158. echo '**********************' . PHP_EOL;
  159. }
  160. $success = (!$this->_aggregates['fails'] && !$this->_aggregates['exceptions']
  161. && $this->_aggregates['cases'] == $this->_aggregates['run']);
  162. if ($success)
  163. {
  164. echo "\033[32m\033[1mOK\033[0m" . PHP_EOL;
  165. }
  166. else
  167. {
  168. echo "\033[31m\033[1mFAILURES!!!\033[0m" . PHP_EOL;
  169. }
  170. echo 'Test cases run: ';
  171. echo $this->_aggregates['run'] . '/' . $this->_aggregates['cases'] . ', ';
  172. echo 'Passes: ' . $this->_aggregates['passes'] . ', ';
  173. echo 'Failures: ' . $this->_aggregates['fails'] . ', ';
  174. echo 'Exceptions: '. $this->_aggregates['exceptions'] . PHP_EOL;
  175. exit((int) !$success);
  176. }
  177. }