Expose engines

By implementing the `engine` method just like in version 3
This commit is contained in:
Mischa ter Smitten 2016-11-11 13:37:20 +01:00
parent f46f042001
commit 175503fafa
2 changed files with 75 additions and 0 deletions

View file

@ -616,4 +616,19 @@ class Cache {
self::set(null, $config); self::set(null, $config);
return $success; return $success;
} }
/**
* Fetch the engine attached to a specific configuration name.
*
* @param string $config Optional string configuration name to get an engine for. Defaults to 'default'.
* @return bool|CacheEngine False if the engine has not been initialized else the engine
*/
public static function engine($config = 'default') {
if (self::isInitialized($config)) {
return self::$_engines[$config];
}
return false;
}
} }

View file

@ -538,4 +538,64 @@ class CacheTest extends CakeTestCase {
$result = Cache::add('test_add_key', 'test data 2', 'default'); $result = Cache::add('test_add_key', 'test data 2', 'default');
$this->assertFalse($result); $this->assertFalse($result);
} }
/**
* Test engine method.
*
* Success, default engine.
*
* @return void
*/
public function testEngineSuccess() {
$actual = Cache::engine();
$this->assertIsA($actual, 'CacheEngine');
$actual = Cache::engine('default');
$this->assertIsA($actual, 'CacheEngine');
}
/**
* Test engine method.
*
* Success, memcached engine.
*
* @return void
*/
public function testEngineSuccessMemcached() {
$this->skipIf(!class_exists('Memcached'), 'Memcached is not installed or configured properly.');
// @codingStandardsIgnoreStart
$socket = @fsockopen('127.0.0.1', 11211, $errno, $errstr, 1);
// @codingStandardsIgnoreEnd
$this->skipIf(!$socket, 'Memcached is not running.');
fclose($socket);
Cache::config('memcached', array(
'engine' => 'Memcached',
'prefix' => 'cake_',
'duration' => 3600
));
$actual = Cache::engine('memcached');
$this->assertTrue($actual->add('test_add_key', 'test data', 10));
$this->assertFalse($actual->add('test_add_key', 'test data', 10));
$this->assertTrue($actual->delete('test_add_key'));
}
/**
* Test engine method.
*
* Failure.
*
* @return void
*/
public function testEngineFailure() {
$actual = Cache::engine('some_config_that_does_not_exist');
$this->assertFalse($actual);
Configure::write('Cache.disable', true);
$actual = Cache::engine();
$this->assertFalse($actual);
}
} }