DotReporter.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Codeception\Extension;
  3. use Codeception\Event\FailEvent;
  4. use Codeception\Events;
  5. use Codeception\Extension;
  6. use Codeception\Subscriber\Console;
  7. /**
  8. * DotReporter provides less verbose output for test execution.
  9. * Like PHPUnit printer it prints dots "." for successful testes and "F" for failures.
  10. *
  11. * ![](https://cloud.githubusercontent.com/assets/220264/26132800/4d23f336-3aab-11e7-81ba-2896a4c623d2.png)
  12. *
  13. * ```bash
  14. * ..........
  15. * ..........
  16. * ..........
  17. * ..........
  18. * ..........
  19. * ..........
  20. * ..........
  21. * ..........
  22. *
  23. * Time: 2.07 seconds, Memory: 20.00MB
  24. *
  25. * OK (80 tests, 124 assertions)
  26. * ```
  27. *
  28. *
  29. * Enable this reporter with `--ext option`
  30. *
  31. * ```
  32. * codecept run --ext DotReporter
  33. * ```
  34. *
  35. * Failures and Errors are printed by a standard Codeception reporter.
  36. * Use this extension as an example for building custom reporters.
  37. */
  38. class DotReporter extends Extension
  39. {
  40. /**
  41. * @var Console
  42. */
  43. protected $standardReporter;
  44. protected $errors = [];
  45. protected $failures = [];
  46. protected $width = 10;
  47. protected $currentPos = 0;
  48. public function _initialize()
  49. {
  50. $this->options['silent'] = false; // turn on printing for this extension
  51. $this->_reconfigure(['settings' => ['silent' => true]]); // turn off printing for everything else
  52. $this->standardReporter = new Console($this->options);
  53. $this->width = $this->standardReporter->detectWidth();
  54. }
  55. // we are listening for events
  56. public static $events = [
  57. Events::SUITE_BEFORE => 'beforeSuite',
  58. Events::TEST_SUCCESS => 'success',
  59. Events::TEST_FAIL => 'fail',
  60. Events::TEST_ERROR => 'error',
  61. Events::TEST_SKIPPED => 'skipped',
  62. Events::TEST_FAIL_PRINT => 'printFailed'
  63. ];
  64. public function beforeSuite()
  65. {
  66. $this->writeln("");
  67. }
  68. public function success()
  69. {
  70. $this->printChar('.');
  71. }
  72. public function fail(FailEvent $e)
  73. {
  74. $this->printChar("<error>F</error>");
  75. }
  76. public function error(FailEvent $e)
  77. {
  78. $this->printChar('<error>E</error>');
  79. }
  80. public function skipped()
  81. {
  82. $this->printChar('S');
  83. }
  84. protected function printChar($char)
  85. {
  86. if ($this->currentPos >= $this->width) {
  87. $this->writeln('');
  88. $this->currentPos = 0;
  89. }
  90. $this->write($char);
  91. $this->currentPos++;
  92. }
  93. public function printFailed(FailEvent $event)
  94. {
  95. $this->standardReporter->printFail($event);
  96. }
  97. }