MediaWiki  1.33.0
CachedBagOStuffTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\TestingAccessWrapper;
4 
8 class 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 
40  $cache->delete( "key$i" );
41  $this->assertEquals( false, $cache->get( "key$i" ) );
42  $this->assertEquals( false, $backend->get( "key$i" ) );
43  }
44  }
45 
50  public function testWriteCacheOnly() {
51  $backend = new HashBagOStuff;
52  $cache = new CachedBagOStuff( $backend );
53 
54  $cache->set( 'foo', 'bar', 0, CachedBagOStuff::WRITE_CACHE_ONLY );
55  $this->assertEquals( 'bar', $cache->get( 'foo' ) );
56  $this->assertFalse( $backend->get( 'foo' ) );
57 
58  $cache->set( 'foo', 'old' );
59  $this->assertEquals( 'old', $cache->get( 'foo' ) );
60  $this->assertEquals( 'old', $backend->get( 'foo' ) );
61 
62  $cache->set( 'foo', 'new', 0, CachedBagOStuff::WRITE_CACHE_ONLY );
63  $this->assertEquals( 'new', $cache->get( 'foo' ) );
64  $this->assertEquals( 'old', $backend->get( 'foo' ) );
65 
66  $cache->delete( 'foo', CachedBagOStuff::WRITE_CACHE_ONLY );
67  $this->assertEquals( 'old', $cache->get( 'foo' ) ); // Reloaded from backend
68  }
69 
73  public function testCacheBackendMisses() {
74  $backend = new HashBagOStuff;
75  $cache = new CachedBagOStuff( $backend );
76 
77  // First hit primes the cache with miss from the backend
78  $this->assertEquals( false, $cache->get( 'foo' ) );
79 
80  // Change the value in the backend
81  $backend->set( 'foo', true );
82 
83  // Second hit returns the cached miss
84  $this->assertEquals( false, $cache->get( 'foo' ) );
85 
86  // But a fresh value is read from the backend
87  $backend->set( 'bar', true );
88  $this->assertEquals( true, $cache->get( 'bar' ) );
89  }
90 
94  public function testSetDebug() {
95  $backend = new HashBagOStuff();
96  $cache = new CachedBagOStuff( $backend );
97  // Access private property 'debugMode'
98  $backend = TestingAccessWrapper::newFromObject( $backend );
99  $cache = TestingAccessWrapper::newFromObject( $cache );
100  $this->assertFalse( $backend->debugMode );
101  $this->assertFalse( $cache->debugMode );
102 
103  $cache->setDebug( true );
104  // Should have set both
105  $this->assertTrue( $backend->debugMode, 'sets backend' );
106  $this->assertTrue( $cache->debugMode, 'sets self' );
107  }
108 
112  public function testExpire() {
113  $backend = $this->getMockBuilder( HashBagOStuff::class )
114  ->setMethods( [ 'deleteObjectsExpiringBefore' ] )
115  ->getMock();
116  $backend->expects( $this->once() )
117  ->method( 'deleteObjectsExpiringBefore' )
118  ->willReturn( false );
119 
120  $cache = new CachedBagOStuff( $backend );
121  $cache->deleteObjectsExpiringBefore( '20110401000000' );
122  }
123 
127  public function testMakeKey() {
128  $backend = $this->getMockBuilder( HashBagOStuff::class )
129  ->setMethods( [ 'makeKey' ] )
130  ->getMock();
131  $backend->method( 'makeKey' )
132  ->willReturn( 'special/logic' );
133 
134  // CachedBagOStuff wraps any backend with a process cache
135  // using HashBagOStuff. Hash has no special key limitations,
136  // but backends often do. Make sure it uses the backend's
137  // makeKey() logic, not the one inherited from HashBagOStuff
138  $cache = new CachedBagOStuff( $backend );
139 
140  $this->assertEquals( 'special/logic', $backend->makeKey( 'special', 'logic' ) );
141  $this->assertEquals( 'special/logic', $cache->makeKey( 'special', 'logic' ) );
142  }
143 
147  public function testMakeGlobalKey() {
148  $backend = $this->getMockBuilder( HashBagOStuff::class )
149  ->setMethods( [ 'makeGlobalKey' ] )
150  ->getMock();
151  $backend->method( 'makeGlobalKey' )
152  ->willReturn( 'special/logic' );
153 
154  $cache = new CachedBagOStuff( $backend );
155 
156  $this->assertEquals( 'special/logic', $backend->makeGlobalKey( 'special', 'logic' ) );
157  $this->assertEquals( 'special/logic', $cache->makeGlobalKey( 'special', 'logic' ) );
158  }
159 }
CachedBagOStuffTest\testCacheBackendMisses
testCacheBackendMisses()
CachedBagOStuff::get.
Definition: CachedBagOStuffTest.php:73
CachedBagOStuffTest\testSetAndDelete
testSetAndDelete()
CachedBagOStuff::set CachedBagOStuff::delete.
Definition: CachedBagOStuffTest.php:31
CachedBagOStuffTest
BagOStuff.
Definition: CachedBagOStuffTest.php:8
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
CachedBagOStuffTest\testWriteCacheOnly
testWriteCacheOnly()
CachedBagOStuff::set CachedBagOStuff::delete.
Definition: CachedBagOStuffTest.php:50
php
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:35
CachedBagOStuffTest\testMakeKey
testMakeKey()
CachedBagOStuff::makeKey.
Definition: CachedBagOStuffTest.php:127
CachedBagOStuffTest\testSetDebug
testSetDebug()
CachedBagOStuff::setDebug.
Definition: CachedBagOStuffTest.php:94
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
CachedBagOStuffTest\testMakeGlobalKey
testMakeGlobalKey()
CachedBagOStuff::makeGlobalKey.
Definition: CachedBagOStuffTest.php:147
CachedBagOStuff
Wrapper around a BagOStuff that caches data in memory.
Definition: CachedBagOStuff.php:37
CachedBagOStuffTest\testGetFromBackend
testGetFromBackend()
CachedBagOStuff::__construct CachedBagOStuff::get.
Definition: CachedBagOStuffTest.php:16
$cache
$cache
Definition: mcc.php:33
CachedBagOStuffTest\testExpire
testExpire()
CachedBagOStuff::deleteObjectsExpiringBefore.
Definition: CachedBagOStuffTest.php:112
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
BagOStuff\WRITE_CACHE_ONLY
const WRITE_CACHE_ONLY
Definition: BagOStuff.php:95