From b4c546f5ae99575be6b96de50228b263cf6e0c28 Mon Sep 17 00:00:00 2001 From: =?utf8?q?M=C3=A1t=C3=A9=20Szab=C3=B3?= Date: Mon, 17 Jun 2019 23:15:25 +0200 Subject: [PATCH] Introduce separate unit tests PHPUnit configuration This changeset lays down the basic groundwork required to implement T89432 and related tickets and is based on exploration done at the Prague Hackathon. The goal is to identify tests in MediaWiki core that can be run without having to install & configure MediaWiki and its dependencies, and provide a way to execute these tests via the standard phpunit entry point, allowing for faster development and integration with existing tooling like IDEs. This changeset creates a new subdirectory under phpunit/ and organizes it into a separate test suite. The environment for this suite is set up via a PHPUnit bootstrap file without a custom entry point. For B/C, this directory is also registered in suite.xml, to ensure that existing CI jobs still pick up tests in the new suite. For initial testing, a single test class, PasswordFactoryTest, was moved to this new suite. You can run the new suite using the follwoing command: $ vendor/bin/phpunit -d memory_limit=512M -c tests/phpunit/unit-tests.xml Bug: T84948 Bug: T89432 Bug: T87781 Change-Id: I69b92db3e70093570e05cc0a64c7780a278b321a --- tests/common/TestsAutoLoader.php | 1 + tests/phpunit/MediaWikiUnitTestCase.php | 29 ++++++++ tests/phpunit/suite.xml | 5 +- tests/phpunit/unit-tests.xml | 42 +++++++++++ .../includes/password/PasswordFactoryTest.php | 2 +- tests/phpunit/unit/initUnitTests.php | 69 +++++++++++++++++++ 6 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 tests/phpunit/MediaWikiUnitTestCase.php create mode 100644 tests/phpunit/unit-tests.xml rename tests/phpunit/{ => unit}/includes/password/PasswordFactoryTest.php (98%) create mode 100644 tests/phpunit/unit/initUnitTests.php diff --git a/tests/common/TestsAutoLoader.php b/tests/common/TestsAutoLoader.php index 861111a775..3b643a5d97 100644 --- a/tests/common/TestsAutoLoader.php +++ b/tests/common/TestsAutoLoader.php @@ -60,6 +60,7 @@ $wgAutoloadClasses += [ 'MediaWikiPHPUnitResultPrinter' => "$testDir/phpunit/MediaWikiPHPUnitResultPrinter.php", 'MediaWikiPHPUnitTestListener' => "$testDir/phpunit/MediaWikiPHPUnitTestListener.php", 'MediaWikiTestCase' => "$testDir/phpunit/MediaWikiTestCase.php", + 'MediaWikiUnitTestCase' => "$testDir/phpunit/MediaWikiUnitTestCase.php", 'MediaWikiTestResult' => "$testDir/phpunit/MediaWikiTestResult.php", 'MediaWikiTestRunner' => "$testDir/phpunit/MediaWikiTestRunner.php", 'PHPUnit4And6Compat' => "$testDir/phpunit/PHPUnit4And6Compat.php", diff --git a/tests/phpunit/MediaWikiUnitTestCase.php b/tests/phpunit/MediaWikiUnitTestCase.php new file mode 100644 index 0000000000..407be208b6 --- /dev/null +++ b/tests/phpunit/MediaWikiUnitTestCase.php @@ -0,0 +1,29 @@ + documentation + + unit + - Utility Broken - Stub diff --git a/tests/phpunit/unit-tests.xml b/tests/phpunit/unit-tests.xml new file mode 100644 index 0000000000..cd4118ca0d --- /dev/null +++ b/tests/phpunit/unit-tests.xml @@ -0,0 +1,42 @@ + + + + + unit + + + + + Broken + + + + + ../../includes + ../../languages + ../../maintenance + + ../../languages/messages + ../../languages/data/normalize-ar.php + ../../languages/data/normalize-ml.php + + + + diff --git a/tests/phpunit/includes/password/PasswordFactoryTest.php b/tests/phpunit/unit/includes/password/PasswordFactoryTest.php similarity index 98% rename from tests/phpunit/includes/password/PasswordFactoryTest.php rename to tests/phpunit/unit/includes/password/PasswordFactoryTest.php index a7b3557516..cbfddd4d72 100644 --- a/tests/phpunit/includes/password/PasswordFactoryTest.php +++ b/tests/phpunit/unit/includes/password/PasswordFactoryTest.php @@ -3,7 +3,7 @@ /** * @covers PasswordFactory */ -class PasswordFactoryTest extends MediaWikiTestCase { +class PasswordFactoryTest extends MediaWikiUnitTestCase { public function testConstruct() { $pf = new PasswordFactory(); $this->assertEquals( [ '' ], array_keys( $pf->getTypes() ) ); diff --git a/tests/phpunit/unit/initUnitTests.php b/tests/phpunit/unit/initUnitTests.php new file mode 100644 index 0000000000..212187711a --- /dev/null +++ b/tests/phpunit/unit/initUnitTests.php @@ -0,0 +1,69 @@ + $value ) { + $GLOBALS[$varName] = $value; + } +} + +define( 'MEDIAWIKI', true ); +define( 'MW_PHPUNIT_TEST', true ); + +// We don't use a settings file here but some code still assumes that one exists +define( 'MW_CONFIG_FILE', 'LocalSettings.php' ); + +$IP = realpath( __DIR__ . '/../../..' ); + +// these variables must be defined before setup runs +$GLOBALS['IP'] = $IP; +$GLOBALS['wgCommandLineMode'] = true; + +require_once "$IP/tests/common/TestSetup.php"; + +wfRequireOnceInGlobalScope( "$IP/includes/AutoLoader.php" ); +wfRequireOnceInGlobalScope( "$IP/includes/Defines.php" ); +wfRequireOnceInGlobalScope( "$IP/includes/DefaultSettings.php" ); +wfRequireOnceInGlobalScope( "$IP/includes/GlobalFunctions.php" ); + +require_once "$IP/tests/common/TestsAutoLoader.php"; + +TestSetup::applyInitialConfig(); -- 2.20.1