2019-01-18 23:28:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-01-10 00:00:51 +00:00
|
|
|
use MediaWiki\Logger\LogCapturingSpi;
|
2019-02-26 12:17:09 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
|
|
|
|
use MediaWiki\Logger\Spi;
|
2020-01-23 18:27:08 +00:00
|
|
|
use PHPUnit\Runner\AfterIncompleteTestHook;
|
|
|
|
|
use PHPUnit\Runner\AfterRiskyTestHook;
|
|
|
|
|
use PHPUnit\Runner\AfterSkippedTestHook;
|
2021-11-26 02:55:26 +00:00
|
|
|
use PHPUnit\Runner\AfterSuccessfulTestHook;
|
2020-01-23 18:27:08 +00:00
|
|
|
use PHPUnit\Runner\AfterTestErrorHook;
|
|
|
|
|
use PHPUnit\Runner\AfterTestFailureHook;
|
|
|
|
|
use PHPUnit\Runner\AfterTestHook;
|
|
|
|
|
use PHPUnit\Runner\AfterTestWarningHook;
|
|
|
|
|
use PHPUnit\Runner\BeforeTestHook;
|
2019-01-18 23:28:26 +00:00
|
|
|
|
|
|
|
|
/**
|
2021-11-26 02:55:26 +00:00
|
|
|
* Replaces the logging SPI on each test run. This allows another component
|
|
|
|
|
* (the printer) to fetch the logs when reporting why a test failed.
|
|
|
|
|
*
|
|
|
|
|
* Also logs test start and end messages to the original log.
|
2019-01-18 23:28:26 +00:00
|
|
|
*/
|
2020-01-23 18:27:08 +00:00
|
|
|
class MediaWikiLoggerPHPUnitExtension implements
|
|
|
|
|
BeforeTestHook,
|
|
|
|
|
AfterRiskyTestHook,
|
|
|
|
|
AfterIncompleteTestHook,
|
|
|
|
|
AfterSkippedTestHook,
|
2021-11-26 02:55:26 +00:00
|
|
|
AfterSuccessfulTestHook,
|
2020-01-23 18:27:08 +00:00
|
|
|
AfterTestErrorHook,
|
|
|
|
|
AfterTestWarningHook,
|
|
|
|
|
AfterTestFailureHook,
|
|
|
|
|
AfterTestHook
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @var string[]
|
|
|
|
|
* @todo Can we avoid global state?
|
|
|
|
|
*/
|
|
|
|
|
public static $testsCollection;
|
2019-01-18 23:28:26 +00:00
|
|
|
/** @var Spi|null */
|
|
|
|
|
private $originalSpi;
|
|
|
|
|
/** @var Spi|null */
|
|
|
|
|
private $spi;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-01-23 18:27:08 +00:00
|
|
|
* @inheritDoc
|
2019-01-18 23:28:26 +00:00
|
|
|
*/
|
2020-01-23 18:27:08 +00:00
|
|
|
public function executeBeforeTest( string $test ): void {
|
2019-01-18 23:28:26 +00:00
|
|
|
$this->originalSpi = LoggerFactory::getProvider();
|
|
|
|
|
$this->spi = new LogCapturingSpi( $this->originalSpi );
|
|
|
|
|
LoggerFactory::registerProvider( $this->spi );
|
2021-11-26 02:55:26 +00:00
|
|
|
$this->log( "Start test $test" );
|
2019-01-18 23:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
2020-01-23 18:27:08 +00:00
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function executeAfterRiskyTest( string $test, string $message, float $time ): void {
|
2019-03-04 21:44:39 +00:00
|
|
|
$this->augmentTestWithLogs( $test );
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-23 18:27:08 +00:00
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function executeAfterIncompleteTest( string $test, string $message, float $time ): void {
|
2019-03-04 21:44:39 +00:00
|
|
|
$this->augmentTestWithLogs( $test );
|
2021-11-26 02:55:26 +00:00
|
|
|
$this->log( "Incomplete test $test: $message" );
|
2019-03-04 21:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
2020-01-23 18:27:08 +00:00
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function executeAfterSkippedTest( string $test, string $message, float $time ): void {
|
2019-03-04 21:44:39 +00:00
|
|
|
$this->augmentTestWithLogs( $test );
|
2021-11-26 02:55:26 +00:00
|
|
|
$this->log( "Skipped test $test: $message" );
|
2019-03-04 21:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
2020-01-23 18:27:08 +00:00
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function executeAfterTestError( string $test, string $message, float $time ): void {
|
2019-03-04 21:44:39 +00:00
|
|
|
$this->augmentTestWithLogs( $test );
|
2021-11-26 02:55:26 +00:00
|
|
|
$this->log( "ERROR in test $test: $message" );
|
2019-03-04 21:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
2020-01-23 18:27:08 +00:00
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function executeAfterTestWarning( string $test, string $message, float $time ): void {
|
2021-11-26 02:55:26 +00:00
|
|
|
$this->log( "Warning in test $test: $message" );
|
2019-03-04 21:44:39 +00:00
|
|
|
$this->augmentTestWithLogs( $test );
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-23 18:27:08 +00:00
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function executeAfterTestFailure( string $test, string $message, float $time ): void {
|
2021-11-26 02:55:26 +00:00
|
|
|
$this->log( "FAILURE in test $test: $message" );
|
2019-03-04 21:44:39 +00:00
|
|
|
$this->augmentTestWithLogs( $test );
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-23 18:27:08 +00:00
|
|
|
private function augmentTestWithLogs( string $test ) {
|
2023-02-09 06:15:32 +00:00
|
|
|
if ( $this->spi && getenv( 'PHPUNIT_LOGS' ) !== '0' ) {
|
2019-03-04 21:44:39 +00:00
|
|
|
$logs = $this->spi->getLogs();
|
|
|
|
|
$formatted = $this->formatLogs( $logs );
|
2020-01-23 18:27:08 +00:00
|
|
|
self::$testsCollection[$test] = $formatted;
|
2019-03-04 21:44:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-26 02:55:26 +00:00
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function executeAfterSuccessfulTest( string $test, float $time ): void {
|
|
|
|
|
$this->log(
|
|
|
|
|
sprintf( "Successful test %s, completed in %.6f seconds",
|
|
|
|
|
$test, $time ) );
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-23 18:27:08 +00:00
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function executeAfterTest( string $test, float $time ): void {
|
2019-01-18 23:28:26 +00:00
|
|
|
LoggerFactory::registerProvider( $this->originalSpi );
|
|
|
|
|
$this->originalSpi = null;
|
|
|
|
|
$this->spi = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get string formatted logs generated during the last
|
|
|
|
|
* test to execute.
|
|
|
|
|
*
|
2019-03-04 21:44:39 +00:00
|
|
|
* @param array $logs
|
2019-01-18 23:28:26 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2019-03-04 21:44:39 +00:00
|
|
|
private function formatLogs( array $logs ) {
|
2019-01-18 23:28:26 +00:00
|
|
|
$message = [];
|
|
|
|
|
foreach ( $logs as $log ) {
|
2019-04-06 22:53:30 +00:00
|
|
|
if ( $log['channel'] === 'PHPUnitCommand' ) {
|
|
|
|
|
// Don't print the log of PHPUnit events while running PHPUnit,
|
|
|
|
|
// because PHPUnit is already printing those already.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-01-18 23:28:26 +00:00
|
|
|
$message[] = sprintf(
|
|
|
|
|
'[%s] [%s] %s %s',
|
|
|
|
|
$log['channel'],
|
|
|
|
|
$log['level'],
|
|
|
|
|
$log['message'],
|
|
|
|
|
json_encode( $log['context'] )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return implode( "\n", $message );
|
|
|
|
|
}
|
2020-01-23 18:27:08 +00:00
|
|
|
|
2021-11-26 02:55:26 +00:00
|
|
|
private function log( $message ) {
|
|
|
|
|
$spi = $this->originalSpi ?: LoggerFactory::getProvider();
|
|
|
|
|
$logger = $spi->getLogger( 'PHPUnit' );
|
|
|
|
|
$logger->debug( $message );
|
|
|
|
|
}
|
2019-01-18 23:28:26 +00:00
|
|
|
}
|