MDL-45513 cache: implemented defines for unit testing alternative caches
[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      * Returns the expected application cache store.
63      * @return string
64      */
65     protected function get_expected_application_cache_store() {
66         $expected = 'cachestore_file';
67         if (defined('TEST_CACHE_USING_APPLICATION_STORE') && preg_match('#[a-zA-Z][a-zA-Z0-9_]*#', TEST_CACHE_USING_APPLICATION_STORE)) {
68             $expected = 'cachestore_'.(string)TEST_CACHE_USING_APPLICATION_STORE;
69         }
70         return $expected;
71     }
73     /**
74      * Tests cache configuration
75      */
76     public function test_cache_config() {
77         global $CFG;
79         if (defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH &&
80             !empty($CFG->altcacheconfigpath)) {
81             // We need to skip this test - it checks the default config structure, but very likely we arn't using the
82             // default config structure here so theres no point in running the test.
83             $this->markTestSkipped('Skipped testing default cache config structure as alt cache path is being used.');
84         }
86         if (defined('TEST_CACHE_USING_APPLICATION_STORE')) {
87             // We need to skip this test - it checks the default config structure, but very likely we arn't using the
88             // default config structure here because we are testing against an alternative application store.
89             $this->markTestSkipped('Skipped testing default cache config structure as alt application store is being used.');
90         }
92         $instance = cache_config::instance();
93         $this->assertInstanceOf('cache_config_phpunittest', $instance);
95         $this->assertTrue(cache_config_phpunittest::config_file_exists());
97         $stores = $instance->get_all_stores();
98         $this->assertCount(3, $stores);
99         foreach ($stores as $name => $store) {
100             // Check its an array.
101             $this->assertInternalType('array', $store);
102             // Check the name is the key.
103             $this->assertEquals($name, $store['name']);
104             // Check that it has been declared default.
105             $this->assertTrue($store['default']);
106             // Required attributes = name + plugin + configuration + modes + features.
107             $this->assertArrayHasKey('name', $store);
108             $this->assertArrayHasKey('plugin', $store);
109             $this->assertArrayHasKey('configuration', $store);
110             $this->assertArrayHasKey('modes', $store);
111             $this->assertArrayHasKey('features', $store);
112         }
114         $modemappings = $instance->get_mode_mappings();
115         $this->assertCount(3, $modemappings);
116         $modes = array(
117             cache_store::MODE_APPLICATION => false,
118             cache_store::MODE_SESSION => false,
119             cache_store::MODE_REQUEST => false,
120         );
121         foreach ($modemappings as $mapping) {
122             // We expect 3 properties.
123             $this->assertCount(3, $mapping);
124             // Required attributes = mode + store.
125             $this->assertArrayHasKey('mode', $mapping);
126             $this->assertArrayHasKey('store', $mapping);
127             // Record the mode.
128             $modes[$mapping['mode']] = true;
129         }
131         // Must have the default 3 modes and no more.
132         $this->assertCount(3, $mapping);
133         foreach ($modes as $mode) {
134             $this->assertTrue($mode);
135         }
137         $definitions = $instance->get_definitions();
138         // The event invalidation definition is required for the cache API and must be there.
139         $this->assertArrayHasKey('core/eventinvalidation', $definitions);
141         $definitionmappings = $instance->get_definition_mappings();
142         foreach ($definitionmappings as $mapping) {
143             // Required attributes = definition + store.
144             $this->assertArrayHasKey('definition', $mapping);
145             $this->assertArrayHasKey('store', $mapping);
146         }
147     }
149     /**
150      * Tests for cache keys that would break on windows.
151      */
152     public function test_windows_nasty_keys() {
153         $instance = cache_config_phpunittest::instance();
154         $instance->phpunit_add_definition('phpunit/windowskeytest', array(
155             'mode' => cache_store::MODE_APPLICATION,
156             'component' => 'phpunit',
157             'area' => 'windowskeytest',
158             'simplekeys' => true,
159             'simpledata' => true
160         ));
161         $cache = cache::make('phpunit', 'windowskeytest');
162         $this->assertTrue($cache->set('contest', 'test data 1'));
163         $this->assertEquals('test data 1', $cache->get('contest'));
164     }
166     /**
167      * Tests the default application cache
168      */
169     public function test_default_application_cache() {
170         $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'applicationtest');
171         $this->assertInstanceOf('cache_application', $cache);
172         $this->run_on_cache($cache);
174         $instance = cache_config_phpunittest::instance(true);
175         $instance->phpunit_add_definition('phpunit/test_default_application_cache', array(
176             'mode' => cache_store::MODE_APPLICATION,
177             'component' => 'phpunit',
178             'area' => 'test_default_application_cache',
179             'staticacceleration' => true,
180             'staticaccelerationsize' => 1
181         ));
182         $cache = cache::make('phpunit', 'test_default_application_cache');
183         $this->assertInstanceOf('cache_application', $cache);
184         $this->run_on_cache($cache);
185     }
187     /**
188      * Tests the default session cache
189      */
190     public function test_default_session_cache() {
191         $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'applicationtest');
192         $this->assertInstanceOf('cache_session', $cache);
193         $this->run_on_cache($cache);
194     }
196     /**
197      * Tests the default request cache
198      */
199     public function test_default_request_cache() {
200         $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'applicationtest');
201         $this->assertInstanceOf('cache_request', $cache);
202         $this->run_on_cache($cache);
203     }
205     /**
206      * Tests using a cache system when there are no stores available (who knows what the admin did to achieve this).
207      */
208     public function test_on_cache_without_store() {
209         $instance = cache_config_phpunittest::instance(true);
210         $instance->phpunit_add_definition('phpunit/nostoretest1', array(
211             'mode' => cache_store::MODE_APPLICATION,
212             'component' => 'phpunit',
213             'area' => 'nostoretest1',
214         ));
215         $instance->phpunit_add_definition('phpunit/nostoretest2', array(
216             'mode' => cache_store::MODE_APPLICATION,
217             'component' => 'phpunit',
218             'area' => 'nostoretest2',
219             'staticacceleration' => true
220         ));
221         $instance->phpunit_remove_stores();
223         $cache = cache::make('phpunit', 'nostoretest1');
224         $this->run_on_cache($cache);
226         $cache = cache::make('phpunit', 'nostoretest2');
227         $this->run_on_cache($cache);
228     }
230     /**
231      * Runs a standard series of access and use tests on a cache instance.
232      *
233      * This function is great because we can use it to ensure all of the loaders perform exactly the same way.
234      *
235      * @param cache_loader $cache
236      */
237     protected function run_on_cache(cache_loader $cache) {
238         $key = 'contestkey';
239         $datascalars = array('test data', null);
240         $dataarray = array('contest' => 'data', 'part' => 'two');
241         $dataobject = (object)$dataarray;
243         foreach ($datascalars as $datascalar) {
244             $this->assertTrue($cache->purge());
246             // Check all read methods.
247             $this->assertFalse($cache->get($key));
248             $this->assertFalse($cache->has($key));
249             $result = $cache->get_many(array($key));
250             $this->assertCount(1, $result);
251             $this->assertFalse(reset($result));
252             $this->assertFalse($cache->has_any(array($key)));
253             $this->assertFalse($cache->has_all(array($key)));
255             // Set the data.
256             $this->assertTrue($cache->set($key, $datascalar));
257             // Setting it more than once should be permitted.
258             $this->assertTrue($cache->set($key, $datascalar));
260             // Recheck the read methods.
261             $this->assertEquals($datascalar, $cache->get($key));
262             $this->assertTrue($cache->has($key));
263             $result = $cache->get_many(array($key));
264             $this->assertCount(1, $result);
265             $this->assertEquals($datascalar, reset($result));
266             $this->assertTrue($cache->has_any(array($key)));
267             $this->assertTrue($cache->has_all(array($key)));
269             // Delete it.
270             $this->assertTrue($cache->delete($key));
272             // Check its gone.
273             $this->assertFalse($cache->get($key));
274             $this->assertFalse($cache->has($key));
275         }
277         // Test arrays.
278         $this->assertTrue($cache->set($key, $dataarray));
279         $this->assertEquals($dataarray, $cache->get($key));
281         // Test objects.
282         $this->assertTrue($cache->set($key, $dataobject));
283         $this->assertEquals($dataobject, $cache->get($key));
285         $specobject = new cache_phpunit_dummy_object('red', 'blue');
286         $this->assertTrue($cache->set($key, $specobject));
287         $result = $cache->get($key);
288         $this->assertInstanceOf('cache_phpunit_dummy_object', $result);
289         $this->assertEquals('red_ptc_wfc', $result->property1);
290         $this->assertEquals('blue_ptc_wfc', $result->property2);
292         // Test array of objects.
293         $specobject = new cache_phpunit_dummy_object('red', 'blue');
294         $data = new cacheable_object_array(array(
295             clone($specobject),
296             clone($specobject),
297             clone($specobject))
298         );
299         $this->assertTrue($cache->set($key, $data));
300         $result = $cache->get($key);
301         $this->assertInstanceOf('cacheable_object_array', $result);
302         $this->assertCount(3, $data);
303         foreach ($result as $item) {
304             $this->assertInstanceOf('cache_phpunit_dummy_object', $item);
305             $this->assertEquals('red_ptc_wfc', $item->property1);
306             $this->assertEquals('blue_ptc_wfc', $item->property2);
307         }
309         // Test set many.
310         $cache->set_many(array('key1' => 'data1', 'key2' => 'data2', 'key3' => null));
311         $this->assertEquals('data1', $cache->get('key1'));
312         $this->assertEquals('data2', $cache->get('key2'));
313         $this->assertEquals(null, $cache->get('key3'));
314         $this->assertTrue($cache->delete('key1'));
315         $this->assertTrue($cache->delete('key2'));
316         $this->assertTrue($cache->delete('key3'));
318         $cache->set_many(array(
319             'key1' => array(1, 2, 3),
320             'key2' => array(3, 2, 1),
321         ));
322         $this->assertInternalType('array', $cache->get('key1'));
323         $this->assertInternalType('array', $cache->get('key2'));
324         $this->assertCount(3, $cache->get('key1'));
325         $this->assertCount(3, $cache->get('key2'));
326         $this->assertInternalType('array', $cache->get_many(array('key1', 'key2')));
327         $this->assertCount(2, $cache->get_many(array('key1', 'key2')));
328         $this->assertEquals(2, $cache->delete_many(array('key1', 'key2')));
330         // Test delete many.
331         $this->assertTrue($cache->set('key1', 'data1'));
332         $this->assertTrue($cache->set('key2', 'data2'));
333         $this->assertTrue($cache->set('key3', null));
335         $this->assertEquals('data1', $cache->get('key1'));
336         $this->assertEquals('data2', $cache->get('key2'));
337         $this->assertEquals(null, $cache->get('key3'));
339         $this->assertEquals(3, $cache->delete_many(array('key1', 'key2', 'key3')));
341         $this->assertFalse($cache->get('key1'));
342         $this->assertFalse($cache->get('key2'));
343         $this->assertFalse($cache->get('key3'));
345         // Quick reference test.
346         $obj = new stdClass;
347         $obj->key = 'value';
348         $ref =& $obj;
349         $this->assertTrue($cache->set('obj', $obj));
351         $obj->key = 'eulav';
352         $var = $cache->get('obj');
353         $this->assertInstanceOf('stdClass', $var);
354         $this->assertEquals('value', $var->key);
356         $ref->key = 'eulav';
357         $var = $cache->get('obj');
358         $this->assertInstanceOf('stdClass', $var);
359         $this->assertEquals('value', $var->key);
361         $this->assertTrue($cache->delete('obj'));
363         // Deep reference test.
364         $obj1 = new stdClass;
365         $obj1->key = 'value';
366         $obj2 = new stdClass;
367         $obj2->key = 'test';
368         $obj3 = new stdClass;
369         $obj3->key = 'pork';
370         $obj1->subobj =& $obj2;
371         $obj2->subobj =& $obj3;
372         $this->assertTrue($cache->set('obj', $obj1));
374         $obj1->key = 'eulav';
375         $obj2->key = 'tset';
376         $obj3->key = 'krop';
377         $var = $cache->get('obj');
378         $this->assertInstanceOf('stdClass', $var);
379         $this->assertEquals('value', $var->key);
380         $this->assertInstanceOf('stdClass', $var->subobj);
381         $this->assertEquals('test', $var->subobj->key);
382         $this->assertInstanceOf('stdClass', $var->subobj->subobj);
383         $this->assertEquals('pork', $var->subobj->subobj->key);
384         $this->assertTrue($cache->delete('obj'));
386         // Death reference test... basicaly we don't want this to die.
387         $obj = new stdClass;
388         $obj->key = 'value';
389         $obj->self =& $obj;
390         $this->assertTrue($cache->set('obj', $obj));
391         $var = $cache->get('obj');
392         $this->assertInstanceOf('stdClass', $var);
393         $this->assertEquals('value', $var->key);
395         // Reference test after retrieve.
396         $obj = new stdClass;
397         $obj->key = 'value';
398         $this->assertTrue($cache->set('obj', $obj));
400         $var1 = $cache->get('obj');
401         $this->assertInstanceOf('stdClass', $var1);
402         $this->assertEquals('value', $var1->key);
403         $var1->key = 'eulav';
404         $this->assertEquals('eulav', $var1->key);
406         $var2 = $cache->get('obj');
407         $this->assertInstanceOf('stdClass', $var2);
408         $this->assertEquals('value', $var2->key);
410         $this->assertTrue($cache->delete('obj'));
412         // Test strictness exceptions.
413         try {
414             $cache->get('exception', MUST_EXIST);
415             $this->fail('Exception expected from cache::get using MUST_EXIST');
416         } catch (Exception $e) {
417             $this->assertTrue(true);
418         }
419         try {
420             $cache->get_many(array('exception1', 'exception2'), MUST_EXIST);
421             $this->fail('Exception expected from cache::get_many using MUST_EXIST');
422         } catch (Exception $e) {
423             $this->assertTrue(true);
424         }
425         $cache->set('test', 'test');
426         try {
427             $cache->get_many(array('test', 'exception'), MUST_EXIST);
428             $this->fail('Exception expected from cache::get_many using MUST_EXIST');
429         } catch (Exception $e) {
430             $this->assertTrue(true);
431         }
432     }
434     /**
435      * Tests a definition using a data loader
436      */
437     public function test_definition_data_loader() {
438         $instance = cache_config_phpunittest::instance(true);
439         $instance->phpunit_add_definition('phpunit/datasourcetest', array(
440             'mode' => cache_store::MODE_APPLICATION,
441             'component' => 'phpunit',
442             'area' => 'datasourcetest',
443             'datasource' => 'cache_phpunit_dummy_datasource',
444             'datasourcefile' => 'cache/tests/fixtures/lib.php'
445         ));
447         $cache = cache::make('phpunit', 'datasourcetest');
448         $this->assertInstanceOf('cache_application', $cache);
450         // Purge it to be sure.
451         $this->assertTrue($cache->purge());
452         // It won't be there yet.
453         $this->assertFalse($cache->has('Test'));
454         // It should load it ;).
455         $this->assertTrue($cache->has('Test', true));
457         // Purge it to be sure.
458         $this->assertTrue($cache->purge());
459         $this->assertEquals('Test has no value really.', $cache->get('Test'));
461         // Test multiple values.
462         $this->assertTrue($cache->purge());
463         $this->assertTrue($cache->set('b', 'B'));
464         $result = $cache->get_many(array('a', 'b', 'c'));
465         $this->assertInternalType('array', $result);
466         $this->assertCount(3, $result);
467         $this->assertArrayHasKey('a', $result);
468         $this->assertArrayHasKey('b', $result);
469         $this->assertArrayHasKey('c', $result);
470         $this->assertEquals('a has no value really.', $result['a']);
471         $this->assertEquals('B', $result['b']);
472         $this->assertEquals('c has no value really.', $result['c']);
473     }
475     /**
476      * Tests a definition using an overridden loader
477      */
478     public function test_definition_overridden_loader() {
479         $instance = cache_config_phpunittest::instance(true);
480         $instance->phpunit_add_definition('phpunit/overridetest', array(
481             'mode' => cache_store::MODE_APPLICATION,
482             'component' => 'phpunit',
483             'area' => 'overridetest',
484             'overrideclass' => 'cache_phpunit_dummy_overrideclass',
485             'overrideclassfile' => 'cache/tests/fixtures/lib.php'
486         ));
487         $cache = cache::make('phpunit', 'overridetest');
488         $this->assertInstanceOf('cache_phpunit_dummy_overrideclass', $cache);
489         $this->assertInstanceOf('cache_application', $cache);
490         // Purge it to be sure.
491         $this->assertTrue($cache->purge());
492         // It won't be there yet.
493         $this->assertFalse($cache->has('Test'));
494         // Add it.
495         $this->assertTrue($cache->set('Test', 'Test has no value really.'));
496         // Check its there.
497         $this->assertEquals('Test has no value really.', $cache->get('Test'));
498     }
500     /**
501      * Test the mappingsonly setting.
502      */
503     public function test_definition_mappings_only() {
504         /** @var cache_config_phpunittest $instance */
505         $instance = cache_config_phpunittest::instance(true);
506         $instance->phpunit_add_definition('phpunit/mappingsonly', array(
507             'mode' => cache_store::MODE_APPLICATION,
508             'component' => 'phpunit',
509             'area' => 'mappingsonly',
510             'mappingsonly' => true
511         ), false);
512         $instance->phpunit_add_definition('phpunit/nonmappingsonly', array(
513             'mode' => cache_store::MODE_APPLICATION,
514             'component' => 'phpunit',
515             'area' => 'nonmappingsonly',
516             'mappingsonly' => false
517         ), false);
519         $cacheonly = cache::make('phpunit', 'mappingsonly');
520         $this->assertInstanceOf('cache_application', $cacheonly);
521         $this->assertEquals('cachestore_dummy', $cacheonly->phpunit_get_store_class());
523         $expected = $this->get_expected_application_cache_store();
524         $cachenon = cache::make('phpunit', 'nonmappingsonly');
525         $this->assertInstanceOf('cache_application', $cachenon);
526         $this->assertEquals($expected, $cachenon->phpunit_get_store_class());
527     }
529     /**
530      * Test a very basic definition.
531      */
532     public function test_definition() {
533         $instance = cache_config_phpunittest::instance();
534         $instance->phpunit_add_definition('phpunit/test', array(
535             'mode' => cache_store::MODE_APPLICATION,
536             'component' => 'phpunit',
537             'area' => 'test',
538         ));
539         $cache = cache::make('phpunit', 'test');
541         $this->assertTrue($cache->set('testkey1', 'test data 1'));
542         $this->assertEquals('test data 1', $cache->get('testkey1'));
543         $this->assertTrue($cache->set('testkey2', 'test data 2'));
544         $this->assertEquals('test data 2', $cache->get('testkey2'));
545     }
547     /**
548      * Test a definition using the simple keys.
549      */
550     public function test_definition_simplekeys() {
551         $instance = cache_config_phpunittest::instance();
552         $instance->phpunit_add_definition('phpunit/simplekeytest', array(
553             'mode' => cache_store::MODE_APPLICATION,
554             'component' => 'phpunit',
555             'area' => 'simplekeytest',
556             'simplekeys' => true
557         ));
558         $cache = cache::make('phpunit', 'simplekeytest');
560         $this->assertTrue($cache->set('testkey1', 'test data 1'));
561         $this->assertEquals('test data 1', $cache->get('testkey1'));
562         $this->assertTrue($cache->set('testkey2', 'test data 2'));
563         $this->assertEquals('test data 2', $cache->get('testkey2'));
565         $cache->purge();
567         $this->assertTrue($cache->set('1', 'test data 1'));
568         $this->assertEquals('test data 1', $cache->get('1'));
569         $this->assertTrue($cache->set('2', 'test data 2'));
570         $this->assertEquals('test data 2', $cache->get('2'));
571     }
573     /**
574      * Test a negative TTL on an application cache.
575      */
576     public function test_application_ttl_negative() {
577         $instance = cache_config_phpunittest::instance(true);
578         $instance->phpunit_add_definition('phpunit/ttltest', array(
579             'mode' => cache_store::MODE_APPLICATION,
580             'component' => 'phpunit',
581             'area' => 'ttltest',
582             'ttl' => -86400 // Set to a day in the past to be extra sure.
583         ));
584         $cache = cache::make('phpunit', 'ttltest');
585         $this->assertInstanceOf('cache_application', $cache);
587         // Purge it to be sure.
588         $this->assertTrue($cache->purge());
589         // It won't be there yet.
590         $this->assertFalse($cache->has('Test'));
591         // Set it now.
592         $this->assertTrue($cache->set('Test', 'Test'));
593         // Check its not there.
594         $this->assertFalse($cache->has('Test'));
595         // Double check by trying to get it.
596         $this->assertFalse($cache->get('Test'));
598         // Test with multiple keys.
599         $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
600         $result = $cache->get_many(array('a', 'b', 'c'));
601         $this->assertInternalType('array', $result);
602         $this->assertCount(3, $result);
603         $this->assertArrayHasKey('a', $result);
604         $this->assertArrayHasKey('b', $result);
605         $this->assertArrayHasKey('c', $result);
606         $this->assertFalse($result['a']);
607         $this->assertFalse($result['b']);
608         $this->assertFalse($result['c']);
610         // Test with multiple keys including missing ones.
611         $result = $cache->get_many(array('a', 'c', 'e'));
612         $this->assertInternalType('array', $result);
613         $this->assertCount(3, $result);
614         $this->assertArrayHasKey('a', $result);
615         $this->assertArrayHasKey('c', $result);
616         $this->assertArrayHasKey('e', $result);
617         $this->assertFalse($result['a']);
618         $this->assertFalse($result['c']);
619         $this->assertFalse($result['e']);
620     }
622     /**
623      * Test a positive TTL on an application cache.
624      */
625     public function test_application_ttl_positive() {
626         $instance = cache_config_phpunittest::instance(true);
627         $instance->phpunit_add_definition('phpunit/ttltest', array(
628             'mode' => cache_store::MODE_APPLICATION,
629             'component' => 'phpunit',
630             'area' => 'ttltest',
631             'ttl' => 86400 // Set to a day in the future to be extra sure.
632         ));
633         $cache = cache::make('phpunit', 'ttltest');
634         $this->assertInstanceOf('cache_application', $cache);
636         // Purge it to be sure.
637         $this->assertTrue($cache->purge());
638         // It won't be there yet.
639         $this->assertFalse($cache->has('Test'));
640         // Set it now.
641         $this->assertTrue($cache->set('Test', 'Test'));
642         // Check its there.
643         $this->assertTrue($cache->has('Test'));
644         // Double check by trying to get it.
645         $this->assertEquals('Test', $cache->get('Test'));
647         // Test with multiple keys.
648         $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
649         $result = $cache->get_many(array('a', 'b', 'c'));
650         $this->assertInternalType('array', $result);
651         $this->assertCount(3, $result);
652         $this->assertArrayHasKey('a', $result);
653         $this->assertArrayHasKey('b', $result);
654         $this->assertArrayHasKey('c', $result);
655         $this->assertEquals('A', $result['a']);
656         $this->assertEquals('B', $result['b']);
657         $this->assertEquals('C', $result['c']);
659         // Test with multiple keys including missing ones.
660         $result = $cache->get_many(array('a', 'c', 'e'));
661         $this->assertInternalType('array', $result);
662         $this->assertCount(3, $result);
663         $this->assertArrayHasKey('a', $result);
664         $this->assertArrayHasKey('c', $result);
665         $this->assertArrayHasKey('e', $result);
666         $this->assertEquals('A', $result['a']);
667         $this->assertEquals('C', $result['c']);
668         $this->assertEquals(false, $result['e']);
669     }
671     /**
672      * Test a negative TTL on an session cache.
673      */
674     public function test_session_ttl_positive() {
675         $instance = cache_config_phpunittest::instance(true);
676         $instance->phpunit_add_definition('phpunit/ttltest', array(
677             'mode' => cache_store::MODE_SESSION,
678             'component' => 'phpunit',
679             'area' => 'ttltest',
680             'ttl' => 86400 // Set to a day in the future to be extra sure.
681         ));
682         $cache = cache::make('phpunit', 'ttltest');
683         $this->assertInstanceOf('cache_session', $cache);
685         // Purge it to be sure.
686         $this->assertTrue($cache->purge());
687         // It won't be there yet.
688         $this->assertFalse($cache->has('Test'));
689         // Set it now.
690         $this->assertTrue($cache->set('Test', 'Test'));
691         // Check its there.
692         $this->assertTrue($cache->has('Test'));
693         // Double check by trying to get it.
694         $this->assertEquals('Test', $cache->get('Test'));
696         // Test with multiple keys.
697         $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
698         $result = $cache->get_many(array('a', 'b', 'c'));
699         $this->assertInternalType('array', $result);
700         $this->assertCount(3, $result);
701         $this->assertArrayHasKey('a', $result);
702         $this->assertArrayHasKey('b', $result);
703         $this->assertArrayHasKey('c', $result);
704         $this->assertEquals('A', $result['a']);
705         $this->assertEquals('B', $result['b']);
706         $this->assertEquals('C', $result['c']);
708         // Test with multiple keys including missing ones.
709         $result = $cache->get_many(array('a', 'c', 'e'));
710         $this->assertInternalType('array', $result);
711         $this->assertCount(3, $result);
712         $this->assertArrayHasKey('a', $result);
713         $this->assertArrayHasKey('c', $result);
714         $this->assertArrayHasKey('e', $result);
715         $this->assertEquals('A', $result['a']);
716         $this->assertEquals('C', $result['c']);
717         $this->assertEquals(false, $result['e']);
718     }
720     /**
721      * Tests manual locking operations on an application cache
722      */
723     public function test_application_manual_locking() {
724         $instance = cache_config_phpunittest::instance();
725         $instance->phpunit_add_definition('phpunit/lockingtest', array(
726             'mode' => cache_store::MODE_APPLICATION,
727             'component' => 'phpunit',
728             'area' => 'lockingtest'
729         ));
730         $cache1 = cache::make('phpunit', 'lockingtest');
731         $cache2 = clone($cache1);
733         $this->assertTrue($cache1->set('testkey', 'test data'));
734         $this->assertTrue($cache2->set('testkey', 'test data'));
736         $this->assertTrue($cache1->acquire_lock('testkey'));
737         $this->assertFalse($cache2->acquire_lock('testkey'));
739         $this->assertTrue($cache1->check_lock_state('testkey'));
740         $this->assertFalse($cache2->check_lock_state('testkey'));
742         $this->assertTrue($cache1->release_lock('testkey'));
743         $this->assertFalse($cache2->release_lock('testkey'));
745         $this->assertTrue($cache1->set('testkey', 'test data'));
746         $this->assertTrue($cache2->set('testkey', 'test data'));
747     }
749     /**
750      * Tests application cache event invalidation
751      */
752     public function test_application_event_invalidation() {
753         $instance = cache_config_phpunittest::instance();
754         $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
755             'mode' => cache_store::MODE_APPLICATION,
756             'component' => 'phpunit',
757             'area' => 'eventinvalidationtest',
758             'invalidationevents' => array(
759                 'crazyevent'
760             )
761         ));
762         $cache = cache::make('phpunit', 'eventinvalidationtest');
764         $this->assertTrue($cache->set('testkey1', 'test data 1'));
765         $this->assertEquals('test data 1', $cache->get('testkey1'));
766         $this->assertTrue($cache->set('testkey2', 'test data 2'));
767         $this->assertEquals('test data 2', $cache->get('testkey2'));
769         // Test invalidating a single entry.
770         cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
772         $this->assertFalse($cache->get('testkey1'));
773         $this->assertEquals('test data 2', $cache->get('testkey2'));
775         $this->assertTrue($cache->set('testkey1', 'test data 1'));
777         // Test invalidating both entries.
778         cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
780         $this->assertFalse($cache->get('testkey1'));
781         $this->assertFalse($cache->get('testkey2'));
782     }
784     /**
785      * Tests session cache event invalidation
786      */
787     public function test_session_event_invalidation() {
788         $instance = cache_config_phpunittest::instance();
789         $instance->phpunit_add_definition('phpunit/test_session_event_invalidation', array(
790             'mode' => cache_store::MODE_SESSION,
791             'component' => 'phpunit',
792             'area' => 'test_session_event_invalidation',
793             'invalidationevents' => array(
794                 'crazyevent'
795             )
796         ));
797         $cache = cache::make('phpunit', 'test_session_event_invalidation');
798         $this->assertInstanceOf('cache_session', $cache);
800         $this->assertTrue($cache->set('testkey1', 'test data 1'));
801         $this->assertEquals('test data 1', $cache->get('testkey1'));
802         $this->assertTrue($cache->set('testkey2', 'test data 2'));
803         $this->assertEquals('test data 2', $cache->get('testkey2'));
805         // Test invalidating a single entry.
806         cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
808         $this->assertFalse($cache->get('testkey1'));
809         $this->assertEquals('test data 2', $cache->get('testkey2'));
811         $this->assertTrue($cache->set('testkey1', 'test data 1'));
813         // Test invalidating both entries.
814         cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
816         $this->assertFalse($cache->get('testkey1'));
817         $this->assertFalse($cache->get('testkey2'));
818     }
820     /**
821      * Tests application cache definition invalidation
822      */
823     public function test_application_definition_invalidation() {
824         $instance = cache_config_phpunittest::instance();
825         $instance->phpunit_add_definition('phpunit/definitioninvalidation', array(
826             'mode' => cache_store::MODE_APPLICATION,
827             'component' => 'phpunit',
828             'area' => 'definitioninvalidation'
829         ));
830         $cache = cache::make('phpunit', 'definitioninvalidation');
831         $this->assertTrue($cache->set('testkey1', 'test data 1'));
832         $this->assertEquals('test data 1', $cache->get('testkey1'));
833         $this->assertTrue($cache->set('testkey2', 'test data 2'));
834         $this->assertEquals('test data 2', $cache->get('testkey2'));
836         cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), 'testkey1');
838         $this->assertFalse($cache->get('testkey1'));
839         $this->assertEquals('test data 2', $cache->get('testkey2'));
841         $this->assertTrue($cache->set('testkey1', 'test data 1'));
843         cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1'));
845         $this->assertFalse($cache->get('testkey1'));
846         $this->assertEquals('test data 2', $cache->get('testkey2'));
848         $this->assertTrue($cache->set('testkey1', 'test data 1'));
850         cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1', 'testkey2'));
852         $this->assertFalse($cache->get('testkey1'));
853         $this->assertFalse($cache->get('testkey2'));
854     }
856     /**
857      * Tests session cache definition invalidation
858      */
859     public function test_session_definition_invalidation() {
860         $instance = cache_config_phpunittest::instance();
861         $instance->phpunit_add_definition('phpunit/test_session_definition_invalidation', array(
862             'mode' => cache_store::MODE_SESSION,
863             'component' => 'phpunit',
864             'area' => 'test_session_definition_invalidation'
865         ));
866         $cache = cache::make('phpunit', 'test_session_definition_invalidation');
867         $this->assertInstanceOf('cache_session', $cache);
868         $this->assertTrue($cache->set('testkey1', 'test data 1'));
869         $this->assertEquals('test data 1', $cache->get('testkey1'));
870         $this->assertTrue($cache->set('testkey2', 'test data 2'));
871         $this->assertEquals('test data 2', $cache->get('testkey2'));
873         cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(), 'testkey1');
875         $this->assertFalse($cache->get('testkey1'));
876         $this->assertEquals('test data 2', $cache->get('testkey2'));
878         $this->assertTrue($cache->set('testkey1', 'test data 1'));
880         cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(),
881                 array('testkey1'));
883         $this->assertFalse($cache->get('testkey1'));
884         $this->assertEquals('test data 2', $cache->get('testkey2'));
886         $this->assertTrue($cache->set('testkey1', 'test data 1'));
888         cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(),
889                 array('testkey1', 'testkey2'));
891         $this->assertFalse($cache->get('testkey1'));
892         $this->assertFalse($cache->get('testkey2'));
893     }
895     /**
896      * Tests application cache event invalidation over a distributed setup.
897      */
898     public function test_distributed_application_event_invalidation() {
899         global $CFG;
900         // This is going to be an intense wee test.
901         // We need to add data the to cache, invalidate it by event, manually force it back without MUC knowing to simulate a
902         // disconnected/distributed setup (think load balanced server using local cache), instantiate the cache again and finally
903         // check that it is not picked up.
904         $instance = cache_config_phpunittest::instance();
905         $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
906             'mode' => cache_store::MODE_APPLICATION,
907             'component' => 'phpunit',
908             'area' => 'eventinvalidationtest',
909             'simplekeys' => true,
910             'simpledata' => true,
911             'invalidationevents' => array(
912                 'crazyevent'
913             )
914         ));
915         $cache = cache::make('phpunit', 'eventinvalidationtest');
916         $this->assertTrue($cache->set('testkey1', 'test data 1'));
917         $this->assertEquals('test data 1', $cache->get('testkey1'));
919         cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
921         $this->assertFalse($cache->get('testkey1'));
923         // OK data added, data invalidated, and invalidation time has been set.
924         // Now we need to manually add back the data and adjust the invalidation time.
925         $hash = md5(cache_store::MODE_APPLICATION.'/phpunit/eventinvalidationtest/'.$CFG->wwwroot.'phpunit');
926         $timefile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/las-cache/lastinvalidation-$hash.cache";
927         // Make sure the file is correct.
928         $this->assertTrue(file_exists($timefile));
929         $timecont = serialize(cache::now() - 60); // Back 60sec in the past to force it to re-invalidate.
930         make_writable_directory(dirname($timefile));
931         file_put_contents($timefile, $timecont);
932         $this->assertTrue(file_exists($timefile));
934         $datafile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/tes-cache/testkey1-$hash.cache";
935         $datacont = serialize("test data 1");
936         make_writable_directory(dirname($datafile));
937         file_put_contents($datafile, $datacont);
938         $this->assertTrue(file_exists($datafile));
940         // Test 1: Rebuild without the event and test its there.
941         cache_factory::reset();
942         $instance = cache_config_phpunittest::instance();
943         $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
944             'mode' => cache_store::MODE_APPLICATION,
945             'component' => 'phpunit',
946             'area' => 'eventinvalidationtest',
947             'simplekeys' => true,
948             'simpledata' => true,
949         ));
950         $cache = cache::make('phpunit', 'eventinvalidationtest');
951         $this->assertEquals('test data 1', $cache->get('testkey1'));
953         // Test 2: Rebuild and test the invalidation of the event via the invalidation cache.
954         cache_factory::reset();
955         $instance = cache_config_phpunittest::instance();
956         $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
957             'mode' => cache_store::MODE_APPLICATION,
958             'component' => 'phpunit',
959             'area' => 'eventinvalidationtest',
960             'simplekeys' => true,
961             'simpledata' => true,
962             'invalidationevents' => array(
963                 'crazyevent'
964             )
965         ));
966         $cache = cache::make('phpunit', 'eventinvalidationtest');
967         $this->assertFalse($cache->get('testkey1'));
968     }
970     /**
971      * Tests application cache event purge
972      */
973     public function test_application_event_purge() {
974         $instance = cache_config_phpunittest::instance();
975         $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
976             'mode' => cache_store::MODE_APPLICATION,
977             'component' => 'phpunit',
978             'area' => 'eventpurgetest',
979             'invalidationevents' => array(
980                 'crazyevent'
981             )
982         ));
983         $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', array(
984             'mode' => cache_store::MODE_APPLICATION,
985             'component' => 'phpunit',
986             'area' => 'eventpurgetestaccelerated',
987             'staticacceleration' => true,
988             'invalidationevents' => array(
989                 'crazyevent'
990             )
991         ));
992         $cache = cache::make('phpunit', 'eventpurgetest');
994         $this->assertTrue($cache->set('testkey1', 'test data 1'));
995         $this->assertEquals('test data 1', $cache->get('testkey1'));
996         $this->assertTrue($cache->set('testkey2', 'test data 2'));
997         $this->assertEquals('test data 2', $cache->get('testkey2'));
999         // Purge the event.
1000         cache_helper::purge_by_event('crazyevent');
1002         // Check things have been removed.
1003         $this->assertFalse($cache->get('testkey1'));
1004         $this->assertFalse($cache->get('testkey2'));
1006         // Now test the static acceleration array.
1007         $cache = cache::make('phpunit', 'eventpurgetestaccelerated');
1008         $this->assertTrue($cache->set('testkey1', 'test data 1'));
1009         $this->assertEquals('test data 1', $cache->get('testkey1'));
1010         $this->assertTrue($cache->set('testkey2', 'test data 2'));
1011         $this->assertEquals('test data 2', $cache->get('testkey2'));
1013         // Purge the event.
1014         cache_helper::purge_by_event('crazyevent');
1016         // Check things have been removed.
1017         $this->assertFalse($cache->get('testkey1'));
1018         $this->assertFalse($cache->get('testkey2'));
1019     }
1021     /**
1022      * Tests session cache event purge
1023      */
1024     public function test_session_event_purge() {
1025         $instance = cache_config_phpunittest::instance();
1026         $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
1027             'mode' => cache_store::MODE_SESSION,
1028             'component' => 'phpunit',
1029             'area' => 'eventpurgetest',
1030             'invalidationevents' => array(
1031                 'crazyevent'
1032             )
1033         ));
1034         $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', array(
1035             'mode' => cache_store::MODE_SESSION,
1036             'component' => 'phpunit',
1037             'area' => 'eventpurgetestaccelerated',
1038             'staticacceleration' => true,
1039             'invalidationevents' => array(
1040                 'crazyevent'
1041             )
1042         ));
1043         $cache = cache::make('phpunit', 'eventpurgetest');
1045         $this->assertTrue($cache->set('testkey1', 'test data 1'));
1046         $this->assertEquals('test data 1', $cache->get('testkey1'));
1047         $this->assertTrue($cache->set('testkey2', 'test data 2'));
1048         $this->assertEquals('test data 2', $cache->get('testkey2'));
1050         // Purge the event.
1051         cache_helper::purge_by_event('crazyevent');
1053         // Check things have been removed.
1054         $this->assertFalse($cache->get('testkey1'));
1055         $this->assertFalse($cache->get('testkey2'));
1057         // Now test the static acceleration array.
1058         $cache = cache::make('phpunit', 'eventpurgetestaccelerated');
1059         $this->assertTrue($cache->set('testkey1', 'test data 1'));
1060         $this->assertEquals('test data 1', $cache->get('testkey1'));
1061         $this->assertTrue($cache->set('testkey2', 'test data 2'));
1062         $this->assertEquals('test data 2', $cache->get('testkey2'));
1064         // Purge the event.
1065         cache_helper::purge_by_event('crazyevent');
1067         // Check things have been removed.
1068         $this->assertFalse($cache->get('testkey1'));
1069         $this->assertFalse($cache->get('testkey2'));
1070     }
1072     /**
1073      * Tests application cache definition purge
1074      */
1075     public function test_application_definition_purge() {
1076         $instance = cache_config_phpunittest::instance();
1077         $instance->phpunit_add_definition('phpunit/definitionpurgetest', array(
1078             'mode' => cache_store::MODE_APPLICATION,
1079             'component' => 'phpunit',
1080             'area' => 'definitionpurgetest',
1081             'invalidationevents' => array(
1082                 'crazyevent'
1083             )
1084         ));
1085         $cache = cache::make('phpunit', 'definitionpurgetest');
1087         $this->assertTrue($cache->set('testkey1', 'test data 1'));
1088         $this->assertEquals('test data 1', $cache->get('testkey1'));
1089         $this->assertTrue($cache->set('testkey2', 'test data 2'));
1090         $this->assertEquals('test data 2', $cache->get('testkey2'));
1092         // Purge the event.
1093         cache_helper::purge_by_definition('phpunit', 'definitionpurgetest');
1095         // Check things have been removed.
1096         $this->assertFalse($cache->get('testkey1'));
1097         $this->assertFalse($cache->get('testkey2'));
1098     }
1100     /**
1101      * Test the use of an alt path.
1102      * If we can generate a config instance we are done :)
1103      */
1104     public function test_alt_cache_path() {
1105         global $CFG;
1106         if ((defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH) || !empty($CFG->altcacheconfigpath)) {
1107             $this->markTestSkipped('Skipped testing alt cache path as it is already being used.');
1108         }
1109         $this->resetAfterTest();
1110         $CFG->altcacheconfigpath = $CFG->dataroot.'/cache/altcacheconfigpath';
1111         $instance = cache_config_phpunittest::instance();
1112         $this->assertInstanceOf('cache_config', $instance);
1113     }
1115     /**
1116      * Test disabling the cache stores.
1117      */
1118     public function test_disable_stores() {
1119         $instance = cache_config_phpunittest::instance();
1120         $instance->phpunit_add_definition('phpunit/disabletest1', array(
1121             'mode' => cache_store::MODE_APPLICATION,
1122             'component' => 'phpunit',
1123             'area' => 'disabletest1'
1124         ));
1125         $instance->phpunit_add_definition('phpunit/disabletest2', array(
1126             'mode' => cache_store::MODE_SESSION,
1127             'component' => 'phpunit',
1128             'area' => 'disabletest2'
1129         ));
1130         $instance->phpunit_add_definition('phpunit/disabletest3', array(
1131             'mode' => cache_store::MODE_REQUEST,
1132             'component' => 'phpunit',
1133             'area' => 'disabletest3'
1134         ));
1136         $caches = array(
1137             'disabletest1' => cache::make('phpunit', 'disabletest1'),
1138             'disabletest2' => cache::make('phpunit', 'disabletest2'),
1139             'disabletest3' => cache::make('phpunit', 'disabletest3')
1140         );
1142         $this->assertInstanceOf('cache_phpunit_application', $caches['disabletest1']);
1143         $this->assertInstanceOf('cache_phpunit_session', $caches['disabletest2']);
1144         $this->assertInstanceOf('cache_phpunit_request', $caches['disabletest3']);
1146         $this->assertEquals('cachestore_file', $caches['disabletest1']->phpunit_get_store_class());
1147         $this->assertEquals('cachestore_session', $caches['disabletest2']->phpunit_get_store_class());
1148         $this->assertEquals('cachestore_static', $caches['disabletest3']->phpunit_get_store_class());
1150         foreach ($caches as $cache) {
1151             $this->assertFalse($cache->get('test'));
1152             $this->assertTrue($cache->set('test', 'test'));
1153             $this->assertEquals('test', $cache->get('test'));
1154         }
1156         cache_factory::disable_stores();
1158         $caches = array(
1159             'disabletest1' => cache::make('phpunit', 'disabletest1'),
1160             'disabletest2' => cache::make('phpunit', 'disabletest2'),
1161             'disabletest3' => cache::make('phpunit', 'disabletest3')
1162         );
1164         $this->assertInstanceOf('cache_phpunit_application', $caches['disabletest1']);
1165         $this->assertInstanceOf('cache_phpunit_session', $caches['disabletest2']);
1166         $this->assertInstanceOf('cache_phpunit_request', $caches['disabletest3']);
1168         $this->assertEquals('cachestore_dummy', $caches['disabletest1']->phpunit_get_store_class());
1169         $this->assertEquals('cachestore_dummy', $caches['disabletest2']->phpunit_get_store_class());
1170         $this->assertEquals('cachestore_dummy', $caches['disabletest3']->phpunit_get_store_class());
1172         foreach ($caches as $cache) {
1173             $this->assertFalse($cache->get('test'));
1174             $this->assertTrue($cache->set('test', 'test'));
1175             $this->assertEquals('test', $cache->get('test'));
1176         }
1177     }
1179     /**
1180      * Test disabling the cache.
1181      */
1182     public function test_disable() {
1183         global $CFG;
1185         if ((defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH) || !empty($CFG->altcacheconfigpath)) {
1186             // We can't run this test as it requires us to delete the cache configuration script which we just
1187             // cant do with a custom path in play.
1188             $this->markTestSkipped('Skipped testing cache disable functionality as alt cache path is being used.');
1189         }
1191         $configfile = $CFG->dataroot.'/muc/config.php';
1193         // That's right, we're deleting the config file.
1194         $this->assertTrue(@unlink($configfile));
1196         // Disable the cache
1197         cache_phpunit_factory::phpunit_disable();
1199         // Check we get the expected disabled factory.
1200         $factory = cache_factory::instance();
1201         $this->assertInstanceOf('cache_factory_disabled', $factory);
1203         // Check we get the expected disabled config.
1204         $config = $factory->create_config_instance();
1205         $this->assertInstanceOf('cache_config_disabled', $config);
1207         // Check we get the expected disabled caches.
1208         $cache = cache::make('phpunit', 'disable');
1209         $this->assertInstanceOf('cache_disabled', $cache);
1211         // Test an application cache.
1212         $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'disable');
1213         $this->assertInstanceOf('cache_disabled', $cache);
1215         $this->assertFalse(file_exists($configfile));
1217         $this->assertFalse($cache->get('test'));
1218         $this->assertFalse($cache->set('test', 'test'));
1219         $this->assertFalse($cache->delete('test'));
1220         $this->assertTrue($cache->purge());
1222         // Test a session cache.
1223         $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'disable');
1224         $this->assertInstanceOf('cache_disabled', $cache);
1226         $this->assertFalse(file_exists($configfile));
1228         $this->assertFalse($cache->get('test'));
1229         $this->assertFalse($cache->set('test', 'test'));
1230         $this->assertFalse($cache->delete('test'));
1231         $this->assertTrue($cache->purge());
1233         // Finally test a request cache.
1234         $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'disable');
1235         $this->assertInstanceOf('cache_disabled', $cache);
1237         $this->assertFalse(file_exists($configfile));
1239         $this->assertFalse($cache->get('test'));
1240         $this->assertFalse($cache->set('test', 'test'));
1241         $this->assertFalse($cache->delete('test'));
1242         $this->assertTrue($cache->purge());
1244         cache_factory::reset();
1246         $factory = cache_factory::instance(true);
1247         $config = $factory->create_config_instance();
1248         $this->assertEquals('cache_config_phpunittest', get_class($config));
1249     }
1251     /**
1252      * Test that multiple application loaders work ok.
1253      */
1254     public function test_multiple_application_loaders() {
1255         $instance = cache_config_phpunittest::instance(true);
1256         $instance->phpunit_add_file_store('phpunittest1');
1257         $instance->phpunit_add_file_store('phpunittest2');
1258         $instance->phpunit_add_definition('phpunit/multi_loader', array(
1259             'mode' => cache_store::MODE_APPLICATION,
1260             'component' => 'phpunit',
1261             'area' => 'multi_loader'
1262         ));
1263         $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
1264         $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
1266         $cache = cache::make('phpunit', 'multi_loader');
1267         $this->assertInstanceOf('cache_application', $cache);
1268         $this->assertFalse($cache->get('test'));
1269         $this->assertTrue($cache->set('test', 'test'));
1270         $this->assertEquals('test', $cache->get('test'));
1271         $this->assertTrue($cache->delete('test'));
1272         $this->assertFalse($cache->get('test'));
1273         $this->assertTrue($cache->set('test', 'test'));
1274         $this->assertTrue($cache->purge());
1275         $this->assertFalse($cache->get('test'));
1277         // Test the many commands.
1278         $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
1279         $result = $cache->get_many(array('a', 'b', 'c'));
1280         $this->assertInternalType('array', $result);
1281         $this->assertCount(3, $result);
1282         $this->assertArrayHasKey('a', $result);
1283         $this->assertArrayHasKey('b', $result);
1284         $this->assertArrayHasKey('c', $result);
1285         $this->assertEquals('A', $result['a']);
1286         $this->assertEquals('B', $result['b']);
1287         $this->assertEquals('C', $result['c']);
1288         $this->assertEquals($result, $cache->get_many(array('a', 'b', 'c')));
1289         $this->assertEquals(2, $cache->delete_many(array('a', 'c')));
1290         $result = $cache->get_many(array('a', 'b', 'c'));
1291         $this->assertInternalType('array', $result);
1292         $this->assertCount(3, $result);
1293         $this->assertArrayHasKey('a', $result);
1294         $this->assertArrayHasKey('b', $result);
1295         $this->assertArrayHasKey('c', $result);
1296         $this->assertFalse($result['a']);
1297         $this->assertEquals('B', $result['b']);
1298         $this->assertFalse($result['c']);
1300         // Test non-recursive deletes.
1301         $this->assertTrue($cache->set('test', 'test'));
1302         $this->assertSame('test', $cache->get('test'));
1303         $this->assertTrue($cache->delete('test', false));
1304         // We should still have it on a deeper loader.
1305         $this->assertSame('test', $cache->get('test'));
1306         // Test non-recusive with many functions.
1307         $this->assertSame(3, $cache->set_many(array(
1308             'one' => 'one',
1309             'two' => 'two',
1310             'three' => 'three'
1311         )));
1312         $this->assertSame('one', $cache->get('one'));
1313         $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1314         $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
1315         $this->assertSame('one', $cache->get('one'));
1316         $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1317     }
1319     /**
1320      * Test that multiple application loaders work ok.
1321      */
1322     public function test_multiple_session_loaders() {
1323         /* @var cache_config_phpunittest $instance */
1324         $instance = cache_config_phpunittest::instance(true);
1325         $instance->phpunit_add_session_store('phpunittest1');
1326         $instance->phpunit_add_session_store('phpunittest2');
1327         $instance->phpunit_add_definition('phpunit/multi_loader', array(
1328             'mode' => cache_store::MODE_SESSION,
1329             'component' => 'phpunit',
1330             'area' => 'multi_loader'
1331         ));
1332         $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
1333         $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
1335         $cache = cache::make('phpunit', 'multi_loader');
1336         $this->assertInstanceOf('cache_session', $cache);
1337         $this->assertFalse($cache->get('test'));
1338         $this->assertTrue($cache->set('test', 'test'));
1339         $this->assertEquals('test', $cache->get('test'));
1340         $this->assertTrue($cache->delete('test'));
1341         $this->assertFalse($cache->get('test'));
1342         $this->assertTrue($cache->set('test', 'test'));
1343         $this->assertTrue($cache->purge());
1344         $this->assertFalse($cache->get('test'));
1346         // Test the many commands.
1347         $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
1348         $result = $cache->get_many(array('a', 'b', 'c'));
1349         $this->assertInternalType('array', $result);
1350         $this->assertCount(3, $result);
1351         $this->assertArrayHasKey('a', $result);
1352         $this->assertArrayHasKey('b', $result);
1353         $this->assertArrayHasKey('c', $result);
1354         $this->assertEquals('A', $result['a']);
1355         $this->assertEquals('B', $result['b']);
1356         $this->assertEquals('C', $result['c']);
1357         $this->assertEquals($result, $cache->get_many(array('a', 'b', 'c')));
1358         $this->assertEquals(2, $cache->delete_many(array('a', 'c')));
1359         $result = $cache->get_many(array('a', 'b', 'c'));
1360         $this->assertInternalType('array', $result);
1361         $this->assertCount(3, $result);
1362         $this->assertArrayHasKey('a', $result);
1363         $this->assertArrayHasKey('b', $result);
1364         $this->assertArrayHasKey('c', $result);
1365         $this->assertFalse($result['a']);
1366         $this->assertEquals('B', $result['b']);
1367         $this->assertFalse($result['c']);
1369         // Test non-recursive deletes.
1370         $this->assertTrue($cache->set('test', 'test'));
1371         $this->assertSame('test', $cache->get('test'));
1372         $this->assertTrue($cache->delete('test', false));
1373         // We should still have it on a deeper loader.
1374         $this->assertSame('test', $cache->get('test'));
1375         // Test non-recusive with many functions.
1376         $this->assertSame(3, $cache->set_many(array(
1377             'one' => 'one',
1378             'two' => 'two',
1379             'three' => 'three'
1380         )));
1381         $this->assertSame('one', $cache->get('one'));
1382         $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1383         $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
1384         $this->assertSame('one', $cache->get('one'));
1385         $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1386     }
1388     /**
1389      * Test switching users with session caches.
1390      */
1391     public function test_session_cache_switch_user() {
1392         $this->resetAfterTest(true);
1393         $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache');
1394         $user1 = $this->getDataGenerator()->create_user();
1395         $user2 = $this->getDataGenerator()->create_user();
1397         // Log in as the first user.
1398         $this->setUser($user1);
1399         $sesskey1 = sesskey();
1401         // Set a basic value in the cache.
1402         $cache->set('var', 1);
1403         $this->assertTrue($cache->has('var'));
1404         $this->assertEquals(1, $cache->get('var'));
1406         // Change to the second user.
1407         $this->setUser($user2);
1408         $sesskey2 = sesskey();
1410         // Make sure the cache doesn't give us the data for the last user.
1411         $this->assertNotEquals($sesskey1, $sesskey2);
1412         $this->assertFalse($cache->has('var'));
1413         $this->assertEquals(false, $cache->get('var'));
1414     }
1416     /**
1417      * Test switching users with session caches.
1418      */
1419     public function test_session_cache_switch_user_application_mapping() {
1420         $this->resetAfterTest(true);
1421         $instance = cache_config_phpunittest::instance(true);
1422         $instance->phpunit_add_file_store('testfilestore');
1423         $instance->phpunit_add_definition('phpunit/testappsession', array(
1424             'mode' => cache_store::MODE_SESSION,
1425             'component' => 'phpunit',
1426             'area' => 'testappsession'
1427         ));
1428         $instance->phpunit_add_definition_mapping('phpunit/testappsession', 'testfilestore', 3);
1429         $cache = cache::make('phpunit', 'testappsession');
1430         $user1 = $this->getDataGenerator()->create_user();
1431         $user2 = $this->getDataGenerator()->create_user();
1433         // Log in as the first user.
1434         $this->setUser($user1);
1435         $sesskey1 = sesskey();
1437         // Set a basic value in the cache.
1438         $cache->set('var', 1);
1439         $this->assertTrue($cache->has('var'));
1440         $this->assertEquals(1, $cache->get('var'));
1442         // Change to the second user.
1443         $this->setUser($user2);
1444         $sesskey2 = sesskey();
1446         // Make sure the cache doesn't give us the data for the last user.
1447         $this->assertNotEquals($sesskey1, $sesskey2);
1448         $this->assertFalse($cache->has('var'));
1449         $this->assertEquals(false, $cache->get('var'));
1450     }
1452     /**
1453      * Test two session caches being used at once to confirm collisions don't occur.
1454      */
1455     public function test_dual_session_caches() {
1456         $instance = cache_config_phpunittest::instance(true);
1457         $instance->phpunit_add_definition('phpunit/testsess1', array(
1458             'mode' => cache_store::MODE_SESSION,
1459             'component' => 'phpunit',
1460             'area' => 'testsess1'
1461         ));
1462         $instance->phpunit_add_definition('phpunit/testsess2', array(
1463             'mode' => cache_store::MODE_SESSION,
1464             'component' => 'phpunit',
1465             'area' => 'testsess2'
1466         ));
1467         $cache1 = cache::make('phpunit', 'testsess1');
1468         $cache2 = cache::make('phpunit', 'testsess2');
1470         $this->assertFalse($cache1->has('test'));
1471         $this->assertFalse($cache2->has('test'));
1473         $this->assertTrue($cache1->set('test', '1'));
1475         $this->assertTrue($cache1->has('test'));
1476         $this->assertFalse($cache2->has('test'));
1478         $this->assertTrue($cache2->set('test', '2'));
1480         $this->assertEquals(1, $cache1->get('test'));
1481         $this->assertEquals(2, $cache2->get('test'));
1483         $this->assertTrue($cache1->delete('test'));
1484     }
1486     /**
1487      * Test multiple session caches when switching user.
1488      */
1489     public function test_session_cache_switch_user_multiple() {
1490         $this->resetAfterTest(true);
1491         $cache1 = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache1');
1492         $cache2 = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache2');
1493         $user1 = $this->getDataGenerator()->create_user();
1494         $user2 = $this->getDataGenerator()->create_user();
1496         // Log in as the first user.
1497         $this->setUser($user1);
1498         $sesskey1 = sesskey();
1500         // Set a basic value in the caches.
1501         $cache1->set('var', 1);
1502         $cache2->set('var', 2);
1503         $this->assertEquals(1, $cache1->get('var'));
1504         $this->assertEquals(2, $cache2->get('var'));
1506         // Change to the second user.
1507         $this->setUser($user2);
1508         $sesskey2 = sesskey();
1510         // Make sure the cache doesn't give us the data for the last user.
1511         // Also make sure that switching the user has lead to both caches being purged.
1512         $this->assertNotEquals($sesskey1, $sesskey2);
1513         $this->assertEquals(false, $cache1->get('var'));
1514         $this->assertEquals(false, $cache2->get('var'));
1515     }
1517     /**
1518      * Test application locking.
1519      */
1520     public function test_application_locking() {
1521         $instance = cache_config_phpunittest::instance(true);
1522         $instance->phpunit_add_definition('phpunit/test_application_locking', array(
1523             'mode' => cache_store::MODE_APPLICATION,
1524             'component' => 'phpunit',
1525             'area' => 'test_application_locking',
1526             'staticacceleration' => true,
1527             'staticaccelerationsize' => 1,
1528             'requirelockingread' => true,
1529             'requirelockingwrite' => true
1530         ));
1531         $cache = cache::make('phpunit', 'test_application_locking');
1532         $this->assertInstanceOf('cache_application', $cache);
1534         $this->assertTrue($cache->set('a', 'A'));
1535         $this->assertTrue($cache->set('b', 'B'));
1536         $this->assertTrue($cache->set('c', 'C'));
1537         $this->assertEquals('A', $cache->get('a'));
1538         $this->assertEquals(array('b' => 'B', 'c' => 'C'), $cache->get_many(array('b', 'c')));
1539         $this->assertTrue($cache->delete('a'));
1540         $this->assertFalse($cache->has('a'));
1541     }
1543     /**
1544      * Test the static cache_helper method purge_stores_used_by_definition.
1545      */
1546     public function test_purge_stores_used_by_definition() {
1547         $instance = cache_config_phpunittest::instance(true);
1548         $instance->phpunit_add_definition('phpunit/test_purge_stores_used_by_definition', array(
1549             'mode' => cache_store::MODE_APPLICATION,
1550             'component' => 'phpunit',
1551             'area' => 'test_purge_stores_used_by_definition'
1552         ));
1553         $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition');
1554         $this->assertInstanceOf('cache_application', $cache);
1555         $this->assertTrue($cache->set('test', 'test'));
1556         unset($cache);
1558         cache_helper::purge_stores_used_by_definition('phpunit', 'test_purge_stores_used_by_definition');
1560         $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition');
1561         $this->assertInstanceOf('cache_application', $cache);
1562         $this->assertFalse($cache->get('test'));
1563     }
1565     /**
1566      * Test purge routines.
1567      */
1568     public function test_purge_routines() {
1569         $instance = cache_config_phpunittest::instance(true);
1570         $instance->phpunit_add_definition('phpunit/purge1', array(
1571             'mode' => cache_store::MODE_APPLICATION,
1572             'component' => 'phpunit',
1573             'area' => 'purge1'
1574         ));
1575         $instance->phpunit_add_definition('phpunit/purge2', array(
1576             'mode' => cache_store::MODE_APPLICATION,
1577             'component' => 'phpunit',
1578             'area' => 'purge2',
1579             'requireidentifiers' => array(
1580                 'id'
1581             )
1582         ));
1584         $factory = cache_factory::instance();
1585         $definition = $factory->create_definition('phpunit', 'purge1');
1586         $this->assertFalse($definition->has_required_identifiers());
1587         $cache = $factory->create_cache($definition);
1588         $this->assertInstanceOf('cache_application', $cache);
1589         $this->assertTrue($cache->set('test', 'test'));
1590         $this->assertTrue($cache->has('test'));
1591         cache_helper::purge_by_definition('phpunit', 'purge1');
1592         $this->assertFalse($cache->has('test'));
1594         $factory = cache_factory::instance();
1595         $definition = $factory->create_definition('phpunit', 'purge2');
1596         $this->assertTrue($definition->has_required_identifiers());
1597         $cache = $factory->create_cache($definition);
1598         $this->assertInstanceOf('cache_application', $cache);
1599         $this->assertTrue($cache->set('test', 'test'));
1600         $this->assertTrue($cache->has('test'));
1601         cache_helper::purge_stores_used_by_definition('phpunit', 'purge2');
1602         $this->assertFalse($cache->has('test'));
1604         try {
1605             cache_helper::purge_by_definition('phpunit', 'purge2');
1606             $this->fail('Should not be able to purge a definition required identifiers without providing them.');
1607         } catch (coding_exception $ex) {
1608             $this->assertContains('Identifier required for cache has not been provided', $ex->getMessage());
1609         }
1610     }
1612     /**
1613      * Test that the default stores all support searching.
1614      */
1615     public function test_defaults_support_searching() {
1616         $instance = cache_config_phpunittest::instance(true);
1617         $instance->phpunit_add_definition('phpunit/search1', array(
1618             'mode' => cache_store::MODE_APPLICATION,
1619             'component' => 'phpunit',
1620             'area' => 'search1',
1621             'requiresearchable' => true
1622         ));
1623         $instance->phpunit_add_definition('phpunit/search2', array(
1624             'mode' => cache_store::MODE_SESSION,
1625             'component' => 'phpunit',
1626             'area' => 'search2',
1627             'requiresearchable' => true
1628         ));
1629         $instance->phpunit_add_definition('phpunit/search3', array(
1630             'mode' => cache_store::MODE_REQUEST,
1631             'component' => 'phpunit',
1632             'area' => 'search3',
1633             'requiresearchable' => true
1634         ));
1635         $factory = cache_factory::instance();
1637         // Test application cache is searchable.
1638         $definition = $factory->create_definition('phpunit', 'search1');
1639         $this->assertInstanceOf('cache_definition', $definition);
1640         $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1641         $cache = $factory->create_cache($definition);
1642         $this->assertInstanceOf('cache_application', $cache);
1643         $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1645         // Test session cache is searchable.
1646         $definition = $factory->create_definition('phpunit', 'search2');
1647         $this->assertInstanceOf('cache_definition', $definition);
1648         $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1649         $cache = $factory->create_cache($definition);
1650         $this->assertInstanceOf('cache_session', $cache);
1651         $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1653         // Test request cache is searchable.
1654         $definition = $factory->create_definition('phpunit', 'search3');
1655         $this->assertInstanceOf('cache_definition', $definition);
1656         $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1657         $cache = $factory->create_cache($definition);
1658         $this->assertInstanceOf('cache_request', $cache);
1659         $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1660     }
1662     public function test_static_acceleration() {
1663         $instance = cache_config_phpunittest::instance();
1664         $instance->phpunit_add_definition('phpunit/accelerated', array(
1665             'mode' => cache_store::MODE_APPLICATION,
1666             'component' => 'phpunit',
1667             'area' => 'accelerated',
1668             'staticacceleration' => true,
1669             'staticaccelerationsize' => 3,
1670         ));
1671         $instance->phpunit_add_definition('phpunit/accelerated2', array(
1672             'mode' => cache_store::MODE_APPLICATION,
1673             'component' => 'phpunit',
1674             'area' => 'accelerated2',
1675             'staticacceleration' => true,
1676             'staticaccelerationsize' => 3,
1677         ));
1678         $instance->phpunit_add_definition('phpunit/accelerated3', array(
1679             'mode' => cache_store::MODE_APPLICATION,
1680             'component' => 'phpunit',
1681             'area' => 'accelerated3',
1682             'staticacceleration' => true,
1683             'staticaccelerationsize' => 3,
1684         ));
1685         $instance->phpunit_add_definition('phpunit/accelerated4', array(
1686             'mode' => cache_store::MODE_APPLICATION,
1687             'component' => 'phpunit',
1688             'area' => 'accelerated4',
1689             'staticacceleration' => true,
1690             'staticaccelerationsize' => 4,
1691         ));
1692         $cache = cache::make('phpunit', 'accelerated');
1693         $this->assertInstanceOf('cache_phpunit_application', $cache);
1695         // Set and get three elements.
1696         $this->assertTrue($cache->set('a', 'A'));
1697         $this->assertTrue($cache->set('b', 'B'));
1698         $this->assertTrue($cache->set('c', 'C'));
1699         $this->assertEquals('A', $cache->get('a'));
1700         $this->assertEquals(array('b' => 'B', 'c' => 'C'), $cache->get_many(array('b', 'c')));
1702         // Make sure all items are in static acceleration array.
1703         $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1704         $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b'));
1705         $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1707         // Add new value and make sure it is in cache and it is in array.
1708         $this->assertTrue($cache->set('d', 'D'));
1709         $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1710         $this->assertEquals('D', $cache->get('d'));
1712         // Now the least recent accessed item (a) is no longer in acceleration array.
1713         $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1714         $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b'));
1715         $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1717         // Adding and deleting element.
1718         $this->assertTrue($cache->set('a', 'A'));
1719         $this->assertTrue($cache->delete('a'));
1720         $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1721         $this->assertFalse($cache->has('a'));
1723         // Make sure "purge" deletes from the array as well.
1724         $cache->purge();
1725         $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1726         $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1727         $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1728         $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1729         $this->assertFalse($cache->phpunit_static_acceleration_get('e'));
1731         // Check that the array holds the last accessed items by get/set.
1732         $this->assertTrue($cache->set('a', 'A'));
1733         $this->assertTrue($cache->set('b', 'B'));
1734         $this->assertTrue($cache->set('c', 'C'));
1735         $this->assertTrue($cache->set('d', 'D'));
1736         $this->assertTrue($cache->set('e', 'E'));
1737         $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1738         $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1739         $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1740         $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1741         $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1743         /** @var cache_phpunit_application $cache */
1744         $cache = cache::make('phpunit', 'accelerated2');
1745         $this->assertInstanceOf('cache_phpunit_application', $cache);
1747         // Check that the array holds the last accessed items by get/set.
1748         $this->assertTrue($cache->set('a', 'A'));
1749         $this->assertTrue($cache->set('b', 'B'));
1750         $this->assertTrue($cache->set('c', 'C'));
1751         $this->assertTrue($cache->set('d', 'D'));
1752         $this->assertTrue($cache->set('e', 'E'));
1753         // Current keys in the array: c, d, e.
1754         $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1755         $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1756         $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1757         $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1758         $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1760         $this->assertEquals('A', $cache->get('a'));
1761         // Current keys in the array: d, e, a.
1762         $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1763         $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1764         $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1765         $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1766         $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1768         // Current keys in the array: d, e, a.
1769         $this->assertEquals(array('c' => 'C'), $cache->get_many(array('c')));
1770         // Current keys in the array: e, a, c.
1771         $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1772         $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1773         $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1774         $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1775         $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1778         $cache = cache::make('phpunit', 'accelerated3');
1779         $this->assertInstanceOf('cache_phpunit_application', $cache);
1781         // Check that the array holds the last accessed items by get/set.
1782         $this->assertTrue($cache->set('a', 'A'));
1783         $this->assertTrue($cache->set('b', 'B'));
1784         $this->assertTrue($cache->set('c', 'C'));
1785         $this->assertTrue($cache->set('d', 'D'));
1786         $this->assertTrue($cache->set('e', 'E'));
1787         $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1788         $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1789         $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1790         $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1791         $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1793         $this->assertTrue($cache->set('b', 'B2'));
1794         $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1795         $this->assertEquals('B2', $cache->phpunit_static_acceleration_get('b'));
1796         $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1797         $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1798         $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1800         $this->assertEquals(2, $cache->set_many(array('b' => 'B3', 'c' => 'C3')));
1801         $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1802         $this->assertEquals('B3', $cache->phpunit_static_acceleration_get('b'));
1803         $this->assertEquals('C3', $cache->phpunit_static_acceleration_get('c'));
1804         $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1805         $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1807         $cache = cache::make('phpunit', 'accelerated4');
1808         $this->assertInstanceOf('cache_phpunit_application', $cache);
1809         $this->assertTrue($cache->set('a', 'A'));
1810         $this->assertTrue($cache->set('a', 'A'));
1811         $this->assertTrue($cache->set('a', 'A'));
1812         $this->assertTrue($cache->set('a', 'A'));
1813         $this->assertTrue($cache->set('a', 'A'));
1814         $this->assertTrue($cache->set('a', 'A'));
1815         $this->assertTrue($cache->set('a', 'A'));
1816         $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1817         $this->assertEquals('A', $cache->get('a'));
1818     }