summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'MLEB/Translate/tests/phpunit/utils/MessageGroupWANCacheTest.php')
-rw-r--r--MLEB/Translate/tests/phpunit/utils/MessageGroupWANCacheTest.php91
1 files changed, 91 insertions, 0 deletions
diff --git a/MLEB/Translate/tests/phpunit/utils/MessageGroupWANCacheTest.php b/MLEB/Translate/tests/phpunit/utils/MessageGroupWANCacheTest.php
new file mode 100644
index 00000000..4887b9f2
--- /dev/null
+++ b/MLEB/Translate/tests/phpunit/utils/MessageGroupWANCacheTest.php
@@ -0,0 +1,91 @@
+<?php
+class MessageGroupWANCacheTest extends MediaWikiIntegrationTestCase {
+ protected $mgCache;
+
+ protected function setUp() : void {
+ parent::setUp();
+ $this->mgCache = new MessageGroupWANCache(
+ new WANObjectCache( [ 'cache' => wfGetCache( 'hash' ) ] )
+ );
+ }
+
+ public function testCacheKeyConfiguration() {
+ $this->expectException( \InvalidArgumentException::class );
+ $this->expectExceptionMessage( 'Invalid cache key' );
+
+ $this->mgCache->configure( [
+ 'regenerator' => function () {
+ return 'hello';
+ }
+ ] );
+ }
+
+ public function testCacheRegeneratorConfig() {
+ $this->expectException( \InvalidArgumentException::class );
+ $this->expectExceptionMessage( 'Invalid regenerator' );
+
+ $this->mgCache->configure( [
+ 'key' => 'test',
+ 'regenerator' => 'hello-world'
+ ] );
+ }
+
+ public function testNoConfigureCall() {
+ $this->expectException( \InvalidArgumentException::class );
+ $this->expectExceptionMessage( 'configure function' );
+
+ $this->mgCache->setValue( [ 'abc' ] );
+ }
+
+ public function testDefaultConfig() {
+ $cacheData = [ 'dummy', 'data' ];
+ $this->mgCache->configure( [
+ 'key' => 'mg-wan-test',
+ 'regenerator' => function () use ( $cacheData ) {
+ return $cacheData;
+ }
+ ] );
+
+ $mgCacheData = $this->mgCache->getValue();
+ $this->assertEquals( $cacheData, $mgCacheData, 'correctly returns the data ' .
+ 'returned in regenerator function.' );
+ }
+
+ public function testTouchCallbackConfig() {
+ $this->expectException( \InvalidArgumentException::class );
+ $this->expectExceptionMessage( 'touchedCallback is not callable' );
+
+ $this->mgCache->configure( [
+ 'key' => 'mg-wan-test',
+ 'regenerator' => function () {
+ return 'hello';
+ },
+ 'touchedCallback' => 'blah'
+ ] );
+ }
+
+ public function testTouchCallbackIsCalled() {
+ $wrapper = new DependencyWrapper( [ 'dummy' ] );
+
+ $mockMgLoader = $this->createMock( MockCacheMessageGroupLoader::class );
+ $this->mgCache->configure( [
+ 'key' => 'mg-wan-test',
+ 'regenerator' => [ $mockMgLoader, 'getGroups' ],
+ 'touchedCallback' => [ $mockMgLoader, 'isExpired' ]
+ ] );
+
+ $mockMgLoader->expects( $this->once() )
+ ->method( 'getGroups' )
+ ->willReturn( $wrapper );
+
+ $mockMgLoader->expects( $this->once() )
+ ->method( 'isExpired' )
+ ->with( $wrapper )
+ ->willReturn( false );
+
+ // touchedCallback is not called the first time,
+ // since the value was just obtained
+ $this->mgCache->getValue();
+ $this->mgCache->getValue();
+ }
+}