diff options
author | Daimona Eaytoy <daimona.wiki@gmail.com> | 2023-07-14 01:40:18 +0200 |
---|---|---|
committer | Daimona Eaytoy <daimona.wiki@gmail.com> | 2023-08-11 00:36:47 +0000 |
commit | ed26774004752fbe2cd029340cd12d2611f6a4e9 (patch) | |
tree | d9d4bdd13a33679795645a39cb787b495acc7b47 /tests/phpunit/MediaWikiUnitTestCase.php | |
parent | 0250f3e12dd936879e47558aded13e683ae05695 (diff) | |
download | mediawikicore-ed26774004752fbe2cd029340cd12d2611f6a4e9.tar.gz mediawikicore-ed26774004752fbe2cd029340cd12d2611f6a4e9.zip |
phpunit: Disallow access to MediaWikiServices in unit tests
Unit tests should never access the global service locator, as that's a
pretty evident sign that the test isn't testing something in isolation.
HttpRequestFactoryTest fails with this restriction, because it uses
the UrlUtils service for parsing URLs. Make it an integration test and
add a TODO about injecting UrlUtils.
Bug: T227900
Change-Id: I5117eab95f57297eb02bed0a964c32d887de3226
Diffstat (limited to 'tests/phpunit/MediaWikiUnitTestCase.php')
-rw-r--r-- | tests/phpunit/MediaWikiUnitTestCase.php | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/tests/phpunit/MediaWikiUnitTestCase.php b/tests/phpunit/MediaWikiUnitTestCase.php index 54f7ee5b5c40..e4f82990e140 100644 --- a/tests/phpunit/MediaWikiUnitTestCase.php +++ b/tests/phpunit/MediaWikiUnitTestCase.php @@ -21,6 +21,7 @@ use MediaWiki\Logger\LoggerFactory; use MediaWiki\Logger\NullSpi; +use MediaWiki\MediaWikiServices; use PHPUnit\Framework\Exception; use PHPUnit\Framework\TestCase; use Wikimedia\ObjectFactory\ObjectFactory; @@ -101,6 +102,8 @@ abstract class MediaWikiUnitTestCase extends TestCase { foreach ( self::$unitGlobals as $key => $value ) { $GLOBALS[ $key ] = $value; } + + MediaWikiServices::disallowGlobalInstanceInUnitTests(); } /** @@ -154,6 +157,9 @@ abstract class MediaWikiUnitTestCase extends TestCase { foreach ( self::$originalGlobals as $key => &$value ) { $GLOBALS[ $key ] =& $value; } + unset( $value ); + + MediaWikiServices::allowGlobalInstanceAfterUnitTests(); } } |