UI.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Codeception\PHPUnit\ResultPrinter;
  3. use Codeception\Event\FailEvent;
  4. use Codeception\Events;
  5. use Codeception\PHPUnit\DispatcherWrapper;
  6. use Codeception\Test\Unit;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Symfony\Component\EventDispatcher\EventDispatcher;
  9. class UI extends \PHPUnit\TextUI\ResultPrinter
  10. {
  11. use DispatcherWrapper;
  12. /**
  13. * @var EventDispatcher
  14. */
  15. protected $dispatcher;
  16. public function __construct(EventDispatcher $dispatcher, $options, $out = null)
  17. {
  18. parent::__construct($out, $options['verbosity'] > OutputInterface::VERBOSITY_NORMAL, $options['colors'] ? 'always' : 'never');
  19. $this->dispatcher = $dispatcher;
  20. }
  21. protected function printDefect(\PHPUnit\Framework\TestFailure $defect, $count)
  22. {
  23. $this->write("\n---------\n");
  24. $this->dispatch(
  25. $this->dispatcher,
  26. Events::TEST_FAIL_PRINT,
  27. new FailEvent($defect->failedTest(), null, $defect->thrownException(), $count)
  28. );
  29. }
  30. /**
  31. * @param \PHPUnit\Framework\TestFailure $defect
  32. */
  33. protected function printDefectTrace(\PHPUnit\Framework\TestFailure $defect)
  34. {
  35. $this->write($defect->getExceptionAsString());
  36. $this->writeNewLine();
  37. $stackTrace = \PHPUnit\Util\Filter::getFilteredStacktrace($defect->thrownException(), false);
  38. foreach ($stackTrace as $i => $frame) {
  39. if (!isset($frame['file'])) {
  40. continue;
  41. }
  42. $this->write(
  43. sprintf(
  44. "#%d %s(%s)",
  45. $i + 1,
  46. $frame['file'],
  47. isset($frame['line']) ? $frame['line'] : '?'
  48. )
  49. );
  50. $this->writeNewLine();
  51. }
  52. }
  53. public function startTest(\PHPUnit\Framework\Test $test)
  54. {
  55. if ($test instanceof Unit) {
  56. parent::startTest($test);
  57. }
  58. }
  59. public function endTest(\PHPUnit\Framework\Test $test, $time)
  60. {
  61. if ($test instanceof \PHPUnit\Framework\TestCase or $test instanceof \Codeception\Test\Test) {
  62. $this->numAssertions += $test->getNumAssertions();
  63. }
  64. $this->lastTestFailed = false;
  65. }
  66. public function addError(\PHPUnit\Framework\Test $test, \Exception $e, $time)
  67. {
  68. $this->lastTestFailed = true;
  69. }
  70. public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\AssertionFailedError $e, $time)
  71. {
  72. $this->lastTestFailed = true;
  73. }
  74. public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Warning $e, $time)
  75. {
  76. $this->lastTestFailed = true;
  77. }
  78. public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Exception $e, $time)
  79. {
  80. $this->lastTestFailed = true;
  81. }
  82. public function addSkippedTest(\PHPUnit\Framework\Test $test, \Exception $e, $time)
  83. {
  84. $this->lastTestFailed = true;
  85. }
  86. }