private $originalSpi;
/** @var Spi|null */
private $spi;
- /** @var array|null */
- private $lastTestLogs;
/**
* A test started.
LoggerFactory::registerProvider( $this->spi );
}
+ public function addRiskyTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
+ $this->augmentTestWithLogs( $test );
+ }
+
+ public function addIncompleteTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
+ $this->augmentTestWithLogs( $test );
+ }
+
+ public function addSkippedTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
+ $this->augmentTestWithLogs( $test );
+ }
+
+ public function addError( PHPUnit_Framework_Test $test, Exception $e, $time ) {
+ $this->augmentTestWithLogs( $test );
+ }
+
+ public function addWarning( PHPUnit_Framework_Test $test, PHPUnit\Framework\Warning $e, $time ) {
+ $this->augmentTestWithLogs( $test );
+ }
+
+ public function addFailure( PHPUnit_Framework_Test $test,
+ PHPUnit_Framework_AssertionFailedError $e, $time
+ ) {
+ $this->augmentTestWithLogs( $test );
+ }
+
+ private function augmentTestWithLogs( PHPUnit_Framework_Test $test ) {
+ if ( $this->spi ) {
+ $logs = $this->spi->getLogs();
+ $formatted = $this->formatLogs( $logs );
+ $test->_formattedMediaWikiLogs = $formatted;
+ }
+ }
+
/**
* A test ended.
*
* @param float $time
*/
public function endTest( PHPUnit_Framework_Test $test, $time ) {
- $this->lastTestLogs = $this->spi->getLogs();
LoggerFactory::registerProvider( $this->originalSpi );
$this->originalSpi = null;
$this->spi = null;
* Get string formatted logs generated during the last
* test to execute.
*
+ * @param array $logs
* @return string
*/
- public function getLog() {
- $logs = $this->lastTestLogs;
- if ( !$logs ) {
- return '';
- }
+ private function formatLogs( array $logs ) {
$message = [];
foreach ( $logs as $log ) {
$message[] = sprintf(
class MediaWikiPHPUnitCommand extends PHPUnit_TextUI_Command {
private $cliArgs;
- private $logListener;
public function __construct( $ignorableOptions, $cliArgs ) {
$ignore = function ( $arg ) {
// Add our own listeners
$this->arguments['listeners'][] = new MediaWikiPHPUnitTestListener;
- $this->logListener = new MediaWikiLoggerPHPUnitTestListener;
- $this->arguments['listeners'][] = $this->logListener;
+ $this->arguments['listeners'][] = new MediaWikiLoggerPHPUnitTestListener;
// Output only to stderr to avoid "Headers already sent" problems
$this->arguments['stderr'] = true;
- // We could create a printer instance and avoid passing the
- // listener statically, but then we have to recreate the
- // appropriate arguments handling + defaults.
+ // Use a custom result printer that includes per-test logging output
+ // when nothing is provided.
if ( !isset( $this->arguments['printer'] ) ) {
$this->arguments['printer'] = MediaWikiPHPUnitResultPrinter::class;
}
}
protected function createRunner() {
- MediaWikiPHPUnitResultPrinter::setLogListener( $this->logListener );
$runner = new MediaWikiTestRunner;
$runner->setMwCliArgs( $this->cliArgs );
return $runner;
<?php
class MediaWikiPHPUnitResultPrinter extends PHPUnit_TextUI_ResultPrinter {
- /** @var MediaWikiLoggerPHPUnitTestListener */
- private static $logListener;
-
- public static function setLogListener( MediaWikiLoggerPHPUnitTestListener $logListener ) {
- self::$logListener = $logListener;
- }
-
protected function printDefectTrace( PHPUnit_Framework_TestFailure $defect ) {
- $log = self::$logListener->getLog();
- if ( $log ) {
- $this->write( "=== Logs generated by test case\n{$log}\n===\n" );
+ $test = $defect->failedTest();
+ if ( $test !== null && isset( $test->_formattedMediaWikiLogs ) ) {
+ $log = $test->_formattedMediaWikiLogs;
+ if ( $log ) {
+ $this->write( "=== Logs generated by test case\n{$log}\n===\n" );
+ }
}
parent::printDefectTrace( $defect );
}