3 use MediaWiki\Logger\LoggerFactory
;
4 use MediaWiki\Logger\Spi
;
5 use MediaWiki\Logger\LogCapturingSpi
;
8 * Replaces the logging SPI on each test run. This allows
9 * another component (the printer) to fetch the logs when
10 * reporting why a test failed.
12 class MediaWikiLoggerPHPUnitTestListener
extends PHPUnit_Framework_BaseTestListener
{
21 * @param PHPUnit_Framework_Test $test
23 public function startTest( PHPUnit_Framework_Test
$test ) {
24 $this->lastTestLogs
= null;
25 $this->originalSpi
= LoggerFactory
::getProvider();
26 $this->spi
= new LogCapturingSpi( $this->originalSpi
);
27 LoggerFactory
::registerProvider( $this->spi
);
30 public function addRiskyTest( PHPUnit_Framework_Test
$test, Exception
$e, $time ) {
31 $this->augmentTestWithLogs( $test );
34 public function addIncompleteTest( PHPUnit_Framework_Test
$test, Exception
$e, $time ) {
35 $this->augmentTestWithLogs( $test );
38 public function addSkippedTest( PHPUnit_Framework_Test
$test, Exception
$e, $time ) {
39 $this->augmentTestWithLogs( $test );
42 public function addError( PHPUnit_Framework_Test
$test, Exception
$e, $time ) {
43 $this->augmentTestWithLogs( $test );
46 public function addWarning( PHPUnit_Framework_Test
$test, PHPUnit\Framework\Warning
$e, $time ) {
47 $this->augmentTestWithLogs( $test );
50 public function addFailure( PHPUnit_Framework_Test
$test,
51 PHPUnit_Framework_AssertionFailedError
$e, $time
53 $this->augmentTestWithLogs( $test );
56 private function augmentTestWithLogs( PHPUnit_Framework_Test
$test ) {
58 $logs = $this->spi
->getLogs();
59 $formatted = $this->formatLogs( $logs );
60 $test->_formattedMediaWikiLogs
= $formatted;
67 * @param PHPUnit_Framework_Test $test
70 public function endTest( PHPUnit_Framework_Test
$test, $time ) {
71 LoggerFactory
::registerProvider( $this->originalSpi
);
72 $this->originalSpi
= null;
77 * Get string formatted logs generated during the last
83 private function formatLogs( array $logs ) {
85 foreach ( $logs as $log ) {
86 if ( $log['channel'] === 'PHPUnitCommand' ) {
87 // Don't print the log of PHPUnit events while running PHPUnit,
88 // because PHPUnit is already printing those already.
96 json_encode( $log['context'] )
99 return implode( "\n", $message );