wiki.techinc.nl/tests/phpunit/mocks/TestLocalisationCache.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

65 lines
2.1 KiB
PHP
Raw Normal View History

<?php
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use Wikimedia\TestingAccessWrapper;
/**
* A test-only LocalisationCache that caches all data in memory for test speed.
*/
class TestLocalisationCache extends LocalisationCache {
/**
* A cache of the parsed data for tests. Services are reset between every test, which forces
* localization to be recached between every test, which is unreasonably slow. As an
* optimization, we cache our data in a static member for tests.
*/
private static MapCacheLRU $testingCache;
private const PROPERTY_NAMES = [ 'data', 'sourceLanguage' ];
/** @var self */
private $selfAccess;
public function __construct( ...$params ) {
parent::__construct( ...$params );
// Limit the cache size (entries are approx. 1 MB each) but not too much. Critical for tests
// that use e.g. 5 different languages, and then the same 5 languages again, and again, …
self::$testingCache ??= new MapCacheLRU( 16 );
$this->selfAccess = TestingAccessWrapper::newFromObject( $this );
}
public function recache( $code ) {
$hookContainer = MediaWikiServices::getInstance()->getHookContainer();
// Test run performance is killed if we have to regenerate l10n for every test
$cacheKey = sha1( json_encode( [
$code,
$this->selfAccess->options->get( MainConfigNames::ExtensionMessagesFiles ),
$this->selfAccess->options->get( MainConfigNames::MessagesDirs ),
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
$hookContainer->getHandlerDescriptions( 'LocalisationCacheRecacheFallback' ),
$hookContainer->getHandlerDescriptions( 'LocalisationCacheRecache' ),
] ) );
$cache = self::$testingCache->get( $cacheKey );
if ( $cache ) {
foreach ( self::PROPERTY_NAMES as $prop ) {
$this->$prop[$code] = $cache[$prop];
}
$loadedItems = $this->selfAccess->loadedItems;
foreach ( $cache['data'] as $key => $_ ) {
$loadedItems[$code][$key] = true;
}
$this->selfAccess->loadedItems = $loadedItems;
return;
}
parent::recache( $code );
$cache = [];
foreach ( self::PROPERTY_NAMES as $prop ) {
$cache[$prop] = $this->$prop[$code];
}
self::$testingCache->set( $cacheKey, $cache );
}
}