MDL-50949 cache: Improve cache performance tests
[moodle.git] / cache / tests / cache_test.php
blob0d6daa71cd8c2f03eb3a40a5c35e0fc4e400cef8
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 the default application cache
193 public function test_default_application_cache() {
194 $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'applicationtest');
195 $this->assertInstanceOf('cache_application', $cache);
196 $this->run_on_cache($cache);
198 $instance = cache_config_testing::instance(true);
199 $instance->phpunit_add_definition('phpunit/test_default_application_cache', array(
200 'mode' => cache_store::MODE_APPLICATION,
201 'component' => 'phpunit',
202 'area' => 'test_default_application_cache',
203 'staticacceleration' => true,
204 'staticaccelerationsize' => 1
206 $cache = cache::make('phpunit', 'test_default_application_cache');
207 $this->assertInstanceOf('cache_application', $cache);
208 $this->run_on_cache($cache);
212 * Tests the default session cache
214 public function test_default_session_cache() {
215 $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'applicationtest');
216 $this->assertInstanceOf('cache_session', $cache);
217 $this->run_on_cache($cache);
221 * Tests the default request cache
223 public function test_default_request_cache() {
224 $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'applicationtest');
225 $this->assertInstanceOf('cache_request', $cache);
226 $this->run_on_cache($cache);
230 * Tests using a cache system when there are no stores available (who knows what the admin did to achieve this).
232 public function test_on_cache_without_store() {
233 $instance = cache_config_testing::instance(true);
234 $instance->phpunit_add_definition('phpunit/nostoretest1', array(
235 'mode' => cache_store::MODE_APPLICATION,
236 'component' => 'phpunit',
237 'area' => 'nostoretest1',
239 $instance->phpunit_add_definition('phpunit/nostoretest2', array(
240 'mode' => cache_store::MODE_APPLICATION,
241 'component' => 'phpunit',
242 'area' => 'nostoretest2',
243 'staticacceleration' => true
245 $instance->phpunit_remove_stores();
247 $cache = cache::make('phpunit', 'nostoretest1');
248 $this->run_on_cache($cache);
250 $cache = cache::make('phpunit', 'nostoretest2');
251 $this->run_on_cache($cache);
255 * Runs a standard series of access and use tests on a cache instance.
257 * This function is great because we can use it to ensure all of the loaders perform exactly the same way.
259 * @param cache_loader $cache
261 protected function run_on_cache(cache_loader $cache) {
262 $key = 'contestkey';
263 $datascalars = array('test data', null);
264 $dataarray = array('contest' => 'data', 'part' => 'two');
265 $dataobject = (object)$dataarray;
267 foreach ($datascalars as $datascalar) {
268 $this->assertTrue($cache->purge());
270 // Check all read methods.
271 $this->assertFalse($cache->get($key));
272 $this->assertFalse($cache->has($key));
273 $result = $cache->get_many(array($key));
274 $this->assertCount(1, $result);
275 $this->assertFalse(reset($result));
276 $this->assertFalse($cache->has_any(array($key)));
277 $this->assertFalse($cache->has_all(array($key)));
279 // Set the data.
280 $this->assertTrue($cache->set($key, $datascalar));
281 // Setting it more than once should be permitted.
282 $this->assertTrue($cache->set($key, $datascalar));
284 // Recheck the read methods.
285 $this->assertEquals($datascalar, $cache->get($key));
286 $this->assertTrue($cache->has($key));
287 $result = $cache->get_many(array($key));
288 $this->assertCount(1, $result);
289 $this->assertEquals($datascalar, reset($result));
290 $this->assertTrue($cache->has_any(array($key)));
291 $this->assertTrue($cache->has_all(array($key)));
293 // Delete it.
294 $this->assertTrue($cache->delete($key));
296 // Check its gone.
297 $this->assertFalse($cache->get($key));
298 $this->assertFalse($cache->has($key));
301 // Test arrays.
302 $this->assertTrue($cache->set($key, $dataarray));
303 $this->assertEquals($dataarray, $cache->get($key));
305 // Test objects.
306 $this->assertTrue($cache->set($key, $dataobject));
307 $this->assertEquals($dataobject, $cache->get($key));
309 $specobject = new cache_phpunit_dummy_object('red', 'blue');
310 $this->assertTrue($cache->set($key, $specobject));
311 $result = $cache->get($key);
312 $this->assertInstanceOf('cache_phpunit_dummy_object', $result);
313 $this->assertEquals('red_ptc_wfc', $result->property1);
314 $this->assertEquals('blue_ptc_wfc', $result->property2);
316 // Test array of objects.
317 $specobject = new cache_phpunit_dummy_object('red', 'blue');
318 $data = new cacheable_object_array(array(
319 clone($specobject),
320 clone($specobject),
321 clone($specobject))
323 $this->assertTrue($cache->set($key, $data));
324 $result = $cache->get($key);
325 $this->assertInstanceOf('cacheable_object_array', $result);
326 $this->assertCount(3, $data);
327 foreach ($result as $item) {
328 $this->assertInstanceOf('cache_phpunit_dummy_object', $item);
329 $this->assertEquals('red_ptc_wfc', $item->property1);
330 $this->assertEquals('blue_ptc_wfc', $item->property2);
333 // Test set many.
334 $cache->set_many(array('key1' => 'data1', 'key2' => 'data2', 'key3' => null));
335 $this->assertEquals('data1', $cache->get('key1'));
336 $this->assertEquals('data2', $cache->get('key2'));
337 $this->assertEquals(null, $cache->get('key3'));
338 $this->assertTrue($cache->delete('key1'));
339 $this->assertTrue($cache->delete('key2'));
340 $this->assertTrue($cache->delete('key3'));
342 $cache->set_many(array(
343 'key1' => array(1, 2, 3),
344 'key2' => array(3, 2, 1),
346 $this->assertInternalType('array', $cache->get('key1'));
347 $this->assertInternalType('array', $cache->get('key2'));
348 $this->assertCount(3, $cache->get('key1'));
349 $this->assertCount(3, $cache->get('key2'));
350 $this->assertInternalType('array', $cache->get_many(array('key1', 'key2')));
351 $this->assertCount(2, $cache->get_many(array('key1', 'key2')));
352 $this->assertEquals(2, $cache->delete_many(array('key1', 'key2')));
354 // Test delete many.
355 $this->assertTrue($cache->set('key1', 'data1'));
356 $this->assertTrue($cache->set('key2', 'data2'));
357 $this->assertTrue($cache->set('key3', null));
359 $this->assertEquals('data1', $cache->get('key1'));
360 $this->assertEquals('data2', $cache->get('key2'));
361 $this->assertEquals(null, $cache->get('key3'));
363 $this->assertEquals(3, $cache->delete_many(array('key1', 'key2', 'key3')));
365 $this->assertFalse($cache->get('key1'));
366 $this->assertFalse($cache->get('key2'));
367 $this->assertFalse($cache->get('key3'));
369 // Quick reference test.
370 $obj = new stdClass;
371 $obj->key = 'value';
372 $ref =& $obj;
373 $this->assertTrue($cache->set('obj', $obj));
375 $obj->key = 'eulav';
376 $var = $cache->get('obj');
377 $this->assertInstanceOf('stdClass', $var);
378 $this->assertEquals('value', $var->key);
380 $ref->key = 'eulav';
381 $var = $cache->get('obj');
382 $this->assertInstanceOf('stdClass', $var);
383 $this->assertEquals('value', $var->key);
385 $this->assertTrue($cache->delete('obj'));
387 // Deep reference test.
388 $obj1 = new stdClass;
389 $obj1->key = 'value';
390 $obj2 = new stdClass;
391 $obj2->key = 'test';
392 $obj3 = new stdClass;
393 $obj3->key = 'pork';
394 $obj1->subobj =& $obj2;
395 $obj2->subobj =& $obj3;
396 $this->assertTrue($cache->set('obj', $obj1));
398 $obj1->key = 'eulav';
399 $obj2->key = 'tset';
400 $obj3->key = 'krop';
401 $var = $cache->get('obj');
402 $this->assertInstanceOf('stdClass', $var);
403 $this->assertEquals('value', $var->key);
404 $this->assertInstanceOf('stdClass', $var->subobj);
405 $this->assertEquals('test', $var->subobj->key);
406 $this->assertInstanceOf('stdClass', $var->subobj->subobj);
407 $this->assertEquals('pork', $var->subobj->subobj->key);
408 $this->assertTrue($cache->delete('obj'));
410 // Death reference test... basicaly we don't want this to die.
411 $obj = new stdClass;
412 $obj->key = 'value';
413 $obj->self =& $obj;
414 $this->assertTrue($cache->set('obj', $obj));
415 $var = $cache->get('obj');
416 $this->assertInstanceOf('stdClass', $var);
417 $this->assertEquals('value', $var->key);
419 // Reference test after retrieve.
420 $obj = new stdClass;
421 $obj->key = 'value';
422 $this->assertTrue($cache->set('obj', $obj));
424 $var1 = $cache->get('obj');
425 $this->assertInstanceOf('stdClass', $var1);
426 $this->assertEquals('value', $var1->key);
427 $var1->key = 'eulav';
428 $this->assertEquals('eulav', $var1->key);
430 $var2 = $cache->get('obj');
431 $this->assertInstanceOf('stdClass', $var2);
432 $this->assertEquals('value', $var2->key);
434 $this->assertTrue($cache->delete('obj'));
436 // Test strictness exceptions.
437 try {
438 $cache->get('exception', MUST_EXIST);
439 $this->fail('Exception expected from cache::get using MUST_EXIST');
440 } catch (Exception $e) {
441 $this->assertTrue(true);
443 try {
444 $cache->get_many(array('exception1', 'exception2'), MUST_EXIST);
445 $this->fail('Exception expected from cache::get_many using MUST_EXIST');
446 } catch (Exception $e) {
447 $this->assertTrue(true);
449 $cache->set('test', 'test');
450 try {
451 $cache->get_many(array('test', 'exception'), MUST_EXIST);
452 $this->fail('Exception expected from cache::get_many using MUST_EXIST');
453 } catch (Exception $e) {
454 $this->assertTrue(true);
459 * Tests a definition using a data loader
461 public function test_definition_data_loader() {
462 $instance = cache_config_testing::instance(true);
463 $instance->phpunit_add_definition('phpunit/datasourcetest', array(
464 'mode' => cache_store::MODE_APPLICATION,
465 'component' => 'phpunit',
466 'area' => 'datasourcetest',
467 'datasource' => 'cache_phpunit_dummy_datasource',
468 'datasourcefile' => 'cache/tests/fixtures/lib.php'
471 $cache = cache::make('phpunit', 'datasourcetest');
472 $this->assertInstanceOf('cache_application', $cache);
474 // Purge it to be sure.
475 $this->assertTrue($cache->purge());
476 // It won't be there yet.
477 $this->assertFalse($cache->has('Test'));
478 // It should load it ;).
479 $this->assertTrue($cache->has('Test', true));
481 // Purge it to be sure.
482 $this->assertTrue($cache->purge());
483 $this->assertEquals('Test has no value really.', $cache->get('Test'));
485 // Test multiple values.
486 $this->assertTrue($cache->purge());
487 $this->assertTrue($cache->set('b', 'B'));
488 $result = $cache->get_many(array('a', 'b', 'c'));
489 $this->assertInternalType('array', $result);
490 $this->assertCount(3, $result);
491 $this->assertArrayHasKey('a', $result);
492 $this->assertArrayHasKey('b', $result);
493 $this->assertArrayHasKey('c', $result);
494 $this->assertEquals('a has no value really.', $result['a']);
495 $this->assertEquals('B', $result['b']);
496 $this->assertEquals('c has no value really.', $result['c']);
500 * Tests a definition using an overridden loader
502 public function test_definition_overridden_loader() {
503 $instance = cache_config_testing::instance(true);
504 $instance->phpunit_add_definition('phpunit/overridetest', array(
505 'mode' => cache_store::MODE_APPLICATION,
506 'component' => 'phpunit',
507 'area' => 'overridetest',
508 'overrideclass' => 'cache_phpunit_dummy_overrideclass',
509 'overrideclassfile' => 'cache/tests/fixtures/lib.php'
511 $cache = cache::make('phpunit', 'overridetest');
512 $this->assertInstanceOf('cache_phpunit_dummy_overrideclass', $cache);
513 $this->assertInstanceOf('cache_application', $cache);
514 // Purge it to be sure.
515 $this->assertTrue($cache->purge());
516 // It won't be there yet.
517 $this->assertFalse($cache->has('Test'));
518 // Add it.
519 $this->assertTrue($cache->set('Test', 'Test has no value really.'));
520 // Check its there.
521 $this->assertEquals('Test has no value really.', $cache->get('Test'));
525 * Test the mappingsonly setting.
527 public function test_definition_mappings_only() {
528 /** @var cache_config_testing $instance */
529 $instance = cache_config_testing::instance(true);
530 $instance->phpunit_add_definition('phpunit/mappingsonly', array(
531 'mode' => cache_store::MODE_APPLICATION,
532 'component' => 'phpunit',
533 'area' => 'mappingsonly',
534 'mappingsonly' => true
535 ), false);
536 $instance->phpunit_add_definition('phpunit/nonmappingsonly', array(
537 'mode' => cache_store::MODE_APPLICATION,
538 'component' => 'phpunit',
539 'area' => 'nonmappingsonly',
540 'mappingsonly' => false
541 ), false);
543 $cacheonly = cache::make('phpunit', 'mappingsonly');
544 $this->assertInstanceOf('cache_application', $cacheonly);
545 $this->assertEquals('cachestore_dummy', $cacheonly->phpunit_get_store_class());
547 $expected = $this->get_expected_application_cache_store();
548 $cachenon = cache::make('phpunit', 'nonmappingsonly');
549 $this->assertInstanceOf('cache_application', $cachenon);
550 $this->assertEquals($expected, $cachenon->phpunit_get_store_class());
554 * Test a very basic definition.
556 public function test_definition() {
557 $instance = cache_config_testing::instance();
558 $instance->phpunit_add_definition('phpunit/test', array(
559 'mode' => cache_store::MODE_APPLICATION,
560 'component' => 'phpunit',
561 'area' => 'test',
563 $cache = cache::make('phpunit', 'test');
565 $this->assertTrue($cache->set('testkey1', 'test data 1'));
566 $this->assertEquals('test data 1', $cache->get('testkey1'));
567 $this->assertTrue($cache->set('testkey2', 'test data 2'));
568 $this->assertEquals('test data 2', $cache->get('testkey2'));
572 * Test a definition using the simple keys.
574 public function test_definition_simplekeys() {
575 $instance = cache_config_testing::instance();
576 $instance->phpunit_add_definition('phpunit/simplekeytest', array(
577 'mode' => cache_store::MODE_APPLICATION,
578 'component' => 'phpunit',
579 'area' => 'simplekeytest',
580 'simplekeys' => true
582 $cache = cache::make('phpunit', 'simplekeytest');
584 $this->assertTrue($cache->set('testkey1', 'test data 1'));
585 $this->assertEquals('test data 1', $cache->get('testkey1'));
586 $this->assertTrue($cache->set('testkey2', 'test data 2'));
587 $this->assertEquals('test data 2', $cache->get('testkey2'));
589 $cache->purge();
591 $this->assertTrue($cache->set('1', 'test data 1'));
592 $this->assertEquals('test data 1', $cache->get('1'));
593 $this->assertTrue($cache->set('2', 'test data 2'));
594 $this->assertEquals('test data 2', $cache->get('2'));
598 * Test a negative TTL on an application cache.
600 public function test_application_ttl_negative() {
601 $instance = cache_config_testing::instance(true);
602 $instance->phpunit_add_definition('phpunit/ttltest', array(
603 'mode' => cache_store::MODE_APPLICATION,
604 'component' => 'phpunit',
605 'area' => 'ttltest',
606 'ttl' => -86400 // Set to a day in the past to be extra sure.
608 $cache = cache::make('phpunit', 'ttltest');
609 $this->assertInstanceOf('cache_application', $cache);
611 // Purge it to be sure.
612 $this->assertTrue($cache->purge());
613 // It won't be there yet.
614 $this->assertFalse($cache->has('Test'));
615 // Set it now.
616 $this->assertTrue($cache->set('Test', 'Test'));
617 // Check its not there.
618 $this->assertFalse($cache->has('Test'));
619 // Double check by trying to get it.
620 $this->assertFalse($cache->get('Test'));
622 // Test with multiple keys.
623 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
624 $result = $cache->get_many(array('a', 'b', 'c'));
625 $this->assertInternalType('array', $result);
626 $this->assertCount(3, $result);
627 $this->assertArrayHasKey('a', $result);
628 $this->assertArrayHasKey('b', $result);
629 $this->assertArrayHasKey('c', $result);
630 $this->assertFalse($result['a']);
631 $this->assertFalse($result['b']);
632 $this->assertFalse($result['c']);
634 // Test with multiple keys including missing ones.
635 $result = $cache->get_many(array('a', 'c', 'e'));
636 $this->assertInternalType('array', $result);
637 $this->assertCount(3, $result);
638 $this->assertArrayHasKey('a', $result);
639 $this->assertArrayHasKey('c', $result);
640 $this->assertArrayHasKey('e', $result);
641 $this->assertFalse($result['a']);
642 $this->assertFalse($result['c']);
643 $this->assertFalse($result['e']);
647 * Test a positive TTL on an application cache.
649 public function test_application_ttl_positive() {
650 $instance = cache_config_testing::instance(true);
651 $instance->phpunit_add_definition('phpunit/ttltest', array(
652 'mode' => cache_store::MODE_APPLICATION,
653 'component' => 'phpunit',
654 'area' => 'ttltest',
655 'ttl' => 86400 // Set to a day in the future to be extra sure.
657 $cache = cache::make('phpunit', 'ttltest');
658 $this->assertInstanceOf('cache_application', $cache);
660 // Purge it to be sure.
661 $this->assertTrue($cache->purge());
662 // It won't be there yet.
663 $this->assertFalse($cache->has('Test'));
664 // Set it now.
665 $this->assertTrue($cache->set('Test', 'Test'));
666 // Check its there.
667 $this->assertTrue($cache->has('Test'));
668 // Double check by trying to get it.
669 $this->assertEquals('Test', $cache->get('Test'));
671 // Test with multiple keys.
672 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
673 $result = $cache->get_many(array('a', 'b', 'c'));
674 $this->assertInternalType('array', $result);
675 $this->assertCount(3, $result);
676 $this->assertArrayHasKey('a', $result);
677 $this->assertArrayHasKey('b', $result);
678 $this->assertArrayHasKey('c', $result);
679 $this->assertEquals('A', $result['a']);
680 $this->assertEquals('B', $result['b']);
681 $this->assertEquals('C', $result['c']);
683 // Test with multiple keys including missing ones.
684 $result = $cache->get_many(array('a', 'c', 'e'));
685 $this->assertInternalType('array', $result);
686 $this->assertCount(3, $result);
687 $this->assertArrayHasKey('a', $result);
688 $this->assertArrayHasKey('c', $result);
689 $this->assertArrayHasKey('e', $result);
690 $this->assertEquals('A', $result['a']);
691 $this->assertEquals('C', $result['c']);
692 $this->assertEquals(false, $result['e']);
696 * Test a negative TTL on an session cache.
698 public function test_session_ttl_positive() {
699 $instance = cache_config_testing::instance(true);
700 $instance->phpunit_add_definition('phpunit/ttltest', array(
701 'mode' => cache_store::MODE_SESSION,
702 'component' => 'phpunit',
703 'area' => 'ttltest',
704 'ttl' => 86400 // Set to a day in the future to be extra sure.
706 $cache = cache::make('phpunit', 'ttltest');
707 $this->assertInstanceOf('cache_session', $cache);
709 // Purge it to be sure.
710 $this->assertTrue($cache->purge());
711 // It won't be there yet.
712 $this->assertFalse($cache->has('Test'));
713 // Set it now.
714 $this->assertTrue($cache->set('Test', 'Test'));
715 // Check its there.
716 $this->assertTrue($cache->has('Test'));
717 // Double check by trying to get it.
718 $this->assertEquals('Test', $cache->get('Test'));
720 // Test with multiple keys.
721 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
722 $result = $cache->get_many(array('a', 'b', 'c'));
723 $this->assertInternalType('array', $result);
724 $this->assertCount(3, $result);
725 $this->assertArrayHasKey('a', $result);
726 $this->assertArrayHasKey('b', $result);
727 $this->assertArrayHasKey('c', $result);
728 $this->assertEquals('A', $result['a']);
729 $this->assertEquals('B', $result['b']);
730 $this->assertEquals('C', $result['c']);
732 // Test with multiple keys including missing ones.
733 $result = $cache->get_many(array('a', 'c', 'e'));
734 $this->assertInternalType('array', $result);
735 $this->assertCount(3, $result);
736 $this->assertArrayHasKey('a', $result);
737 $this->assertArrayHasKey('c', $result);
738 $this->assertArrayHasKey('e', $result);
739 $this->assertEquals('A', $result['a']);
740 $this->assertEquals('C', $result['c']);
741 $this->assertEquals(false, $result['e']);
745 * Tests manual locking operations on an application cache
747 public function test_application_manual_locking() {
748 $instance = cache_config_testing::instance();
749 $instance->phpunit_add_definition('phpunit/lockingtest', array(
750 'mode' => cache_store::MODE_APPLICATION,
751 'component' => 'phpunit',
752 'area' => 'lockingtest'
754 $cache1 = cache::make('phpunit', 'lockingtest');
755 $cache2 = clone($cache1);
757 $this->assertTrue($cache1->set('testkey', 'test data'));
758 $this->assertTrue($cache2->set('testkey', 'test data'));
760 $this->assertTrue($cache1->acquire_lock('testkey'));
761 $this->assertFalse($cache2->acquire_lock('testkey'));
763 $this->assertTrue($cache1->check_lock_state('testkey'));
764 $this->assertFalse($cache2->check_lock_state('testkey'));
766 $this->assertTrue($cache1->release_lock('testkey'));
767 $this->assertFalse($cache2->release_lock('testkey'));
769 $this->assertTrue($cache1->set('testkey', 'test data'));
770 $this->assertTrue($cache2->set('testkey', 'test data'));
774 * Tests application cache event invalidation
776 public function test_application_event_invalidation() {
777 $instance = cache_config_testing::instance();
778 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
779 'mode' => cache_store::MODE_APPLICATION,
780 'component' => 'phpunit',
781 'area' => 'eventinvalidationtest',
782 'invalidationevents' => array(
783 'crazyevent'
786 $cache = cache::make('phpunit', 'eventinvalidationtest');
788 $this->assertTrue($cache->set('testkey1', 'test data 1'));
789 $this->assertEquals('test data 1', $cache->get('testkey1'));
790 $this->assertTrue($cache->set('testkey2', 'test data 2'));
791 $this->assertEquals('test data 2', $cache->get('testkey2'));
793 // Test invalidating a single entry.
794 cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
796 $this->assertFalse($cache->get('testkey1'));
797 $this->assertEquals('test data 2', $cache->get('testkey2'));
799 $this->assertTrue($cache->set('testkey1', 'test data 1'));
801 // Test invalidating both entries.
802 cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
804 $this->assertFalse($cache->get('testkey1'));
805 $this->assertFalse($cache->get('testkey2'));
809 * Tests session cache event invalidation
811 public function test_session_event_invalidation() {
812 $instance = cache_config_testing::instance();
813 $instance->phpunit_add_definition('phpunit/test_session_event_invalidation', array(
814 'mode' => cache_store::MODE_SESSION,
815 'component' => 'phpunit',
816 'area' => 'test_session_event_invalidation',
817 'invalidationevents' => array(
818 'crazyevent'
821 $cache = cache::make('phpunit', 'test_session_event_invalidation');
822 $this->assertInstanceOf('cache_session', $cache);
824 $this->assertTrue($cache->set('testkey1', 'test data 1'));
825 $this->assertEquals('test data 1', $cache->get('testkey1'));
826 $this->assertTrue($cache->set('testkey2', 'test data 2'));
827 $this->assertEquals('test data 2', $cache->get('testkey2'));
829 // Test invalidating a single entry.
830 cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
832 $this->assertFalse($cache->get('testkey1'));
833 $this->assertEquals('test data 2', $cache->get('testkey2'));
835 $this->assertTrue($cache->set('testkey1', 'test data 1'));
837 // Test invalidating both entries.
838 cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
840 $this->assertFalse($cache->get('testkey1'));
841 $this->assertFalse($cache->get('testkey2'));
845 * Tests application cache definition invalidation
847 public function test_application_definition_invalidation() {
848 $instance = cache_config_testing::instance();
849 $instance->phpunit_add_definition('phpunit/definitioninvalidation', array(
850 'mode' => cache_store::MODE_APPLICATION,
851 'component' => 'phpunit',
852 'area' => 'definitioninvalidation'
854 $cache = cache::make('phpunit', 'definitioninvalidation');
855 $this->assertTrue($cache->set('testkey1', 'test data 1'));
856 $this->assertEquals('test data 1', $cache->get('testkey1'));
857 $this->assertTrue($cache->set('testkey2', 'test data 2'));
858 $this->assertEquals('test data 2', $cache->get('testkey2'));
860 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), 'testkey1');
862 $this->assertFalse($cache->get('testkey1'));
863 $this->assertEquals('test data 2', $cache->get('testkey2'));
865 $this->assertTrue($cache->set('testkey1', 'test data 1'));
867 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1'));
869 $this->assertFalse($cache->get('testkey1'));
870 $this->assertEquals('test data 2', $cache->get('testkey2'));
872 $this->assertTrue($cache->set('testkey1', 'test data 1'));
874 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1', 'testkey2'));
876 $this->assertFalse($cache->get('testkey1'));
877 $this->assertFalse($cache->get('testkey2'));
881 * Tests session cache definition invalidation
883 public function test_session_definition_invalidation() {
884 $instance = cache_config_testing::instance();
885 $instance->phpunit_add_definition('phpunit/test_session_definition_invalidation', array(
886 'mode' => cache_store::MODE_SESSION,
887 'component' => 'phpunit',
888 'area' => 'test_session_definition_invalidation'
890 $cache = cache::make('phpunit', 'test_session_definition_invalidation');
891 $this->assertInstanceOf('cache_session', $cache);
892 $this->assertTrue($cache->set('testkey1', 'test data 1'));
893 $this->assertEquals('test data 1', $cache->get('testkey1'));
894 $this->assertTrue($cache->set('testkey2', 'test data 2'));
895 $this->assertEquals('test data 2', $cache->get('testkey2'));
897 cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(), 'testkey1');
899 $this->assertFalse($cache->get('testkey1'));
900 $this->assertEquals('test data 2', $cache->get('testkey2'));
902 $this->assertTrue($cache->set('testkey1', 'test data 1'));
904 cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(),
905 array('testkey1'));
907 $this->assertFalse($cache->get('testkey1'));
908 $this->assertEquals('test data 2', $cache->get('testkey2'));
910 $this->assertTrue($cache->set('testkey1', 'test data 1'));
912 cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(),
913 array('testkey1', 'testkey2'));
915 $this->assertFalse($cache->get('testkey1'));
916 $this->assertFalse($cache->get('testkey2'));
920 * Tests application cache event invalidation over a distributed setup.
922 public function test_distributed_application_event_invalidation() {
923 global $CFG;
924 // This is going to be an intense wee test.
925 // We need to add data the to cache, invalidate it by event, manually force it back without MUC knowing to simulate a
926 // disconnected/distributed setup (think load balanced server using local cache), instantiate the cache again and finally
927 // check that it is not picked up.
928 $instance = cache_config_testing::instance();
929 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
930 'mode' => cache_store::MODE_APPLICATION,
931 'component' => 'phpunit',
932 'area' => 'eventinvalidationtest',
933 'simplekeys' => true,
934 'simpledata' => true,
935 'invalidationevents' => array(
936 'crazyevent'
939 $cache = cache::make('phpunit', 'eventinvalidationtest');
940 $this->assertTrue($cache->set('testkey1', 'test data 1'));
941 $this->assertEquals('test data 1', $cache->get('testkey1'));
943 cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
945 $this->assertFalse($cache->get('testkey1'));
947 // OK data added, data invalidated, and invalidation time has been set.
948 // Now we need to manually add back the data and adjust the invalidation time.
949 $hash = md5(cache_store::MODE_APPLICATION.'/phpunit/eventinvalidationtest/'.$CFG->wwwroot.'phpunit');
950 $timefile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/las-cache/lastinvalidation-$hash.cache";
951 // Make sure the file is correct.
952 $this->assertTrue(file_exists($timefile));
953 $timecont = serialize(cache::now() - 60); // Back 60sec in the past to force it to re-invalidate.
954 make_writable_directory(dirname($timefile));
955 file_put_contents($timefile, $timecont);
956 $this->assertTrue(file_exists($timefile));
958 $datafile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/tes-cache/testkey1-$hash.cache";
959 $datacont = serialize("test data 1");
960 make_writable_directory(dirname($datafile));
961 file_put_contents($datafile, $datacont);
962 $this->assertTrue(file_exists($datafile));
964 // Test 1: Rebuild without the event and test its there.
965 cache_factory::reset();
966 $instance = cache_config_testing::instance();
967 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
968 'mode' => cache_store::MODE_APPLICATION,
969 'component' => 'phpunit',
970 'area' => 'eventinvalidationtest',
971 'simplekeys' => true,
972 'simpledata' => true,
974 $cache = cache::make('phpunit', 'eventinvalidationtest');
975 $this->assertEquals('test data 1', $cache->get('testkey1'));
977 // Test 2: Rebuild and test the invalidation of the event via the invalidation cache.
978 cache_factory::reset();
979 $instance = cache_config_testing::instance();
980 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
981 'mode' => cache_store::MODE_APPLICATION,
982 'component' => 'phpunit',
983 'area' => 'eventinvalidationtest',
984 'simplekeys' => true,
985 'simpledata' => true,
986 'invalidationevents' => array(
987 'crazyevent'
990 $cache = cache::make('phpunit', 'eventinvalidationtest');
991 $this->assertFalse($cache->get('testkey1'));
995 * Tests application cache event purge
997 public function test_application_event_purge() {
998 $instance = cache_config_testing::instance();
999 $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
1000 'mode' => cache_store::MODE_APPLICATION,
1001 'component' => 'phpunit',
1002 'area' => 'eventpurgetest',
1003 'invalidationevents' => array(
1004 'crazyevent'
1007 $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', array(
1008 'mode' => cache_store::MODE_APPLICATION,
1009 'component' => 'phpunit',
1010 'area' => 'eventpurgetestaccelerated',
1011 'staticacceleration' => true,
1012 'invalidationevents' => array(
1013 'crazyevent'
1016 $cache = cache::make('phpunit', 'eventpurgetest');
1018 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1019 $this->assertEquals('test data 1', $cache->get('testkey1'));
1020 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1021 $this->assertEquals('test data 2', $cache->get('testkey2'));
1023 // Purge the event.
1024 cache_helper::purge_by_event('crazyevent');
1026 // Check things have been removed.
1027 $this->assertFalse($cache->get('testkey1'));
1028 $this->assertFalse($cache->get('testkey2'));
1030 // Now test the static acceleration array.
1031 $cache = cache::make('phpunit', 'eventpurgetestaccelerated');
1032 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1033 $this->assertEquals('test data 1', $cache->get('testkey1'));
1034 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1035 $this->assertEquals('test data 2', $cache->get('testkey2'));
1037 // Purge the event.
1038 cache_helper::purge_by_event('crazyevent');
1040 // Check things have been removed.
1041 $this->assertFalse($cache->get('testkey1'));
1042 $this->assertFalse($cache->get('testkey2'));
1046 * Tests session cache event purge
1048 public function test_session_event_purge() {
1049 $instance = cache_config_testing::instance();
1050 $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
1051 'mode' => cache_store::MODE_SESSION,
1052 'component' => 'phpunit',
1053 'area' => 'eventpurgetest',
1054 'invalidationevents' => array(
1055 'crazyevent'
1058 $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', array(
1059 'mode' => cache_store::MODE_SESSION,
1060 'component' => 'phpunit',
1061 'area' => 'eventpurgetestaccelerated',
1062 'staticacceleration' => true,
1063 'invalidationevents' => array(
1064 'crazyevent'
1067 $cache = cache::make('phpunit', 'eventpurgetest');
1069 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1070 $this->assertEquals('test data 1', $cache->get('testkey1'));
1071 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1072 $this->assertEquals('test data 2', $cache->get('testkey2'));
1074 // Purge the event.
1075 cache_helper::purge_by_event('crazyevent');
1077 // Check things have been removed.
1078 $this->assertFalse($cache->get('testkey1'));
1079 $this->assertFalse($cache->get('testkey2'));
1081 // Now test the static acceleration array.
1082 $cache = cache::make('phpunit', 'eventpurgetestaccelerated');
1083 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1084 $this->assertEquals('test data 1', $cache->get('testkey1'));
1085 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1086 $this->assertEquals('test data 2', $cache->get('testkey2'));
1088 // Purge the event.
1089 cache_helper::purge_by_event('crazyevent');
1091 // Check things have been removed.
1092 $this->assertFalse($cache->get('testkey1'));
1093 $this->assertFalse($cache->get('testkey2'));
1097 * Tests application cache definition purge
1099 public function test_application_definition_purge() {
1100 $instance = cache_config_testing::instance();
1101 $instance->phpunit_add_definition('phpunit/definitionpurgetest', array(
1102 'mode' => cache_store::MODE_APPLICATION,
1103 'component' => 'phpunit',
1104 'area' => 'definitionpurgetest',
1105 'invalidationevents' => array(
1106 'crazyevent'
1109 $cache = cache::make('phpunit', 'definitionpurgetest');
1111 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1112 $this->assertEquals('test data 1', $cache->get('testkey1'));
1113 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1114 $this->assertEquals('test data 2', $cache->get('testkey2'));
1116 // Purge the event.
1117 cache_helper::purge_by_definition('phpunit', 'definitionpurgetest');
1119 // Check things have been removed.
1120 $this->assertFalse($cache->get('testkey1'));
1121 $this->assertFalse($cache->get('testkey2'));
1125 * Test the use of an alt path.
1126 * If we can generate a config instance we are done :)
1128 public function test_alt_cache_path() {
1129 global $CFG;
1130 if ((defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH) || !empty($CFG->altcacheconfigpath)) {
1131 $this->markTestSkipped('Skipped testing alt cache path as it is already being used.');
1133 $this->resetAfterTest();
1134 $CFG->altcacheconfigpath = $CFG->dataroot.'/cache/altcacheconfigpath';
1135 $instance = cache_config_testing::instance();
1136 $this->assertInstanceOf('cache_config', $instance);
1140 * Test disabling the cache stores.
1142 public function test_disable_stores() {
1143 $instance = cache_config_testing::instance();
1144 $instance->phpunit_add_definition('phpunit/disabletest1', array(
1145 'mode' => cache_store::MODE_APPLICATION,
1146 'component' => 'phpunit',
1147 'area' => 'disabletest1'
1149 $instance->phpunit_add_definition('phpunit/disabletest2', array(
1150 'mode' => cache_store::MODE_SESSION,
1151 'component' => 'phpunit',
1152 'area' => 'disabletest2'
1154 $instance->phpunit_add_definition('phpunit/disabletest3', array(
1155 'mode' => cache_store::MODE_REQUEST,
1156 'component' => 'phpunit',
1157 'area' => 'disabletest3'
1160 $caches = array(
1161 'disabletest1' => cache::make('phpunit', 'disabletest1'),
1162 'disabletest2' => cache::make('phpunit', 'disabletest2'),
1163 'disabletest3' => cache::make('phpunit', 'disabletest3')
1166 $this->assertInstanceOf('cache_phpunit_application', $caches['disabletest1']);
1167 $this->assertInstanceOf('cache_phpunit_session', $caches['disabletest2']);
1168 $this->assertInstanceOf('cache_phpunit_request', $caches['disabletest3']);
1170 $this->assertEquals('cachestore_file', $caches['disabletest1']->phpunit_get_store_class());
1171 $this->assertEquals('cachestore_session', $caches['disabletest2']->phpunit_get_store_class());
1172 $this->assertEquals('cachestore_static', $caches['disabletest3']->phpunit_get_store_class());
1174 foreach ($caches as $cache) {
1175 $this->assertFalse($cache->get('test'));
1176 $this->assertTrue($cache->set('test', 'test'));
1177 $this->assertEquals('test', $cache->get('test'));
1180 cache_factory::disable_stores();
1182 $caches = array(
1183 'disabletest1' => cache::make('phpunit', 'disabletest1'),
1184 'disabletest2' => cache::make('phpunit', 'disabletest2'),
1185 'disabletest3' => cache::make('phpunit', 'disabletest3')
1188 $this->assertInstanceOf('cache_phpunit_application', $caches['disabletest1']);
1189 $this->assertInstanceOf('cache_phpunit_session', $caches['disabletest2']);
1190 $this->assertInstanceOf('cache_phpunit_request', $caches['disabletest3']);
1192 $this->assertEquals('cachestore_dummy', $caches['disabletest1']->phpunit_get_store_class());
1193 $this->assertEquals('cachestore_dummy', $caches['disabletest2']->phpunit_get_store_class());
1194 $this->assertEquals('cachestore_dummy', $caches['disabletest3']->phpunit_get_store_class());
1196 foreach ($caches as $cache) {
1197 $this->assertFalse($cache->get('test'));
1198 $this->assertTrue($cache->set('test', 'test'));
1199 $this->assertEquals('test', $cache->get('test'));
1204 * Test disabling the cache.
1206 public function test_disable() {
1207 global $CFG;
1209 if ((defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH) || !empty($CFG->altcacheconfigpath)) {
1210 // We can't run this test as it requires us to delete the cache configuration script which we just
1211 // cant do with a custom path in play.
1212 $this->markTestSkipped('Skipped testing cache disable functionality as alt cache path is being used.');
1215 $configfile = $CFG->dataroot.'/muc/config.php';
1217 // That's right, we're deleting the config file.
1218 $this->assertTrue(@unlink($configfile));
1220 // Disable the cache
1221 cache_phpunit_factory::phpunit_disable();
1223 // Check we get the expected disabled factory.
1224 $factory = cache_factory::instance();
1225 $this->assertInstanceOf('cache_factory_disabled', $factory);
1227 // Check we get the expected disabled config.
1228 $config = $factory->create_config_instance();
1229 $this->assertInstanceOf('cache_config_disabled', $config);
1231 // Check we get the expected disabled caches.
1232 $cache = cache::make('phpunit', 'disable');
1233 $this->assertInstanceOf('cache_disabled', $cache);
1235 // Test an application cache.
1236 $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'disable');
1237 $this->assertInstanceOf('cache_disabled', $cache);
1239 $this->assertFalse(file_exists($configfile));
1241 $this->assertFalse($cache->get('test'));
1242 $this->assertFalse($cache->set('test', 'test'));
1243 $this->assertFalse($cache->delete('test'));
1244 $this->assertTrue($cache->purge());
1246 // Test a session cache.
1247 $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'disable');
1248 $this->assertInstanceOf('cache_disabled', $cache);
1250 $this->assertFalse(file_exists($configfile));
1252 $this->assertFalse($cache->get('test'));
1253 $this->assertFalse($cache->set('test', 'test'));
1254 $this->assertFalse($cache->delete('test'));
1255 $this->assertTrue($cache->purge());
1257 // Finally test a request cache.
1258 $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'disable');
1259 $this->assertInstanceOf('cache_disabled', $cache);
1261 $this->assertFalse(file_exists($configfile));
1263 $this->assertFalse($cache->get('test'));
1264 $this->assertFalse($cache->set('test', 'test'));
1265 $this->assertFalse($cache->delete('test'));
1266 $this->assertTrue($cache->purge());
1268 cache_factory::reset();
1270 $factory = cache_factory::instance(true);
1271 $config = $factory->create_config_instance();
1272 $this->assertEquals('cache_config_testing', get_class($config));
1276 * Test that multiple application loaders work ok.
1278 public function test_multiple_application_loaders() {
1279 $instance = cache_config_testing::instance(true);
1280 $instance->phpunit_add_file_store('phpunittest1');
1281 $instance->phpunit_add_file_store('phpunittest2');
1282 $instance->phpunit_add_definition('phpunit/multi_loader', array(
1283 'mode' => cache_store::MODE_APPLICATION,
1284 'component' => 'phpunit',
1285 'area' => 'multi_loader'
1287 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
1288 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
1290 $cache = cache::make('phpunit', 'multi_loader');
1291 $this->assertInstanceOf('cache_application', $cache);
1292 $this->assertFalse($cache->get('test'));
1293 $this->assertTrue($cache->set('test', 'test'));
1294 $this->assertEquals('test', $cache->get('test'));
1295 $this->assertTrue($cache->delete('test'));
1296 $this->assertFalse($cache->get('test'));
1297 $this->assertTrue($cache->set('test', 'test'));
1298 $this->assertTrue($cache->purge());
1299 $this->assertFalse($cache->get('test'));
1301 // Test the many commands.
1302 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
1303 $result = $cache->get_many(array('a', 'b', 'c'));
1304 $this->assertInternalType('array', $result);
1305 $this->assertCount(3, $result);
1306 $this->assertArrayHasKey('a', $result);
1307 $this->assertArrayHasKey('b', $result);
1308 $this->assertArrayHasKey('c', $result);
1309 $this->assertEquals('A', $result['a']);
1310 $this->assertEquals('B', $result['b']);
1311 $this->assertEquals('C', $result['c']);
1312 $this->assertEquals($result, $cache->get_many(array('a', 'b', 'c')));
1313 $this->assertEquals(2, $cache->delete_many(array('a', 'c')));
1314 $result = $cache->get_many(array('a', 'b', 'c'));
1315 $this->assertInternalType('array', $result);
1316 $this->assertCount(3, $result);
1317 $this->assertArrayHasKey('a', $result);
1318 $this->assertArrayHasKey('b', $result);
1319 $this->assertArrayHasKey('c', $result);
1320 $this->assertFalse($result['a']);
1321 $this->assertEquals('B', $result['b']);
1322 $this->assertFalse($result['c']);
1324 // Test non-recursive deletes.
1325 $this->assertTrue($cache->set('test', 'test'));
1326 $this->assertSame('test', $cache->get('test'));
1327 $this->assertTrue($cache->delete('test', false));
1328 // We should still have it on a deeper loader.
1329 $this->assertSame('test', $cache->get('test'));
1330 // Test non-recusive with many functions.
1331 $this->assertSame(3, $cache->set_many(array(
1332 'one' => 'one',
1333 'two' => 'two',
1334 'three' => 'three'
1335 )));
1336 $this->assertSame('one', $cache->get('one'));
1337 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1338 $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
1339 $this->assertSame('one', $cache->get('one'));
1340 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1344 * Test that multiple application loaders work ok.
1346 public function test_multiple_session_loaders() {
1347 /* @var cache_config_testing $instance */
1348 $instance = cache_config_testing::instance(true);
1349 $instance->phpunit_add_session_store('phpunittest1');
1350 $instance->phpunit_add_session_store('phpunittest2');
1351 $instance->phpunit_add_definition('phpunit/multi_loader', array(
1352 'mode' => cache_store::MODE_SESSION,
1353 'component' => 'phpunit',
1354 'area' => 'multi_loader'
1356 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
1357 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
1359 $cache = cache::make('phpunit', 'multi_loader');
1360 $this->assertInstanceOf('cache_session', $cache);
1361 $this->assertFalse($cache->get('test'));
1362 $this->assertTrue($cache->set('test', 'test'));
1363 $this->assertEquals('test', $cache->get('test'));
1364 $this->assertTrue($cache->delete('test'));
1365 $this->assertFalse($cache->get('test'));
1366 $this->assertTrue($cache->set('test', 'test'));
1367 $this->assertTrue($cache->purge());
1368 $this->assertFalse($cache->get('test'));
1370 // Test the many commands.
1371 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
1372 $result = $cache->get_many(array('a', 'b', 'c'));
1373 $this->assertInternalType('array', $result);
1374 $this->assertCount(3, $result);
1375 $this->assertArrayHasKey('a', $result);
1376 $this->assertArrayHasKey('b', $result);
1377 $this->assertArrayHasKey('c', $result);
1378 $this->assertEquals('A', $result['a']);
1379 $this->assertEquals('B', $result['b']);
1380 $this->assertEquals('C', $result['c']);
1381 $this->assertEquals($result, $cache->get_many(array('a', 'b', 'c')));
1382 $this->assertEquals(2, $cache->delete_many(array('a', 'c')));
1383 $result = $cache->get_many(array('a', 'b', 'c'));
1384 $this->assertInternalType('array', $result);
1385 $this->assertCount(3, $result);
1386 $this->assertArrayHasKey('a', $result);
1387 $this->assertArrayHasKey('b', $result);
1388 $this->assertArrayHasKey('c', $result);
1389 $this->assertFalse($result['a']);
1390 $this->assertEquals('B', $result['b']);
1391 $this->assertFalse($result['c']);
1393 // Test non-recursive deletes.
1394 $this->assertTrue($cache->set('test', 'test'));
1395 $this->assertSame('test', $cache->get('test'));
1396 $this->assertTrue($cache->delete('test', false));
1397 // We should still have it on a deeper loader.
1398 $this->assertSame('test', $cache->get('test'));
1399 // Test non-recusive with many functions.
1400 $this->assertSame(3, $cache->set_many(array(
1401 'one' => 'one',
1402 'two' => 'two',
1403 'three' => 'three'
1404 )));
1405 $this->assertSame('one', $cache->get('one'));
1406 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1407 $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
1408 $this->assertSame('one', $cache->get('one'));
1409 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1413 * Test switching users with session caches.
1415 public function test_session_cache_switch_user() {
1416 $this->resetAfterTest(true);
1417 $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache');
1418 $user1 = $this->getDataGenerator()->create_user();
1419 $user2 = $this->getDataGenerator()->create_user();
1421 // Log in as the first user.
1422 $this->setUser($user1);
1423 $sesskey1 = sesskey();
1425 // Set a basic value in the cache.
1426 $cache->set('var', 1);
1427 $this->assertTrue($cache->has('var'));
1428 $this->assertEquals(1, $cache->get('var'));
1430 // Change to the second user.
1431 $this->setUser($user2);
1432 $sesskey2 = sesskey();
1434 // Make sure the cache doesn't give us the data for the last user.
1435 $this->assertNotEquals($sesskey1, $sesskey2);
1436 $this->assertFalse($cache->has('var'));
1437 $this->assertEquals(false, $cache->get('var'));
1441 * Test switching users with session caches.
1443 public function test_session_cache_switch_user_application_mapping() {
1444 $this->resetAfterTest(true);
1445 $instance = cache_config_testing::instance(true);
1446 $instance->phpunit_add_file_store('testfilestore');
1447 $instance->phpunit_add_definition('phpunit/testappsession', array(
1448 'mode' => cache_store::MODE_SESSION,
1449 'component' => 'phpunit',
1450 'area' => 'testappsession'
1452 $instance->phpunit_add_definition_mapping('phpunit/testappsession', 'testfilestore', 3);
1453 $cache = cache::make('phpunit', 'testappsession');
1454 $user1 = $this->getDataGenerator()->create_user();
1455 $user2 = $this->getDataGenerator()->create_user();
1457 // Log in as the first user.
1458 $this->setUser($user1);
1459 $sesskey1 = sesskey();
1461 // Set a basic value in the cache.
1462 $cache->set('var', 1);
1463 $this->assertTrue($cache->has('var'));
1464 $this->assertEquals(1, $cache->get('var'));
1466 // Change to the second user.
1467 $this->setUser($user2);
1468 $sesskey2 = sesskey();
1470 // Make sure the cache doesn't give us the data for the last user.
1471 $this->assertNotEquals($sesskey1, $sesskey2);
1472 $this->assertFalse($cache->has('var'));
1473 $this->assertEquals(false, $cache->get('var'));
1477 * Test two session caches being used at once to confirm collisions don't occur.
1479 public function test_dual_session_caches() {
1480 $instance = cache_config_testing::instance(true);
1481 $instance->phpunit_add_definition('phpunit/testsess1', array(
1482 'mode' => cache_store::MODE_SESSION,
1483 'component' => 'phpunit',
1484 'area' => 'testsess1'
1486 $instance->phpunit_add_definition('phpunit/testsess2', array(
1487 'mode' => cache_store::MODE_SESSION,
1488 'component' => 'phpunit',
1489 'area' => 'testsess2'
1491 $cache1 = cache::make('phpunit', 'testsess1');
1492 $cache2 = cache::make('phpunit', 'testsess2');
1494 $this->assertFalse($cache1->has('test'));
1495 $this->assertFalse($cache2->has('test'));
1497 $this->assertTrue($cache1->set('test', '1'));
1499 $this->assertTrue($cache1->has('test'));
1500 $this->assertFalse($cache2->has('test'));
1502 $this->assertTrue($cache2->set('test', '2'));
1504 $this->assertEquals(1, $cache1->get('test'));
1505 $this->assertEquals(2, $cache2->get('test'));
1507 $this->assertTrue($cache1->delete('test'));
1511 * Test multiple session caches when switching user.
1513 public function test_session_cache_switch_user_multiple() {
1514 $this->resetAfterTest(true);
1515 $cache1 = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache1');
1516 $cache2 = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache2');
1517 $user1 = $this->getDataGenerator()->create_user();
1518 $user2 = $this->getDataGenerator()->create_user();
1520 // Log in as the first user.
1521 $this->setUser($user1);
1522 $sesskey1 = sesskey();
1524 // Set a basic value in the caches.
1525 $cache1->set('var', 1);
1526 $cache2->set('var', 2);
1527 $this->assertEquals(1, $cache1->get('var'));
1528 $this->assertEquals(2, $cache2->get('var'));
1530 // Change to the second user.
1531 $this->setUser($user2);
1532 $sesskey2 = sesskey();
1534 // Make sure the cache doesn't give us the data for the last user.
1535 // Also make sure that switching the user has lead to both caches being purged.
1536 $this->assertNotEquals($sesskey1, $sesskey2);
1537 $this->assertEquals(false, $cache1->get('var'));
1538 $this->assertEquals(false, $cache2->get('var'));
1542 * Test application locking.
1544 public function test_application_locking() {
1545 $instance = cache_config_testing::instance(true);
1546 $instance->phpunit_add_definition('phpunit/test_application_locking', array(
1547 'mode' => cache_store::MODE_APPLICATION,
1548 'component' => 'phpunit',
1549 'area' => 'test_application_locking',
1550 'staticacceleration' => true,
1551 'staticaccelerationsize' => 1,
1552 'requirelockingread' => true,
1553 'requirelockingwrite' => true
1555 $cache = cache::make('phpunit', 'test_application_locking');
1556 $this->assertInstanceOf('cache_application', $cache);
1558 $this->assertTrue($cache->set('a', 'A'));
1559 $this->assertTrue($cache->set('b', 'B'));
1560 $this->assertTrue($cache->set('c', 'C'));
1561 $this->assertEquals('A', $cache->get('a'));
1562 $this->assertEquals(array('b' => 'B', 'c' => 'C'), $cache->get_many(array('b', 'c')));
1563 $this->assertTrue($cache->delete('a'));
1564 $this->assertFalse($cache->has('a'));
1568 * Test the static cache_helper method purge_stores_used_by_definition.
1570 public function test_purge_stores_used_by_definition() {
1571 $instance = cache_config_testing::instance(true);
1572 $instance->phpunit_add_definition('phpunit/test_purge_stores_used_by_definition', array(
1573 'mode' => cache_store::MODE_APPLICATION,
1574 'component' => 'phpunit',
1575 'area' => 'test_purge_stores_used_by_definition'
1577 $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition');
1578 $this->assertInstanceOf('cache_application', $cache);
1579 $this->assertTrue($cache->set('test', 'test'));
1580 unset($cache);
1582 cache_helper::purge_stores_used_by_definition('phpunit', 'test_purge_stores_used_by_definition');
1584 $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition');
1585 $this->assertInstanceOf('cache_application', $cache);
1586 $this->assertFalse($cache->get('test'));
1590 * Test purge routines.
1592 public function test_purge_routines() {
1593 $instance = cache_config_testing::instance(true);
1594 $instance->phpunit_add_definition('phpunit/purge1', array(
1595 'mode' => cache_store::MODE_APPLICATION,
1596 'component' => 'phpunit',
1597 'area' => 'purge1'
1599 $instance->phpunit_add_definition('phpunit/purge2', array(
1600 'mode' => cache_store::MODE_APPLICATION,
1601 'component' => 'phpunit',
1602 'area' => 'purge2',
1603 'requireidentifiers' => array(
1604 'id'
1608 $factory = cache_factory::instance();
1609 $definition = $factory->create_definition('phpunit', 'purge1');
1610 $this->assertFalse($definition->has_required_identifiers());
1611 $cache = $factory->create_cache($definition);
1612 $this->assertInstanceOf('cache_application', $cache);
1613 $this->assertTrue($cache->set('test', 'test'));
1614 $this->assertTrue($cache->has('test'));
1615 cache_helper::purge_by_definition('phpunit', 'purge1');
1616 $this->assertFalse($cache->has('test'));
1618 $factory = cache_factory::instance();
1619 $definition = $factory->create_definition('phpunit', 'purge2');
1620 $this->assertTrue($definition->has_required_identifiers());
1621 $cache = $factory->create_cache($definition);
1622 $this->assertInstanceOf('cache_application', $cache);
1623 $this->assertTrue($cache->set('test', 'test'));
1624 $this->assertTrue($cache->has('test'));
1625 cache_helper::purge_stores_used_by_definition('phpunit', 'purge2');
1626 $this->assertFalse($cache->has('test'));
1628 try {
1629 cache_helper::purge_by_definition('phpunit', 'purge2');
1630 $this->fail('Should not be able to purge a definition required identifiers without providing them.');
1631 } catch (coding_exception $ex) {
1632 $this->assertContains('Identifier required for cache has not been provided', $ex->getMessage());
1637 * Test that the default stores all support searching.
1639 public function test_defaults_support_searching() {
1640 $instance = cache_config_testing::instance(true);
1641 $instance->phpunit_add_definition('phpunit/search1', array(
1642 'mode' => cache_store::MODE_APPLICATION,
1643 'component' => 'phpunit',
1644 'area' => 'search1',
1645 'requiresearchable' => true
1647 $instance->phpunit_add_definition('phpunit/search2', array(
1648 'mode' => cache_store::MODE_SESSION,
1649 'component' => 'phpunit',
1650 'area' => 'search2',
1651 'requiresearchable' => true
1653 $instance->phpunit_add_definition('phpunit/search3', array(
1654 'mode' => cache_store::MODE_REQUEST,
1655 'component' => 'phpunit',
1656 'area' => 'search3',
1657 'requiresearchable' => true
1659 $factory = cache_factory::instance();
1661 // Test application cache is searchable.
1662 $definition = $factory->create_definition('phpunit', 'search1');
1663 $this->assertInstanceOf('cache_definition', $definition);
1664 $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1665 $cache = $factory->create_cache($definition);
1666 $this->assertInstanceOf('cache_application', $cache);
1667 $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1669 // Test session cache is searchable.
1670 $definition = $factory->create_definition('phpunit', 'search2');
1671 $this->assertInstanceOf('cache_definition', $definition);
1672 $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1673 $cache = $factory->create_cache($definition);
1674 $this->assertInstanceOf('cache_session', $cache);
1675 $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1677 // Test request cache is searchable.
1678 $definition = $factory->create_definition('phpunit', 'search3');
1679 $this->assertInstanceOf('cache_definition', $definition);
1680 $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1681 $cache = $factory->create_cache($definition);
1682 $this->assertInstanceOf('cache_request', $cache);
1683 $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1686 public function test_static_acceleration() {
1687 $instance = cache_config_testing::instance();
1688 $instance->phpunit_add_definition('phpunit/accelerated', array(
1689 'mode' => cache_store::MODE_APPLICATION,
1690 'component' => 'phpunit',
1691 'area' => 'accelerated',
1692 'staticacceleration' => true,
1693 'staticaccelerationsize' => 3,
1695 $instance->phpunit_add_definition('phpunit/accelerated2', array(
1696 'mode' => cache_store::MODE_APPLICATION,
1697 'component' => 'phpunit',
1698 'area' => 'accelerated2',
1699 'staticacceleration' => true,
1700 'staticaccelerationsize' => 3,
1702 $instance->phpunit_add_definition('phpunit/accelerated3', array(
1703 'mode' => cache_store::MODE_APPLICATION,
1704 'component' => 'phpunit',
1705 'area' => 'accelerated3',
1706 'staticacceleration' => true,
1707 'staticaccelerationsize' => 3,
1709 $instance->phpunit_add_definition('phpunit/accelerated4', array(
1710 'mode' => cache_store::MODE_APPLICATION,
1711 'component' => 'phpunit',
1712 'area' => 'accelerated4',
1713 'staticacceleration' => true,
1714 'staticaccelerationsize' => 4,
1716 $instance->phpunit_add_definition('phpunit/simpledataarea1', array(
1717 'mode' => cache_store::MODE_APPLICATION,
1718 'component' => 'phpunit',
1719 'area' => 'simpledataarea1',
1720 'staticacceleration' => true,
1721 'simpledata' => false
1723 $instance->phpunit_add_definition('phpunit/simpledataarea2', array(
1724 'mode' => cache_store::MODE_APPLICATION,
1725 'component' => 'phpunit',
1726 'area' => 'simpledataarea2',
1727 'staticacceleration' => true,
1728 'simpledata' => true
1731 $cache = cache::make('phpunit', 'accelerated');
1732 $this->assertInstanceOf('cache_phpunit_application', $cache);
1734 // Set and get three elements.
1735 $this->assertTrue($cache->set('a', 'A'));
1736 $this->assertTrue($cache->set('b', 'B'));
1737 $this->assertTrue($cache->set('c', 'C'));
1738 $this->assertEquals('A', $cache->get('a'));
1739 $this->assertEquals(array('b' => 'B', 'c' => 'C'), $cache->get_many(array('b', 'c')));
1741 // Make sure all items are in static acceleration array.
1742 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1743 $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b'));
1744 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1746 // Add new value and make sure it is in cache and it is in array.
1747 $this->assertTrue($cache->set('d', 'D'));
1748 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1749 $this->assertEquals('D', $cache->get('d'));
1751 // Now the least recent accessed item (a) is no longer in acceleration array.
1752 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1753 $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b'));
1754 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1756 // Adding and deleting element.
1757 $this->assertTrue($cache->set('a', 'A'));
1758 $this->assertTrue($cache->delete('a'));
1759 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1760 $this->assertFalse($cache->has('a'));
1762 // Make sure "purge" deletes from the array as well.
1763 $cache->purge();
1764 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1765 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1766 $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1767 $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1768 $this->assertFalse($cache->phpunit_static_acceleration_get('e'));
1770 // Check that the array holds the last accessed items by get/set.
1771 $this->assertTrue($cache->set('a', 'A'));
1772 $this->assertTrue($cache->set('b', 'B'));
1773 $this->assertTrue($cache->set('c', 'C'));
1774 $this->assertTrue($cache->set('d', 'D'));
1775 $this->assertTrue($cache->set('e', 'E'));
1776 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1777 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1778 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1779 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1780 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1782 /** @var cache_phpunit_application $cache */
1783 $cache = cache::make('phpunit', 'accelerated2');
1784 $this->assertInstanceOf('cache_phpunit_application', $cache);
1786 // Check that the array holds the last accessed items by get/set.
1787 $this->assertTrue($cache->set('a', 'A'));
1788 $this->assertTrue($cache->set('b', 'B'));
1789 $this->assertTrue($cache->set('c', 'C'));
1790 $this->assertTrue($cache->set('d', 'D'));
1791 $this->assertTrue($cache->set('e', 'E'));
1792 // Current keys in the array: c, d, e.
1793 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1794 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1795 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1796 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1797 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1799 $this->assertEquals('A', $cache->get('a'));
1800 // Current keys in the array: d, e, a.
1801 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1802 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1803 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1804 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1805 $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1807 // Current keys in the array: d, e, a.
1808 $this->assertEquals(array('c' => 'C'), $cache->get_many(array('c')));
1809 // Current keys in the array: e, a, c.
1810 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1811 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1812 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1813 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1814 $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1817 $cache = cache::make('phpunit', 'accelerated3');
1818 $this->assertInstanceOf('cache_phpunit_application', $cache);
1820 // Check that the array holds the last accessed items by get/set.
1821 $this->assertTrue($cache->set('a', 'A'));
1822 $this->assertTrue($cache->set('b', 'B'));
1823 $this->assertTrue($cache->set('c', 'C'));
1824 $this->assertTrue($cache->set('d', 'D'));
1825 $this->assertTrue($cache->set('e', 'E'));
1826 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1827 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1828 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1829 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1830 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1832 $this->assertTrue($cache->set('b', 'B2'));
1833 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1834 $this->assertEquals('B2', $cache->phpunit_static_acceleration_get('b'));
1835 $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1836 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1837 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1839 $this->assertEquals(2, $cache->set_many(array('b' => 'B3', 'c' => 'C3')));
1840 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1841 $this->assertEquals('B3', $cache->phpunit_static_acceleration_get('b'));
1842 $this->assertEquals('C3', $cache->phpunit_static_acceleration_get('c'));
1843 $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1844 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1846 $cache = cache::make('phpunit', 'accelerated4');
1847 $this->assertInstanceOf('cache_phpunit_application', $cache);
1848 $this->assertTrue($cache->set('a', 'A'));
1849 $this->assertTrue($cache->set('a', 'A'));
1850 $this->assertTrue($cache->set('a', 'A'));
1851 $this->assertTrue($cache->set('a', 'A'));
1852 $this->assertTrue($cache->set('a', 'A'));
1853 $this->assertTrue($cache->set('a', 'A'));
1854 $this->assertTrue($cache->set('a', 'A'));
1855 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1856 $this->assertEquals('A', $cache->get('a'));
1858 // Setting simpledata to false objects are cloned when retrieving data.
1859 $cache = cache::make('phpunit', 'simpledataarea1');
1860 $notreallysimple = new stdClass();
1861 $notreallysimple->name = 'a';
1862 $cache->set('a', $notreallysimple);
1863 $returnedinstance1 = $cache->get('a');
1864 $returnedinstance2 = $cache->get('a');
1865 $returnedinstance1->name = 'b';
1866 $this->assertEquals('a', $returnedinstance2->name);
1868 // Setting simpledata to true we assume that data does not contain references.
1869 $cache = cache::make('phpunit', 'simpledataarea2');
1870 $notreallysimple = new stdClass();
1871 $notreallysimple->name = 'a';
1872 $cache->set('a', $notreallysimple);
1873 $returnedinstance1 = $cache->get('a');
1874 $returnedinstance2 = $cache->get('a');
1875 $returnedinstance1->name = 'b';
1876 $this->assertEquals('b', $returnedinstance2->name);
1879 public function test_performance_debug() {
1880 global $CFG;
1881 $this->resetAfterTest(true);
1882 $CFG->perfdebug = 15;
1884 $instance = cache_config_testing::instance();
1885 $applicationid = 'phpunit/applicationperf';
1886 $instance->phpunit_add_definition($applicationid, array(
1887 'mode' => cache_store::MODE_APPLICATION,
1888 'component' => 'phpunit',
1889 'area' => 'applicationperf'
1891 $sessionid = 'phpunit/sessionperf';
1892 $instance->phpunit_add_definition($sessionid, array(
1893 'mode' => cache_store::MODE_SESSION,
1894 'component' => 'phpunit',
1895 'area' => 'sessionperf'
1897 $requestid = 'phpunit/requestperf';
1898 $instance->phpunit_add_definition($requestid, array(
1899 'mode' => cache_store::MODE_REQUEST,
1900 'component' => 'phpunit',
1901 'area' => 'requestperf'
1904 $application = cache::make('phpunit', 'applicationperf');
1905 $session = cache::make('phpunit', 'sessionperf');
1906 $request = cache::make('phpunit', 'requestperf');
1908 // Check that no stats are recorded for these definitions yet.
1909 $stats = cache_helper::get_stats();
1910 $this->assertArrayNotHasKey($applicationid, $stats);
1911 $this->assertArrayHasKey($sessionid, $stats); // Session cache sets a key on construct.
1912 $this->assertArrayNotHasKey($requestid, $stats);
1914 // Check that stores register misses.
1915 $this->assertFalse($application->get('missMe'));
1916 $this->assertFalse($application->get('missMe'));
1917 $this->assertFalse($session->get('missMe'));
1918 $this->assertFalse($session->get('missMe'));
1919 $this->assertFalse($session->get('missMe'));
1920 $this->assertFalse($request->get('missMe'));
1921 $this->assertFalse($request->get('missMe'));
1922 $this->assertFalse($request->get('missMe'));
1923 $this->assertFalse($request->get('missMe'));
1925 $endstats = cache_helper::get_stats();
1926 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['misses']);
1927 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['hits']);
1928 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets']);
1929 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['misses']);
1930 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['hits']);
1931 $this->assertEquals(1, $endstats[$sessionid]['stores']['cachestore_session']['sets']);
1932 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['misses']);
1933 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['hits']);
1934 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets']);
1936 $startstats = cache_helper::get_stats();
1938 // Check that stores register sets.
1939 $this->assertTrue($application->set('setMe1', 1));
1940 $this->assertTrue($application->set('setMe2', 2));
1941 $this->assertTrue($session->set('setMe1', 1));
1942 $this->assertTrue($session->set('setMe2', 2));
1943 $this->assertTrue($session->set('setMe3', 3));
1944 $this->assertTrue($request->set('setMe1', 1));
1945 $this->assertTrue($request->set('setMe2', 2));
1946 $this->assertTrue($request->set('setMe3', 3));
1947 $this->assertTrue($request->set('setMe4', 4));
1949 $endstats = cache_helper::get_stats();
1950 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] -
1951 $startstats[$applicationid]['stores']['cachestore_file']['misses']);
1952 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['hits'] -
1953 $startstats[$applicationid]['stores']['cachestore_file']['hits']);
1954 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['sets'] -
1955 $startstats[$applicationid]['stores']['cachestore_file']['sets']);
1956 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] -
1957 $startstats[$sessionid]['stores']['cachestore_session']['misses']);
1958 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['hits'] -
1959 $startstats[$sessionid]['stores']['cachestore_session']['hits']);
1960 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['sets'] -
1961 $startstats[$sessionid]['stores']['cachestore_session']['sets']);
1962 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] -
1963 $startstats[$requestid]['stores']['cachestore_static']['misses']);
1964 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['hits'] -
1965 $startstats[$requestid]['stores']['cachestore_static']['hits']);
1966 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['sets'] -
1967 $startstats[$requestid]['stores']['cachestore_static']['sets']);
1969 $startstats = cache_helper::get_stats();
1971 // Check that stores register hits.
1972 $this->assertEquals($application->get('setMe1'), 1);
1973 $this->assertEquals($application->get('setMe2'), 2);
1974 $this->assertEquals($session->get('setMe1'), 1);
1975 $this->assertEquals($session->get('setMe2'), 2);
1976 $this->assertEquals($session->get('setMe3'), 3);
1977 $this->assertEquals($request->get('setMe1'), 1);
1978 $this->assertEquals($request->get('setMe2'), 2);
1979 $this->assertEquals($request->get('setMe3'), 3);
1980 $this->assertEquals($request->get('setMe4'), 4);
1982 $endstats = cache_helper::get_stats();
1983 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] -
1984 $startstats[$applicationid]['stores']['cachestore_file']['misses']);
1985 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['hits'] -
1986 $startstats[$applicationid]['stores']['cachestore_file']['hits']);
1987 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets'] -
1988 $startstats[$applicationid]['stores']['cachestore_file']['sets']);
1989 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] -
1990 $startstats[$sessionid]['stores']['cachestore_session']['misses']);
1991 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['hits'] -
1992 $startstats[$sessionid]['stores']['cachestore_session']['hits']);
1993 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['sets'] -
1994 $startstats[$sessionid]['stores']['cachestore_session']['sets']);
1995 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] -
1996 $startstats[$requestid]['stores']['cachestore_static']['misses']);
1997 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['hits'] -
1998 $startstats[$requestid]['stores']['cachestore_static']['hits']);
1999 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets'] -
2000 $startstats[$requestid]['stores']['cachestore_static']['sets']);
2002 $startstats = cache_helper::get_stats();
2004 // Check that stores register through get_many.
2005 $application->get_many(array('setMe1', 'setMe2'));
2006 $session->get_many(array('setMe1', 'setMe2', 'setMe3'));
2007 $request->get_many(array('setMe1', 'setMe2', 'setMe3', 'setMe4'));
2009 $endstats = cache_helper::get_stats();
2010 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] -
2011 $startstats[$applicationid]['stores']['cachestore_file']['misses']);
2012 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['hits'] -
2013 $startstats[$applicationid]['stores']['cachestore_file']['hits']);
2014 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets'] -
2015 $startstats[$applicationid]['stores']['cachestore_file']['sets']);
2016 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] -
2017 $startstats[$sessionid]['stores']['cachestore_session']['misses']);
2018 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['hits'] -
2019 $startstats[$sessionid]['stores']['cachestore_session']['hits']);
2020 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['sets'] -
2021 $startstats[$sessionid]['stores']['cachestore_session']['sets']);
2022 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] -
2023 $startstats[$requestid]['stores']['cachestore_static']['misses']);
2024 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['hits'] -
2025 $startstats[$requestid]['stores']['cachestore_static']['hits']);
2026 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets'] -
2027 $startstats[$requestid]['stores']['cachestore_static']['sets']);