wiki.techinc.nl/tests/phpunit/includes/objectcache/ObjectCacheTest.php
Timo Tijhof b586d83422 objectcache: Complete coverage for newAnything()
* Fix typo that disabled testNewAnythingNoAccel().
  Follows-up c5a0fa5bed, accidentally committed a local hack
  to disable the test.

* Add missing case other types falling back and no DB.
* Add missing case of no other types and no DB.

Change-Id: If158f21053f0b3741f2625fe4455fdb31955a22f
2017-04-10 14:52:04 -07:00

115 lines
2.9 KiB
PHP

<?php
class ObjectCacheTest extends MediaWikiTestCase {
protected function setUp() {
// Parent calls ObjectCache::clear() among other things
parent::setUp();
$this->setCacheConfig();
$this->setMwGlobals( [
'wgMainCacheType' => CACHE_NONE,
'wgMessageCacheType' => CACHE_NONE,
'wgParserCacheType' => CACHE_NONE,
] );
}
private function setCacheConfig( $arr = [] ) {
$defaults = [
CACHE_NONE => [ 'class' => 'EmptyBagOStuff' ],
CACHE_DB => [ 'class' => 'SqlBagOStuff' ],
CACHE_ANYTHING => [ 'factory' => 'ObjectCache::newAnything' ],
// Mock ACCEL with 'hash' as being installed.
// This makes tests deterministic regardless of APC.
CACHE_ACCEL => [ 'class' => 'HashBagOStuff' ],
'hash' => [ 'class' => 'HashBagOStuff' ],
];
$this->setMwGlobals( 'wgObjectCaches', $arr + $defaults );
}
/** @covers ObjectCache::newAnything */
public function testNewAnythingNothing() {
$this->assertInstanceOf(
SqlBagOStuff::class,
ObjectCache::newAnything( [] ),
'No available types. Fallback to DB'
);
}
/** @covers ObjectCache::newAnything */
public function testNewAnythingHash() {
$this->setMwGlobals( [
'wgMainCacheType' => 'hash'
] );
$this->assertInstanceOf(
HashBagOStuff::class,
ObjectCache::newAnything( [] ),
'Use an available type (hash)'
);
}
/** @covers ObjectCache::newAnything */
public function testNewAnythingAccel() {
$this->setMwGlobals( [
'wgMainCacheType' => CACHE_ACCEL
] );
$this->assertInstanceOf(
HashBagOStuff::class,
ObjectCache::newAnything( [] ),
'Use an available type (CACHE_ACCEL)'
);
}
/** @covers ObjectCache::newAnything */
public function testNewAnythingNoAccel() {
$this->setMwGlobals( [
'wgMainCacheType' => CACHE_ACCEL
] );
$this->setCacheConfig( [
// Mock APC not being installed (T160519, T147161)
CACHE_ACCEL => [ 'class' => 'EmptyBagOStuff' ]
] );
$this->assertInstanceOf(
SqlBagOStuff::class,
ObjectCache::newAnything( [] ),
'Fallback to DB if available types fall back to Empty'
);
}
/** @covers ObjectCache::newAnything */
public function testNewAnythingNoAccelNoDb() {
$this->overrideMwServices(); // Ensures restore on tear down
MediaWiki\MediaWikiServices::disableStorageBackend();
$this->setMwGlobals( [
'wgMainCacheType' => CACHE_ACCEL
] );
$this->setCacheConfig( [
// Mock APC not being installed (T160519, T147161)
CACHE_ACCEL => [ 'class' => 'EmptyBagOStuff' ]
] );
$this->assertInstanceOf(
EmptyBagOStuff::class,
ObjectCache::newAnything( [] ),
'Fallback to none if available types and DB are unavailable'
);
}
/** @covers ObjectCache::newAnything */
public function testNewAnythingNothingNoDb() {
$this->overrideMwServices();
MediaWiki\MediaWikiServices::disableStorageBackend();
$this->assertInstanceOf(
EmptyBagOStuff::class,
ObjectCache::newAnything( [] ),
'No available types or DB. Fallback to none.'
);
}
}