Merge "Stash WatchedItem changes so that the jobs run from the queue"
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiLoggerPHPUnitTestListener.php
1 <?php
2
3 use MediaWiki\Logger\LoggerFactory;
4 use MediaWiki\Logger\Spi;
5 use MediaWiki\Logger\LogCapturingSpi;
6
7 /**
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.
11 */
12 class MediaWikiLoggerPHPUnitTestListener extends PHPUnit_Framework_BaseTestListener {
13 /** @var Spi|null */
14 private $originalSpi;
15 /** @var Spi|null */
16 private $spi;
17 /** @var array|null */
18 private $lastTestLogs;
19
20 /**
21 * A test started.
22 *
23 * @param PHPUnit_Framework_Test $test
24 */
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 );
30 }
31
32 /**
33 * A test ended.
34 *
35 * @param PHPUnit_Framework_Test $test
36 * @param float $time
37 */
38 public function endTest( PHPUnit_Framework_Test $test, $time ) {
39 $this->lastTestLogs = $this->spi->getLogs();
40 LoggerFactory::registerProvider( $this->originalSpi );
41 $this->originalSpi = null;
42 $this->spi = null;
43 }
44
45 /**
46 * Get string formatted logs generated during the last
47 * test to execute.
48 *
49 * @return string
50 */
51 public function getLog() {
52 $logs = $this->lastTestLogs;
53 if ( !$logs ) {
54 return '';
55 }
56 $message = [];
57 foreach ( $logs as $log ) {
58 $message[] = sprintf(
59 '[%s] [%s] %s %s',
60 $log['channel'],
61 $log['level'],
62 $log['message'],
63 json_encode( $log['context'] )
64 );
65 }
66 return implode( "\n", $message );
67 }
68 }