SimpleReporter.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Codeception\Extension;
  3. use Codeception\Event\TestEvent;
  4. use Codeception\Events;
  5. use Codeception\Extension;
  6. use Codeception\Test\Descriptor;
  7. /**
  8. * This extension demonstrates how you can implement console output of your own.
  9. * Recommended to be used for development purposes only.
  10. */
  11. class SimpleReporter extends Extension
  12. {
  13. public function _initialize()
  14. {
  15. $this->options['silent'] = false; // turn on printing for this extension
  16. $this->_reconfigure(['settings' => ['silent' => true]]); // turn off printing for everything else
  17. }
  18. // we are listening for events
  19. public static $events = [
  20. Events::SUITE_BEFORE => 'beforeSuite',
  21. Events::TEST_END => 'after',
  22. Events::TEST_SUCCESS => 'success',
  23. Events::TEST_FAIL => 'fail',
  24. Events::TEST_ERROR => 'error',
  25. ];
  26. public function beforeSuite()
  27. {
  28. $this->writeln("");
  29. }
  30. public function success()
  31. {
  32. $this->write('[+] ');
  33. }
  34. public function fail()
  35. {
  36. $this->write('[-] ');
  37. }
  38. public function error()
  39. {
  40. $this->write('[E] ');
  41. }
  42. // we are printing test status and time taken
  43. public function after(TestEvent $e)
  44. {
  45. $seconds_input = $e->getTime();
  46. // stack overflow: http://stackoverflow.com/questions/16825240/how-to-convert-microtime-to-hhmmssuu
  47. $seconds = (int)($milliseconds = (int)($seconds_input * 1000)) / 1000;
  48. $time = ($seconds % 60) . (($milliseconds === 0) ? '' : '.' . $milliseconds);
  49. $this->write(Descriptor::getTestSignature($e->getTest()));
  50. $this->writeln(' (' . $time . 's)');
  51. }
  52. }