diff options
-rw-r--r-- | includes/ServiceWiring.php | 12 | ||||
-rw-r--r-- | includes/objectcache/ObjectCache.php | 4 | ||||
-rw-r--r-- | tests/phpunit/MediaWikiIntegrationTestCase.php | 20 | ||||
-rw-r--r-- | tests/phpunit/includes/objectcache/ObjectCacheTest.php | 8 |
4 files changed, 27 insertions, 17 deletions
diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index bb5c8b20e012..b57265d3c695 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -2325,6 +2325,18 @@ return [ ); }, + '_LocalClusterCache' => static function ( MediaWikiServices $services ): BagOStuff { + $mainConfig = $services->getMainConfig(); + $id = $mainConfig->get( MainConfigNames::MainCacheType ); + $params = $mainConfig->get( MainConfigNames::ObjectCaches )[$id] ?? null; + if ( !$params ) { + throw new UnexpectedValueException( + "\$wgObjectCaches must have \"$id\" set (via \$wgMainCacheType)" + ); + } + return ObjectCache::newFromParams( $params, $services ); + }, + '_MediaWikiTitleCodec' => static function ( MediaWikiServices $services ): MediaWikiTitleCodec { return new MediaWikiTitleCodec( $services->getContentLanguage(), diff --git a/includes/objectcache/ObjectCache.php b/includes/objectcache/ObjectCache.php index b6505ee7088f..288293ea9aed 100644 --- a/includes/objectcache/ObjectCache.php +++ b/includes/objectcache/ObjectCache.php @@ -298,9 +298,7 @@ class ObjectCache { * @return BagOStuff */ public static function getLocalClusterInstance() { - global $wgMainCacheType; - - return self::getInstance( $wgMainCacheType ); + return MediaWikiServices::getInstance()->get( '_LocalClusterCache' ); } /** diff --git a/tests/phpunit/MediaWikiIntegrationTestCase.php b/tests/phpunit/MediaWikiIntegrationTestCase.php index c8a9871d96bb..1f35d92a4d52 100644 --- a/tests/phpunit/MediaWikiIntegrationTestCase.php +++ b/tests/phpunit/MediaWikiIntegrationTestCase.php @@ -891,23 +891,25 @@ abstract class MediaWikiIntegrationTestCase extends PHPUnit\Framework\TestCase { * * @note This will cause any existing service instances to be reset. * - * @param string|BagOStuff $cache - * + * @param string|int|BagOStuff $cache * @return string|int The new value of the MainCacheType setting. */ protected function setMainCache( $cache ) { if ( $cache instanceof BagOStuff ) { - // ObjectCache::$instances is reset after each test by resetNonGlobalServices(). - ObjectCache::$instances[ 'UTCache' ] = $cache; - $cache = 'UTCache'; + $cacheId = 'UTCache'; + ObjectCache::$instances[ $cacheId ] = $cache; + } else { + $cacheId = $cache; + $cache = ObjectCache::getInstance( $cacheId ); } - if ( !is_string( $cache ) && !is_int( $cache ) ) { - throw new InvalidArgumentException( 'Bad type of $cache parameter: ' . get_debug_type( $cache ) ); + if ( !is_string( $cacheId ) && !is_int( $cacheId ) ) { + throw new InvalidArgumentException( 'Bad type of $cache parameter: ' . get_debug_type( $cacheId ) ); } - $this->overrideConfigValue( MainConfigNames::MainCacheType, $cache ); - return $cache; + $this->overrideConfigValue( MainConfigNames::MainCacheType, $cacheId ); + $this->setService( '_LocalClusterCache', $cache ); + return $cacheId; } /** diff --git a/tests/phpunit/includes/objectcache/ObjectCacheTest.php b/tests/phpunit/includes/objectcache/ObjectCacheTest.php index a6477b0ba843..ac3b77f1854f 100644 --- a/tests/phpunit/includes/objectcache/ObjectCacheTest.php +++ b/tests/phpunit/includes/objectcache/ObjectCacheTest.php @@ -62,12 +62,11 @@ class ObjectCacheTest extends MediaWikiIntegrationTestCase { } public function testNewAnythingNoAccel() { - $this->setMainCache( CACHE_ACCEL ); - + // Mock APC not being installed (T160519, T147161) $this->setCacheConfig( [ - // Mock APC not being installed (T160519, T147161) CACHE_ACCEL => [ 'class' => EmptyBagOStuff::class ] ] ); + $this->setMainCache( CACHE_ACCEL ); $this->assertInstanceOf( SqlBagOStuff::class, @@ -77,12 +76,11 @@ class ObjectCacheTest extends MediaWikiIntegrationTestCase { } public function testNewAnythingNoAccelNoDb() { - $this->setMainCache( CACHE_ACCEL ); - $this->setCacheConfig( [ // Mock APC not being installed (T160519, T147161) CACHE_ACCEL => [ 'class' => EmptyBagOStuff::class ] ] ); + $this->setMainCache( CACHE_ACCEL ); $this->getServiceContainer()->disableStorage(); |