MediaWiki  1.30.2
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 
14  public function testGetFromBackend() {
15  $backend = new HashBagOStuff;
16  $cache = new CachedBagOStuff( $backend );
17 
18  $backend->set( 'foo', 'bar' );
19  $this->assertEquals( 'bar', $cache->get( 'foo' ) );
20 
21  $backend->set( 'foo', 'baz' );
22  $this->assertEquals( 'bar', $cache->get( 'foo' ), 'cached' );
23  }
24 
29  public function testSetAndDelete() {
30  $backend = new HashBagOStuff;
31  $cache = new CachedBagOStuff( $backend );
32 
33  for ( $i = 0; $i < 10; $i++ ) {
34  $cache->set( "key$i", 1 );
35  $this->assertEquals( 1, $cache->get( "key$i" ) );
36  $this->assertEquals( 1, $backend->get( "key$i" ) );
37  $cache->delete( "key$i" );
38  $this->assertEquals( false, $cache->get( "key$i" ) );
39  $this->assertEquals( false, $backend->get( "key$i" ) );
40  }
41  }
42 
47  public function testWriteCacheOnly() {
48  $backend = new HashBagOStuff;
49  $cache = new CachedBagOStuff( $backend );
50 
51  $cache->set( 'foo', 'bar', 0, CachedBagOStuff::WRITE_CACHE_ONLY );
52  $this->assertEquals( 'bar', $cache->get( 'foo' ) );
53  $this->assertFalse( $backend->get( 'foo' ) );
54 
55  $cache->set( 'foo', 'old' );
56  $this->assertEquals( 'old', $cache->get( 'foo' ) );
57  $this->assertEquals( 'old', $backend->get( 'foo' ) );
58 
59  $cache->set( 'foo', 'new', 0, CachedBagOStuff::WRITE_CACHE_ONLY );
60  $this->assertEquals( 'new', $cache->get( 'foo' ) );
61  $this->assertEquals( 'old', $backend->get( 'foo' ) );
62 
63  $cache->delete( 'foo', CachedBagOStuff::WRITE_CACHE_ONLY );
64  $this->assertEquals( 'old', $cache->get( 'foo' ) ); // Reloaded from backend
65  }
66 
70  public function testCacheBackendMisses() {
71  $backend = new HashBagOStuff;
72  $cache = new CachedBagOStuff( $backend );
73 
74  // First hit primes the cache with miss from the backend
75  $this->assertEquals( false, $cache->get( 'foo' ) );
76 
77  // Change the value in the backend
78  $backend->set( 'foo', true );
79 
80  // Second hit returns the cached miss
81  $this->assertEquals( false, $cache->get( 'foo' ) );
82 
83  // But a fresh value is read from the backend
84  $backend->set( 'bar', true );
85  $this->assertEquals( true, $cache->get( 'bar' ) );
86  }
87 
91  public function testSetDebug() {
92  $backend = new HashBagOStuff();
93  $cache = new CachedBagOStuff( $backend );
94  // Access private property 'debugMode'
95  $backend = TestingAccessWrapper::newFromObject( $backend );
96  $cache = TestingAccessWrapper::newFromObject( $cache );
97  $this->assertFalse( $backend->debugMode );
98  $this->assertFalse( $cache->debugMode );
99 
100  $cache->setDebug( true );
101  // Should have set both
102  $this->assertTrue( $backend->debugMode, 'sets backend' );
103  $this->assertTrue( $cache->debugMode, 'sets self' );
104  }
105 
109  public function testExpire() {
110  $backend = $this->getMockBuilder( HashBagOStuff::class )
111  ->setMethods( [ 'deleteObjectsExpiringBefore' ] )
112  ->getMock();
113  $backend->expects( $this->once() )
114  ->method( 'deleteObjectsExpiringBefore' )
115  ->willReturn( false );
116 
117  $cache = new CachedBagOStuff( $backend );
118  $cache->deleteObjectsExpiringBefore( '20110401000000' );
119  }
120 
124  public function testMakeKey() {
125  $backend = $this->getMockBuilder( HashBagOStuff::class )
126  ->setMethods( [ 'makeKey' ] )
127  ->getMock();
128  $backend->method( 'makeKey' )
129  ->willReturn( 'special/logic' );
130 
131  // CachedBagOStuff wraps any backend with a process cache
132  // using HashBagOStuff. Hash has no special key limitations,
133  // but backends often do. Make sure it uses the backend's
134  // makeKey() logic, not the one inherited from HashBagOStuff
135  $cache = new CachedBagOStuff( $backend );
136 
137  $this->assertEquals( 'special/logic', $backend->makeKey( 'special', 'logic' ) );
138  $this->assertEquals( 'special/logic', $cache->makeKey( 'special', 'logic' ) );
139  }
140 
144  public function testMakeGlobalKey() {
145  $backend = $this->getMockBuilder( HashBagOStuff::class )
146  ->setMethods( [ 'makeGlobalKey' ] )
147  ->getMock();
148  $backend->method( 'makeGlobalKey' )
149  ->willReturn( 'special/logic' );
150 
151  $cache = new CachedBagOStuff( $backend );
152 
153  $this->assertEquals( 'special/logic', $backend->makeGlobalKey( 'special', 'logic' ) );
154  $this->assertEquals( 'special/logic', $cache->makeGlobalKey( 'special', 'logic' ) );
155  }
156 }
CachedBagOStuffTest\testCacheBackendMisses
testCacheBackendMisses()
CachedBagOStuff::doGet.
Definition: CachedBagOStuffTest.php:70
CachedBagOStuffTest\testSetAndDelete
testSetAndDelete()
CachedBagOStuff::set CachedBagOStuff::delete.
Definition: CachedBagOStuffTest.php:29
CachedBagOStuffTest
BagOStuff.
Definition: CachedBagOStuffTest.php:8
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
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\testWriteCacheOnly
testWriteCacheOnly()
CachedBagOStuff::set CachedBagOStuff::delete.
Definition: CachedBagOStuffTest.php:47
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:124
CachedBagOStuffTest\testSetDebug
testSetDebug()
CachedBagOStuff::setDebug.
Definition: CachedBagOStuffTest.php:91
CachedBagOStuffTest\testMakeGlobalKey
testMakeGlobalKey()
CachedBagOStuff::makeGlobalKey.
Definition: CachedBagOStuffTest.php:144
CachedBagOStuff
Wrapper around a BagOStuff that caches data in memory.
Definition: CachedBagOStuff.php:36
CachedBagOStuffTest\testGetFromBackend
testGetFromBackend()
CachedBagOStuff::__construct CachedBagOStuff::doGet.
Definition: CachedBagOStuffTest.php:14
$cache
$cache
Definition: mcc.php:33
CachedBagOStuffTest\testExpire
testExpire()
CachedBagOStuff::deleteObjectsExpiringBefore.
Definition: CachedBagOStuffTest.php:109
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:87