MediaWiki REL1_31
CachedBagOStuffTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\TestingAccessWrapper;
4
8class CachedBagOStuffTest extends PHPUnit\Framework\TestCase {
9
10 use MediaWikiCoversValidator;
11
16 public function testGetFromBackend() {
17 $backend = new HashBagOStuff;
18 $cache = new CachedBagOStuff( $backend );
19
20 $backend->set( 'foo', 'bar' );
21 $this->assertEquals( 'bar', $cache->get( 'foo' ) );
22
23 $backend->set( 'foo', 'baz' );
24 $this->assertEquals( 'bar', $cache->get( 'foo' ), 'cached' );
25 }
26
31 public function testSetAndDelete() {
32 $backend = new HashBagOStuff;
33 $cache = new CachedBagOStuff( $backend );
34
35 for ( $i = 0; $i < 10; $i++ ) {
36 $cache->set( "key$i", 1 );
37 $this->assertEquals( 1, $cache->get( "key$i" ) );
38 $this->assertEquals( 1, $backend->get( "key$i" ) );
39 $cache->delete( "key$i" );
40 $this->assertEquals( false, $cache->get( "key$i" ) );
41 $this->assertEquals( false, $backend->get( "key$i" ) );
42 }
43 }
44
49 public function testWriteCacheOnly() {
50 $backend = new HashBagOStuff;
51 $cache = new CachedBagOStuff( $backend );
52
53 $cache->set( 'foo', 'bar', 0, CachedBagOStuff::WRITE_CACHE_ONLY );
54 $this->assertEquals( 'bar', $cache->get( 'foo' ) );
55 $this->assertFalse( $backend->get( 'foo' ) );
56
57 $cache->set( 'foo', 'old' );
58 $this->assertEquals( 'old', $cache->get( 'foo' ) );
59 $this->assertEquals( 'old', $backend->get( 'foo' ) );
60
61 $cache->set( 'foo', 'new', 0, CachedBagOStuff::WRITE_CACHE_ONLY );
62 $this->assertEquals( 'new', $cache->get( 'foo' ) );
63 $this->assertEquals( 'old', $backend->get( 'foo' ) );
64
65 $cache->delete( 'foo', CachedBagOStuff::WRITE_CACHE_ONLY );
66 $this->assertEquals( 'old', $cache->get( 'foo' ) ); // Reloaded from backend
67 }
68
72 public function testCacheBackendMisses() {
73 $backend = new HashBagOStuff;
74 $cache = new CachedBagOStuff( $backend );
75
76 // First hit primes the cache with miss from the backend
77 $this->assertEquals( false, $cache->get( 'foo' ) );
78
79 // Change the value in the backend
80 $backend->set( 'foo', true );
81
82 // Second hit returns the cached miss
83 $this->assertEquals( false, $cache->get( 'foo' ) );
84
85 // But a fresh value is read from the backend
86 $backend->set( 'bar', true );
87 $this->assertEquals( true, $cache->get( 'bar' ) );
88 }
89
93 public function testSetDebug() {
94 $backend = new HashBagOStuff();
95 $cache = new CachedBagOStuff( $backend );
96 // Access private property 'debugMode'
97 $backend = TestingAccessWrapper::newFromObject( $backend );
98 $cache = TestingAccessWrapper::newFromObject( $cache );
99 $this->assertFalse( $backend->debugMode );
100 $this->assertFalse( $cache->debugMode );
101
102 $cache->setDebug( true );
103 // Should have set both
104 $this->assertTrue( $backend->debugMode, 'sets backend' );
105 $this->assertTrue( $cache->debugMode, 'sets self' );
106 }
107
111 public function testExpire() {
112 $backend = $this->getMockBuilder( HashBagOStuff::class )
113 ->setMethods( [ 'deleteObjectsExpiringBefore' ] )
114 ->getMock();
115 $backend->expects( $this->once() )
116 ->method( 'deleteObjectsExpiringBefore' )
117 ->willReturn( false );
118
119 $cache = new CachedBagOStuff( $backend );
120 $cache->deleteObjectsExpiringBefore( '20110401000000' );
121 }
122
126 public function testMakeKey() {
127 $backend = $this->getMockBuilder( HashBagOStuff::class )
128 ->setMethods( [ 'makeKey' ] )
129 ->getMock();
130 $backend->method( 'makeKey' )
131 ->willReturn( 'special/logic' );
132
133 // CachedBagOStuff wraps any backend with a process cache
134 // using HashBagOStuff. Hash has no special key limitations,
135 // but backends often do. Make sure it uses the backend's
136 // makeKey() logic, not the one inherited from HashBagOStuff
137 $cache = new CachedBagOStuff( $backend );
138
139 $this->assertEquals( 'special/logic', $backend->makeKey( 'special', 'logic' ) );
140 $this->assertEquals( 'special/logic', $cache->makeKey( 'special', 'logic' ) );
141 }
142
146 public function testMakeGlobalKey() {
147 $backend = $this->getMockBuilder( HashBagOStuff::class )
148 ->setMethods( [ 'makeGlobalKey' ] )
149 ->getMock();
150 $backend->method( 'makeGlobalKey' )
151 ->willReturn( 'special/logic' );
152
153 $cache = new CachedBagOStuff( $backend );
154
155 $this->assertEquals( 'special/logic', $backend->makeGlobalKey( 'special', 'logic' ) );
156 $this->assertEquals( 'special/logic', $cache->makeGlobalKey( 'special', 'logic' ) );
157 }
158}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
testMakeKey()
CachedBagOStuff::makeKey.
testExpire()
CachedBagOStuff::deleteObjectsExpiringBefore.
testSetDebug()
CachedBagOStuff::setDebug.
testCacheBackendMisses()
CachedBagOStuff::doGet.
testMakeGlobalKey()
CachedBagOStuff::makeGlobalKey.
testSetAndDelete()
CachedBagOStuff::set CachedBagOStuff::delete.
testWriteCacheOnly()
CachedBagOStuff::set CachedBagOStuff::delete.
testGetFromBackend()
CachedBagOStuff::__construct CachedBagOStuff::doGet.
Wrapper around a BagOStuff that caches data in memory.
Simple store for keeping values in an associative array for the current process.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
$cache
Definition mcc.php:33