MediaWikiPHPUnitTestListener.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. class MediaWikiPHPUnitTestListener extends PHPUnit_Framework_BaseTestListener {
  3. /**
  4. * @var string
  5. */
  6. protected $logChannel = 'PHPUnitCommand';
  7. protected function getTestName( PHPUnit_Framework_Test $test ) {
  8. $name = get_class( $test );
  9. if ( $test instanceof PHPUnit\Framework\TestCase ) {
  10. $name .= '::' . $test->getName( true );
  11. }
  12. return $name;
  13. }
  14. protected function getErrorName( Exception $exception ) {
  15. $name = get_class( $exception );
  16. $name = "[$name] " . $exception->getMessage();
  17. return $name;
  18. }
  19. /**
  20. * An error occurred.
  21. *
  22. * @param PHPUnit_Framework_Test $test
  23. * @param Exception $e
  24. * @param float $time
  25. */
  26. public function addError( PHPUnit_Framework_Test $test, Exception $e, $time ) {
  27. wfDebugLog(
  28. $this->logChannel,
  29. 'ERROR in ' . $this->getTestName( $test ) . ': ' . $this->getErrorName( $e )
  30. );
  31. }
  32. /**
  33. * A failure occurred.
  34. *
  35. * @param PHPUnit_Framework_Test $test
  36. * @param PHPUnit_Framework_AssertionFailedError $e
  37. * @param float $time
  38. */
  39. public function addFailure( PHPUnit_Framework_Test $test,
  40. PHPUnit_Framework_AssertionFailedError $e, $time
  41. ) {
  42. wfDebugLog(
  43. $this->logChannel,
  44. 'FAILURE in ' . $this->getTestName( $test ) . ': ' . $this->getErrorName( $e )
  45. );
  46. }
  47. /**
  48. * Incomplete test.
  49. *
  50. * @param PHPUnit_Framework_Test $test
  51. * @param Exception $e
  52. * @param float $time
  53. */
  54. public function addIncompleteTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
  55. wfDebugLog(
  56. $this->logChannel,
  57. 'Incomplete test ' . $this->getTestName( $test ) . ': ' . $this->getErrorName( $e )
  58. );
  59. }
  60. /**
  61. * Skipped test.
  62. *
  63. * @param PHPUnit_Framework_Test $test
  64. * @param Exception $e
  65. * @param float $time
  66. */
  67. public function addSkippedTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
  68. wfDebugLog(
  69. $this->logChannel,
  70. 'Skipped test ' . $this->getTestName( $test ) . ': ' . $this->getErrorName( $e )
  71. );
  72. }
  73. /**
  74. * A test suite started.
  75. *
  76. * @param PHPUnit_Framework_TestSuite $suite
  77. */
  78. public function startTestSuite( PHPUnit_Framework_TestSuite $suite ) {
  79. wfDebugLog( $this->logChannel, 'START suite ' . $suite->getName() );
  80. }
  81. /**
  82. * A test suite ended.
  83. *
  84. * @param PHPUnit_Framework_TestSuite $suite
  85. */
  86. public function endTestSuite( PHPUnit_Framework_TestSuite $suite ) {
  87. wfDebugLog( $this->logChannel, 'END suite ' . $suite->getName() );
  88. }
  89. /**
  90. * A test started.
  91. *
  92. * @param PHPUnit_Framework_Test $test
  93. */
  94. public function startTest( PHPUnit_Framework_Test $test ) {
  95. Hooks::run( 'MediaWikiPHPUnitTest::startTest', [ $test ] );
  96. wfDebugLog( $this->logChannel, 'Start test ' . $this->getTestName( $test ) );
  97. }
  98. /**
  99. * A test ended.
  100. *
  101. * @param PHPUnit_Framework_Test $test
  102. * @param float $time
  103. */
  104. public function endTest( PHPUnit_Framework_Test $test, $time ) {
  105. Hooks::run( 'MediaWikiPHPUnitTest::endTest', [ $test, $time ] );
  106. wfDebugLog( $this->logChannel, 'End test ' . $this->getTestName( $test ) );
  107. }
  108. }