wiki.techinc.nl/tests/phpunit/MediaWikiLoggerPHPUnitExtension.php
Daimona Eaytoy 86c0a64e91 Use PHPUnit hooks for augmented logs
The TestListener interface is deprecated. This replacement has the
downside of using a public static property, but there seems to be no
other way to pass extra data to the printer (BTW: the ResultPrinter
class is internal, so it'd be even better if we could avoid subclassing
it).

Bug: T243600
Change-Id: I083146b7336ac09dfd077c8e6817553738282662
2020-09-22 11:28:10 +00:00

121 lines
3.2 KiB
PHP

<?php
use MediaWiki\Logger\LogCapturingSpi;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\Logger\Spi;
use PHPUnit\Runner\AfterIncompleteTestHook;
use PHPUnit\Runner\AfterRiskyTestHook;
use PHPUnit\Runner\AfterSkippedTestHook;
use PHPUnit\Runner\AfterTestErrorHook;
use PHPUnit\Runner\AfterTestFailureHook;
use PHPUnit\Runner\AfterTestHook;
use PHPUnit\Runner\AfterTestWarningHook;
use PHPUnit\Runner\BeforeTestHook;
/**
* Replaces the logging SPI on each test run. This allows
* another component (the printer) to fetch the logs when
* reporting why a test failed.
*/
class MediaWikiLoggerPHPUnitExtension implements
BeforeTestHook,
AfterRiskyTestHook,
AfterIncompleteTestHook,
AfterSkippedTestHook,
AfterTestErrorHook,
AfterTestWarningHook,
AfterTestFailureHook,
AfterTestHook
{
/**
* @var string[]
* @todo Can we avoid global state?
*/
public static $testsCollection;
/** @var Spi|null */
private $originalSpi;
/** @var Spi|null */
private $spi;
/**
* @inheritDoc
*/
public function executeBeforeTest( string $test ): void {
$this->lastTestLogs = null;
$this->originalSpi = LoggerFactory::getProvider();
$this->spi = new LogCapturingSpi( $this->originalSpi );
LoggerFactory::registerProvider( $this->spi );
}
/** @inheritDoc */
public function executeAfterRiskyTest( string $test, string $message, float $time ): void {
$this->augmentTestWithLogs( $test );
}
/** @inheritDoc */
public function executeAfterIncompleteTest( string $test, string $message, float $time ): void {
$this->augmentTestWithLogs( $test );
}
/** @inheritDoc */
public function executeAfterSkippedTest( string $test, string $message, float $time ): void {
$this->augmentTestWithLogs( $test );
}
/** @inheritDoc */
public function executeAfterTestError( string $test, string $message, float $time ): void {
$this->augmentTestWithLogs( $test );
}
/** @inheritDoc */
public function executeAfterTestWarning( string $test, string $message, float $time ): void {
$this->augmentTestWithLogs( $test );
}
/** @inheritDoc */
public function executeAfterTestFailure( string $test, string $message, float $time ): void {
$this->augmentTestWithLogs( $test );
}
private function augmentTestWithLogs( string $test ) {
if ( $this->spi ) {
$logs = $this->spi->getLogs();
$formatted = $this->formatLogs( $logs );
self::$testsCollection[$test] = $formatted;
}
}
/** @inheritDoc */
public function executeAfterTest( string $test, float $time ): void {
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
*/
private function formatLogs( array $logs ) {
$message = [];
foreach ( $logs as $log ) {
if ( $log['channel'] === 'PHPUnitCommand' ) {
// Don't print the log of PHPUnit events while running PHPUnit,
// because PHPUnit is already printing those already.
continue;
}
$message[] = sprintf(
'[%s] [%s] %s %s',
$log['channel'],
$log['level'],
$log['message'],
json_encode( $log['context'] )
);
}
return implode( "\n", $message );
}
}