Page MenuHomePhabricator

Improve MediaWiki\Sniffs\PHPUnit\PHPUnitTestTrait::isTestClass()
Open, Needs TriagePublic

Description

In MediaWiki\Sniffs\PHPUnit\PHPUnitTestTrait, you currently have

private static $PHPUNIT_CLASSES = [
    // @phan-suppress-previous-line PhanReadOnlyPrivateProperty Traits cannot have constants
    'MediaWikiTestCase' => 'MediaWikiTestCase',
    'MediaWikiUnitTestCase' => 'MediaWikiUnitTestCase',
    'MediaWikiIntegrationTestCase' => 'MediaWikiIntegrationTestCase',
    'PHPUnit_Framework_TestCase' => 'PHPUnit_Framework_TestCase',
    // This class may be 'use'd, but checking for that would be complicated
    'PHPUnit\\Framework\\TestCase' => 'PHPUnit\\Framework\\TestCase',
];

The MediaWiki entries, of course, only make sense within MediaWiki and not for any reusers like us. But that's ok.

The entry for PHPUnit\Framework\TestCase is admittedly not comprehensive.

You also have

return array_key_exists( $extendedClass, self::$PHPUNIT_CLASSES ) ||
    (bool)preg_match(
        '/(?:Test(?:Case)?(?:Base)?|Suite)$/',
        $phpcsFile->getDeclarationName( $classToken )
    );

which checks for extending a class in the list, or the class itself happening to end in "Test", "TestCase", "TestBase", "TestCaseBase", or "Suite".

It would be nice if we could use phpcs's <config> or <properties> to extend this list. But even looking for extending any class name ending in "TestCase", "TestBase", "TestCaseBase", or "Suite" would be an improvement.

Event Timeline

Another option might be to make the trait stuff protected rather than private, so we can subclass the relevant tests and override them.

Change 698289 had a related patch set uploaded (by DannyS712; author: DannyS712):

[mediawiki/tools/codesniffer@master] PHPUnitTestTrait: check if extending a class that sounds like a test

https://gerrit.wikimedia.org/r/698289