/** * Generates an instance of the cache store that can be used for testing. * * @param cache_definition $definition * @return cachestore_memcached|false */ public static function initialise_test_instance(cache_definition $definition) { if (!self::are_requirements_met()) { return false; } $config = get_config('cachestore_memcached'); if (empty($config->testservers)) { return false; } $configuration = array(); $configuration['servers'] = $config->testservers; if (!empty($config->testcompression)) { $configuration['compression'] = $config->testcompression; } if (!empty($config->testserialiser)) { $configuration['serialiser'] = $config->testserialiser; } if (!empty($config->testprefix)) { $configuration['prefix'] = $config->testprefix; } if (!empty($config->testhash)) { $configuration['hash'] = $config->testhash; } if (!empty($config->testbufferwrites)) { $configuration['bufferwrites'] = $config->testbufferwrites; } $store = new cachestore_memcached('Test memcached', $configuration); $store->initialise($definition); return $store; }
/** * Generates an instance of the cache store that can be used for testing. * * @param cache_definition $definition * @return cachestore_memcached|false */ public static function initialise_test_instance(cache_definition $definition) { if (!self::are_requirements_met()) { return false; } $config = get_config('cachestore_memcached'); if (empty($config->testservers)) { return false; } $configuration = array(); $configuration['servers'] = explode("\n", $config->testservers); if (!empty($config->testcompression)) { $configuration['compression'] = $config->testcompression; } if (!empty($config->testserialiser)) { $configuration['serialiser'] = $config->testserialiser; } if (!empty($config->testprefix)) { $configuration['prefix'] = $config->testprefix; } if (!empty($config->testhash)) { $configuration['hash'] = $config->testhash; } if (!empty($config->testbufferwrites)) { $configuration['bufferwrites'] = $config->testbufferwrites; } if (!empty($config->testclustered)) { $configuration['clustered'] = $config->testclustered; } if (!empty($config->testsetservers)) { $configuration['setservers'] = explode("\n", $config->testsetservers); } if (!empty($config->testname)) { $name = $config->testname; } else { $name = 'Test memcached'; } $store = new cachestore_memcached($name, $configuration); // If store is ready then only initialise. if ($store->is_ready()) { $store->initialise($definition); } return $store; }
/** * Creates a test instance for unit tests if possible. * @param cache_definition $definition * @return bool|cachestore_memcached */ public static function initialise_unit_test_instance(cache_definition $definition) { if (!self::are_requirements_met()) { return false; } if (!defined('TEST_CACHESTORE_MEMCACHED_TESTSERVERS')) { return false; } $configuration = array(); $configuration['servers'] = explode("\n", TEST_CACHESTORE_MEMCACHED_TESTSERVERS); $store = new cachestore_memcached('Test memcached', $configuration); $store->initialise($definition); return $store; }
/** * Tests the valid keys to ensure they work. */ public function test_valid_keys() { if (!cachestore_memcached::are_requirements_met() || !defined('TEST_CACHESTORE_MEMCACHED_TESTSERVERS')) { $this->markTestSkipped('Could not test cachestore_memcached. Requirements are not met.'); } $this->resetAfterTest(true); $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_memcached', 'phpunit_test'); $instance = new cachestore_memcached('Memcached Test', cachestore_memcached::unit_test_configuration()); if (!$instance->is_ready()) { // Something prevented memcached store to be inited (extension, TEST_CACHESTORE_MEMCACHED_TESTSERVERS...). $this->markTestSkipped(); } $instance->initialise($definition); $keys = array('abc', 'ABC', '123', 'aB1', '1aB', 'a-1', '1-a', '-a1', 'a1-', 'a_1', '1_a', '_a1', 'a1_'); // Set some keys. foreach ($keys as $key) { $this->assertTrue($instance->set($key, $key), "Failed to set key `{$key}`"); } // Get some keys. foreach ($keys as $key) { $this->assertEquals($key, $instance->get($key), "Failed to get key `{$key}`"); } // Try get many. $values = $instance->get_many($keys); foreach ($values as $key => $value) { $this->assertEquals($key, $value); } // Reset a key. $this->assertTrue($instance->set($keys[0], 'New'), "Failed to reset key `{$key}`"); $this->assertEquals('New', $instance->get($keys[0]), "Failed to get reset key `{$key}`"); // Delete and check that we can't retrieve. foreach ($keys as $key) { $this->assertTrue($instance->delete($key), "Failed to delete key `{$key}`"); $this->assertFalse($instance->get($key), "Retrieved deleted key `{$key}`"); } // Try set many, and check that count is correct. $many = array(); foreach ($keys as $key) { $many[] = array('key' => $key, 'value' => $key); } $returncount = $instance->set_many($many); $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match'); // Check keys retrieved with get_many. $values = $instance->get_many($keys); foreach ($keys as $key) { $this->assertTrue(isset($values[$key]), "Failed to get_many key `{$key}`"); $this->assertEquals($key, $values[$key], "Failed to match get_many key `{$key}`"); } // Delete many, make sure count matches. $returncount = $instance->delete_many($keys); $this->assertEquals(count($many), $returncount, 'Delete many count didn\'t match'); // Check that each key was deleted. foreach ($keys as $key) { $this->assertFalse($instance->get($key), "Retrieved many deleted key `{$key}`"); } // Set the keys again. $returncount = $instance->set_many($many); $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match'); // Purge. $this->assertTrue($instance->purge(), 'Failure to purge'); // Delete and check that we can't retrieve. foreach ($keys as $key) { $this->assertFalse($instance->get($key), "Retrieved purged key `{$key}`"); } }