7d8fe16434aa2e72d30b1cd114e373104aeaa5d9
[moodle.git] / cache / tests / cache_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * PHPunit tests for the cache API
19  *
20  * This file is part of Moodle's cache API, affectionately called MUC.
21  * It contains the components that are requried in order to use caching.
22  *
23  * @package    core
24  * @category   cache
25  * @copyright  2012 Sam Hemelryk
26  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27  */
29 defined('MOODLE_INTERNAL') || die();
31 // Include the necessary evils.
32 global $CFG;
33 require_once($CFG->dirroot.'/cache/locallib.php');
34 require_once($CFG->dirroot.'/cache/tests/fixtures/lib.php');
36 /**
37  * PHPunit tests for the cache API
38  *
39  * @copyright  2012 Sam Hemelryk
40  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41  */
42 class core_cache_testcase extends advanced_testcase {
44     /**
45      * Set things back to the default before each test.
46      */
47     public function setUp() {
48         parent::setUp();
49         cache_factory::reset();
50         cache_config_phpunittest::create_default_configuration();
51     }
53     /**
54      * Final task is to reset the cache system
55      */
56     public static function tearDownAfterClass() {
57         parent::tearDownAfterClass();
58         cache_factory::reset();
59     }
61     /**
62      * Tests cache configuration
63      */
64     public function test_cache_config() {
65         global $CFG;
67         if (!empty($CFG->altcacheconfigpath)) {
68             // We need to skip this test - it checks the default config structure, but very likely we arn't using the
69             // default config structure here so theres no point in running the test.
70             $this->markTestSkipped('Skipped testing default cache config structure as alt cache path is being used.');
71         }
73         $instance = cache_config::instance();
74         $this->assertInstanceOf('cache_config_phpunittest', $instance);
76         $this->assertTrue(cache_config_phpunittest::config_file_exists());
78         $stores = $instance->get_all_stores();
79         $this->assertCount(3, $stores);
80         foreach ($stores as $name => $store) {
81             // Check its an array.
82             $this->assertInternalType('array', $store);
83             // Check the name is the key.
84             $this->assertEquals($name, $store['name']);
85             // Check that it has been declared default.
86             $this->assertTrue($store['default']);
87             // Required attributes = name + plugin + configuration + modes + features.
88             $this->assertArrayHasKey('name', $store);
89             $this->assertArrayHasKey('plugin', $store);
90             $this->assertArrayHasKey('configuration', $store);
91             $this->assertArrayHasKey('modes', $store);
92             $this->assertArrayHasKey('features', $store);
93         }
95         $modemappings = $instance->get_mode_mappings();
96         $this->assertCount(3, $modemappings);
97         $modes = array(
98             cache_store::MODE_APPLICATION => false,
99             cache_store::MODE_SESSION => false,
100             cache_store::MODE_REQUEST => false,
101         );
102         foreach ($modemappings as $mapping) {
103             // We expect 3 properties.
104             $this->assertCount(3, $mapping);
105             // Required attributes = mode + store.
106             $this->assertArrayHasKey('mode', $mapping);
107             $this->assertArrayHasKey('store', $mapping);
108             // Record the mode.
109             $modes[$mapping['mode']] = true;
110         }
112         // Must have the default 3 modes and no more.
113         $this->assertCount(3, $mapping);
114         foreach ($modes as $mode) {
115             $this->assertTrue($mode);
116         }
118         $definitions = $instance->get_definitions();
119         // The event invalidation definition is required for the cache API and must be there.
120         $this->assertArrayHasKey('core/eventinvalidation', $definitions);
122         $definitionmappings = $instance->get_definition_mappings();
123         foreach ($definitionmappings as $mapping) {
124             // Required attributes = definition + store.
125             $this->assertArrayHasKey('definition', $mapping);
126             $this->assertArrayHasKey('store', $mapping);
127         }
128     }
130     /**
131      * Tests for cache keys that would break on windows.
132      */
133     public function test_windows_nasty_keys() {
134         $instance = cache_config_phpunittest::instance();
135         $instance->phpunit_add_definition('phpunit/windowskeytest', array(
136             'mode' => cache_store::MODE_APPLICATION,
137             'component' => 'phpunit',
138             'area' => 'windowskeytest',
139             'simplekeys' => true,
140             'simpledata' => true
141         ));
142         $cache = cache::make('phpunit', 'windowskeytest');
143         $this->assertTrue($cache->set('contest', 'test data 1'));
144         $this->assertEquals('test data 1', $cache->get('contest'));
145     }
147     /**
148      * Tests the default application cache
149      */
150     public function test_default_application_cache() {
151         $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'applicationtest');
152         $this->assertInstanceOf('cache_application', $cache);
153         $this->run_on_cache($cache);
155         $instance = cache_config_phpunittest::instance(true);
156         $instance->phpunit_add_definition('phpunit/test_default_application_cache', array(
157             'mode' => cache_store::MODE_APPLICATION,
158             'component' => 'phpunit',
159             'area' => 'test_default_application_cache',
160             'staticacceleration' => true,
161             'staticaccelerationsize' => 1
162         ));
163         $cache = cache::make('phpunit', 'test_default_application_cache');
164         $this->assertInstanceOf('cache_application', $cache);
165         $this->run_on_cache($cache);
166     }
168     /**
169      * Tests the default session cache
170      */
171     public function test_default_session_cache() {
172         $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'applicationtest');
173         $this->assertInstanceOf('cache_session', $cache);
174         $this->run_on_cache($cache);
175     }
177     /**
178      * Tests the default request cache
179      */
180     public function test_default_request_cache() {
181         $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'applicationtest');
182         $this->assertInstanceOf('cache_request', $cache);
183         $this->run_on_cache($cache);
184     }
186     /**
187      * Tests using a cache system when there are no stores available (who knows what the admin did to achieve this).
188      */
189     public function test_on_cache_without_store() {
190         $instance = cache_config_phpunittest::instance(true);
191         $instance->phpunit_add_definition('phpunit/nostoretest1', array(
192             'mode' => cache_store::MODE_APPLICATION,
193             'component' => 'phpunit',
194             'area' => 'nostoretest1',
195         ));
196         $instance->phpunit_add_definition('phpunit/nostoretest2', array(
197             'mode' => cache_store::MODE_APPLICATION,
198             'component' => 'phpunit',
199             'area' => 'nostoretest2',
200             'staticacceleration' => true
201         ));
202         $instance->phpunit_remove_stores();
204         $cache = cache::make('phpunit', 'nostoretest1');
205         $this->run_on_cache($cache);
207         $cache = cache::make('phpunit', 'nostoretest2');
208         $this->run_on_cache($cache);
209     }
211     /**
212      * Runs a standard series of access and use tests on a cache instance.
213      *
214      * This function is great because we can use it to ensure all of the loaders perform exactly the same way.
215      *
216      * @param cache_loader $cache
217      */
218     protected function run_on_cache(cache_loader $cache) {
219         $key = 'contestkey';
220         $datascalars = array('test data', null);
221         $dataarray = array('contest' => 'data', 'part' => 'two');
222         $dataobject = (object)$dataarray;
224         foreach ($datascalars as $datascalar) {
225             $this->assertTrue($cache->purge());
227             // Check all read methods.
228             $this->assertFalse($cache->get($key));
229             $this->assertFalse($cache->has($key));
230             $result = $cache->get_many(array($key));
231             $this->assertCount(1, $result);
232             $this->assertFalse(reset($result));
233             $this->assertFalse($cache->has_any(array($key)));
234             $this->assertFalse($cache->has_all(array($key)));
236             // Set the data.
237             $this->assertTrue($cache->set($key, $datascalar));
238             // Setting it more than once should be permitted.
239             $this->assertTrue($cache->set($key, $datascalar));
241             // Recheck the read methods.
242             $this->assertEquals($datascalar, $cache->get($key));
243             $this->assertTrue($cache->has($key));
244             $result = $cache->get_many(array($key));
245             $this->assertCount(1, $result);
246             $this->assertEquals($datascalar, reset($result));
247             $this->assertTrue($cache->has_any(array($key)));
248             $this->assertTrue($cache->has_all(array($key)));
250             // Delete it.
251             $this->assertTrue($cache->delete($key));
253             // Check its gone.
254             $this->assertFalse($cache->get($key));
255             $this->assertFalse($cache->has($key));
256         }
258         // Test arrays.
259         $this->assertTrue($cache->set($key, $dataarray));
260         $this->assertEquals($dataarray, $cache->get($key));
262         // Test objects.
263         $this->assertTrue($cache->set($key, $dataobject));
264         $this->assertEquals($dataobject, $cache->get($key));
266         $specobject = new cache_phpunit_dummy_object('red', 'blue');
267         $this->assertTrue($cache->set($key, $specobject));
268         $result = $cache->get($key);
269         $this->assertInstanceOf('cache_phpunit_dummy_object', $result);
270         $this->assertEquals('red_ptc_wfc', $result->property1);
271         $this->assertEquals('blue_ptc_wfc', $result->property2);
273         // Test array of objects.
274         $specobject = new cache_phpunit_dummy_object('red', 'blue');
275         $data = new cacheable_object_array(array(
276             clone($specobject),
277             clone($specobject),
278             clone($specobject))
279         );
280         $this->assertTrue($cache->set($key, $data));
281         $result = $cache->get($key);
282         $this->assertInstanceOf('cacheable_object_array', $result);
283         $this->assertCount(3, $data);
284         foreach ($result as $item) {
285             $this->assertInstanceOf('cache_phpunit_dummy_object', $item);
286             $this->assertEquals('red_ptc_wfc', $item->property1);
287             $this->assertEquals('blue_ptc_wfc', $item->property2);
288         }
290         // Test set many.
291         $cache->set_many(array('key1' => 'data1', 'key2' => 'data2', 'key3' => null));
292         $this->assertEquals('data1', $cache->get('key1'));
293         $this->assertEquals('data2', $cache->get('key2'));
294         $this->assertEquals(null, $cache->get('key3'));
295         $this->assertTrue($cache->delete('key1'));
296         $this->assertTrue($cache->delete('key2'));
297         $this->assertTrue($cache->delete('key3'));
299         $cache->set_many(array(
300             'key1' => array(1, 2, 3),
301             'key2' => array(3, 2, 1),
302         ));
303         $this->assertInternalType('array', $cache->get('key1'));
304         $this->assertInternalType('array', $cache->get('key2'));
305         $this->assertCount(3, $cache->get('key1'));
306         $this->assertCount(3, $cache->get('key2'));
307         $this->assertInternalType('array', $cache->get_many(array('key1', 'key2')));
308         $this->assertCount(2, $cache->get_many(array('key1', 'key2')));
309         $this->assertEquals(2, $cache->delete_many(array('key1', 'key2')));
311         // Test delete many.
312         $this->assertTrue($cache->set('key1', 'data1'));
313         $this->assertTrue($cache->set('key2', 'data2'));
314         $this->assertTrue($cache->set('key3', null));
316         $this->assertEquals('data1', $cache->get('key1'));
317         $this->assertEquals('data2', $cache->get('key2'));
318         $this->assertEquals(null, $cache->get('key3'));
320         $this->assertEquals(3, $cache->delete_many(array('key1', 'key2', 'key3')));
322         $this->assertFalse($cache->get('key1'));
323         $this->assertFalse($cache->get('key2'));
324         $this->assertFalse($cache->get('key3'));
326         // Quick reference test.
327         $obj = new stdClass;
328         $obj->key = 'value';
329         $ref =& $obj;
330         $this->assertTrue($cache->set('obj', $obj));
332         $obj->key = 'eulav';
333         $var = $cache->get('obj');
334         $this->assertInstanceOf('stdClass', $var);
335         $this->assertEquals('value', $var->key);
337         $ref->key = 'eulav';
338         $var = $cache->get('obj');
339         $this->assertInstanceOf('stdClass', $var);
340         $this->assertEquals('value', $var->key);
342         $this->assertTrue($cache->delete('obj'));
344         // Deep reference test.
345         $obj1 = new stdClass;
346         $obj1->key = 'value';
347         $obj2 = new stdClass;
348         $obj2->key = 'test';
349         $obj3 = new stdClass;
350         $obj3->key = 'pork';
351         $obj1->subobj =& $obj2;
352         $obj2->subobj =& $obj3;
353         $this->assertTrue($cache->set('obj', $obj1));
355         $obj1->key = 'eulav';
356         $obj2->key = 'tset';
357         $obj3->key = 'krop';
358         $var = $cache->get('obj');
359         $this->assertInstanceOf('stdClass', $var);
360         $this->assertEquals('value', $var->key);
361         $this->assertInstanceOf('stdClass', $var->subobj);
362         $this->assertEquals('test', $var->subobj->key);
363         $this->assertInstanceOf('stdClass', $var->subobj->subobj);
364         $this->assertEquals('pork', $var->subobj->subobj->key);
365         $this->assertTrue($cache->delete('obj'));
367         // Death reference test... basicaly we don't want this to die.
368         $obj = new stdClass;
369         $obj->key = 'value';
370         $obj->self =& $obj;
371         $this->assertTrue($cache->set('obj', $obj));
372         $var = $cache->get('obj');
373         $this->assertInstanceOf('stdClass', $var);
374         $this->assertEquals('value', $var->key);
376         // Reference test after retrieve.
377         $obj = new stdClass;
378         $obj->key = 'value';
379         $this->assertTrue($cache->set('obj', $obj));
381         $var1 = $cache->get('obj');
382         $this->assertInstanceOf('stdClass', $var1);
383         $this->assertEquals('value', $var1->key);
384         $var1->key = 'eulav';
385         $this->assertEquals('eulav', $var1->key);
387         $var2 = $cache->get('obj');
388         $this->assertInstanceOf('stdClass', $var2);
389         $this->assertEquals('value', $var2->key);
391         $this->assertTrue($cache->delete('obj'));
393         // Test strictness exceptions.
394         try {
395             $cache->get('exception', MUST_EXIST);
396             $this->fail('Exception expected from cache::get using MUST_EXIST');
397         } catch (Exception $e) {
398             $this->assertTrue(true);
399         }
400         try {
401             $cache->get_many(array('exception1', 'exception2'), MUST_EXIST);
402             $this->fail('Exception expected from cache::get_many using MUST_EXIST');
403         } catch (Exception $e) {
404             $this->assertTrue(true);
405         }
406         $cache->set('test', 'test');
407         try {
408             $cache->get_many(array('test', 'exception'), MUST_EXIST);
409             $this->fail('Exception expected from cache::get_many using MUST_EXIST');
410         } catch (Exception $e) {
411             $this->assertTrue(true);
412         }
413     }
415     /**
416      * Tests a definition using a data loader
417      */
418     public function test_definition_data_loader() {
419         $instance = cache_config_phpunittest::instance(true);
420         $instance->phpunit_add_definition('phpunit/datasourcetest', array(
421             'mode' => cache_store::MODE_APPLICATION,
422             'component' => 'phpunit',
423             'area' => 'datasourcetest',
424             'datasource' => 'cache_phpunit_dummy_datasource',
425             'datasourcefile' => 'cache/tests/fixtures/lib.php'
426         ));
428         $cache = cache::make('phpunit', 'datasourcetest');
429         $this->assertInstanceOf('cache_application', $cache);
431         // Purge it to be sure.
432         $this->assertTrue($cache->purge());
433         // It won't be there yet.
434         $this->assertFalse($cache->has('Test'));
435         // It should load it ;).
436         $this->assertTrue($cache->has('Test', true));
438         // Purge it to be sure.
439         $this->assertTrue($cache->purge());
440         $this->assertEquals('Test has no value really.', $cache->get('Test'));
442         // Test multiple values.
443         $this->assertTrue($cache->purge());
444         $this->assertTrue($cache->set('b', 'B'));
445         $result = $cache->get_many(array('a', 'b', 'c'));
446         $this->assertInternalType('array', $result);
447         $this->assertCount(3, $result);
448         $this->assertArrayHasKey('a', $result);
449         $this->assertArrayHasKey('b', $result);
450         $this->assertArrayHasKey('c', $result);
451         $this->assertEquals('a has no value really.', $result['a']);
452         $this->assertEquals('B', $result['b']);
453         $this->assertEquals('c has no value really.', $result['c']);
454     }
456     /**
457      * Tests a definition using an overridden loader
458      */
459     public function test_definition_overridden_loader() {
460         $instance = cache_config_phpunittest::instance(true);
461         $instance->phpunit_add_definition('phpunit/overridetest', array(
462             'mode' => cache_store::MODE_APPLICATION,
463             'component' => 'phpunit',
464             'area' => 'overridetest',
465             'overrideclass' => 'cache_phpunit_dummy_overrideclass',
466             'overrideclassfile' => 'cache/tests/fixtures/lib.php'
467         ));
468         $cache = cache::make('phpunit', 'overridetest');
469         $this->assertInstanceOf('cache_phpunit_dummy_overrideclass', $cache);
470         $this->assertInstanceOf('cache_application', $cache);
471         // Purge it to be sure.
472         $this->assertTrue($cache->purge());
473         // It won't be there yet.
474         $this->assertFalse($cache->has('Test'));
475         // Add it.
476         $this->assertTrue($cache->set('Test', 'Test has no value really.'));
477         // Check its there.
478         $this->assertEquals('Test has no value really.', $cache->get('Test'));
479     }
481     /**
482      * Test the mappingsonly setting.
483      */
484     public function test_definition_mappings_only() {
485         $instance = cache_config_phpunittest::instance(true);
486         $instance->phpunit_add_definition('phpunit/mappingsonly', array(
487             'mode' => cache_store::MODE_APPLICATION,
488             'component' => 'phpunit',
489             'area' => 'mappingsonly',
490             'mappingsonly' => true
491         ));
492         $instance->phpunit_add_definition('phpunit/nonmappingsonly', array(
493             'mode' => cache_store::MODE_APPLICATION,
494             'component' => 'phpunit',
495             'area' => 'nonmappingsonly',
496             'mappingsonly' => false
497         ));
499         $cacheonly = cache::make('phpunit', 'mappingsonly');
500         $this->assertInstanceOf('cache_application', $cacheonly);
501         $this->assertEquals('cachestore_dummy', $cacheonly->phpunit_get_store_class());
503         $cachenon = cache::make('phpunit', 'nonmappingsonly');
504         $this->assertInstanceOf('cache_application', $cachenon);
505         $this->assertEquals('cachestore_file', $cachenon->phpunit_get_store_class());
506     }
508     /**
509      * Test a very basic definition.
510      */
511     public function test_definition() {
512         $instance = cache_config_phpunittest::instance();
513         $instance->phpunit_add_definition('phpunit/test', array(
514             'mode' => cache_store::MODE_APPLICATION,
515             'component' => 'phpunit',
516             'area' => 'test',
517         ));
518         $cache = cache::make('phpunit', 'test');
520         $this->assertTrue($cache->set('testkey1', 'test data 1'));
521         $this->assertEquals('test data 1', $cache->get('testkey1'));
522         $this->assertTrue($cache->set('testkey2', 'test data 2'));
523         $this->assertEquals('test data 2', $cache->get('testkey2'));
524     }
526     /**
527      * Test a definition using the simple keys.
528      */
529     public function test_definition_simplekeys() {
530         $instance = cache_config_phpunittest::instance();
531         $instance->phpunit_add_definition('phpunit/simplekeytest', array(
532             'mode' => cache_store::MODE_APPLICATION,
533             'component' => 'phpunit',
534             'area' => 'simplekeytest',
535             'simplekeys' => true
536         ));
537         $cache = cache::make('phpunit', 'simplekeytest');
539         $this->assertTrue($cache->set('testkey1', 'test data 1'));
540         $this->assertEquals('test data 1', $cache->get('testkey1'));
541         $this->assertTrue($cache->set('testkey2', 'test data 2'));
542         $this->assertEquals('test data 2', $cache->get('testkey2'));
544         $cache->purge();
546         $this->assertTrue($cache->set('1', 'test data 1'));
547         $this->assertEquals('test data 1', $cache->get('1'));
548         $this->assertTrue($cache->set('2', 'test data 2'));
549         $this->assertEquals('test data 2', $cache->get('2'));
550     }
552     /**
553      * Test a negative TTL on an application cache.
554      */
555     public function test_application_ttl_negative() {
556         $instance = cache_config_phpunittest::instance(true);
557         $instance->phpunit_add_definition('phpunit/ttltest', array(
558             'mode' => cache_store::MODE_APPLICATION,
559             'component' => 'phpunit',
560             'area' => 'ttltest',
561             'ttl' => -86400 // Set to a day in the past to be extra sure.
562         ));
563         $cache = cache::make('phpunit', 'ttltest');
564         $this->assertInstanceOf('cache_application', $cache);
566         // Purge it to be sure.
567         $this->assertTrue($cache->purge());
568         // It won't be there yet.
569         $this->assertFalse($cache->has('Test'));
570         // Set it now.
571         $this->assertTrue($cache->set('Test', 'Test'));
572         // Check its not there.
573         $this->assertFalse($cache->has('Test'));
574         // Double check by trying to get it.
575         $this->assertFalse($cache->get('Test'));
577         // Test with multiple keys.
578         $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
579         $result = $cache->get_many(array('a', 'b', 'c'));
580         $this->assertInternalType('array', $result);
581         $this->assertCount(3, $result);
582         $this->assertArrayHasKey('a', $result);
583         $this->assertArrayHasKey('b', $result);
584         $this->assertArrayHasKey('c', $result);
585         $this->assertFalse($result['a']);
586         $this->assertFalse($result['b']);
587         $this->assertFalse($result['c']);
589         // Test with multiple keys including missing ones.
590         $result = $cache->get_many(array('a', 'c', 'e'));
591         $this->assertInternalType('array', $result);
592         $this->assertCount(3, $result);
593         $this->assertArrayHasKey('a', $result);
594         $this->assertArrayHasKey('c', $result);
595         $this->assertArrayHasKey('e', $result);
596         $this->assertFalse($result['a']);
597         $this->assertFalse($result['c']);
598         $this->assertFalse($result['e']);
599     }
601     /**
602      * Test a positive TTL on an application cache.
603      */
604     public function test_application_ttl_positive() {
605         $instance = cache_config_phpunittest::instance(true);
606         $instance->phpunit_add_definition('phpunit/ttltest', array(
607             'mode' => cache_store::MODE_APPLICATION,
608             'component' => 'phpunit',
609             'area' => 'ttltest',
610             'ttl' => 86400 // Set to a day in the future to be extra sure.
611         ));
612         $cache = cache::make('phpunit', 'ttltest');
613         $this->assertInstanceOf('cache_application', $cache);
615         // Purge it to be sure.
616         $this->assertTrue($cache->purge());
617         // It won't be there yet.
618         $this->assertFalse($cache->has('Test'));
619         // Set it now.
620         $this->assertTrue($cache->set('Test', 'Test'));
621         // Check its there.
622         $this->assertTrue($cache->has('Test'));
623         // Double check by trying to get it.
624         $this->assertEquals('Test', $cache->get('Test'));
626         // Test with multiple keys.
627         $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
628         $result = $cache->get_many(array('a', 'b', 'c'));
629         $this->assertInternalType('array', $result);
630         $this->assertCount(3, $result);
631         $this->assertArrayHasKey('a', $result);
632         $this->assertArrayHasKey('b', $result);
633         $this->assertArrayHasKey('c', $result);
634         $this->assertEquals('A', $result['a']);
635         $this->assertEquals('B', $result['b']);
636         $this->assertEquals('C', $result['c']);
638         // Test with multiple keys including missing ones.
639         $result = $cache->get_many(array('a', 'c', 'e'));
640         $this->assertInternalType('array', $result);
641         $this->assertCount(3, $result);
642         $this->assertArrayHasKey('a', $result);
643         $this->assertArrayHasKey('c', $result);
644         $this->assertArrayHasKey('e', $result);
645         $this->assertEquals('A', $result['a']);
646         $this->assertEquals('C', $result['c']);
647         $this->assertEquals(false, $result['e']);
648     }
650     /**
651      * Test a negative TTL on an session cache.
652      */
653     public function test_session_ttl_positive() {
654         $instance = cache_config_phpunittest::instance(true);
655         $instance->phpunit_add_definition('phpunit/ttltest', array(
656             'mode' => cache_store::MODE_SESSION,
657             'component' => 'phpunit',
658             'area' => 'ttltest',
659             'ttl' => 86400 // Set to a day in the future to be extra sure.
660         ));
661         $cache = cache::make('phpunit', 'ttltest');
662         $this->assertInstanceOf('cache_session', $cache);
664         // Purge it to be sure.
665         $this->assertTrue($cache->purge());
666         // It won't be there yet.
667         $this->assertFalse($cache->has('Test'));
668         // Set it now.
669         $this->assertTrue($cache->set('Test', 'Test'));
670         // Check its there.
671         $this->assertTrue($cache->has('Test'));
672         // Double check by trying to get it.
673         $this->assertEquals('Test', $cache->get('Test'));
675         // Test with multiple keys.
676         $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
677         $result = $cache->get_many(array('a', 'b', 'c'));
678         $this->assertInternalType('array', $result);
679         $this->assertCount(3, $result);
680         $this->assertArrayHasKey('a', $result);
681         $this->assertArrayHasKey('b', $result);
682         $this->assertArrayHasKey('c', $result);
683         $this->assertEquals('A', $result['a']);
684         $this->assertEquals('B', $result['b']);
685         $this->assertEquals('C', $result['c']);
687         // Test with multiple keys including missing ones.
688         $result = $cache->get_many(array('a', 'c', 'e'));
689         $this->assertInternalType('array', $result);
690         $this->assertCount(3, $result);
691         $this->assertArrayHasKey('a', $result);
692         $this->assertArrayHasKey('c', $result);
693         $this->assertArrayHasKey('e', $result);
694         $this->assertEquals('A', $result['a']);
695         $this->assertEquals('C', $result['c']);
696         $this->assertEquals(false, $result['e']);
697     }
699     /**
700      * Tests manual locking operations on an application cache
701      */
702     public function test_application_manual_locking() {
703         $instance = cache_config_phpunittest::instance();
704         $instance->phpunit_add_definition('phpunit/lockingtest', array(
705             'mode' => cache_store::MODE_APPLICATION,
706             'component' => 'phpunit',
707             'area' => 'lockingtest'
708         ));
709         $cache1 = cache::make('phpunit', 'lockingtest');
710         $cache2 = clone($cache1);
712         $this->assertTrue($cache1->set('testkey', 'test data'));
713         $this->assertTrue($cache2->set('testkey', 'test data'));
715         $this->assertTrue($cache1->acquire_lock('testkey'));
716         $this->assertFalse($cache2->acquire_lock('testkey'));
718         $this->assertTrue($cache1->check_lock_state('testkey'));
719         $this->assertFalse($cache2->check_lock_state('testkey'));
721         $this->assertTrue($cache1->release_lock('testkey'));
722         $this->assertFalse($cache2->release_lock('testkey'));
724         $this->assertTrue($cache1->set('testkey', 'test data'));
725         $this->assertTrue($cache2->set('testkey', 'test data'));
726     }
728     /**
729      * Tests application cache event invalidation
730      */
731     public function test_application_event_invalidation() {
732         $instance = cache_config_phpunittest::instance();
733         $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
734             'mode' => cache_store::MODE_APPLICATION,
735             'component' => 'phpunit',
736             'area' => 'eventinvalidationtest',
737             'invalidationevents' => array(
738                 'crazyevent'
739             )
740         ));
741         $cache = cache::make('phpunit', 'eventinvalidationtest');
743         $this->assertTrue($cache->set('testkey1', 'test data 1'));
744         $this->assertEquals('test data 1', $cache->get('testkey1'));
745         $this->assertTrue($cache->set('testkey2', 'test data 2'));
746         $this->assertEquals('test data 2', $cache->get('testkey2'));
748         // Test invalidating a single entry.
749         cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
751         $this->assertFalse($cache->get('testkey1'));
752         $this->assertEquals('test data 2', $cache->get('testkey2'));
754         $this->assertTrue($cache->set('testkey1', 'test data 1'));
756         // Test invalidating both entries.
757         cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
759         $this->assertFalse($cache->get('testkey1'));
760         $this->assertFalse($cache->get('testkey2'));
761     }
763     /**
764      * Tests session cache event invalidation
765      */
766     public function test_session_event_invalidation() {
767         $instance = cache_config_phpunittest::instance();
768         $instance->phpunit_add_definition('phpunit/test_session_event_invalidation', array(
769             'mode' => cache_store::MODE_SESSION,
770             'component' => 'phpunit',
771             'area' => 'test_session_event_invalidation',
772             'invalidationevents' => array(
773                 'crazyevent'
774             )
775         ));
776         $cache = cache::make('phpunit', 'test_session_event_invalidation');
777         $this->assertInstanceOf('cache_session', $cache);
779         $this->assertTrue($cache->set('testkey1', 'test data 1'));
780         $this->assertEquals('test data 1', $cache->get('testkey1'));
781         $this->assertTrue($cache->set('testkey2', 'test data 2'));
782         $this->assertEquals('test data 2', $cache->get('testkey2'));
784         // Test invalidating a single entry.
785         cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
787         $this->assertFalse($cache->get('testkey1'));
788         $this->assertEquals('test data 2', $cache->get('testkey2'));
790         $this->assertTrue($cache->set('testkey1', 'test data 1'));
792         // Test invalidating both entries.
793         cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
795         $this->assertFalse($cache->get('testkey1'));
796         $this->assertFalse($cache->get('testkey2'));
797     }
799     /**
800      * Tests application cache definition invalidation
801      */
802     public function test_application_definition_invalidation() {
803         $instance = cache_config_phpunittest::instance();
804         $instance->phpunit_add_definition('phpunit/definitioninvalidation', array(
805             'mode' => cache_store::MODE_APPLICATION,
806             'component' => 'phpunit',
807             'area' => 'definitioninvalidation'
808         ));
809         $cache = cache::make('phpunit', 'definitioninvalidation');
810         $this->assertTrue($cache->set('testkey1', 'test data 1'));
811         $this->assertEquals('test data 1', $cache->get('testkey1'));
812         $this->assertTrue($cache->set('testkey2', 'test data 2'));
813         $this->assertEquals('test data 2', $cache->get('testkey2'));
815         cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), 'testkey1');
817         $this->assertFalse($cache->get('testkey1'));
818         $this->assertEquals('test data 2', $cache->get('testkey2'));
820         $this->assertTrue($cache->set('testkey1', 'test data 1'));
822         cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1'));
824         $this->assertFalse($cache->get('testkey1'));
825         $this->assertEquals('test data 2', $cache->get('testkey2'));
827         $this->assertTrue($cache->set('testkey1', 'test data 1'));
829         cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1', 'testkey2'));
831         $this->assertFalse($cache->get('testkey1'));
832         $this->assertFalse($cache->get('testkey2'));
833     }
835     /**
836      * Tests session cache definition invalidation
837      */
838     public function test_session_definition_invalidation() {
839         $instance = cache_config_phpunittest::instance();
840         $instance->phpunit_add_definition('phpunit/test_session_definition_invalidation', array(
841             'mode' => cache_store::MODE_SESSION,
842             'component' => 'phpunit',
843             'area' => 'test_session_definition_invalidation'
844         ));
845         $cache = cache::make('phpunit', 'test_session_definition_invalidation');
846         $this->assertInstanceOf('cache_session', $cache);
847         $this->assertTrue($cache->set('testkey1', 'test data 1'));
848         $this->assertEquals('test data 1', $cache->get('testkey1'));
849         $this->assertTrue($cache->set('testkey2', 'test data 2'));
850         $this->assertEquals('test data 2', $cache->get('testkey2'));
852         cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(), 'testkey1');
854         $this->assertFalse($cache->get('testkey1'));
855         $this->assertEquals('test data 2', $cache->get('testkey2'));
857         $this->assertTrue($cache->set('testkey1', 'test data 1'));
859         cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(),
860                 array('testkey1'));
862         $this->assertFalse($cache->get('testkey1'));
863         $this->assertEquals('test data 2', $cache->get('testkey2'));
865         $this->assertTrue($cache->set('testkey1', 'test data 1'));
867         cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(),
868                 array('testkey1', 'testkey2'));
870         $this->assertFalse($cache->get('testkey1'));
871         $this->assertFalse($cache->get('testkey2'));
872     }
874     /**
875      * Tests application cache event invalidation over a distributed setup.
876      */
877     public function test_distributed_application_event_invalidation() {
878         global $CFG;
879         // This is going to be an intense wee test.
880         // We need to add data the to cache, invalidate it by event, manually force it back without MUC knowing to simulate a
881         // disconnected/distributed setup (think load balanced server using local cache), instantiate the cache again and finally
882         // check that it is not picked up.
883         $instance = cache_config_phpunittest::instance();
884         $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
885             'mode' => cache_store::MODE_APPLICATION,
886             'component' => 'phpunit',
887             'area' => 'eventinvalidationtest',
888             'simplekeys' => true,
889             'simpledata' => true,
890             'invalidationevents' => array(
891                 'crazyevent'
892             )
893         ));
894         $cache = cache::make('phpunit', 'eventinvalidationtest');
895         $this->assertTrue($cache->set('testkey1', 'test data 1'));
896         $this->assertEquals('test data 1', $cache->get('testkey1'));
898         cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
900         $this->assertFalse($cache->get('testkey1'));
902         // OK data added, data invalidated, and invalidation time has been set.
903         // Now we need to manually add back the data and adjust the invalidation time.
904         $hash = md5(cache_store::MODE_APPLICATION.'/phpunit/eventinvalidationtest/'.$CFG->wwwroot.'phpunit');
905         $timefile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/las-cache/lastinvalidation-$hash.cache";
906         // Make sure the file is correct.
907         $this->assertTrue(file_exists($timefile));
908         $timecont = serialize(cache::now() - 60); // Back 60sec in the past to force it to re-invalidate.
909         make_writable_directory(dirname($timefile));
910         file_put_contents($timefile, $timecont);
911         $this->assertTrue(file_exists($timefile));
913         $datafile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/tes-cache/testkey1-$hash.cache";
914         $datacont = serialize("test data 1");
915         make_writable_directory(dirname($datafile));
916         file_put_contents($datafile, $datacont);
917         $this->assertTrue(file_exists($datafile));
919         // Test 1: Rebuild without the event and test its there.
920         cache_factory::reset();
921         $instance = cache_config_phpunittest::instance();
922         $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
923             'mode' => cache_store::MODE_APPLICATION,
924             'component' => 'phpunit',
925             'area' => 'eventinvalidationtest',
926             'simplekeys' => true,
927             'simpledata' => true,
928         ));
929         $cache = cache::make('phpunit', 'eventinvalidationtest');
930         $this->assertEquals('test data 1', $cache->get('testkey1'));
932         // Test 2: Rebuild and test the invalidation of the event via the invalidation cache.
933         cache_factory::reset();
934         $instance = cache_config_phpunittest::instance();
935         $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
936             'mode' => cache_store::MODE_APPLICATION,
937             'component' => 'phpunit',
938             'area' => 'eventinvalidationtest',
939             'simplekeys' => true,
940             'simpledata' => true,
941             'invalidationevents' => array(
942                 'crazyevent'
943             )
944         ));
945         $cache = cache::make('phpunit', 'eventinvalidationtest');
946         $this->assertFalse($cache->get('testkey1'));
947     }
949     /**
950      * Tests application cache event purge
951      */
952     public function test_application_event_purge() {
953         $instance = cache_config_phpunittest::instance();
954         $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
955             'mode' => cache_store::MODE_APPLICATION,
956             'component' => 'phpunit',
957             'area' => 'eventpurgetest',
958             'invalidationevents' => array(
959                 'crazyevent'
960             )
961         ));
962         $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', array(
963             'mode' => cache_store::MODE_APPLICATION,
964             'component' => 'phpunit',
965             'area' => 'eventpurgetestaccelerated',
966             'staticacceleration' => true,
967             'invalidationevents' => array(
968                 'crazyevent'
969             )
970         ));
971         $cache = cache::make('phpunit', 'eventpurgetest');
973         $this->assertTrue($cache->set('testkey1', 'test data 1'));
974         $this->assertEquals('test data 1', $cache->get('testkey1'));
975         $this->assertTrue($cache->set('testkey2', 'test data 2'));
976         $this->assertEquals('test data 2', $cache->get('testkey2'));
978         // Purge the event.
979         cache_helper::purge_by_event('crazyevent');
981         // Check things have been removed.
982         $this->assertFalse($cache->get('testkey1'));
983         $this->assertFalse($cache->get('testkey2'));
985         // Now test the static acceleration array.
986         $cache = cache::make('phpunit', 'eventpurgetestaccelerated');
987         $this->assertTrue($cache->set('testkey1', 'test data 1'));
988         $this->assertEquals('test data 1', $cache->get('testkey1'));
989         $this->assertTrue($cache->set('testkey2', 'test data 2'));
990         $this->assertEquals('test data 2', $cache->get('testkey2'));
992         // Purge the event.
993         cache_helper::purge_by_event('crazyevent');
995         // Check things have been removed.
996         $this->assertFalse($cache->get('testkey1'));
997         $this->assertFalse($cache->get('testkey2'));
998     }
1000     /**
1001      * Tests session cache event purge
1002      */
1003     public function test_session_event_purge() {
1004         $instance = cache_config_phpunittest::instance();
1005         $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
1006             'mode' => cache_store::MODE_SESSION,
1007             'component' => 'phpunit',
1008             'area' => 'eventpurgetest',
1009             'invalidationevents' => array(
1010                 'crazyevent'
1011             )
1012         ));
1013         $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', array(
1014             'mode' => cache_store::MODE_SESSION,
1015             'component' => 'phpunit',
1016             'area' => 'eventpurgetestaccelerated',
1017             'staticacceleration' => true,
1018             'invalidationevents' => array(
1019                 'crazyevent'
1020             )
1021         ));
1022         $cache = cache::make('phpunit', 'eventpurgetest');
1024         $this->assertTrue($cache->set('testkey1', 'test data 1'));
1025         $this->assertEquals('test data 1', $cache->get('testkey1'));
1026         $this->assertTrue($cache->set('testkey2', 'test data 2'));
1027         $this->assertEquals('test data 2', $cache->get('testkey2'));
1029         // Purge the event.
1030         cache_helper::purge_by_event('crazyevent');
1032         // Check things have been removed.
1033         $this->assertFalse($cache->get('testkey1'));
1034         $this->assertFalse($cache->get('testkey2'));
1036         // Now test the static acceleration array.
1037         $cache = cache::make('phpunit', 'eventpurgetestaccelerated');
1038         $this->assertTrue($cache->set('testkey1', 'test data 1'));
1039         $this->assertEquals('test data 1', $cache->get('testkey1'));
1040         $this->assertTrue($cache->set('testkey2', 'test data 2'));
1041         $this->assertEquals('test data 2', $cache->get('testkey2'));
1043         // Purge the event.
1044         cache_helper::purge_by_event('crazyevent');
1046         // Check things have been removed.
1047         $this->assertFalse($cache->get('testkey1'));
1048         $this->assertFalse($cache->get('testkey2'));
1049     }
1051     /**
1052      * Tests application cache definition purge
1053      */
1054     public function test_application_definition_purge() {
1055         $instance = cache_config_phpunittest::instance();
1056         $instance->phpunit_add_definition('phpunit/definitionpurgetest', array(
1057             'mode' => cache_store::MODE_APPLICATION,
1058             'component' => 'phpunit',
1059             'area' => 'definitionpurgetest',
1060             'invalidationevents' => array(
1061                 'crazyevent'
1062             )
1063         ));
1064         $cache = cache::make('phpunit', 'definitionpurgetest');
1066         $this->assertTrue($cache->set('testkey1', 'test data 1'));
1067         $this->assertEquals('test data 1', $cache->get('testkey1'));
1068         $this->assertTrue($cache->set('testkey2', 'test data 2'));
1069         $this->assertEquals('test data 2', $cache->get('testkey2'));
1071         // Purge the event.
1072         cache_helper::purge_by_definition('phpunit', 'definitionpurgetest');
1074         // Check things have been removed.
1075         $this->assertFalse($cache->get('testkey1'));
1076         $this->assertFalse($cache->get('testkey2'));
1077     }
1079     /**
1080      * Test the use of an alt path.
1081      * If we can generate a config instance we are done :)
1082      */
1083     public function test_alt_cache_path() {
1084         global $CFG;
1085         if ($CFG->altcacheconfigpath) {
1086             $this->markTestSkipped('Skipped testing alt cache path as it is already being used.');
1087         }
1088         $this->resetAfterTest();
1089         $CFG->altcacheconfigpath = $CFG->dataroot.'/cache/altcacheconfigpath';
1090         $instance = cache_config_phpunittest::instance();
1091         $this->assertInstanceOf('cache_config', $instance);
1092     }
1094     /**
1095      * Test disabling the cache stores.
1096      */
1097     public function test_disable_stores() {
1098         $instance = cache_config_phpunittest::instance();
1099         $instance->phpunit_add_definition('phpunit/disabletest1', array(
1100             'mode' => cache_store::MODE_APPLICATION,
1101             'component' => 'phpunit',
1102             'area' => 'disabletest1'
1103         ));
1104         $instance->phpunit_add_definition('phpunit/disabletest2', array(
1105             'mode' => cache_store::MODE_SESSION,
1106             'component' => 'phpunit',
1107             'area' => 'disabletest2'
1108         ));
1109         $instance->phpunit_add_definition('phpunit/disabletest3', array(
1110             'mode' => cache_store::MODE_REQUEST,
1111             'component' => 'phpunit',
1112             'area' => 'disabletest3'
1113         ));
1115         $caches = array(
1116             'disabletest1' => cache::make('phpunit', 'disabletest1'),
1117             'disabletest2' => cache::make('phpunit', 'disabletest2'),
1118             'disabletest3' => cache::make('phpunit', 'disabletest3')
1119         );
1121         $this->assertInstanceOf('cache_phpunit_application', $caches['disabletest1']);
1122         $this->assertInstanceOf('cache_phpunit_session', $caches['disabletest2']);
1123         $this->assertInstanceOf('cache_phpunit_request', $caches['disabletest3']);
1125         $this->assertEquals('cachestore_file', $caches['disabletest1']->phpunit_get_store_class());
1126         $this->assertEquals('cachestore_session', $caches['disabletest2']->phpunit_get_store_class());
1127         $this->assertEquals('cachestore_static', $caches['disabletest3']->phpunit_get_store_class());
1129         foreach ($caches as $cache) {
1130             $this->assertFalse($cache->get('test'));
1131             $this->assertTrue($cache->set('test', 'test'));
1132             $this->assertEquals('test', $cache->get('test'));
1133         }
1135         cache_factory::disable_stores();
1137         $caches = array(
1138             'disabletest1' => cache::make('phpunit', 'disabletest1'),
1139             'disabletest2' => cache::make('phpunit', 'disabletest2'),
1140             'disabletest3' => cache::make('phpunit', 'disabletest3')
1141         );
1143         $this->assertInstanceOf('cache_phpunit_application', $caches['disabletest1']);
1144         $this->assertInstanceOf('cache_phpunit_session', $caches['disabletest2']);
1145         $this->assertInstanceOf('cache_phpunit_request', $caches['disabletest3']);
1147         $this->assertEquals('cachestore_dummy', $caches['disabletest1']->phpunit_get_store_class());
1148         $this->assertEquals('cachestore_dummy', $caches['disabletest2']->phpunit_get_store_class());
1149         $this->assertEquals('cachestore_dummy', $caches['disabletest3']->phpunit_get_store_class());
1151         foreach ($caches as $cache) {
1152             $this->assertFalse($cache->get('test'));
1153             $this->assertTrue($cache->set('test', 'test'));
1154             $this->assertEquals('test', $cache->get('test'));
1155         }
1156     }
1158     /**
1159      * Test disabling the cache.
1160      */
1161     public function test_disable() {
1162         global $CFG;
1164         if (!empty($CFG->altcacheconfigpath)) {
1165             // We can't run this test as it requires us to delete the cache configuration script which we just
1166             // cant do with a custom path in play.
1167             $this->markTestSkipped('Skipped testing cache disable functionality as alt cache path is being used.');
1168         }
1170         $configfile = $CFG->dataroot.'/muc/config.php';
1172         // That's right, we're deleting the config file.
1173         $this->assertTrue(@unlink($configfile));
1175         // Disable the cache
1176         cache_phpunit_factory::phpunit_disable();
1178         // Check we get the expected disabled factory.
1179         $factory = cache_factory::instance();
1180         $this->assertInstanceOf('cache_factory_disabled', $factory);
1182         // Check we get the expected disabled config.
1183         $config = $factory->create_config_instance();
1184         $this->assertInstanceOf('cache_config_disabled', $config);
1186         // Check we get the expected disabled caches.
1187         $cache = cache::make('phpunit', 'disable');
1188         $this->assertInstanceOf('cache_disabled', $cache);
1190         // Test an application cache.
1191         $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'disable');
1192         $this->assertInstanceOf('cache_disabled', $cache);
1194         $this->assertFalse(file_exists($configfile));
1196         $this->assertFalse($cache->get('test'));
1197         $this->assertFalse($cache->set('test', 'test'));
1198         $this->assertFalse($cache->delete('test'));
1199         $this->assertTrue($cache->purge());
1201         // Test a session cache.
1202         $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'disable');
1203         $this->assertInstanceOf('cache_disabled', $cache);
1205         $this->assertFalse(file_exists($configfile));
1207         $this->assertFalse($cache->get('test'));
1208         $this->assertFalse($cache->set('test', 'test'));
1209         $this->assertFalse($cache->delete('test'));
1210         $this->assertTrue($cache->purge());
1212         // Finally test a request cache.
1213         $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'disable');
1214         $this->assertInstanceOf('cache_disabled', $cache);
1216         $this->assertFalse(file_exists($configfile));
1218         $this->assertFalse($cache->get('test'));
1219         $this->assertFalse($cache->set('test', 'test'));
1220         $this->assertFalse($cache->delete('test'));
1221         $this->assertTrue($cache->purge());
1223         cache_factory::reset();
1225         $factory = cache_factory::instance(true);
1226         $config = $factory->create_config_instance();
1227         $this->assertEquals('cache_config_phpunittest', get_class($config));
1228     }
1230     /**
1231      * Test that multiple application loaders work ok.
1232      */
1233     public function test_multiple_application_loaders() {
1234         $instance = cache_config_phpunittest::instance(true);
1235         $instance->phpunit_add_file_store('phpunittest1');
1236         $instance->phpunit_add_file_store('phpunittest2');
1237         $instance->phpunit_add_definition('phpunit/multi_loader', array(
1238             'mode' => cache_store::MODE_APPLICATION,
1239             'component' => 'phpunit',
1240             'area' => 'multi_loader'
1241         ));
1242         $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
1243         $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
1245         $cache = cache::make('phpunit', 'multi_loader');
1246         $this->assertInstanceOf('cache_application', $cache);
1247         $this->assertFalse($cache->get('test'));
1248         $this->assertTrue($cache->set('test', 'test'));
1249         $this->assertEquals('test', $cache->get('test'));
1250         $this->assertTrue($cache->delete('test'));
1251         $this->assertFalse($cache->get('test'));
1252         $this->assertTrue($cache->set('test', 'test'));
1253         $this->assertTrue($cache->purge());
1254         $this->assertFalse($cache->get('test'));
1256         // Test the many commands.
1257         $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
1258         $result = $cache->get_many(array('a', 'b', 'c'));
1259         $this->assertInternalType('array', $result);
1260         $this->assertCount(3, $result);
1261         $this->assertArrayHasKey('a', $result);
1262         $this->assertArrayHasKey('b', $result);
1263         $this->assertArrayHasKey('c', $result);
1264         $this->assertEquals('A', $result['a']);
1265         $this->assertEquals('B', $result['b']);
1266         $this->assertEquals('C', $result['c']);
1267         $this->assertEquals($result, $cache->get_many(array('a', 'b', 'c')));
1268         $this->assertEquals(2, $cache->delete_many(array('a', 'c')));
1269         $result = $cache->get_many(array('a', 'b', 'c'));
1270         $this->assertInternalType('array', $result);
1271         $this->assertCount(3, $result);
1272         $this->assertArrayHasKey('a', $result);
1273         $this->assertArrayHasKey('b', $result);
1274         $this->assertArrayHasKey('c', $result);
1275         $this->assertFalse($result['a']);
1276         $this->assertEquals('B', $result['b']);
1277         $this->assertFalse($result['c']);
1279         // Test non-recursive deletes.
1280         $this->assertTrue($cache->set('test', 'test'));
1281         $this->assertSame('test', $cache->get('test'));
1282         $this->assertTrue($cache->delete('test', false));
1283         // We should still have it on a deeper loader.
1284         $this->assertSame('test', $cache->get('test'));
1285         // Test non-recusive with many functions.
1286         $this->assertSame(3, $cache->set_many(array(
1287             'one' => 'one',
1288             'two' => 'two',
1289             'three' => 'three'
1290         )));
1291         $this->assertSame('one', $cache->get('one'));
1292         $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1293         $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
1294         $this->assertSame('one', $cache->get('one'));
1295         $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1296     }
1298     /**
1299      * Test that multiple application loaders work ok.
1300      */
1301     public function test_multiple_session_loaders() {
1302         /* @var cache_config_phpunittest $instance */
1303         $instance = cache_config_phpunittest::instance(true);
1304         $instance->phpunit_add_session_store('phpunittest1');
1305         $instance->phpunit_add_session_store('phpunittest2');
1306         $instance->phpunit_add_definition('phpunit/multi_loader', array(
1307             'mode' => cache_store::MODE_SESSION,
1308             'component' => 'phpunit',
1309             'area' => 'multi_loader'
1310         ));
1311         $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
1312         $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
1314         $cache = cache::make('phpunit', 'multi_loader');
1315         $this->assertInstanceOf('cache_session', $cache);
1316         $this->assertFalse($cache->get('test'));
1317         $this->assertTrue($cache->set('test', 'test'));
1318         $this->assertEquals('test', $cache->get('test'));
1319         $this->assertTrue($cache->delete('test'));
1320         $this->assertFalse($cache->get('test'));
1321         $this->assertTrue($cache->set('test', 'test'));
1322         $this->assertTrue($cache->purge());
1323         $this->assertFalse($cache->get('test'));
1325         // Test the many commands.
1326         $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
1327         $result = $cache->get_many(array('a', 'b', 'c'));
1328         $this->assertInternalType('array', $result);
1329         $this->assertCount(3, $result);
1330         $this->assertArrayHasKey('a', $result);
1331         $this->assertArrayHasKey('b', $result);
1332         $this->assertArrayHasKey('c', $result);
1333         $this->assertEquals('A', $result['a']);
1334         $this->assertEquals('B', $result['b']);
1335         $this->assertEquals('C', $result['c']);
1336         $this->assertEquals($result, $cache->get_many(array('a', 'b', 'c')));
1337         $this->assertEquals(2, $cache->delete_many(array('a', 'c')));
1338         $result = $cache->get_many(array('a', 'b', 'c'));
1339         $this->assertInternalType('array', $result);
1340         $this->assertCount(3, $result);
1341         $this->assertArrayHasKey('a', $result);
1342         $this->assertArrayHasKey('b', $result);
1343         $this->assertArrayHasKey('c', $result);
1344         $this->assertFalse($result['a']);
1345         $this->assertEquals('B', $result['b']);
1346         $this->assertFalse($result['c']);
1348         // Test non-recursive deletes.
1349         $this->assertTrue($cache->set('test', 'test'));
1350         $this->assertSame('test', $cache->get('test'));
1351         $this->assertTrue($cache->delete('test', false));
1352         // We should still have it on a deeper loader.
1353         $this->assertSame('test', $cache->get('test'));
1354         // Test non-recusive with many functions.
1355         $this->assertSame(3, $cache->set_many(array(
1356             'one' => 'one',
1357             'two' => 'two',
1358             'three' => 'three'
1359         )));
1360         $this->assertSame('one', $cache->get('one'));
1361         $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1362         $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
1363         $this->assertSame('one', $cache->get('one'));
1364         $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1365     }
1367     /**
1368      * Test switching users with session caches.
1369      */
1370     public function test_session_cache_switch_user() {
1371         $this->resetAfterTest(true);
1372         $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache');
1373         $user1 = $this->getDataGenerator()->create_user();
1374         $user2 = $this->getDataGenerator()->create_user();
1376         // Log in as the first user.
1377         $this->setUser($user1);
1378         $sesskey1 = sesskey();
1380         // Set a basic value in the cache.
1381         $cache->set('var', 1);
1382         $this->assertTrue($cache->has('var'));
1383         $this->assertEquals(1, $cache->get('var'));
1385         // Change to the second user.
1386         $this->setUser($user2);
1387         $sesskey2 = sesskey();
1389         // Make sure the cache doesn't give us the data for the last user.
1390         $this->assertNotEquals($sesskey1, $sesskey2);
1391         $this->assertFalse($cache->has('var'));
1392         $this->assertEquals(false, $cache->get('var'));
1393     }
1395     /**
1396      * Test switching users with session caches.
1397      */
1398     public function test_session_cache_switch_user_application_mapping() {
1399         $this->resetAfterTest(true);
1400         $instance = cache_config_phpunittest::instance(true);
1401         $instance->phpunit_add_file_store('testfilestore');
1402         $instance->phpunit_add_definition('phpunit/testappsession', array(
1403             'mode' => cache_store::MODE_SESSION,
1404             'component' => 'phpunit',
1405             'area' => 'testappsession'
1406         ));
1407         $instance->phpunit_add_definition_mapping('phpunit/testappsession', 'testfilestore', 3);
1408         $cache = cache::make('phpunit', 'testappsession');
1409         $user1 = $this->getDataGenerator()->create_user();
1410         $user2 = $this->getDataGenerator()->create_user();
1412         // Log in as the first user.
1413         $this->setUser($user1);
1414         $sesskey1 = sesskey();
1416         // Set a basic value in the cache.
1417         $cache->set('var', 1);
1418         $this->assertTrue($cache->has('var'));
1419         $this->assertEquals(1, $cache->get('var'));
1421         // Change to the second user.
1422         $this->setUser($user2);
1423         $sesskey2 = sesskey();
1425         // Make sure the cache doesn't give us the data for the last user.
1426         $this->assertNotEquals($sesskey1, $sesskey2);
1427         $this->assertFalse($cache->has('var'));
1428         $this->assertEquals(false, $cache->get('var'));
1429     }
1431     /**
1432      * Test two session caches being used at once to confirm collisions don't occur.
1433      */
1434     public function test_dual_session_caches() {
1435         $instance = cache_config_phpunittest::instance(true);
1436         $instance->phpunit_add_definition('phpunit/testsess1', array(
1437             'mode' => cache_store::MODE_SESSION,
1438             'component' => 'phpunit',
1439             'area' => 'testsess1'
1440         ));
1441         $instance->phpunit_add_definition('phpunit/testsess2', array(
1442             'mode' => cache_store::MODE_SESSION,
1443             'component' => 'phpunit',
1444             'area' => 'testsess2'
1445         ));
1446         $cache1 = cache::make('phpunit', 'testsess1');
1447         $cache2 = cache::make('phpunit', 'testsess2');
1449         $this->assertFalse($cache1->has('test'));
1450         $this->assertFalse($cache2->has('test'));
1452         $this->assertTrue($cache1->set('test', '1'));
1454         $this->assertTrue($cache1->has('test'));
1455         $this->assertFalse($cache2->has('test'));
1457         $this->assertTrue($cache2->set('test', '2'));
1459         $this->assertEquals(1, $cache1->get('test'));
1460         $this->assertEquals(2, $cache2->get('test'));
1462         $this->assertTrue($cache1->delete('test'));
1463     }
1465     /**
1466      * Test multiple session caches when switching user.
1467      */
1468     public function test_session_cache_switch_user_multiple() {
1469         $this->resetAfterTest(true);
1470         $cache1 = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache1');
1471         $cache2 = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache2');
1472         $user1 = $this->getDataGenerator()->create_user();
1473         $user2 = $this->getDataGenerator()->create_user();
1475         // Log in as the first user.
1476         $this->setUser($user1);
1477         $sesskey1 = sesskey();
1479         // Set a basic value in the caches.
1480         $cache1->set('var', 1);
1481         $cache2->set('var', 2);
1482         $this->assertEquals(1, $cache1->get('var'));
1483         $this->assertEquals(2, $cache2->get('var'));
1485         // Change to the second user.
1486         $this->setUser($user2);
1487         $sesskey2 = sesskey();
1489         // Make sure the cache doesn't give us the data for the last user.
1490         // Also make sure that switching the user has lead to both caches being purged.
1491         $this->assertNotEquals($sesskey1, $sesskey2);
1492         $this->assertEquals(false, $cache1->get('var'));
1493         $this->assertEquals(false, $cache2->get('var'));
1494     }
1496     /**
1497      * Test application locking.
1498      */
1499     public function test_application_locking() {
1500         $instance = cache_config_phpunittest::instance(true);
1501         $instance->phpunit_add_definition('phpunit/test_application_locking', array(
1502             'mode' => cache_store::MODE_APPLICATION,
1503             'component' => 'phpunit',
1504             'area' => 'test_application_locking',
1505             'staticacceleration' => true,
1506             'staticaccelerationsize' => 1,
1507             'requirelockingread' => true,
1508             'requirelockingwrite' => true
1509         ));
1510         $cache = cache::make('phpunit', 'test_application_locking');
1511         $this->assertInstanceOf('cache_application', $cache);
1513         $this->assertTrue($cache->set('a', 'A'));
1514         $this->assertTrue($cache->set('b', 'B'));
1515         $this->assertTrue($cache->set('c', 'C'));
1516         $this->assertEquals('A', $cache->get('a'));
1517         $this->assertEquals(array('b' => 'B', 'c' => 'C'), $cache->get_many(array('b', 'c')));
1518         $this->assertTrue($cache->delete('a'));
1519         $this->assertFalse($cache->has('a'));
1520     }
1522     /**
1523      * Test the static cache_helper method purge_stores_used_by_definition.
1524      */
1525     public function test_purge_stores_used_by_definition() {
1526         $instance = cache_config_phpunittest::instance(true);
1527         $instance->phpunit_add_definition('phpunit/test_purge_stores_used_by_definition', array(
1528             'mode' => cache_store::MODE_APPLICATION,
1529             'component' => 'phpunit',
1530             'area' => 'test_purge_stores_used_by_definition'
1531         ));
1532         $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition');
1533         $this->assertInstanceOf('cache_application', $cache);
1534         $this->assertTrue($cache->set('test', 'test'));
1535         unset($cache);
1537         cache_helper::purge_stores_used_by_definition('phpunit', 'test_purge_stores_used_by_definition');
1539         $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition');
1540         $this->assertInstanceOf('cache_application', $cache);
1541         $this->assertFalse($cache->get('test'));
1542     }
1544     /**
1545      * Test purge routines.
1546      */
1547     public function test_purge_routines() {
1548         $instance = cache_config_phpunittest::instance(true);
1549         $instance->phpunit_add_definition('phpunit/purge1', array(
1550             'mode' => cache_store::MODE_APPLICATION,
1551             'component' => 'phpunit',
1552             'area' => 'purge1'
1553         ));
1554         $instance->phpunit_add_definition('phpunit/purge2', array(
1555             'mode' => cache_store::MODE_APPLICATION,
1556             'component' => 'phpunit',
1557             'area' => 'purge2',
1558             'requireidentifiers' => array(
1559                 'id'
1560             )
1561         ));
1563         $factory = cache_factory::instance();
1564         $definition = $factory->create_definition('phpunit', 'purge1');
1565         $this->assertFalse($definition->has_required_identifiers());
1566         $cache = $factory->create_cache($definition);
1567         $this->assertInstanceOf('cache_application', $cache);
1568         $this->assertTrue($cache->set('test', 'test'));
1569         $this->assertTrue($cache->has('test'));
1570         cache_helper::purge_by_definition('phpunit', 'purge1');
1571         $this->assertFalse($cache->has('test'));
1573         $factory = cache_factory::instance();
1574         $definition = $factory->create_definition('phpunit', 'purge2');
1575         $this->assertTrue($definition->has_required_identifiers());
1576         $cache = $factory->create_cache($definition);
1577         $this->assertInstanceOf('cache_application', $cache);
1578         $this->assertTrue($cache->set('test', 'test'));
1579         $this->assertTrue($cache->has('test'));
1580         cache_helper::purge_stores_used_by_definition('phpunit', 'purge2');
1581         $this->assertFalse($cache->has('test'));
1583         try {
1584             cache_helper::purge_by_definition('phpunit', 'purge2');
1585             $this->fail('Should not be able to purge a definition required identifiers without providing them.');
1586         } catch (coding_exception $ex) {
1587             $this->assertContains('Identifier required for cache has not been provided', $ex->getMessage());
1588         }
1589     }
1591     /**
1592      * Test that the default stores all support searching.
1593      */
1594     public function test_defaults_support_searching() {
1595         $instance = cache_config_phpunittest::instance(true);
1596         $instance->phpunit_add_definition('phpunit/search1', array(
1597             'mode' => cache_store::MODE_APPLICATION,
1598             'component' => 'phpunit',
1599             'area' => 'search1',
1600             'requiresearchable' => true
1601         ));
1602         $instance->phpunit_add_definition('phpunit/search2', array(
1603             'mode' => cache_store::MODE_SESSION,
1604             'component' => 'phpunit',
1605             'area' => 'search2',
1606             'requiresearchable' => true
1607         ));
1608         $instance->phpunit_add_definition('phpunit/search3', array(
1609             'mode' => cache_store::MODE_REQUEST,
1610             'component' => 'phpunit',
1611             'area' => 'search3',
1612             'requiresearchable' => true
1613         ));
1614         $factory = cache_factory::instance();
1616         // Test application cache is searchable.
1617         $definition = $factory->create_definition('phpunit', 'search1');
1618         $this->assertInstanceOf('cache_definition', $definition);
1619         $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1620         $cache = $factory->create_cache($definition);
1621         $this->assertInstanceOf('cache_application', $cache);
1622         $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1624         // Test session cache is searchable.
1625         $definition = $factory->create_definition('phpunit', 'search2');
1626         $this->assertInstanceOf('cache_definition', $definition);
1627         $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1628         $cache = $factory->create_cache($definition);
1629         $this->assertInstanceOf('cache_session', $cache);
1630         $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1632         // Test request cache is searchable.
1633         $definition = $factory->create_definition('phpunit', 'search3');
1634         $this->assertInstanceOf('cache_definition', $definition);
1635         $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1636         $cache = $factory->create_cache($definition);
1637         $this->assertInstanceOf('cache_request', $cache);
1638         $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1639     }
1641     public function test_static_acceleration() {
1642         $instance = cache_config_phpunittest::instance();
1643         $instance->phpunit_add_definition('phpunit/accelerated', array(
1644             'mode' => cache_store::MODE_APPLICATION,
1645             'component' => 'phpunit',
1646             'area' => 'accelerated',
1647             'staticacceleration' => true,
1648             'staticaccelerationsize' => 3,
1649         ));
1650         $instance->phpunit_add_definition('phpunit/accelerated2', array(
1651             'mode' => cache_store::MODE_APPLICATION,
1652             'component' => 'phpunit',
1653             'area' => 'accelerated2',
1654             'staticacceleration' => true,
1655             'staticaccelerationsize' => 3,
1656         ));
1657         $instance->phpunit_add_definition('phpunit/accelerated3', array(
1658             'mode' => cache_store::MODE_APPLICATION,
1659             'component' => 'phpunit',
1660             'area' => 'accelerated3',
1661             'staticacceleration' => true,
1662             'staticaccelerationsize' => 3,
1663         ));
1664         $instance->phpunit_add_definition('phpunit/accelerated4', array(
1665             'mode' => cache_store::MODE_APPLICATION,
1666             'component' => 'phpunit',
1667             'area' => 'accelerated4',
1668             'staticacceleration' => true,
1669             'staticaccelerationsize' => 4,
1670         ));
1671         $cache = cache::make('phpunit', 'accelerated');
1672         $this->assertInstanceOf('cache_phpunit_application', $cache);
1674         // Set and get three elements.
1675         $this->assertTrue($cache->set('a', 'A'));
1676         $this->assertTrue($cache->set('b', 'B'));
1677         $this->assertTrue($cache->set('c', 'C'));
1678         $this->assertEquals('A', $cache->get('a'));
1679         $this->assertEquals(array('b' => 'B', 'c' => 'C'), $cache->get_many(array('b', 'c')));
1681         // Make sure all items are in static acceleration array.
1682         $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1683         $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b'));
1684         $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1686         // Add new value and make sure it is in cache and it is in array.
1687         $this->assertTrue($cache->set('d', 'D'));
1688         $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1689         $this->assertEquals('D', $cache->get('d'));
1691         // Now the least recent accessed item (a) is no longer in acceleration array.
1692         $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1693         $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b'));
1694         $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1696         // Adding and deleting element.
1697         $this->assertTrue($cache->set('a', 'A'));
1698         $this->assertTrue($cache->delete('a'));
1699         $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1700         $this->assertFalse($cache->has('a'));
1702         // Make sure "purge" deletes from the array as well.
1703         $cache->purge();
1704         $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1705         $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1706         $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1707         $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1708         $this->assertFalse($cache->phpunit_static_acceleration_get('e'));
1710         // Check that the array holds the last accessed items by get/set.
1711         $this->assertTrue($cache->set('a', 'A'));
1712         $this->assertTrue($cache->set('b', 'B'));
1713         $this->assertTrue($cache->set('c', 'C'));
1714         $this->assertTrue($cache->set('d', 'D'));
1715         $this->assertTrue($cache->set('e', 'E'));
1716         $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1717         $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1718         $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1719         $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1720         $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1722         /** @var cache_phpunit_application $cache */
1723         $cache = cache::make('phpunit', 'accelerated2');
1724         $this->assertInstanceOf('cache_phpunit_application', $cache);
1726         // Check that the array holds the last accessed items by get/set.
1727         $this->assertTrue($cache->set('a', 'A'));
1728         $this->assertTrue($cache->set('b', 'B'));
1729         $this->assertTrue($cache->set('c', 'C'));
1730         $this->assertTrue($cache->set('d', 'D'));
1731         $this->assertTrue($cache->set('e', 'E'));
1732         // Current keys in the array: c, d, e.
1733         $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1734         $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1735         $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1736         $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1737         $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1739         $this->assertEquals('A', $cache->get('a'));
1740         // Current keys in the array: d, e, a.
1741         $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1742         $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1743         $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1744         $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1745         $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1747         // Current keys in the array: d, e, a.
1748         $this->assertEquals(array('c' => 'C'), $cache->get_many(array('c')));
1749         // Current keys in the array: e, a, c.
1750         $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1751         $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1752         $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1753         $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1754         $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1757         $cache = cache::make('phpunit', 'accelerated3');
1758         $this->assertInstanceOf('cache_phpunit_application', $cache);
1760         // Check that the array holds the last accessed items by get/set.
1761         $this->assertTrue($cache->set('a', 'A'));
1762         $this->assertTrue($cache->set('b', 'B'));
1763         $this->assertTrue($cache->set('c', 'C'));
1764         $this->assertTrue($cache->set('d', 'D'));
1765         $this->assertTrue($cache->set('e', 'E'));
1766         $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1767         $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1768         $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1769         $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1770         $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1772         $this->assertTrue($cache->set('b', 'B2'));
1773         $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1774         $this->assertEquals('B2', $cache->phpunit_static_acceleration_get('b'));
1775         $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1776         $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1777         $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1779         $this->assertEquals(2, $cache->set_many(array('b' => 'B3', 'c' => 'C3')));
1780         $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1781         $this->assertEquals('B3', $cache->phpunit_static_acceleration_get('b'));
1782         $this->assertEquals('C3', $cache->phpunit_static_acceleration_get('c'));
1783         $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1784         $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1786         $cache = cache::make('phpunit', 'accelerated4');
1787         $this->assertInstanceOf('cache_phpunit_application', $cache);
1788         $this->assertTrue($cache->set('a', 'A'));
1789         $this->assertTrue($cache->set('a', 'A'));
1790         $this->assertTrue($cache->set('a', 'A'));
1791         $this->assertTrue($cache->set('a', 'A'));
1792         $this->assertTrue($cache->set('a', 'A'));
1793         $this->assertTrue($cache->set('a', 'A'));
1794         $this->assertTrue($cache->set('a', 'A'));
1795         $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1796         $this->assertEquals('A', $cache->get('a'));
1797     }