wiki.techinc.nl/tests/phpunit/includes/objectcache/ObjectCacheTest.php
Timo Tijhof bf34238252 objectcache,rdbms: Widen needlessly narrow @covers test annotations
We'd lose more useful coverage and spend more effort keeping these
accurate through refactors etc than is worth the theoretically "bad"
accidental coverage. Plus, even then we still very often forget to
update these and waste time digging into seemingly uncovered code
and either write duplicate test or discover they are already covered.

Especially for private methods this can be a flawed endavour as
we tend to trust tests when changing those and thus feel we shouldn't
update tests, except it's riddled with private method mentions for
coverage purposes (not in terms of actual called code).

I think the best practice is to write good narrow unit tests,
not to write bad tests and then hide their coverage. Generally
that's what we do. Maybe these are useful when invoking a huge
legacy class, but generally we don't need these I think, at least
in the libs our team maintains.

Change-Id: I4c7d826c7ec654b9efa20778c46c498784661f1c
2023-02-21 21:27:40 +00:00

105 lines
2.6 KiB
PHP

<?php
use MediaWiki\MainConfigNames;
/**
* @covers ObjectCache
* @group BagOStuff
*/
class ObjectCacheTest extends MediaWikiIntegrationTestCase {
protected function setUp(): void {
// Parent calls ObjectCache::clear() among other things
parent::setUp();
$this->setCacheConfig();
$this->setMainCache( CACHE_NONE );
$this->overrideConfigValues( [
MainConfigNames::MessageCacheType => CACHE_NONE,
MainConfigNames::ParserCacheType => CACHE_NONE,
] );
}
private function setCacheConfig( $arr = [] ) {
$defaults = [
CACHE_NONE => [ 'class' => EmptyBagOStuff::class ],
CACHE_DB => [ 'class' => SqlBagOStuff::class ],
CACHE_ANYTHING => [ 'factory' => 'ObjectCache::newAnything' ],
// Mock ACCEL with 'hash' as being installed.
// This makes tests deterministic regardless of APC.
CACHE_ACCEL => [ 'class' => HashBagOStuff::class ],
'hash' => [ 'class' => HashBagOStuff::class ],
];
$this->overrideConfigValue( MainConfigNames::ObjectCaches, $arr + $defaults );
}
public function testNewAnythingNothing() {
$this->assertInstanceOf(
SqlBagOStuff::class,
ObjectCache::newAnything( [] ),
'No available types. Fallback to DB'
);
}
public function testNewAnythingHash() {
$this->setMainCache( CACHE_HASH );
$this->assertInstanceOf(
HashBagOStuff::class,
ObjectCache::newAnything( [] ),
'Use an available type (hash)'
);
}
public function testNewAnythingAccel() {
$this->setMainCache( CACHE_ACCEL );
$this->assertInstanceOf(
HashBagOStuff::class,
ObjectCache::newAnything( [] ),
'Use an available type (CACHE_ACCEL)'
);
}
public function testNewAnythingNoAccel() {
$this->setMainCache( CACHE_ACCEL );
$this->setCacheConfig( [
// Mock APC not being installed (T160519, T147161)
CACHE_ACCEL => [ 'class' => EmptyBagOStuff::class ]
] );
$this->assertInstanceOf(
SqlBagOStuff::class,
ObjectCache::newAnything( [] ),
'Fallback to DB if available types fall back to Empty'
);
}
public function testNewAnythingNoAccelNoDb() {
$this->setMainCache( CACHE_ACCEL );
$this->setCacheConfig( [
// Mock APC not being installed (T160519, T147161)
CACHE_ACCEL => [ 'class' => EmptyBagOStuff::class ]
] );
$this->getServiceContainer()->disableStorage();
$this->assertInstanceOf(
EmptyBagOStuff::class,
ObjectCache::newAnything( [] ),
'Fallback to none if available types and DB are unavailable'
);
}
public function testNewAnythingNothingNoDb() {
$this->getServiceContainer()->disableStorage();
$this->assertInstanceOf(
EmptyBagOStuff::class,
ObjectCache::newAnything( [] ),
'No available types or DB. Fallback to none.'
);
}
}