Moodle release 3.4.1
[moodle.git] / cache / tests / cache_test.php
blob8d04cd18dfaa73c8b6b009cc0ec8364810689701
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.
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
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.
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
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
39 * @copyright 2012 Sam Hemelryk
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class core_cache_testcase extends advanced_testcase {
44 /**
45 * Set things back to the default before each test.
47 public function setUp() {
48 parent::setUp();
49 cache_factory::reset();
50 cache_config_testing::create_default_configuration();
53 /**
54 * Final task is to reset the cache system
56 public static function tearDownAfterClass() {
57 parent::tearDownAfterClass();
58 cache_factory::reset();
61 /**
62 * Returns the expected application cache store.
63 * @return string
65 protected function get_expected_application_cache_store() {
66 global $CFG;
67 $expected = 'cachestore_file';
69 // Verify if we are using any of the available ways to use a different application store within tests.
70 if (defined('TEST_CACHE_USING_APPLICATION_STORE') && preg_match('#[a-zA-Z][a-zA-Z0-9_]*#', TEST_CACHE_USING_APPLICATION_STORE)) {
71 // 1st way. Using some of the testing servers.
72 $expected = 'cachestore_'.(string)TEST_CACHE_USING_APPLICATION_STORE;
74 } else if (defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH && !empty($CFG->altcacheconfigpath)) {
75 // 2nd way. Using an alternative configuration.
76 $defaultstores = cache_helper::get_stores_suitable_for_mode_default();
77 $instance = cache_config::instance();
78 // Iterate over defined mode mappings until we get an application one not being the default.
79 foreach ($instance->get_mode_mappings() as $mapping) {
80 // If the store is not for application mode, ignore.
81 if ($mapping['mode'] !== cache_store::MODE_APPLICATION) {
82 continue;
84 // If the store matches some default mapping store name, ignore.
85 if (array_key_exists($mapping['store'], $defaultstores) && !empty($defaultstores[$mapping['store']]['default'])) {
86 continue;
88 // Arrived here, have found an application mode store not being the default mapped one (file),
89 // that's the one we are using in the configuration for sure.
90 $expected = 'cachestore_'.$mapping['store'];
94 return $expected;
97 /**
98 * Tests cache configuration
100 public function test_cache_config() {
101 global $CFG;
103 if (defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH &&
104 !empty($CFG->altcacheconfigpath)) {
105 // We need to skip this test - it checks the default config structure, but very likely we arn't using the
106 // default config structure here so theres no point in running the test.
107 $this->markTestSkipped('Skipped testing default cache config structure as alt cache path is being used.');
110 if (defined('TEST_CACHE_USING_APPLICATION_STORE')) {
111 // We need to skip this test - it checks the default config structure, but very likely we arn't using the
112 // default config structure here because we are testing against an alternative application store.
113 $this->markTestSkipped('Skipped testing default cache config structure as alt application store is being used.');
116 $instance = cache_config::instance();
117 $this->assertInstanceOf('cache_config_testing', $instance);
119 $this->assertTrue(cache_config_testing::config_file_exists());
121 $stores = $instance->get_all_stores();
122 $this->assertCount(3, $stores);
123 foreach ($stores as $name => $store) {
124 // Check its an array.
125 $this->assertInternalType('array', $store);
126 // Check the name is the key.
127 $this->assertEquals($name, $store['name']);
128 // Check that it has been declared default.
129 $this->assertTrue($store['default']);
130 // Required attributes = name + plugin + configuration + modes + features.
131 $this->assertArrayHasKey('name', $store);
132 $this->assertArrayHasKey('plugin', $store);
133 $this->assertArrayHasKey('configuration', $store);
134 $this->assertArrayHasKey('modes', $store);
135 $this->assertArrayHasKey('features', $store);
138 $modemappings = $instance->get_mode_mappings();
139 $this->assertCount(3, $modemappings);
140 $modes = array(
141 cache_store::MODE_APPLICATION => false,
142 cache_store::MODE_SESSION => false,
143 cache_store::MODE_REQUEST => false,
145 foreach ($modemappings as $mapping) {
146 // We expect 3 properties.
147 $this->assertCount(3, $mapping);
148 // Required attributes = mode + store.
149 $this->assertArrayHasKey('mode', $mapping);
150 $this->assertArrayHasKey('store', $mapping);
151 // Record the mode.
152 $modes[$mapping['mode']] = true;
155 // Must have the default 3 modes and no more.
156 $this->assertCount(3, $mapping);
157 foreach ($modes as $mode) {
158 $this->assertTrue($mode);
161 $definitions = $instance->get_definitions();
162 // The event invalidation definition is required for the cache API and must be there.
163 $this->assertArrayHasKey('core/eventinvalidation', $definitions);
165 $definitionmappings = $instance->get_definition_mappings();
166 foreach ($definitionmappings as $mapping) {
167 // Required attributes = definition + store.
168 $this->assertArrayHasKey('definition', $mapping);
169 $this->assertArrayHasKey('store', $mapping);
174 * Tests for cache keys that would break on windows.
176 public function test_windows_nasty_keys() {
177 $instance = cache_config_testing::instance();
178 $instance->phpunit_add_definition('phpunit/windowskeytest', array(
179 'mode' => cache_store::MODE_APPLICATION,
180 'component' => 'phpunit',
181 'area' => 'windowskeytest',
182 'simplekeys' => true,
183 'simpledata' => true
185 $cache = cache::make('phpunit', 'windowskeytest');
186 $this->assertTrue($cache->set('contest', 'test data 1'));
187 $this->assertEquals('test data 1', $cache->get('contest'));
191 * Tests set_identifiers fails post cache creation.
193 * set_identifiers cannot be called after initial cache instantiation, as you need to create a difference cache.
195 public function test_set_identifiers() {
196 $instance = cache_config_testing::instance();
197 $instance->phpunit_add_definition('phpunit/identifier', array(
198 'mode' => cache_store::MODE_APPLICATION,
199 'component' => 'phpunit',
200 'area' => 'identifier',
201 'simplekeys' => true,
202 'simpledata' => true,
203 'staticacceleration' => true
205 $cache = cache::make('phpunit', 'identifier', array('area'));
206 $this->assertTrue($cache->set('contest', 'test data 1'));
207 $this->assertEquals('test data 1', $cache->get('contest'));
209 $this->expectException('coding_exception');
210 $cache->set_identifiers(array());
214 * Tests the default application cache
216 public function test_default_application_cache() {
217 $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'applicationtest');
218 $this->assertInstanceOf('cache_application', $cache);
219 $this->run_on_cache($cache);
221 $instance = cache_config_testing::instance(true);
222 $instance->phpunit_add_definition('phpunit/test_default_application_cache', array(
223 'mode' => cache_store::MODE_APPLICATION,
224 'component' => 'phpunit',
225 'area' => 'test_default_application_cache',
226 'staticacceleration' => true,
227 'staticaccelerationsize' => 1
229 $cache = cache::make('phpunit', 'test_default_application_cache');
230 $this->assertInstanceOf('cache_application', $cache);
231 $this->run_on_cache($cache);
235 * Tests the default session cache
237 public function test_default_session_cache() {
238 $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'applicationtest');
239 $this->assertInstanceOf('cache_session', $cache);
240 $this->run_on_cache($cache);
244 * Tests the default request cache
246 public function test_default_request_cache() {
247 $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'applicationtest');
248 $this->assertInstanceOf('cache_request', $cache);
249 $this->run_on_cache($cache);
253 * Tests using a cache system when there are no stores available (who knows what the admin did to achieve this).
255 public function test_on_cache_without_store() {
256 $instance = cache_config_testing::instance(true);
257 $instance->phpunit_add_definition('phpunit/nostoretest1', array(
258 'mode' => cache_store::MODE_APPLICATION,
259 'component' => 'phpunit',
260 'area' => 'nostoretest1',
262 $instance->phpunit_add_definition('phpunit/nostoretest2', array(
263 'mode' => cache_store::MODE_APPLICATION,
264 'component' => 'phpunit',
265 'area' => 'nostoretest2',
266 'staticacceleration' => true
268 $instance->phpunit_remove_stores();
270 $cache = cache::make('phpunit', 'nostoretest1');
271 $this->run_on_cache($cache);
273 $cache = cache::make('phpunit', 'nostoretest2');
274 $this->run_on_cache($cache);
278 * Runs a standard series of access and use tests on a cache instance.
280 * This function is great because we can use it to ensure all of the loaders perform exactly the same way.
282 * @param cache_loader $cache
284 protected function run_on_cache(cache_loader $cache) {
285 $key = 'contestkey';
286 $datascalars = array('test data', null);
287 $dataarray = array('contest' => 'data', 'part' => 'two');
288 $dataobject = (object)$dataarray;
290 foreach ($datascalars as $datascalar) {
291 $this->assertTrue($cache->purge());
293 // Check all read methods.
294 $this->assertFalse($cache->get($key));
295 $this->assertFalse($cache->has($key));
296 $result = $cache->get_many(array($key));
297 $this->assertCount(1, $result);
298 $this->assertFalse(reset($result));
299 $this->assertFalse($cache->has_any(array($key)));
300 $this->assertFalse($cache->has_all(array($key)));
302 // Set the data.
303 $this->assertTrue($cache->set($key, $datascalar));
304 // Setting it more than once should be permitted.
305 $this->assertTrue($cache->set($key, $datascalar));
307 // Recheck the read methods.
308 $this->assertEquals($datascalar, $cache->get($key));
309 $this->assertTrue($cache->has($key));
310 $result = $cache->get_many(array($key));
311 $this->assertCount(1, $result);
312 $this->assertEquals($datascalar, reset($result));
313 $this->assertTrue($cache->has_any(array($key)));
314 $this->assertTrue($cache->has_all(array($key)));
316 // Delete it.
317 $this->assertTrue($cache->delete($key));
319 // Check its gone.
320 $this->assertFalse($cache->get($key));
321 $this->assertFalse($cache->has($key));
324 // Test arrays.
325 $this->assertTrue($cache->set($key, $dataarray));
326 $this->assertEquals($dataarray, $cache->get($key));
328 // Test objects.
329 $this->assertTrue($cache->set($key, $dataobject));
330 $this->assertEquals($dataobject, $cache->get($key));
332 $starttime = microtime(true);
333 $specobject = new cache_phpunit_dummy_object('red', 'blue', $starttime);
334 $this->assertTrue($cache->set($key, $specobject));
335 $result = $cache->get($key);
336 $this->assertInstanceOf('cache_phpunit_dummy_object', $result);
337 $this->assertEquals('red_ptc_wfc', $result->property1);
338 $this->assertEquals('blue_ptc_wfc', $result->property2);
339 $this->assertGreaterThan($starttime, $result->propertytime);
341 // Test array of objects.
342 $specobject = new cache_phpunit_dummy_object('red', 'blue', $starttime);
343 $data = new cacheable_object_array(array(
344 clone($specobject),
345 clone($specobject),
346 clone($specobject))
348 $this->assertTrue($cache->set($key, $data));
349 $result = $cache->get($key);
350 $this->assertInstanceOf('cacheable_object_array', $result);
351 $this->assertCount(3, $data);
352 foreach ($result as $item) {
353 $this->assertInstanceOf('cache_phpunit_dummy_object', $item);
354 $this->assertEquals('red_ptc_wfc', $item->property1);
355 $this->assertEquals('blue_ptc_wfc', $item->property2);
356 // Ensure that wake from cache is called in all cases.
357 $this->assertGreaterThan($starttime, $item->propertytime);
360 // Test set many.
361 $cache->set_many(array('key1' => 'data1', 'key2' => 'data2', 'key3' => null));
362 $this->assertEquals('data1', $cache->get('key1'));
363 $this->assertEquals('data2', $cache->get('key2'));
364 $this->assertEquals(null, $cache->get('key3'));
365 $this->assertTrue($cache->delete('key1'));
366 $this->assertTrue($cache->delete('key2'));
367 $this->assertTrue($cache->delete('key3'));
369 $cache->set_many(array(
370 'key1' => array(1, 2, 3),
371 'key2' => array(3, 2, 1),
373 $this->assertInternalType('array', $cache->get('key1'));
374 $this->assertInternalType('array', $cache->get('key2'));
375 $this->assertCount(3, $cache->get('key1'));
376 $this->assertCount(3, $cache->get('key2'));
377 $this->assertInternalType('array', $cache->get_many(array('key1', 'key2')));
378 $this->assertCount(2, $cache->get_many(array('key1', 'key2')));
379 $this->assertEquals(2, $cache->delete_many(array('key1', 'key2')));
381 // Test delete many.
382 $this->assertTrue($cache->set('key1', 'data1'));
383 $this->assertTrue($cache->set('key2', 'data2'));
384 $this->assertTrue($cache->set('key3', null));
386 $this->assertEquals('data1', $cache->get('key1'));
387 $this->assertEquals('data2', $cache->get('key2'));
388 $this->assertEquals(null, $cache->get('key3'));
390 $this->assertEquals(3, $cache->delete_many(array('key1', 'key2', 'key3')));
392 $this->assertFalse($cache->get('key1'));
393 $this->assertFalse($cache->get('key2'));
394 $this->assertFalse($cache->get('key3'));
396 // Quick reference test.
397 $obj = new stdClass;
398 $obj->key = 'value';
399 $ref =& $obj;
400 $this->assertTrue($cache->set('obj', $obj));
402 $obj->key = 'eulav';
403 $var = $cache->get('obj');
404 $this->assertInstanceOf('stdClass', $var);
405 $this->assertEquals('value', $var->key);
407 $ref->key = 'eulav';
408 $var = $cache->get('obj');
409 $this->assertInstanceOf('stdClass', $var);
410 $this->assertEquals('value', $var->key);
412 $this->assertTrue($cache->delete('obj'));
414 // Deep reference test.
415 $obj1 = new stdClass;
416 $obj1->key = 'value';
417 $obj2 = new stdClass;
418 $obj2->key = 'test';
419 $obj3 = new stdClass;
420 $obj3->key = 'pork';
421 $obj1->subobj =& $obj2;
422 $obj2->subobj =& $obj3;
423 $this->assertTrue($cache->set('obj', $obj1));
425 $obj1->key = 'eulav';
426 $obj2->key = 'tset';
427 $obj3->key = 'krop';
428 $var = $cache->get('obj');
429 $this->assertInstanceOf('stdClass', $var);
430 $this->assertEquals('value', $var->key);
431 $this->assertInstanceOf('stdClass', $var->subobj);
432 $this->assertEquals('test', $var->subobj->key);
433 $this->assertInstanceOf('stdClass', $var->subobj->subobj);
434 $this->assertEquals('pork', $var->subobj->subobj->key);
435 $this->assertTrue($cache->delete('obj'));
437 // Death reference test... basically we don't want this to die.
438 $obj = new stdClass;
439 $obj->key = 'value';
440 $obj->self =& $obj;
441 $this->assertTrue($cache->set('obj', $obj));
442 $var = $cache->get('obj');
443 $this->assertInstanceOf('stdClass', $var);
444 $this->assertEquals('value', $var->key);
446 // Reference test after retrieve.
447 $obj = new stdClass;
448 $obj->key = 'value';
449 $this->assertTrue($cache->set('obj', $obj));
451 $var1 = $cache->get('obj');
452 $this->assertInstanceOf('stdClass', $var1);
453 $this->assertEquals('value', $var1->key);
454 $var1->key = 'eulav';
455 $this->assertEquals('eulav', $var1->key);
457 $var2 = $cache->get('obj');
458 $this->assertInstanceOf('stdClass', $var2);
459 $this->assertEquals('value', $var2->key);
461 $this->assertTrue($cache->delete('obj'));
463 // Death reference test on get_many... basically we don't want this to die.
464 $obj = new stdClass;
465 $obj->key = 'value';
466 $obj->self =& $obj;
467 $this->assertEquals(1, $cache->set_many(array('obj' => $obj)));
468 $var = $cache->get_many(array('obj'));
469 $this->assertInstanceOf('stdClass', $var['obj']);
470 $this->assertEquals('value', $var['obj']->key);
472 // Reference test after retrieve.
473 $obj = new stdClass;
474 $obj->key = 'value';
475 $this->assertEquals(1, $cache->set_many(array('obj' => $obj)));
477 $var1 = $cache->get_many(array('obj'));
478 $this->assertInstanceOf('stdClass', $var1['obj']);
479 $this->assertEquals('value', $var1['obj']->key);
480 $var1['obj']->key = 'eulav';
481 $this->assertEquals('eulav', $var1['obj']->key);
483 $var2 = $cache->get_many(array('obj'));
484 $this->assertInstanceOf('stdClass', $var2['obj']);
485 $this->assertEquals('value', $var2['obj']->key);
487 $this->assertTrue($cache->delete('obj'));
489 // Test strictness exceptions.
490 try {
491 $cache->get('exception', MUST_EXIST);
492 $this->fail('Exception expected from cache::get using MUST_EXIST');
493 } catch (Exception $e) {
494 $this->assertTrue(true);
496 try {
497 $cache->get_many(array('exception1', 'exception2'), MUST_EXIST);
498 $this->fail('Exception expected from cache::get_many using MUST_EXIST');
499 } catch (Exception $e) {
500 $this->assertTrue(true);
502 $cache->set('test', 'test');
503 try {
504 $cache->get_many(array('test', 'exception'), MUST_EXIST);
505 $this->fail('Exception expected from cache::get_many using MUST_EXIST');
506 } catch (Exception $e) {
507 $this->assertTrue(true);
512 * Tests a definition using a data loader
514 public function test_definition_data_loader() {
515 $instance = cache_config_testing::instance(true);
516 $instance->phpunit_add_definition('phpunit/datasourcetest', array(
517 'mode' => cache_store::MODE_APPLICATION,
518 'component' => 'phpunit',
519 'area' => 'datasourcetest',
520 'datasource' => 'cache_phpunit_dummy_datasource',
521 'datasourcefile' => 'cache/tests/fixtures/lib.php'
524 $cache = cache::make('phpunit', 'datasourcetest');
525 $this->assertInstanceOf('cache_application', $cache);
527 // Purge it to be sure.
528 $this->assertTrue($cache->purge());
529 // It won't be there yet.
530 $this->assertFalse($cache->has('Test'));
531 // It should load it ;).
532 $this->assertTrue($cache->has('Test', true));
534 // Purge it to be sure.
535 $this->assertTrue($cache->purge());
536 $this->assertEquals('Test has no value really.', $cache->get('Test'));
538 // Test multiple values.
539 $this->assertTrue($cache->purge());
540 $this->assertTrue($cache->set('b', 'B'));
541 $result = $cache->get_many(array('a', 'b', 'c'));
542 $this->assertInternalType('array', $result);
543 $this->assertCount(3, $result);
544 $this->assertArrayHasKey('a', $result);
545 $this->assertArrayHasKey('b', $result);
546 $this->assertArrayHasKey('c', $result);
547 $this->assertEquals('a has no value really.', $result['a']);
548 $this->assertEquals('B', $result['b']);
549 $this->assertEquals('c has no value really.', $result['c']);
553 * Tests a definition using an overridden loader
555 public function test_definition_overridden_loader() {
556 $instance = cache_config_testing::instance(true);
557 $instance->phpunit_add_definition('phpunit/overridetest', array(
558 'mode' => cache_store::MODE_APPLICATION,
559 'component' => 'phpunit',
560 'area' => 'overridetest',
561 'overrideclass' => 'cache_phpunit_dummy_overrideclass',
562 'overrideclassfile' => 'cache/tests/fixtures/lib.php'
564 $cache = cache::make('phpunit', 'overridetest');
565 $this->assertInstanceOf('cache_phpunit_dummy_overrideclass', $cache);
566 $this->assertInstanceOf('cache_application', $cache);
567 // Purge it to be sure.
568 $this->assertTrue($cache->purge());
569 // It won't be there yet.
570 $this->assertFalse($cache->has('Test'));
571 // Add it.
572 $this->assertTrue($cache->set('Test', 'Test has no value really.'));
573 // Check its there.
574 $this->assertEquals('Test has no value really.', $cache->get('Test'));
578 * Test the mappingsonly setting.
580 public function test_definition_mappings_only() {
581 /** @var cache_config_testing $instance */
582 $instance = cache_config_testing::instance(true);
583 $instance->phpunit_add_definition('phpunit/mappingsonly', array(
584 'mode' => cache_store::MODE_APPLICATION,
585 'component' => 'phpunit',
586 'area' => 'mappingsonly',
587 'mappingsonly' => true
588 ), false);
589 $instance->phpunit_add_definition('phpunit/nonmappingsonly', array(
590 'mode' => cache_store::MODE_APPLICATION,
591 'component' => 'phpunit',
592 'area' => 'nonmappingsonly',
593 'mappingsonly' => false
594 ), false);
596 $cacheonly = cache::make('phpunit', 'mappingsonly');
597 $this->assertInstanceOf('cache_application', $cacheonly);
598 $this->assertEquals('cachestore_dummy', $cacheonly->phpunit_get_store_class());
600 $expected = $this->get_expected_application_cache_store();
601 $cachenon = cache::make('phpunit', 'nonmappingsonly');
602 $this->assertInstanceOf('cache_application', $cachenon);
603 $this->assertEquals($expected, $cachenon->phpunit_get_store_class());
607 * Test a very basic definition.
609 public function test_definition() {
610 $instance = cache_config_testing::instance();
611 $instance->phpunit_add_definition('phpunit/test', array(
612 'mode' => cache_store::MODE_APPLICATION,
613 'component' => 'phpunit',
614 'area' => 'test',
616 $cache = cache::make('phpunit', 'test');
618 $this->assertTrue($cache->set('testkey1', 'test data 1'));
619 $this->assertEquals('test data 1', $cache->get('testkey1'));
620 $this->assertTrue($cache->set('testkey2', 'test data 2'));
621 $this->assertEquals('test data 2', $cache->get('testkey2'));
625 * Test a definition using the simple keys.
627 public function test_definition_simplekeys() {
628 $instance = cache_config_testing::instance();
629 $instance->phpunit_add_definition('phpunit/simplekeytest', array(
630 'mode' => cache_store::MODE_APPLICATION,
631 'component' => 'phpunit',
632 'area' => 'simplekeytest',
633 'simplekeys' => true
635 $cache = cache::make('phpunit', 'simplekeytest');
637 $this->assertTrue($cache->set('testkey1', 'test data 1'));
638 $this->assertEquals('test data 1', $cache->get('testkey1'));
639 $this->assertTrue($cache->set('testkey2', 'test data 2'));
640 $this->assertEquals('test data 2', $cache->get('testkey2'));
642 $cache->purge();
644 $this->assertTrue($cache->set('1', 'test data 1'));
645 $this->assertEquals('test data 1', $cache->get('1'));
646 $this->assertTrue($cache->set('2', 'test data 2'));
647 $this->assertEquals('test data 2', $cache->get('2'));
651 * Test a negative TTL on an application cache.
653 public function test_application_ttl_negative() {
654 $instance = cache_config_testing::instance(true);
655 $instance->phpunit_add_definition('phpunit/ttltest', array(
656 'mode' => cache_store::MODE_APPLICATION,
657 'component' => 'phpunit',
658 'area' => 'ttltest',
659 'ttl' => -86400 // Set to a day in the past to be extra sure.
661 $cache = cache::make('phpunit', 'ttltest');
662 $this->assertInstanceOf('cache_application', $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 not there.
671 $this->assertFalse($cache->has('Test'));
672 // Double check by trying to get it.
673 $this->assertFalse($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->assertFalse($result['a']);
684 $this->assertFalse($result['b']);
685 $this->assertFalse($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->assertFalse($result['a']);
695 $this->assertFalse($result['c']);
696 $this->assertFalse($result['e']);
700 * Test a positive TTL on an application cache.
702 public function test_application_ttl_positive() {
703 $instance = cache_config_testing::instance(true);
704 $instance->phpunit_add_definition('phpunit/ttltest', array(
705 'mode' => cache_store::MODE_APPLICATION,
706 'component' => 'phpunit',
707 'area' => 'ttltest',
708 'ttl' => 86400 // Set to a day in the future to be extra sure.
710 $cache = cache::make('phpunit', 'ttltest');
711 $this->assertInstanceOf('cache_application', $cache);
713 // Purge it to be sure.
714 $this->assertTrue($cache->purge());
715 // It won't be there yet.
716 $this->assertFalse($cache->has('Test'));
717 // Set it now.
718 $this->assertTrue($cache->set('Test', 'Test'));
719 // Check its there.
720 $this->assertTrue($cache->has('Test'));
721 // Double check by trying to get it.
722 $this->assertEquals('Test', $cache->get('Test'));
724 // Test with multiple keys.
725 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
726 $result = $cache->get_many(array('a', 'b', 'c'));
727 $this->assertInternalType('array', $result);
728 $this->assertCount(3, $result);
729 $this->assertArrayHasKey('a', $result);
730 $this->assertArrayHasKey('b', $result);
731 $this->assertArrayHasKey('c', $result);
732 $this->assertEquals('A', $result['a']);
733 $this->assertEquals('B', $result['b']);
734 $this->assertEquals('C', $result['c']);
736 // Test with multiple keys including missing ones.
737 $result = $cache->get_many(array('a', 'c', 'e'));
738 $this->assertInternalType('array', $result);
739 $this->assertCount(3, $result);
740 $this->assertArrayHasKey('a', $result);
741 $this->assertArrayHasKey('c', $result);
742 $this->assertArrayHasKey('e', $result);
743 $this->assertEquals('A', $result['a']);
744 $this->assertEquals('C', $result['c']);
745 $this->assertEquals(false, $result['e']);
749 * Test a negative TTL on an session cache.
751 public function test_session_ttl_positive() {
752 $instance = cache_config_testing::instance(true);
753 $instance->phpunit_add_definition('phpunit/ttltest', array(
754 'mode' => cache_store::MODE_SESSION,
755 'component' => 'phpunit',
756 'area' => 'ttltest',
757 'ttl' => 86400 // Set to a day in the future to be extra sure.
759 $cache = cache::make('phpunit', 'ttltest');
760 $this->assertInstanceOf('cache_session', $cache);
762 // Purge it to be sure.
763 $this->assertTrue($cache->purge());
764 // It won't be there yet.
765 $this->assertFalse($cache->has('Test'));
766 // Set it now.
767 $this->assertTrue($cache->set('Test', 'Test'));
768 // Check its there.
769 $this->assertTrue($cache->has('Test'));
770 // Double check by trying to get it.
771 $this->assertEquals('Test', $cache->get('Test'));
773 // Test with multiple keys.
774 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
775 $result = $cache->get_many(array('a', 'b', 'c'));
776 $this->assertInternalType('array', $result);
777 $this->assertCount(3, $result);
778 $this->assertArrayHasKey('a', $result);
779 $this->assertArrayHasKey('b', $result);
780 $this->assertArrayHasKey('c', $result);
781 $this->assertEquals('A', $result['a']);
782 $this->assertEquals('B', $result['b']);
783 $this->assertEquals('C', $result['c']);
785 // Test with multiple keys including missing ones.
786 $result = $cache->get_many(array('a', 'c', 'e'));
787 $this->assertInternalType('array', $result);
788 $this->assertCount(3, $result);
789 $this->assertArrayHasKey('a', $result);
790 $this->assertArrayHasKey('c', $result);
791 $this->assertArrayHasKey('e', $result);
792 $this->assertEquals('A', $result['a']);
793 $this->assertEquals('C', $result['c']);
794 $this->assertEquals(false, $result['e']);
798 * Tests manual locking operations on an application cache
800 public function test_application_manual_locking() {
801 $instance = cache_config_testing::instance();
802 $instance->phpunit_add_definition('phpunit/lockingtest', array(
803 'mode' => cache_store::MODE_APPLICATION,
804 'component' => 'phpunit',
805 'area' => 'lockingtest'
807 $cache1 = cache::make('phpunit', 'lockingtest');
808 $cache2 = clone($cache1);
810 $this->assertTrue($cache1->set('testkey', 'test data'));
811 $this->assertTrue($cache2->set('testkey', 'test data'));
813 $this->assertTrue($cache1->acquire_lock('testkey'));
814 $this->assertFalse($cache2->acquire_lock('testkey'));
816 $this->assertTrue($cache1->check_lock_state('testkey'));
817 $this->assertFalse($cache2->check_lock_state('testkey'));
819 $this->assertTrue($cache1->release_lock('testkey'));
820 $this->assertFalse($cache2->release_lock('testkey'));
822 $this->assertTrue($cache1->set('testkey', 'test data'));
823 $this->assertTrue($cache2->set('testkey', 'test data'));
827 * Tests application cache event invalidation
829 public function test_application_event_invalidation() {
830 $instance = cache_config_testing::instance();
831 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
832 'mode' => cache_store::MODE_APPLICATION,
833 'component' => 'phpunit',
834 'area' => 'eventinvalidationtest',
835 'invalidationevents' => array(
836 'crazyevent'
839 $cache = cache::make('phpunit', 'eventinvalidationtest');
841 $this->assertTrue($cache->set('testkey1', 'test data 1'));
842 $this->assertEquals('test data 1', $cache->get('testkey1'));
843 $this->assertTrue($cache->set('testkey2', 'test data 2'));
844 $this->assertEquals('test data 2', $cache->get('testkey2'));
846 // Test invalidating a single entry.
847 cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
849 $this->assertFalse($cache->get('testkey1'));
850 $this->assertEquals('test data 2', $cache->get('testkey2'));
852 $this->assertTrue($cache->set('testkey1', 'test data 1'));
854 // Test invalidating both entries.
855 cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
857 $this->assertFalse($cache->get('testkey1'));
858 $this->assertFalse($cache->get('testkey2'));
862 * Tests session cache event invalidation
864 public function test_session_event_invalidation() {
865 $instance = cache_config_testing::instance();
866 $instance->phpunit_add_definition('phpunit/test_session_event_invalidation', array(
867 'mode' => cache_store::MODE_SESSION,
868 'component' => 'phpunit',
869 'area' => 'test_session_event_invalidation',
870 'invalidationevents' => array(
871 'crazyevent'
874 $cache = cache::make('phpunit', 'test_session_event_invalidation');
875 $this->assertInstanceOf('cache_session', $cache);
877 $this->assertTrue($cache->set('testkey1', 'test data 1'));
878 $this->assertEquals('test data 1', $cache->get('testkey1'));
879 $this->assertTrue($cache->set('testkey2', 'test data 2'));
880 $this->assertEquals('test data 2', $cache->get('testkey2'));
882 // Test invalidating a single entry.
883 cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
885 $this->assertFalse($cache->get('testkey1'));
886 $this->assertEquals('test data 2', $cache->get('testkey2'));
888 $this->assertTrue($cache->set('testkey1', 'test data 1'));
890 // Test invalidating both entries.
891 cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
893 $this->assertFalse($cache->get('testkey1'));
894 $this->assertFalse($cache->get('testkey2'));
898 * Tests application cache definition invalidation
900 public function test_application_definition_invalidation() {
901 $instance = cache_config_testing::instance();
902 $instance->phpunit_add_definition('phpunit/definitioninvalidation', array(
903 'mode' => cache_store::MODE_APPLICATION,
904 'component' => 'phpunit',
905 'area' => 'definitioninvalidation'
907 $cache = cache::make('phpunit', 'definitioninvalidation');
908 $this->assertTrue($cache->set('testkey1', 'test data 1'));
909 $this->assertEquals('test data 1', $cache->get('testkey1'));
910 $this->assertTrue($cache->set('testkey2', 'test data 2'));
911 $this->assertEquals('test data 2', $cache->get('testkey2'));
913 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), 'testkey1');
915 $this->assertFalse($cache->get('testkey1'));
916 $this->assertEquals('test data 2', $cache->get('testkey2'));
918 $this->assertTrue($cache->set('testkey1', 'test data 1'));
920 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1'));
922 $this->assertFalse($cache->get('testkey1'));
923 $this->assertEquals('test data 2', $cache->get('testkey2'));
925 $this->assertTrue($cache->set('testkey1', 'test data 1'));
927 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1', 'testkey2'));
929 $this->assertFalse($cache->get('testkey1'));
930 $this->assertFalse($cache->get('testkey2'));
934 * Tests session cache definition invalidation
936 public function test_session_definition_invalidation() {
937 $instance = cache_config_testing::instance();
938 $instance->phpunit_add_definition('phpunit/test_session_definition_invalidation', array(
939 'mode' => cache_store::MODE_SESSION,
940 'component' => 'phpunit',
941 'area' => 'test_session_definition_invalidation'
943 $cache = cache::make('phpunit', 'test_session_definition_invalidation');
944 $this->assertInstanceOf('cache_session', $cache);
945 $this->assertTrue($cache->set('testkey1', 'test data 1'));
946 $this->assertEquals('test data 1', $cache->get('testkey1'));
947 $this->assertTrue($cache->set('testkey2', 'test data 2'));
948 $this->assertEquals('test data 2', $cache->get('testkey2'));
950 cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(), 'testkey1');
952 $this->assertFalse($cache->get('testkey1'));
953 $this->assertEquals('test data 2', $cache->get('testkey2'));
955 $this->assertTrue($cache->set('testkey1', 'test data 1'));
957 cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(),
958 array('testkey1'));
960 $this->assertFalse($cache->get('testkey1'));
961 $this->assertEquals('test data 2', $cache->get('testkey2'));
963 $this->assertTrue($cache->set('testkey1', 'test data 1'));
965 cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(),
966 array('testkey1', 'testkey2'));
968 $this->assertFalse($cache->get('testkey1'));
969 $this->assertFalse($cache->get('testkey2'));
973 * Tests application cache event invalidation over a distributed setup.
975 public function test_distributed_application_event_invalidation() {
976 global $CFG;
977 // This is going to be an intense wee test.
978 // We need to add data the to cache, invalidate it by event, manually force it back without MUC knowing to simulate a
979 // disconnected/distributed setup (think load balanced server using local cache), instantiate the cache again and finally
980 // check that it is not picked up.
981 $instance = cache_config_testing::instance();
982 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
983 'mode' => cache_store::MODE_APPLICATION,
984 'component' => 'phpunit',
985 'area' => 'eventinvalidationtest',
986 'simplekeys' => true,
987 'simpledata' => true,
988 'invalidationevents' => array(
989 'crazyevent'
992 $cache = cache::make('phpunit', 'eventinvalidationtest');
993 $this->assertTrue($cache->set('testkey1', 'test data 1'));
994 $this->assertEquals('test data 1', $cache->get('testkey1'));
996 cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
998 $this->assertFalse($cache->get('testkey1'));
1000 // OK data added, data invalidated, and invalidation time has been set.
1001 // Now we need to manually add back the data and adjust the invalidation time.
1002 $hash = md5(cache_store::MODE_APPLICATION.'/phpunit/eventinvalidationtest/'.$CFG->wwwroot.'phpunit');
1003 $timefile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/las-cache/lastinvalidation-$hash.cache";
1004 // Make sure the file is correct.
1005 $this->assertTrue(file_exists($timefile));
1006 $timecont = serialize(cache::now() - 60); // Back 60sec in the past to force it to re-invalidate.
1007 make_writable_directory(dirname($timefile));
1008 file_put_contents($timefile, $timecont);
1009 $this->assertTrue(file_exists($timefile));
1011 $datafile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/tes-cache/testkey1-$hash.cache";
1012 $datacont = serialize("test data 1");
1013 make_writable_directory(dirname($datafile));
1014 file_put_contents($datafile, $datacont);
1015 $this->assertTrue(file_exists($datafile));
1017 // Test 1: Rebuild without the event and test its there.
1018 cache_factory::reset();
1019 $instance = cache_config_testing::instance();
1020 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
1021 'mode' => cache_store::MODE_APPLICATION,
1022 'component' => 'phpunit',
1023 'area' => 'eventinvalidationtest',
1024 'simplekeys' => true,
1025 'simpledata' => true,
1027 $cache = cache::make('phpunit', 'eventinvalidationtest');
1028 $this->assertEquals('test data 1', $cache->get('testkey1'));
1030 // Test 2: Rebuild and test the invalidation of the event via the invalidation cache.
1031 cache_factory::reset();
1032 $instance = cache_config_testing::instance();
1033 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
1034 'mode' => cache_store::MODE_APPLICATION,
1035 'component' => 'phpunit',
1036 'area' => 'eventinvalidationtest',
1037 'simplekeys' => true,
1038 'simpledata' => true,
1039 'invalidationevents' => array(
1040 'crazyevent'
1043 $cache = cache::make('phpunit', 'eventinvalidationtest');
1044 $this->assertFalse($cache->get('testkey1'));
1046 // Test 3: Verify that an existing lastinvalidation cache file is updated when needed.
1048 // Make a new cache class. This should should invalidate testkey2.
1049 $cache = cache::make('phpunit', 'eventinvalidationtest');
1050 // Timestamp should have updated to cache::now().
1051 $this->assertEquals(cache::now(), $cache->get('lastinvalidation'));
1053 // Set testkey2 data.
1054 $cache->set('testkey2', 'test data 2');
1055 // Backdate the event invalidation time by 30 seconds.
1056 $invalidationcache = cache::make('core', 'eventinvalidation');
1057 $invalidationcache->set('crazyevent', array('testkey2' => cache::now() - 30));
1058 // Lastinvalidation should already be cache::now().
1059 $this->assertEquals(cache::now(), $cache->get('lastinvalidation'));
1060 // Set it to 15 seconds ago so that we know if it changes.
1061 $cache->set('lastinvalidation', cache::now() - 15);
1062 // Make a new cache class. This should not invalidate anything.
1063 cache_factory::instance()->reset_cache_instances();
1064 $cache = cache::make('phpunit', 'eventinvalidationtest');
1065 // Lastinvalidation shouldn't change since it was already newer than invalidation event.
1066 $this->assertEquals(cache::now() - 15, $cache->get('lastinvalidation'));
1068 // Now set the event invalidation to newer than the lastinvalidation time.
1069 $invalidationcache->set('crazyevent', array('testkey2' => cache::now() - 5));
1070 // Make a new cache class. This should should invalidate testkey2.
1071 cache_factory::instance()->reset_cache_instances();
1072 $cache = cache::make('phpunit', 'eventinvalidationtest');
1073 // Lastinvalidation timestamp should have updated to cache::now().
1074 $this->assertEquals(cache::now(), $cache->get('lastinvalidation'));
1076 // Now simulate a purge_by_event 5 seconds ago.
1077 $invalidationcache = cache::make('core', 'eventinvalidation');
1078 $invalidationcache->set('crazyevent', array('purged' => cache::now() - 5));
1079 // Set our lastinvalidation timestamp to 15 seconds ago.
1080 $cache->set('lastinvalidation', cache::now() - 15);
1081 // Make a new cache class. This should invalidate the cache.
1082 cache_factory::instance()->reset_cache_instances();
1083 $cache = cache::make('phpunit', 'eventinvalidationtest');
1084 // Lastinvalidation timestamp should have updated to cache::now().
1085 $this->assertEquals(cache::now(), $cache->get('lastinvalidation'));
1090 * Tests application cache event purge
1092 public function test_application_event_purge() {
1093 $instance = cache_config_testing::instance();
1094 $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
1095 'mode' => cache_store::MODE_APPLICATION,
1096 'component' => 'phpunit',
1097 'area' => 'eventpurgetest',
1098 'invalidationevents' => array(
1099 'crazyevent'
1102 $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', array(
1103 'mode' => cache_store::MODE_APPLICATION,
1104 'component' => 'phpunit',
1105 'area' => 'eventpurgetestaccelerated',
1106 'staticacceleration' => true,
1107 'invalidationevents' => array(
1108 'crazyevent'
1111 $cache = cache::make('phpunit', 'eventpurgetest');
1113 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1114 $this->assertEquals('test data 1', $cache->get('testkey1'));
1115 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1116 $this->assertEquals('test data 2', $cache->get('testkey2'));
1118 // Purge the event.
1119 cache_helper::purge_by_event('crazyevent');
1121 // Check things have been removed.
1122 $this->assertFalse($cache->get('testkey1'));
1123 $this->assertFalse($cache->get('testkey2'));
1125 // Now test the static acceleration array.
1126 $cache = cache::make('phpunit', 'eventpurgetestaccelerated');
1127 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1128 $this->assertEquals('test data 1', $cache->get('testkey1'));
1129 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1130 $this->assertEquals('test data 2', $cache->get('testkey2'));
1132 // Purge the event.
1133 cache_helper::purge_by_event('crazyevent');
1135 // Check things have been removed.
1136 $this->assertFalse($cache->get('testkey1'));
1137 $this->assertFalse($cache->get('testkey2'));
1141 * Tests session cache event purge
1143 public function test_session_event_purge() {
1144 $instance = cache_config_testing::instance();
1145 $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
1146 'mode' => cache_store::MODE_SESSION,
1147 'component' => 'phpunit',
1148 'area' => 'eventpurgetest',
1149 'invalidationevents' => array(
1150 'crazyevent'
1153 $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', array(
1154 'mode' => cache_store::MODE_SESSION,
1155 'component' => 'phpunit',
1156 'area' => 'eventpurgetestaccelerated',
1157 'staticacceleration' => true,
1158 'invalidationevents' => array(
1159 'crazyevent'
1162 $cache = cache::make('phpunit', 'eventpurgetest');
1164 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1165 $this->assertEquals('test data 1', $cache->get('testkey1'));
1166 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1167 $this->assertEquals('test data 2', $cache->get('testkey2'));
1169 // Purge the event.
1170 cache_helper::purge_by_event('crazyevent');
1172 // Check things have been removed.
1173 $this->assertFalse($cache->get('testkey1'));
1174 $this->assertFalse($cache->get('testkey2'));
1176 // Now test the static acceleration array.
1177 $cache = cache::make('phpunit', 'eventpurgetestaccelerated');
1178 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1179 $this->assertEquals('test data 1', $cache->get('testkey1'));
1180 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1181 $this->assertEquals('test data 2', $cache->get('testkey2'));
1183 // Purge the event.
1184 cache_helper::purge_by_event('crazyevent');
1186 // Check things have been removed.
1187 $this->assertFalse($cache->get('testkey1'));
1188 $this->assertFalse($cache->get('testkey2'));
1192 * Tests application cache definition purge
1194 public function test_application_definition_purge() {
1195 $instance = cache_config_testing::instance();
1196 $instance->phpunit_add_definition('phpunit/definitionpurgetest', array(
1197 'mode' => cache_store::MODE_APPLICATION,
1198 'component' => 'phpunit',
1199 'area' => 'definitionpurgetest',
1200 'invalidationevents' => array(
1201 'crazyevent'
1204 $cache = cache::make('phpunit', 'definitionpurgetest');
1206 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1207 $this->assertEquals('test data 1', $cache->get('testkey1'));
1208 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1209 $this->assertEquals('test data 2', $cache->get('testkey2'));
1211 // Purge the event.
1212 cache_helper::purge_by_definition('phpunit', 'definitionpurgetest');
1214 // Check things have been removed.
1215 $this->assertFalse($cache->get('testkey1'));
1216 $this->assertFalse($cache->get('testkey2'));
1220 * Test the use of an alt path.
1221 * If we can generate a config instance we are done :)
1223 public function test_alt_cache_path() {
1224 global $CFG;
1225 if ((defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH) || !empty($CFG->altcacheconfigpath)) {
1226 $this->markTestSkipped('Skipped testing alt cache path as it is already being used.');
1228 $this->resetAfterTest();
1229 $CFG->altcacheconfigpath = $CFG->dataroot.'/cache/altcacheconfigpath';
1230 $instance = cache_config_testing::instance();
1231 $this->assertInstanceOf('cache_config', $instance);
1235 * Test disabling the cache stores.
1237 public function test_disable_stores() {
1238 $instance = cache_config_testing::instance();
1239 $instance->phpunit_add_definition('phpunit/disabletest1', array(
1240 'mode' => cache_store::MODE_APPLICATION,
1241 'component' => 'phpunit',
1242 'area' => 'disabletest1'
1244 $instance->phpunit_add_definition('phpunit/disabletest2', array(
1245 'mode' => cache_store::MODE_SESSION,
1246 'component' => 'phpunit',
1247 'area' => 'disabletest2'
1249 $instance->phpunit_add_definition('phpunit/disabletest3', array(
1250 'mode' => cache_store::MODE_REQUEST,
1251 'component' => 'phpunit',
1252 'area' => 'disabletest3'
1255 $caches = array(
1256 'disabletest1' => cache::make('phpunit', 'disabletest1'),
1257 'disabletest2' => cache::make('phpunit', 'disabletest2'),
1258 'disabletest3' => cache::make('phpunit', 'disabletest3')
1261 $this->assertInstanceOf('cache_phpunit_application', $caches['disabletest1']);
1262 $this->assertInstanceOf('cache_phpunit_session', $caches['disabletest2']);
1263 $this->assertInstanceOf('cache_phpunit_request', $caches['disabletest3']);
1265 $this->assertEquals('cachestore_file', $caches['disabletest1']->phpunit_get_store_class());
1266 $this->assertEquals('cachestore_session', $caches['disabletest2']->phpunit_get_store_class());
1267 $this->assertEquals('cachestore_static', $caches['disabletest3']->phpunit_get_store_class());
1269 foreach ($caches as $cache) {
1270 $this->assertFalse($cache->get('test'));
1271 $this->assertTrue($cache->set('test', 'test'));
1272 $this->assertEquals('test', $cache->get('test'));
1275 cache_factory::disable_stores();
1277 $caches = array(
1278 'disabletest1' => cache::make('phpunit', 'disabletest1'),
1279 'disabletest2' => cache::make('phpunit', 'disabletest2'),
1280 'disabletest3' => cache::make('phpunit', 'disabletest3')
1283 $this->assertInstanceOf('cache_phpunit_application', $caches['disabletest1']);
1284 $this->assertInstanceOf('cache_phpunit_session', $caches['disabletest2']);
1285 $this->assertInstanceOf('cache_phpunit_request', $caches['disabletest3']);
1287 $this->assertEquals('cachestore_dummy', $caches['disabletest1']->phpunit_get_store_class());
1288 $this->assertEquals('cachestore_dummy', $caches['disabletest2']->phpunit_get_store_class());
1289 $this->assertEquals('cachestore_dummy', $caches['disabletest3']->phpunit_get_store_class());
1291 foreach ($caches as $cache) {
1292 $this->assertFalse($cache->get('test'));
1293 $this->assertTrue($cache->set('test', 'test'));
1294 $this->assertEquals('test', $cache->get('test'));
1299 * Test disabling the cache.
1301 public function test_disable() {
1302 global $CFG;
1304 if ((defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH) || !empty($CFG->altcacheconfigpath)) {
1305 // We can't run this test as it requires us to delete the cache configuration script which we just
1306 // cant do with a custom path in play.
1307 $this->markTestSkipped('Skipped testing cache disable functionality as alt cache path is being used.');
1310 $configfile = $CFG->dataroot.'/muc/config.php';
1312 // The config file will not exist yet as we've not done anything with the cache.
1313 // reset_all_data removes the file and without a call to create a configuration it doesn't exist
1314 // as yet.
1315 $this->assertFileNotExists($configfile);
1317 // Disable the cache
1318 cache_phpunit_factory::phpunit_disable();
1320 // Check we get the expected disabled factory.
1321 $factory = cache_factory::instance();
1322 $this->assertInstanceOf('cache_factory_disabled', $factory);
1324 // Check we get the expected disabled config.
1325 $config = $factory->create_config_instance();
1326 $this->assertInstanceOf('cache_config_disabled', $config);
1328 // Check we get the expected disabled caches.
1329 $cache = cache::make('phpunit', 'disable');
1330 $this->assertInstanceOf('cache_disabled', $cache);
1332 // Test an application cache.
1333 $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'disable');
1334 $this->assertInstanceOf('cache_disabled', $cache);
1336 $this->assertFalse(file_exists($configfile));
1338 $this->assertFalse($cache->get('test'));
1339 $this->assertFalse($cache->set('test', 'test'));
1340 $this->assertFalse($cache->delete('test'));
1341 $this->assertTrue($cache->purge());
1343 // Test a session cache.
1344 $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'disable');
1345 $this->assertInstanceOf('cache_disabled', $cache);
1347 $this->assertFalse(file_exists($configfile));
1349 $this->assertFalse($cache->get('test'));
1350 $this->assertFalse($cache->set('test', 'test'));
1351 $this->assertFalse($cache->delete('test'));
1352 $this->assertTrue($cache->purge());
1354 // Finally test a request cache.
1355 $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'disable');
1356 $this->assertInstanceOf('cache_disabled', $cache);
1358 $this->assertFalse(file_exists($configfile));
1360 $this->assertFalse($cache->get('test'));
1361 $this->assertFalse($cache->set('test', 'test'));
1362 $this->assertFalse($cache->delete('test'));
1363 $this->assertTrue($cache->purge());
1365 cache_factory::reset();
1367 $factory = cache_factory::instance(true);
1368 $config = $factory->create_config_instance();
1369 $this->assertEquals('cache_config_testing', get_class($config));
1373 * Test that multiple application loaders work ok.
1375 public function test_multiple_application_loaders() {
1376 $instance = cache_config_testing::instance(true);
1377 $instance->phpunit_add_file_store('phpunittest1');
1378 $instance->phpunit_add_file_store('phpunittest2');
1379 $instance->phpunit_add_definition('phpunit/multi_loader', array(
1380 'mode' => cache_store::MODE_APPLICATION,
1381 'component' => 'phpunit',
1382 'area' => 'multi_loader'
1384 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
1385 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
1387 $cache = cache::make('phpunit', 'multi_loader');
1388 $this->assertInstanceOf('cache_application', $cache);
1389 $this->assertFalse($cache->get('test'));
1390 $this->assertTrue($cache->set('test', 'test'));
1391 $this->assertEquals('test', $cache->get('test'));
1392 $this->assertTrue($cache->delete('test'));
1393 $this->assertFalse($cache->get('test'));
1394 $this->assertTrue($cache->set('test', 'test'));
1395 $this->assertTrue($cache->purge());
1396 $this->assertFalse($cache->get('test'));
1398 // Test the many commands.
1399 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
1400 $result = $cache->get_many(array('a', 'b', 'c'));
1401 $this->assertInternalType('array', $result);
1402 $this->assertCount(3, $result);
1403 $this->assertArrayHasKey('a', $result);
1404 $this->assertArrayHasKey('b', $result);
1405 $this->assertArrayHasKey('c', $result);
1406 $this->assertEquals('A', $result['a']);
1407 $this->assertEquals('B', $result['b']);
1408 $this->assertEquals('C', $result['c']);
1409 $this->assertEquals($result, $cache->get_many(array('a', 'b', 'c')));
1410 $this->assertEquals(2, $cache->delete_many(array('a', 'c')));
1411 $result = $cache->get_many(array('a', 'b', 'c'));
1412 $this->assertInternalType('array', $result);
1413 $this->assertCount(3, $result);
1414 $this->assertArrayHasKey('a', $result);
1415 $this->assertArrayHasKey('b', $result);
1416 $this->assertArrayHasKey('c', $result);
1417 $this->assertFalse($result['a']);
1418 $this->assertEquals('B', $result['b']);
1419 $this->assertFalse($result['c']);
1421 // Test non-recursive deletes.
1422 $this->assertTrue($cache->set('test', 'test'));
1423 $this->assertSame('test', $cache->get('test'));
1424 $this->assertTrue($cache->delete('test', false));
1425 // We should still have it on a deeper loader.
1426 $this->assertSame('test', $cache->get('test'));
1427 // Test non-recusive with many functions.
1428 $this->assertSame(3, $cache->set_many(array(
1429 'one' => 'one',
1430 'two' => 'two',
1431 'three' => 'three'
1432 )));
1433 $this->assertSame('one', $cache->get('one'));
1434 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1435 $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
1436 $this->assertSame('one', $cache->get('one'));
1437 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1441 * Test that multiple application loaders work ok.
1443 public function test_multiple_session_loaders() {
1444 /* @var cache_config_testing $instance */
1445 $instance = cache_config_testing::instance(true);
1446 $instance->phpunit_add_session_store('phpunittest1');
1447 $instance->phpunit_add_session_store('phpunittest2');
1448 $instance->phpunit_add_definition('phpunit/multi_loader', array(
1449 'mode' => cache_store::MODE_SESSION,
1450 'component' => 'phpunit',
1451 'area' => 'multi_loader'
1453 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
1454 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
1456 $cache = cache::make('phpunit', 'multi_loader');
1457 $this->assertInstanceOf('cache_session', $cache);
1458 $this->assertFalse($cache->get('test'));
1459 $this->assertTrue($cache->set('test', 'test'));
1460 $this->assertEquals('test', $cache->get('test'));
1461 $this->assertTrue($cache->delete('test'));
1462 $this->assertFalse($cache->get('test'));
1463 $this->assertTrue($cache->set('test', 'test'));
1464 $this->assertTrue($cache->purge());
1465 $this->assertFalse($cache->get('test'));
1467 // Test the many commands.
1468 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
1469 $result = $cache->get_many(array('a', 'b', 'c'));
1470 $this->assertInternalType('array', $result);
1471 $this->assertCount(3, $result);
1472 $this->assertArrayHasKey('a', $result);
1473 $this->assertArrayHasKey('b', $result);
1474 $this->assertArrayHasKey('c', $result);
1475 $this->assertEquals('A', $result['a']);
1476 $this->assertEquals('B', $result['b']);
1477 $this->assertEquals('C', $result['c']);
1478 $this->assertEquals($result, $cache->get_many(array('a', 'b', 'c')));
1479 $this->assertEquals(2, $cache->delete_many(array('a', 'c')));
1480 $result = $cache->get_many(array('a', 'b', 'c'));
1481 $this->assertInternalType('array', $result);
1482 $this->assertCount(3, $result);
1483 $this->assertArrayHasKey('a', $result);
1484 $this->assertArrayHasKey('b', $result);
1485 $this->assertArrayHasKey('c', $result);
1486 $this->assertFalse($result['a']);
1487 $this->assertEquals('B', $result['b']);
1488 $this->assertFalse($result['c']);
1490 // Test non-recursive deletes.
1491 $this->assertTrue($cache->set('test', 'test'));
1492 $this->assertSame('test', $cache->get('test'));
1493 $this->assertTrue($cache->delete('test', false));
1494 // We should still have it on a deeper loader.
1495 $this->assertSame('test', $cache->get('test'));
1496 // Test non-recusive with many functions.
1497 $this->assertSame(3, $cache->set_many(array(
1498 'one' => 'one',
1499 'two' => 'two',
1500 'three' => 'three'
1501 )));
1502 $this->assertSame('one', $cache->get('one'));
1503 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1504 $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
1505 $this->assertSame('one', $cache->get('one'));
1506 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1510 * Test switching users with session caches.
1512 public function test_session_cache_switch_user() {
1513 $this->resetAfterTest(true);
1514 $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache');
1515 $user1 = $this->getDataGenerator()->create_user();
1516 $user2 = $this->getDataGenerator()->create_user();
1518 // Log in as the first user.
1519 $this->setUser($user1);
1520 $sesskey1 = sesskey();
1522 // Set a basic value in the cache.
1523 $cache->set('var', 1);
1524 $this->assertTrue($cache->has('var'));
1525 $this->assertEquals(1, $cache->get('var'));
1527 // Change to the second user.
1528 $this->setUser($user2);
1529 $sesskey2 = sesskey();
1531 // Make sure the cache doesn't give us the data for the last user.
1532 $this->assertNotEquals($sesskey1, $sesskey2);
1533 $this->assertFalse($cache->has('var'));
1534 $this->assertEquals(false, $cache->get('var'));
1538 * Test switching users with session caches.
1540 public function test_session_cache_switch_user_application_mapping() {
1541 $this->resetAfterTest(true);
1542 $instance = cache_config_testing::instance(true);
1543 $instance->phpunit_add_file_store('testfilestore');
1544 $instance->phpunit_add_definition('phpunit/testappsession', array(
1545 'mode' => cache_store::MODE_SESSION,
1546 'component' => 'phpunit',
1547 'area' => 'testappsession'
1549 $instance->phpunit_add_definition_mapping('phpunit/testappsession', 'testfilestore', 3);
1550 $cache = cache::make('phpunit', 'testappsession');
1551 $user1 = $this->getDataGenerator()->create_user();
1552 $user2 = $this->getDataGenerator()->create_user();
1554 // Log in as the first user.
1555 $this->setUser($user1);
1556 $sesskey1 = sesskey();
1558 // Set a basic value in the cache.
1559 $cache->set('var', 1);
1560 $this->assertTrue($cache->has('var'));
1561 $this->assertEquals(1, $cache->get('var'));
1563 // Change to the second user.
1564 $this->setUser($user2);
1565 $sesskey2 = sesskey();
1567 // Make sure the cache doesn't give us the data for the last user.
1568 $this->assertNotEquals($sesskey1, $sesskey2);
1569 $this->assertFalse($cache->has('var'));
1570 $this->assertEquals(false, $cache->get('var'));
1574 * Test two session caches being used at once to confirm collisions don't occur.
1576 public function test_dual_session_caches() {
1577 $instance = cache_config_testing::instance(true);
1578 $instance->phpunit_add_definition('phpunit/testsess1', array(
1579 'mode' => cache_store::MODE_SESSION,
1580 'component' => 'phpunit',
1581 'area' => 'testsess1'
1583 $instance->phpunit_add_definition('phpunit/testsess2', array(
1584 'mode' => cache_store::MODE_SESSION,
1585 'component' => 'phpunit',
1586 'area' => 'testsess2'
1588 $cache1 = cache::make('phpunit', 'testsess1');
1589 $cache2 = cache::make('phpunit', 'testsess2');
1591 $this->assertFalse($cache1->has('test'));
1592 $this->assertFalse($cache2->has('test'));
1594 $this->assertTrue($cache1->set('test', '1'));
1596 $this->assertTrue($cache1->has('test'));
1597 $this->assertFalse($cache2->has('test'));
1599 $this->assertTrue($cache2->set('test', '2'));
1601 $this->assertEquals(1, $cache1->get('test'));
1602 $this->assertEquals(2, $cache2->get('test'));
1604 $this->assertTrue($cache1->delete('test'));
1608 * Test multiple session caches when switching user.
1610 public function test_session_cache_switch_user_multiple() {
1611 $this->resetAfterTest(true);
1612 $cache1 = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache1');
1613 $cache2 = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache2');
1614 $user1 = $this->getDataGenerator()->create_user();
1615 $user2 = $this->getDataGenerator()->create_user();
1617 // Log in as the first user.
1618 $this->setUser($user1);
1619 $sesskey1 = sesskey();
1621 // Set a basic value in the caches.
1622 $cache1->set('var', 1);
1623 $cache2->set('var', 2);
1624 $this->assertEquals(1, $cache1->get('var'));
1625 $this->assertEquals(2, $cache2->get('var'));
1627 // Change to the second user.
1628 $this->setUser($user2);
1629 $sesskey2 = sesskey();
1631 // Make sure the cache doesn't give us the data for the last user.
1632 // Also make sure that switching the user has lead to both caches being purged.
1633 $this->assertNotEquals($sesskey1, $sesskey2);
1634 $this->assertEquals(false, $cache1->get('var'));
1635 $this->assertEquals(false, $cache2->get('var'));
1639 * Test application locking.
1641 public function test_application_locking() {
1642 $instance = cache_config_testing::instance(true);
1643 $instance->phpunit_add_definition('phpunit/test_application_locking', array(
1644 'mode' => cache_store::MODE_APPLICATION,
1645 'component' => 'phpunit',
1646 'area' => 'test_application_locking',
1647 'staticacceleration' => true,
1648 'staticaccelerationsize' => 1,
1649 'requirelockingread' => true,
1650 'requirelockingwrite' => true
1652 $cache = cache::make('phpunit', 'test_application_locking');
1653 $this->assertInstanceOf('cache_application', $cache);
1655 $this->assertTrue($cache->set('a', 'A'));
1656 $this->assertTrue($cache->set('b', 'B'));
1657 $this->assertTrue($cache->set('c', 'C'));
1658 $this->assertEquals('A', $cache->get('a'));
1659 $this->assertEquals(array('b' => 'B', 'c' => 'C'), $cache->get_many(array('b', 'c')));
1660 $this->assertTrue($cache->delete('a'));
1661 $this->assertFalse($cache->has('a'));
1665 * Test the static cache_helper method purge_stores_used_by_definition.
1667 public function test_purge_stores_used_by_definition() {
1668 $instance = cache_config_testing::instance(true);
1669 $instance->phpunit_add_definition('phpunit/test_purge_stores_used_by_definition', array(
1670 'mode' => cache_store::MODE_APPLICATION,
1671 'component' => 'phpunit',
1672 'area' => 'test_purge_stores_used_by_definition'
1674 $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition');
1675 $this->assertInstanceOf('cache_application', $cache);
1676 $this->assertTrue($cache->set('test', 'test'));
1677 unset($cache);
1679 cache_helper::purge_stores_used_by_definition('phpunit', 'test_purge_stores_used_by_definition');
1681 $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition');
1682 $this->assertInstanceOf('cache_application', $cache);
1683 $this->assertFalse($cache->get('test'));
1687 * Test purge routines.
1689 public function test_purge_routines() {
1690 $instance = cache_config_testing::instance(true);
1691 $instance->phpunit_add_definition('phpunit/purge1', array(
1692 'mode' => cache_store::MODE_APPLICATION,
1693 'component' => 'phpunit',
1694 'area' => 'purge1'
1696 $instance->phpunit_add_definition('phpunit/purge2', array(
1697 'mode' => cache_store::MODE_APPLICATION,
1698 'component' => 'phpunit',
1699 'area' => 'purge2',
1700 'requireidentifiers' => array(
1701 'id'
1705 $factory = cache_factory::instance();
1706 $definition = $factory->create_definition('phpunit', 'purge1');
1707 $this->assertFalse($definition->has_required_identifiers());
1708 $cache = $factory->create_cache($definition);
1709 $this->assertInstanceOf('cache_application', $cache);
1710 $this->assertTrue($cache->set('test', 'test'));
1711 $this->assertTrue($cache->has('test'));
1712 cache_helper::purge_by_definition('phpunit', 'purge1');
1713 $this->assertFalse($cache->has('test'));
1715 $factory = cache_factory::instance();
1716 $definition = $factory->create_definition('phpunit', 'purge2');
1717 $this->assertTrue($definition->has_required_identifiers());
1718 $cache = $factory->create_cache($definition);
1719 $this->assertInstanceOf('cache_application', $cache);
1720 $this->assertTrue($cache->set('test', 'test'));
1721 $this->assertTrue($cache->has('test'));
1722 cache_helper::purge_stores_used_by_definition('phpunit', 'purge2');
1723 $this->assertFalse($cache->has('test'));
1725 try {
1726 cache_helper::purge_by_definition('phpunit', 'purge2');
1727 $this->fail('Should not be able to purge a definition required identifiers without providing them.');
1728 } catch (coding_exception $ex) {
1729 $this->assertContains('Identifier required for cache has not been provided', $ex->getMessage());
1734 * Tests that ad-hoc caches are correctly purged with a purge_all call.
1736 public function test_purge_all_with_adhoc_caches() {
1737 $cache = \cache::make_from_params(\cache_store::MODE_REQUEST, 'core_cache', 'test');
1738 $cache->set('test', 123);
1739 cache_helper::purge_all();
1740 $this->assertFalse($cache->get('test'));
1744 * Test that the default stores all support searching.
1746 public function test_defaults_support_searching() {
1747 $instance = cache_config_testing::instance(true);
1748 $instance->phpunit_add_definition('phpunit/search1', array(
1749 'mode' => cache_store::MODE_APPLICATION,
1750 'component' => 'phpunit',
1751 'area' => 'search1',
1752 'requiresearchable' => true
1754 $instance->phpunit_add_definition('phpunit/search2', array(
1755 'mode' => cache_store::MODE_SESSION,
1756 'component' => 'phpunit',
1757 'area' => 'search2',
1758 'requiresearchable' => true
1760 $instance->phpunit_add_definition('phpunit/search3', array(
1761 'mode' => cache_store::MODE_REQUEST,
1762 'component' => 'phpunit',
1763 'area' => 'search3',
1764 'requiresearchable' => true
1766 $factory = cache_factory::instance();
1768 // Test application cache is searchable.
1769 $definition = $factory->create_definition('phpunit', 'search1');
1770 $this->assertInstanceOf('cache_definition', $definition);
1771 $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1772 $cache = $factory->create_cache($definition);
1773 $this->assertInstanceOf('cache_application', $cache);
1774 $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1776 // Test session cache is searchable.
1777 $definition = $factory->create_definition('phpunit', 'search2');
1778 $this->assertInstanceOf('cache_definition', $definition);
1779 $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1780 $cache = $factory->create_cache($definition);
1781 $this->assertInstanceOf('cache_session', $cache);
1782 $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1784 // Test request cache is searchable.
1785 $definition = $factory->create_definition('phpunit', 'search3');
1786 $this->assertInstanceOf('cache_definition', $definition);
1787 $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1788 $cache = $factory->create_cache($definition);
1789 $this->assertInstanceOf('cache_request', $cache);
1790 $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1794 * Test static acceleration
1796 * Note: All the assertGreaterThanOrEqual() in this test should be assertGreaterThan() be because of some microtime()
1797 * resolution problems under some OSs / PHP versions, we are accepting equal as valid outcome. For more info see MDL-57147.
1799 public function test_static_acceleration() {
1800 $instance = cache_config_testing::instance();
1801 $instance->phpunit_add_definition('phpunit/accelerated', array(
1802 'mode' => cache_store::MODE_APPLICATION,
1803 'component' => 'phpunit',
1804 'area' => 'accelerated',
1805 'staticacceleration' => true,
1806 'staticaccelerationsize' => 3,
1808 $instance->phpunit_add_definition('phpunit/accelerated2', array(
1809 'mode' => cache_store::MODE_APPLICATION,
1810 'component' => 'phpunit',
1811 'area' => 'accelerated2',
1812 'staticacceleration' => true,
1813 'staticaccelerationsize' => 3,
1815 $instance->phpunit_add_definition('phpunit/accelerated3', array(
1816 'mode' => cache_store::MODE_APPLICATION,
1817 'component' => 'phpunit',
1818 'area' => 'accelerated3',
1819 'staticacceleration' => true,
1820 'staticaccelerationsize' => 3,
1822 $instance->phpunit_add_definition('phpunit/accelerated4', array(
1823 'mode' => cache_store::MODE_APPLICATION,
1824 'component' => 'phpunit',
1825 'area' => 'accelerated4',
1826 'staticacceleration' => true,
1827 'staticaccelerationsize' => 4,
1829 $instance->phpunit_add_definition('phpunit/simpledataarea1', array(
1830 'mode' => cache_store::MODE_APPLICATION,
1831 'component' => 'phpunit',
1832 'area' => 'simpledataarea1',
1833 'staticacceleration' => true,
1834 'simpledata' => false
1836 $instance->phpunit_add_definition('phpunit/simpledataarea2', array(
1837 'mode' => cache_store::MODE_APPLICATION,
1838 'component' => 'phpunit',
1839 'area' => 'simpledataarea2',
1840 'staticacceleration' => true,
1841 'simpledata' => true
1844 $cache = cache::make('phpunit', 'accelerated');
1845 $this->assertInstanceOf('cache_phpunit_application', $cache);
1847 // Set and get three elements.
1848 $this->assertTrue($cache->set('a', 'A'));
1849 $this->assertTrue($cache->set('b', 'B'));
1850 $this->assertTrue($cache->set('c', 'C'));
1851 $this->assertEquals('A', $cache->get('a'));
1852 $this->assertEquals(array('b' => 'B', 'c' => 'C'), $cache->get_many(array('b', 'c')));
1854 // Make sure all items are in static acceleration array.
1855 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1856 $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b'));
1857 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1859 // Add new value and make sure it is in cache and it is in array.
1860 $this->assertTrue($cache->set('d', 'D'));
1861 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1862 $this->assertEquals('D', $cache->get('d'));
1864 // Now the least recent accessed item (a) is no longer in acceleration array.
1865 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1866 $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b'));
1867 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1869 // Adding and deleting element.
1870 $this->assertTrue($cache->set('a', 'A'));
1871 $this->assertTrue($cache->delete('a'));
1872 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1873 $this->assertFalse($cache->has('a'));
1875 // Make sure "purge" deletes from the array as well.
1876 $cache->purge();
1877 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1878 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1879 $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1880 $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1881 $this->assertFalse($cache->phpunit_static_acceleration_get('e'));
1883 // Check that the array holds the last accessed items by get/set.
1884 $this->assertTrue($cache->set('a', 'A'));
1885 $this->assertTrue($cache->set('b', 'B'));
1886 $this->assertTrue($cache->set('c', 'C'));
1887 $this->assertTrue($cache->set('d', 'D'));
1888 $this->assertTrue($cache->set('e', 'E'));
1889 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1890 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1891 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1892 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1893 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1895 // Store a cacheable_object, get many times and ensure each time wake_for_cache is used.
1896 // Both get and get_many are tested. Two cache entries are used to ensure the times aren't
1897 // confused with multiple calls to get()/get_many().
1898 $startmicrotime = microtime(true);
1899 $cacheableobject = new cache_phpunit_dummy_object(1, 1, $startmicrotime);
1900 $cacheableobject2 = new cache_phpunit_dummy_object(2, 2, $startmicrotime);
1901 $this->assertTrue($cache->set('a', $cacheableobject));
1902 $this->assertTrue($cache->set('b', $cacheableobject2));
1903 $staticaccelerationreturntime = $cache->phpunit_static_acceleration_get('a')->propertytime;
1904 $staticaccelerationreturntimeb = $cache->phpunit_static_acceleration_get('b')->propertytime;
1905 $this->assertGreaterThanOrEqual($startmicrotime, $staticaccelerationreturntime, 'Restore time of static must be newer.');
1907 // Reset the static cache without resetting backing store.
1908 $cache->phpunit_static_acceleration_purge();
1910 // Get the value from the backend store, populating the static cache.
1911 $cachevalue = $cache->get('a');
1912 $this->assertInstanceOf('cache_phpunit_dummy_object', $cachevalue);
1913 $this->assertGreaterThanOrEqual($staticaccelerationreturntime, $cachevalue->propertytime);
1914 $backingstorereturntime = $cachevalue->propertytime;
1916 $results = $cache->get_many(array('b'));
1917 $this->assertInstanceOf('cache_phpunit_dummy_object', $results['b']);
1918 $this->assertGreaterThanOrEqual($staticaccelerationreturntimeb, $results['b']->propertytime);
1919 $backingstorereturntimeb = $results['b']->propertytime;
1921 // Obtain the value again and confirm that static cache is using wake_from_cache.
1922 // Upon failure, the times are not adjusted as wake_from_cache is skipped as the
1923 // value is stored serialized in the static acceleration cache.
1924 $cachevalue = $cache->phpunit_static_acceleration_get('a');
1925 $this->assertInstanceOf('cache_phpunit_dummy_object', $cachevalue);
1926 $this->assertGreaterThanOrEqual($backingstorereturntime, $cachevalue->propertytime);
1928 $results = $cache->get_many(array('b'));
1929 $this->assertInstanceOf('cache_phpunit_dummy_object', $results['b']);
1930 $this->assertGreaterThanOrEqual($backingstorereturntimeb, $results['b']->propertytime);
1932 /** @var cache_phpunit_application $cache */
1933 $cache = cache::make('phpunit', 'accelerated2');
1934 $this->assertInstanceOf('cache_phpunit_application', $cache);
1936 // Check that the array holds the last accessed items by get/set.
1937 $this->assertTrue($cache->set('a', 'A'));
1938 $this->assertTrue($cache->set('b', 'B'));
1939 $this->assertTrue($cache->set('c', 'C'));
1940 $this->assertTrue($cache->set('d', 'D'));
1941 $this->assertTrue($cache->set('e', 'E'));
1942 // Current keys in the array: c, d, e.
1943 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1944 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1945 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1946 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1947 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1949 $this->assertEquals('A', $cache->get('a'));
1950 // Current keys in the array: d, e, a.
1951 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1952 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1953 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1954 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1955 $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1957 // Current keys in the array: d, e, a.
1958 $this->assertEquals(array('c' => 'C'), $cache->get_many(array('c')));
1959 // Current keys in the array: e, a, c.
1960 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1961 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1962 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1963 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1964 $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1967 $cache = cache::make('phpunit', 'accelerated3');
1968 $this->assertInstanceOf('cache_phpunit_application', $cache);
1970 // Check that the array holds the last accessed items by get/set.
1971 $this->assertTrue($cache->set('a', 'A'));
1972 $this->assertTrue($cache->set('b', 'B'));
1973 $this->assertTrue($cache->set('c', 'C'));
1974 $this->assertTrue($cache->set('d', 'D'));
1975 $this->assertTrue($cache->set('e', 'E'));
1976 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1977 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1978 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1979 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1980 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1982 $this->assertTrue($cache->set('b', 'B2'));
1983 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1984 $this->assertEquals('B2', $cache->phpunit_static_acceleration_get('b'));
1985 $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1986 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1987 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1989 $this->assertEquals(2, $cache->set_many(array('b' => 'B3', 'c' => 'C3')));
1990 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1991 $this->assertEquals('B3', $cache->phpunit_static_acceleration_get('b'));
1992 $this->assertEquals('C3', $cache->phpunit_static_acceleration_get('c'));
1993 $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1994 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1996 $cache = cache::make('phpunit', 'accelerated4');
1997 $this->assertInstanceOf('cache_phpunit_application', $cache);
1998 $this->assertTrue($cache->set('a', 'A'));
1999 $this->assertTrue($cache->set('a', 'A'));
2000 $this->assertTrue($cache->set('a', 'A'));
2001 $this->assertTrue($cache->set('a', 'A'));
2002 $this->assertTrue($cache->set('a', 'A'));
2003 $this->assertTrue($cache->set('a', 'A'));
2004 $this->assertTrue($cache->set('a', 'A'));
2005 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
2006 $this->assertEquals('A', $cache->get('a'));
2008 // Setting simpledata to false objects are cloned when retrieving data.
2009 $cache = cache::make('phpunit', 'simpledataarea1');
2010 $notreallysimple = new stdClass();
2011 $notreallysimple->name = 'a';
2012 $cache->set('a', $notreallysimple);
2013 $returnedinstance1 = $cache->get('a');
2014 $returnedinstance2 = $cache->get('a');
2015 $returnedinstance1->name = 'b';
2016 $this->assertEquals('a', $returnedinstance2->name);
2018 // Setting simpledata to true we assume that data does not contain references.
2019 $cache = cache::make('phpunit', 'simpledataarea2');
2020 $notreallysimple = new stdClass();
2021 $notreallysimple->name = 'a';
2022 $cache->set('a', $notreallysimple);
2023 $returnedinstance1 = $cache->get('a');
2024 $returnedinstance2 = $cache->get('a');
2025 $returnedinstance1->name = 'b';
2026 $this->assertEquals('b', $returnedinstance2->name);
2029 public function test_identifiers_have_separate_caches() {
2030 $cachepg = cache::make('core', 'databasemeta', array('dbfamily' => 'pgsql'));
2031 $cachepg->set(1, 'here');
2032 $cachemy = cache::make('core', 'databasemeta', array('dbfamily' => 'mysql'));
2033 $cachemy->set(2, 'there');
2034 $this->assertEquals('here', $cachepg->get(1));
2035 $this->assertEquals('there', $cachemy->get(2));
2036 $this->assertFalse($cachemy->get(1));
2039 public function test_performance_debug() {
2040 global $CFG;
2041 $this->resetAfterTest(true);
2042 $CFG->perfdebug = 15;
2044 $instance = cache_config_testing::instance();
2045 $applicationid = 'phpunit/applicationperf';
2046 $instance->phpunit_add_definition($applicationid, array(
2047 'mode' => cache_store::MODE_APPLICATION,
2048 'component' => 'phpunit',
2049 'area' => 'applicationperf'
2051 $sessionid = 'phpunit/sessionperf';
2052 $instance->phpunit_add_definition($sessionid, array(
2053 'mode' => cache_store::MODE_SESSION,
2054 'component' => 'phpunit',
2055 'area' => 'sessionperf'
2057 $requestid = 'phpunit/requestperf';
2058 $instance->phpunit_add_definition($requestid, array(
2059 'mode' => cache_store::MODE_REQUEST,
2060 'component' => 'phpunit',
2061 'area' => 'requestperf'
2064 $application = cache::make('phpunit', 'applicationperf');
2065 $session = cache::make('phpunit', 'sessionperf');
2066 $request = cache::make('phpunit', 'requestperf');
2068 // Check that no stats are recorded for these definitions yet.
2069 $stats = cache_helper::get_stats();
2070 $this->assertArrayNotHasKey($applicationid, $stats);
2071 $this->assertArrayHasKey($sessionid, $stats); // Session cache sets a key on construct.
2072 $this->assertArrayNotHasKey($requestid, $stats);
2074 // Check that stores register misses.
2075 $this->assertFalse($application->get('missMe'));
2076 $this->assertFalse($application->get('missMe'));
2077 $this->assertFalse($session->get('missMe'));
2078 $this->assertFalse($session->get('missMe'));
2079 $this->assertFalse($session->get('missMe'));
2080 $this->assertFalse($request->get('missMe'));
2081 $this->assertFalse($request->get('missMe'));
2082 $this->assertFalse($request->get('missMe'));
2083 $this->assertFalse($request->get('missMe'));
2085 $endstats = cache_helper::get_stats();
2086 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['misses']);
2087 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['hits']);
2088 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets']);
2089 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['misses']);
2090 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['hits']);
2091 $this->assertEquals(1, $endstats[$sessionid]['stores']['cachestore_session']['sets']);
2092 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['misses']);
2093 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['hits']);
2094 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets']);
2096 $startstats = cache_helper::get_stats();
2098 // Check that stores register sets.
2099 $this->assertTrue($application->set('setMe1', 1));
2100 $this->assertTrue($application->set('setMe2', 2));
2101 $this->assertTrue($session->set('setMe1', 1));
2102 $this->assertTrue($session->set('setMe2', 2));
2103 $this->assertTrue($session->set('setMe3', 3));
2104 $this->assertTrue($request->set('setMe1', 1));
2105 $this->assertTrue($request->set('setMe2', 2));
2106 $this->assertTrue($request->set('setMe3', 3));
2107 $this->assertTrue($request->set('setMe4', 4));
2109 $endstats = cache_helper::get_stats();
2110 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] -
2111 $startstats[$applicationid]['stores']['cachestore_file']['misses']);
2112 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['hits'] -
2113 $startstats[$applicationid]['stores']['cachestore_file']['hits']);
2114 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['sets'] -
2115 $startstats[$applicationid]['stores']['cachestore_file']['sets']);
2116 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] -
2117 $startstats[$sessionid]['stores']['cachestore_session']['misses']);
2118 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['hits'] -
2119 $startstats[$sessionid]['stores']['cachestore_session']['hits']);
2120 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['sets'] -
2121 $startstats[$sessionid]['stores']['cachestore_session']['sets']);
2122 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] -
2123 $startstats[$requestid]['stores']['cachestore_static']['misses']);
2124 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['hits'] -
2125 $startstats[$requestid]['stores']['cachestore_static']['hits']);
2126 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['sets'] -
2127 $startstats[$requestid]['stores']['cachestore_static']['sets']);
2129 $startstats = cache_helper::get_stats();
2131 // Check that stores register hits.
2132 $this->assertEquals($application->get('setMe1'), 1);
2133 $this->assertEquals($application->get('setMe2'), 2);
2134 $this->assertEquals($session->get('setMe1'), 1);
2135 $this->assertEquals($session->get('setMe2'), 2);
2136 $this->assertEquals($session->get('setMe3'), 3);
2137 $this->assertEquals($request->get('setMe1'), 1);
2138 $this->assertEquals($request->get('setMe2'), 2);
2139 $this->assertEquals($request->get('setMe3'), 3);
2140 $this->assertEquals($request->get('setMe4'), 4);
2142 $endstats = cache_helper::get_stats();
2143 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] -
2144 $startstats[$applicationid]['stores']['cachestore_file']['misses']);
2145 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['hits'] -
2146 $startstats[$applicationid]['stores']['cachestore_file']['hits']);
2147 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets'] -
2148 $startstats[$applicationid]['stores']['cachestore_file']['sets']);
2149 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] -
2150 $startstats[$sessionid]['stores']['cachestore_session']['misses']);
2151 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['hits'] -
2152 $startstats[$sessionid]['stores']['cachestore_session']['hits']);
2153 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['sets'] -
2154 $startstats[$sessionid]['stores']['cachestore_session']['sets']);
2155 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] -
2156 $startstats[$requestid]['stores']['cachestore_static']['misses']);
2157 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['hits'] -
2158 $startstats[$requestid]['stores']['cachestore_static']['hits']);
2159 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets'] -
2160 $startstats[$requestid]['stores']['cachestore_static']['sets']);
2162 $startstats = cache_helper::get_stats();
2164 // Check that stores register through get_many.
2165 $application->get_many(array('setMe1', 'setMe2'));
2166 $session->get_many(array('setMe1', 'setMe2', 'setMe3'));
2167 $request->get_many(array('setMe1', 'setMe2', 'setMe3', 'setMe4'));
2169 $endstats = cache_helper::get_stats();
2170 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] -
2171 $startstats[$applicationid]['stores']['cachestore_file']['misses']);
2172 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['hits'] -
2173 $startstats[$applicationid]['stores']['cachestore_file']['hits']);
2174 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets'] -
2175 $startstats[$applicationid]['stores']['cachestore_file']['sets']);
2176 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] -
2177 $startstats[$sessionid]['stores']['cachestore_session']['misses']);
2178 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['hits'] -
2179 $startstats[$sessionid]['stores']['cachestore_session']['hits']);
2180 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['sets'] -
2181 $startstats[$sessionid]['stores']['cachestore_session']['sets']);
2182 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] -
2183 $startstats[$requestid]['stores']['cachestore_static']['misses']);
2184 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['hits'] -
2185 $startstats[$requestid]['stores']['cachestore_static']['hits']);
2186 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets'] -
2187 $startstats[$requestid]['stores']['cachestore_static']['sets']);
2190 public function test_static_cache() {
2191 global $CFG;
2192 $this->resetAfterTest(true);
2193 $CFG->perfdebug = 15;
2195 // Create cache store with static acceleration.
2196 $instance = cache_config_testing::instance();
2197 $applicationid = 'phpunit/applicationperf';
2198 $instance->phpunit_add_definition($applicationid, array(
2199 'mode' => cache_store::MODE_APPLICATION,
2200 'component' => 'phpunit',
2201 'area' => 'applicationperf',
2202 'simplekeys' => true,
2203 'staticacceleration' => true,
2204 'staticaccelerationsize' => 3
2207 $application = cache::make('phpunit', 'applicationperf');
2209 // Check that stores register sets.
2210 $this->assertTrue($application->set('setMe1', 1));
2211 $this->assertTrue($application->set('setMe2', 0));
2212 $this->assertTrue($application->set('setMe3', array()));
2213 $this->assertTrue($application->get('setMe1') !== false);
2214 $this->assertTrue($application->get('setMe2') !== false);
2215 $this->assertTrue($application->get('setMe3') !== false);
2217 // Check that the static acceleration worked, even on empty arrays and the number 0.
2218 $endstats = cache_helper::get_stats();
2219 $this->assertEquals(0, $endstats[$applicationid]['stores']['** static acceleration **']['misses']);
2220 $this->assertEquals(3, $endstats[$applicationid]['stores']['** static acceleration **']['hits']);
2223 public function test_performance_debug_off() {
2224 global $CFG;
2225 $this->resetAfterTest(true);
2226 $CFG->perfdebug = 7;
2228 $instance = cache_config_testing::instance();
2229 $applicationid = 'phpunit/applicationperfoff';
2230 $instance->phpunit_add_definition($applicationid, array(
2231 'mode' => cache_store::MODE_APPLICATION,
2232 'component' => 'phpunit',
2233 'area' => 'applicationperfoff'
2235 $sessionid = 'phpunit/sessionperfoff';
2236 $instance->phpunit_add_definition($sessionid, array(
2237 'mode' => cache_store::MODE_SESSION,
2238 'component' => 'phpunit',
2239 'area' => 'sessionperfoff'
2241 $requestid = 'phpunit/requestperfoff';
2242 $instance->phpunit_add_definition($requestid, array(
2243 'mode' => cache_store::MODE_REQUEST,
2244 'component' => 'phpunit',
2245 'area' => 'requestperfoff'
2248 $application = cache::make('phpunit', 'applicationperfoff');
2249 $session = cache::make('phpunit', 'sessionperfoff');
2250 $request = cache::make('phpunit', 'requestperfoff');
2252 // Check that no stats are recorded for these definitions yet.
2253 $stats = cache_helper::get_stats();
2254 $this->assertArrayNotHasKey($applicationid, $stats);
2255 $this->assertArrayNotHasKey($sessionid, $stats);
2256 $this->assertArrayNotHasKey($requestid, $stats);
2258 // Trigger cache misses, cache sets and cache hits.
2259 $this->assertFalse($application->get('missMe'));
2260 $this->assertTrue($application->set('setMe', 1));
2261 $this->assertEquals(1, $application->get('setMe'));
2262 $this->assertFalse($session->get('missMe'));
2263 $this->assertTrue($session->set('setMe', 3));
2264 $this->assertEquals(3, $session->get('setMe'));
2265 $this->assertFalse($request->get('missMe'));
2266 $this->assertTrue($request->set('setMe', 4));
2267 $this->assertEquals(4, $request->get('setMe'));
2269 // Check that no stats are being recorded for these definitions.
2270 $endstats = cache_helper::get_stats();
2271 $this->assertArrayNotHasKey($applicationid, $endstats);
2272 $this->assertArrayNotHasKey($sessionid, $endstats);
2273 $this->assertArrayNotHasKey($requestid, $endstats);