/** * Test the maxsize option. */ public function test_maxsize() { $defid = 'phpunit/testmaxsize'; $config = cache_config_phpunittest::instance(); $config->phpunit_add_definition($defid, array('mode' => cache_store::MODE_REQUEST, 'component' => 'phpunit', 'area' => 'testmaxsize', 'maxsize' => 3)); $definition = cache_definition::load($defid, $config->get_definition_by_id($defid)); $instance = cachestore_static::initialise_test_instance($definition); $this->assertTrue($instance->set('key1', 'value1')); $this->assertTrue($instance->set('key2', 'value2')); $this->assertTrue($instance->set('key3', 'value3')); $this->assertTrue($instance->has('key1')); $this->assertTrue($instance->has('key2')); $this->assertTrue($instance->has('key3')); $this->assertTrue($instance->set('key4', 'value4')); $this->assertTrue($instance->set('key5', 'value5')); $this->assertFalse($instance->has('key1')); $this->assertFalse($instance->has('key2')); $this->assertTrue($instance->has('key3')); $this->assertTrue($instance->has('key4')); $this->assertTrue($instance->has('key5')); $this->assertFalse($instance->get('key1')); $this->assertFalse($instance->get('key2')); $this->assertEquals('value3', $instance->get('key3')); $this->assertEquals('value4', $instance->get('key4')); $this->assertEquals('value5', $instance->get('key5')); // Test adding one more. $this->assertTrue($instance->set('key6', 'value6')); $this->assertFalse($instance->get('key3')); // Test reducing and then adding to make sure we don't lost one. $this->assertTrue($instance->delete('key6')); $this->assertTrue($instance->set('key7', 'value7')); $this->assertEquals('value4', $instance->get('key4')); // Set the same key three times to make sure it doesn't count overrides. for ($i = 0; $i < 3; $i++) { $this->assertTrue($instance->set('key8', 'value8')); } $this->assertEquals('value7', $instance->get('key7'), 'Overrides are incorrectly incrementing size'); // Test adding many. $this->assertEquals(3, $instance->set_many(array(array('key' => 'keyA', 'value' => 'valueA'), array('key' => 'keyB', 'value' => 'valueB'), array('key' => 'keyC', 'value' => 'valueC')))); $this->assertEquals(array('key4' => false, 'key5' => false, 'key6' => false, 'key7' => false, 'keyA' => 'valueA', 'keyB' => 'valueB', 'keyC' => 'valueC'), $instance->get_many(array('key4', 'key5', 'key6', 'key7', 'keyA', 'keyB', 'keyC'))); }
/** * Test that multiple loaders work ok. */ public function test_multiple_loaders() { $instance = cache_config_phpunittest::instance(true); $instance->phpunit_add_file_store('phpunittest1'); $instance->phpunit_add_file_store('phpunittest2'); $instance->phpunit_add_definition('phpunit/multi_loader', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'multi_loader')); $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3); $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2); $cache = cache::make('phpunit', 'multi_loader'); $this->assertInstanceOf('cache_application', $cache); $this->assertFalse($cache->get('test')); $this->assertTrue($cache->set('test', 'test')); $this->assertEquals('test', $cache->get('test')); }
/** * Test the hash_key functionality. */ public function test_hash_key() { global $CFG; $currentdebugging = $CFG->debug; $CFG->debug = E_ALL; // First with simplekeys $instance = cache_config_phpunittest::instance(true); $instance->phpunit_add_definition('phpunit/hashtest', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'hashtest', 'simplekeys' => true)); $factory = cache_factory::instance(); $definition = $factory->create_definition('phpunit', 'hashtest'); $result = cache_helper::hash_key('test', $definition); $this->assertEquals('test-' . $definition->generate_single_key_prefix(), $result); try { cache_helper::hash_key('test/test', $definition); $this->fail('Invalid key was allowed, you should see this.'); } catch (coding_exception $e) { $this->assertEquals('test/test', $e->debuginfo); } // Second without simple keys $instance->phpunit_add_definition('phpunit/hashtest2', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'hashtest2', 'simplekeys' => false)); $definition = $factory->create_definition('phpunit', 'hashtest2'); $result = cache_helper::hash_key('test', $definition); $this->assertEquals(sha1($definition->generate_single_key_prefix() . '-test'), $result); $result = cache_helper::hash_key('test/test', $definition); $this->assertEquals(sha1($definition->generate_single_key_prefix() . '-test/test'), $result); $CFG->debug = $currentdebugging; }
public function test_static_acceleration() { $instance = cache_config_phpunittest::instance(); $instance->phpunit_add_definition('phpunit/accelerated', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'accelerated', 'staticacceleration' => true, 'staticaccelerationsize' => 3)); $instance->phpunit_add_definition('phpunit/accelerated2', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'accelerated2', 'staticacceleration' => true, 'staticaccelerationsize' => 3)); $instance->phpunit_add_definition('phpunit/accelerated3', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'accelerated3', 'staticacceleration' => true, 'staticaccelerationsize' => 3)); $instance->phpunit_add_definition('phpunit/accelerated4', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'accelerated4', 'staticacceleration' => true, 'staticaccelerationsize' => 4)); $cache = cache::make('phpunit', 'accelerated'); $this->assertInstanceOf('cache_phpunit_application', $cache); // Set and get three elements. $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('b', 'B')); $this->assertTrue($cache->set('c', 'C')); $this->assertEquals('A', $cache->get('a')); $this->assertEquals(array('b' => 'B', 'c' => 'C'), $cache->get_many(array('b', 'c'))); // Make sure all items are in static acceleration array. $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a')); $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b')); $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); // Add new value and make sure it is in cache and it is in array. $this->assertTrue($cache->set('d', 'D')); $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); $this->assertEquals('D', $cache->get('d')); // Now the least recent accessed item (a) is no longer in acceleration array. $this->assertFalse($cache->phpunit_static_acceleration_get('a')); $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b')); $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); // Adding and deleting element. $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->delete('a')); $this->assertFalse($cache->phpunit_static_acceleration_get('a')); $this->assertFalse($cache->has('a')); // Make sure "purge" deletes from the array as well. $cache->purge(); $this->assertFalse($cache->phpunit_static_acceleration_get('a')); $this->assertFalse($cache->phpunit_static_acceleration_get('b')); $this->assertFalse($cache->phpunit_static_acceleration_get('c')); $this->assertFalse($cache->phpunit_static_acceleration_get('d')); $this->assertFalse($cache->phpunit_static_acceleration_get('e')); // Check that the array holds the last accessed items by get/set. $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('b', 'B')); $this->assertTrue($cache->set('c', 'C')); $this->assertTrue($cache->set('d', 'D')); $this->assertTrue($cache->set('e', 'E')); $this->assertFalse($cache->phpunit_static_acceleration_get('a')); $this->assertFalse($cache->phpunit_static_acceleration_get('b')); $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); /** @var cache_phpunit_application $cache */ $cache = cache::make('phpunit', 'accelerated2'); $this->assertInstanceOf('cache_phpunit_application', $cache); // Check that the array holds the last accessed items by get/set. $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('b', 'B')); $this->assertTrue($cache->set('c', 'C')); $this->assertTrue($cache->set('d', 'D')); $this->assertTrue($cache->set('e', 'E')); // Current keys in the array: c, d, e. $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); $this->assertFalse($cache->phpunit_static_acceleration_get('a')); $this->assertFalse($cache->phpunit_static_acceleration_get('b')); $this->assertEquals('A', $cache->get('a')); // Current keys in the array: d, e, a. $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a')); $this->assertFalse($cache->phpunit_static_acceleration_get('b')); $this->assertFalse($cache->phpunit_static_acceleration_get('c')); // Current keys in the array: d, e, a. $this->assertEquals(array('c' => 'C'), $cache->get_many(array('c'))); // Current keys in the array: e, a, c. $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a')); $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); $this->assertFalse($cache->phpunit_static_acceleration_get('b')); $this->assertFalse($cache->phpunit_static_acceleration_get('d')); $cache = cache::make('phpunit', 'accelerated3'); $this->assertInstanceOf('cache_phpunit_application', $cache); // Check that the array holds the last accessed items by get/set. $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('b', 'B')); $this->assertTrue($cache->set('c', 'C')); $this->assertTrue($cache->set('d', 'D')); $this->assertTrue($cache->set('e', 'E')); $this->assertFalse($cache->phpunit_static_acceleration_get('a')); $this->assertFalse($cache->phpunit_static_acceleration_get('b')); $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); $this->assertTrue($cache->set('b', 'B2')); $this->assertFalse($cache->phpunit_static_acceleration_get('a')); $this->assertEquals('B2', $cache->phpunit_static_acceleration_get('b')); $this->assertFalse($cache->phpunit_static_acceleration_get('c')); $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); $this->assertEquals(2, $cache->set_many(array('b' => 'B3', 'c' => 'C3'))); $this->assertFalse($cache->phpunit_static_acceleration_get('a')); $this->assertEquals('B3', $cache->phpunit_static_acceleration_get('b')); $this->assertEquals('C3', $cache->phpunit_static_acceleration_get('c')); $this->assertFalse($cache->phpunit_static_acceleration_get('d')); $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); $cache = cache::make('phpunit', 'accelerated4'); $this->assertInstanceOf('cache_phpunit_application', $cache); $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('a', 'A')); $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a')); $this->assertEquals('A', $cache->get('a')); }
/** * Test the use of an alt path. * If we can generate a config instance we are done :) */ public function test_alt_cache_path() { global $CFG; $this->resetAfterTest(); $CFG->altcacheconfigpath = $CFG->dataroot . '/cache/altcacheconfigpath'; $instance = cache_config_phpunittest::instance(); $this->assertInstanceOf('cache_config', $instance); }
/** * Test purge routines. */ public function test_purge_routines() { $instance = cache_config_phpunittest::instance(true); $instance->phpunit_add_definition('phpunit/purge1', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'purge1')); $instance->phpunit_add_definition('phpunit/purge2', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'purge2', 'requireidentifiers' => array('id'))); $factory = cache_factory::instance(); $definition = $factory->create_definition('phpunit', 'purge1'); $this->assertFalse($definition->has_required_identifiers()); $cache = $factory->create_cache($definition); $this->assertInstanceOf('cache_application', $cache); $this->assertTrue($cache->set('test', 'test')); $this->assertTrue($cache->has('test')); cache_helper::purge_by_definition('phpunit', 'purge1'); $this->assertFalse($cache->has('test')); $factory = cache_factory::instance(); $definition = $factory->create_definition('phpunit', 'purge2'); $this->assertTrue($definition->has_required_identifiers()); $cache = $factory->create_cache($definition); $this->assertInstanceOf('cache_application', $cache); $this->assertTrue($cache->set('test', 'test')); $this->assertTrue($cache->has('test')); cache_helper::purge_stores_used_by_definition('phpunit', 'purge2'); $this->assertFalse($cache->has('test')); try { cache_helper::purge_by_definition('phpunit', 'purge2'); $this->fail('Should not be able to purge a definition required identifiers without providing them.'); } catch (coding_exception $ex) { $this->assertContains('Identifier required for cache has not been provided', $ex->getMessage()); } }
public function test_ttl() { $config = cache_config_phpunittest::instance(); $config->phpunit_add_definition('phpunit/three', array('mode' => cache_store::MODE_SESSION, 'component' => 'phpunit', 'area' => 'three', 'maxsize' => 3, 'ttl' => 3)); $cachethree = cache::make('phpunit', 'three'); // Make sure that when cache with ttl is full the elements that were added first are deleted first regardless of access time. $cachethree->set('key1', 'value1'); $cachethree->set('key2', 'value2'); $cachethree->set('key3', 'value3'); $cachethree->set('key4', 'value4'); $this->assertFalse($cachethree->get('key1')); $this->assertEquals('value4', $cachethree->get('key4')); $cachethree->set('key5', 'value5'); $this->assertFalse($cachethree->get('key2')); $this->assertEquals('value4', $cachethree->get('key4')); $cachethree->set_many(array('key6' => 'value6', 'key7' => 'value7')); $this->assertEquals(array('key3' => false, 'key4' => false, 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7'), $cachethree->get_many(array('key3', 'key4', 'key5', 'key6', 'key7'))); }
/** * Test setting some definition mappings. */ public function test_set_definition_mappings() { $config = cache_config_phpunittest::instance(true); $config->phpunit_add_definition('phpunit/testdefinition', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'testdefinition')); $config = cache_config_writer::instance(); $this->assertTrue($config->add_store_instance('setdefinitiontest', 'file')); $this->assertInternalType('array', $config->get_definition_by_id('phpunit/testdefinition')); $config->set_definition_mappings('phpunit/testdefinition', array('setdefinitiontest', 'default_application')); try { $config->set_definition_mappings('phpunit/testdefinition', array('something that does not exist')); $this->fail('You should not be able to set a mapping for a store that does not exist.'); } catch (Exception $e) { $this->assertInstanceOf('coding_exception', $e); } try { $config->set_definition_mappings('something/crazy', array('setdefinitiontest')); $this->fail('You should not be able to set a mapping for a definition that does not exist.'); } catch (Exception $e) { $this->assertInstanceOf('coding_exception', $e); } }