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
{
17 /** @var array|null */
18 private $lastTestLogs;
23 * @param PHPUnit_Framework_Test $test
25 public function startTest( PHPUnit_Framework_Test
$test ) {
26 $this->lastTestLogs
= null;
27 $this->originalSpi
= LoggerFactory
::getProvider();
28 $this->spi
= new LogCapturingSpi( $this->originalSpi
);
29 LoggerFactory
::registerProvider( $this->spi
);
35 * @param PHPUnit_Framework_Test $test
38 public function endTest( PHPUnit_Framework_Test
$test, $time ) {
39 $this->lastTestLogs
= $this->spi
->getLogs();
40 LoggerFactory
::registerProvider( $this->originalSpi
);
41 $this->originalSpi
= null;
46 * Get string formatted logs generated during the last
51 public function getLog() {
52 $logs = $this->lastTestLogs
;
57 foreach ( $logs as $log ) {
63 json_encode( $log['context'] )
66 return implode( "\n", $message );