phpunit: Remove MediaWikiPHPUnitTestListener

The two hooks were moved to a PHPUnit extensions which uses hooks. The
rest was simply deleted. The reason for this change is that TestListener
was removed in PHPUnit 9, with PHPUnit hooks as replacement.

No hooks exist for startSuite/endSuite, plus we should avoid adding too
much functionality: if the output of PHPUnit isn't clear, that should be
reported upstream. Additionally, PHPUnit hooks were also deprecated in
PHPUnit 10, which means that extending PHPUnit becomes more and more
difficult. As such, the two hooks will be hard-deprecated & removed
soon.

Removing all these wfDebugLog and other function calls should also
hopefully speed up the suite.

Bug: T243600
Bug: T225730
Change-Id: I2b67cd0074aca4b0ea76696ce9f24be68f5f88f8
This commit is contained in:
Daimona Eaytoy 2021-03-20 15:48:35 +01:00
parent d1d5a4bc55
commit 8176dac4a6
5 changed files with 22 additions and 140 deletions

View file

@ -59,11 +59,11 @@ $wgAutoloadClasses += [
'MediaWikiCliOptions' => "$testDir/phpunit/MediaWikiCliOptions.php",
'MediaWikiCoversValidator' => "$testDir/phpunit/MediaWikiCoversValidator.php",
'MediaWikiGroupValidator' => "$testDir/phpunit/MediaWikiGroupValidator.php",
'MediaWikiHooksPHPUnitExtension' => "$testDir/phpunit/MediaWikiHooksPHPUnitExtension.php",
'MediaWikiLangTestCase' => "$testDir/phpunit/MediaWikiLangTestCase.php",
'MediaWikiLoggerPHPUnitExtension' => "$testDir/phpunit/MediaWikiLoggerPHPUnitExtension.php",
'MediaWikiPHPUnitCommand' => "$testDir/phpunit/MediaWikiPHPUnitCommand.php",
'MediaWikiPHPUnitResultPrinter' => "$testDir/phpunit/MediaWikiPHPUnitResultPrinter.php",
'MediaWikiPHPUnitTestListener' => "$testDir/phpunit/MediaWikiPHPUnitTestListener.php",
'MediaWikiTestCase' => "$testDir/phpunit/MediaWikiIntegrationTestCase.php",
'MediaWikiTestCaseTrait' => "$testDir/phpunit/MediaWikiTestCaseTrait.php",
'MediaWikiUnitTestCase' => "$testDir/phpunit/MediaWikiUnitTestCase.php",

View file

@ -0,0 +1,20 @@
<?php
use PHPUnit\Runner\AfterTestHook;
use PHPUnit\Runner\BeforeTestHook;
/**
* Runs a hook whenever a test starts or ends
*/
class MediaWikiHooksPHPUnitExtension implements BeforeTestHook, AfterTestHook {
/** @inheritDoc */
public function executeBeforeTest( string $test ): void {
Hooks::runner()->onMediaWikiPHPUnitTest__startTest( $test );
}
/** @inheritDoc */
public function executeAfterTest( string $test, float $time ): void {
Hooks::runner()->onMediaWikiPHPUnitTest__endTest( $test, $time );
}
}

View file

@ -9,9 +9,6 @@ class MediaWikiPHPUnitCommand extends Command {
$this->arguments['configuration'] = __DIR__ . '/suite.xml';
}
// Add our own listeners
$this->arguments['listeners'][] = new MediaWikiPHPUnitTestListener;
// Output only to stderr to avoid "Headers already sent" problems
$this->arguments['stderr'] = true;

View file

@ -1,136 +0,0 @@
<?php
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestListenerDefaultImplementation;
use PHPUnit\Framework\TestSuite;
/**
* @todo Use TestHook interfaces. But there seems to be no replacement for start/endTestSuite!
*/
class MediaWikiPHPUnitTestListener implements TestListener {
use TestListenerDefaultImplementation;
/**
* @var string
*/
protected $logChannel = 'PHPUnitCommand';
/**
* @param Test $test
* @return string
*/
protected function getTestName( Test $test ) : string {
$name = get_class( $test );
if ( $test instanceof TestCase ) {
$name .= '::' . $test->getName( true );
}
return $name;
}
protected function getErrorName( Throwable $exception ) : string {
$name = get_class( $exception );
$name = "[$name] " . $exception->getMessage();
return $name;
}
/**
* An error occurred.
*
* @param Test $test
* @param Throwable $e
* @param float $time
*/
public function addError( Test $test, Throwable $e, float $time ) : void {
wfDebugLog(
$this->logChannel,
'ERROR in ' . $this->getTestName( $test ) . ': ' . $this->getErrorName( $e )
);
}
/**
* A failure occurred.
*
* @param Test $test
* @param AssertionFailedError $e
* @param float $time
*/
public function addFailure( Test $test, AssertionFailedError $e, float $time ) : void {
wfDebugLog(
$this->logChannel,
'FAILURE in ' . $this->getTestName( $test ) . ': ' . $this->getErrorName( $e )
);
}
/**
* Incomplete test.
*
* @param Test $test
* @param Throwable $t
* @param float $time
*/
public function addIncompleteTest( Test $test, Throwable $t, float $time ) : void {
wfDebugLog(
$this->logChannel,
'Incomplete test ' . $this->getTestName( $test ) . ': ' . $this->getErrorName( $t )
);
}
/**
* Skipped test.
*
* @param Test $test
* @param Throwable $t
* @param float $time
*/
public function addSkippedTest( Test $test, Throwable $t, float $time ) : void {
wfDebugLog(
$this->logChannel,
'Skipped test ' . $this->getTestName( $test ) . ': ' . $this->getErrorName( $t )
);
}
/**
* A test suite started.
*
* @param TestSuite $suite
*/
public function startTestSuite( TestSuite $suite ) : void {
wfDebugLog( $this->logChannel, 'START suite ' . $suite->getName() );
}
/**
* A test suite ended.
*
* @param TestSuite $suite
*/
public function endTestSuite( TestSuite $suite ) : void {
wfDebugLog( $this->logChannel, 'END suite ' . $suite->getName() );
}
/**
* A test started.
*
* @param Test $test
*/
public function startTest( Test $test ) : void {
Hooks::runner()->onMediaWikiPHPUnitTest__startTest( $test );
wfDebugLog( $this->logChannel, 'Start test ' . $this->getTestName( $test ) );
}
/**
* A test ended.
*
* @param Test $test
* @param float $time
*/
public function endTest( Test $test, float $time ) : void {
Hooks::runner()->onMediaWikiPHPUnitTest__endTest( $test, $time );
wfDebugLog( $this->logChannel, 'End test ' . $this->getTestName( $test ) );
}
}

View file

@ -92,5 +92,6 @@
</listeners>
<extensions>
<extension class="MediaWikiLoggerPHPUnitExtension" />
<extension class="MediaWikiHooksPHPUnitExtension" />
</extensions>
</phpunit>