wiki.techinc.nl/tests/phpunit/includes/parser/CacheTimeTest.php
daniel 6eea7d7ed5 Add test infra for ParserCache serialization/deserialization
Based on Daniel's work at Ia6e70179b7ee5ce4e93888585ccc30d92da165c3
however was changed enough to move into a separate changeset.

More acceptance tests and data will be added in a followup commit.

Bug: T264397
Change-Id: I135187e83cbfa02b97c5656f0752f8bf1ceb58d0
2020-10-09 08:14:57 -06:00

120 lines
3 KiB
PHP

<?php
namespace MediaWiki\Tests\Parser;
use CacheTime;
use MediaWikiIntegrationTestCase;
use MWTimestamp;
/**
* @covers CacheTime
* @package MediaWiki\Tests\Parser
*/
class CacheTimeTest extends MediaWikiIntegrationTestCase {
use SerializationTestTrait;
protected function setUp(): void {
parent::setUp();
MWTimestamp::setFakeTime( ParserCacheSerializationTestCases::FAKE_TIME );
$this->setMwGlobals( [
'wgParserCacheExpireTime' => ParserCacheSerializationTestCases::FAKE_CACHE_EXPIRY
] );
}
protected function tearDown() : void {
MWTimestamp::setFakeTime( null );
parent::tearDown();
}
/**
* Overrides SerializationTestTrait::getClassToTest
* @return string
*/
protected function getClassToTest() {
return CacheTime::class;
}
/**
* Overrides SerializationTestTrait::getSerializedDataPath
* @return string
*/
protected function getSerializedDataPath(): string {
return __DIR__ . '/../../data/ParserCache';
}
/**
* Overrides SerializationTestTrait::getTestInstancesAndAssertions
* @return array
*/
protected function getTestInstancesAndAssertions(): array {
return ParserCacheSerializationTestCases::getCacheTimeTestCases();
}
/**
* Overrides SerializationTestTrait::getSupportedSerializationFormats
* @return array
*/
protected function getSupportedSerializationFormats(): array {
return ParserCacheSerializationTestCases::getSupportedSerializationFormats(
$this->getClassToTest()
);
}
public function testCacheExpiryDoesNotIncrease() {
$cacheTime = new CacheTime();
$this->assertSame(
ParserCacheSerializationTestCases::FAKE_CACHE_EXPIRY,
$cacheTime->getCacheExpiry()
);
$cacheTime->updateCacheExpiry( 10 );
$this->assertSame( 10, $cacheTime->getCacheExpiry() );
$cacheTime->updateCacheExpiry( 5 );
$this->assertSame( 5, $cacheTime->getCacheExpiry() );
$cacheTime->updateCacheExpiry( 100500 );
$this->assertSame( 5, $cacheTime->getCacheExpiry() );
}
public function testCacheExpiryDoesNotIncreaseNotNegative() {
$cacheTime = new CacheTime();
$cacheTime->updateCacheExpiry( -10 );
$this->assertSame( 0, $cacheTime->getCacheExpiry() );
}
public function testCacheExpiryNotMoreThenGlobal() {
$cacheTime = new CacheTime();
$cacheTime->updateCacheExpiry(
ParserCacheSerializationTestCases::FAKE_CACHE_EXPIRY + 1
);
$this->assertSame(
ParserCacheSerializationTestCases::FAKE_CACHE_EXPIRY,
$cacheTime->getCacheExpiry()
);
}
public function testExpired() {
$cacheTime = new CacheTime();
$cacheTime->updateCacheExpiry( 0 );
$this->assertTrue( $cacheTime->expired( MWTimestamp::now() ) );
$cacheTime = new CacheTime();
$cacheTime->setCacheTime( MWTimestamp::now() );
$this->assertTrue(
$cacheTime->expired(
MWTimestamp::convert( TS_MW, MWTimestamp::now( TS_UNIX ) + 10 )
)
);
$cacheTime = new CacheTime();
$cacheTime->updateCacheExpiry( 10 );
$cacheTime->setCacheTime( MWTimestamp::now() );
$this->assertTrue(
$cacheTime->expired(
MWTimestamp::convert( TS_MW, MWTimestamp::now( TS_UNIX ) + 15 )
)
);
}
}