Skip to content

Commit

Permalink
Expose engines
Browse files Browse the repository at this point in the history
By implementing the `engine` method just like in version 3
  • Loading branch information
tersmitten committed Nov 11, 2016
1 parent f46f042 commit 175503f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/Cake/Cache/Cache.php
Expand Up @@ -616,4 +616,19 @@ public static function add($key, $value, $config = 'default') {
self::set(null, $config);
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;
}

}
60 changes: 60 additions & 0 deletions lib/Cake/Test/Case/Cache/CacheTest.php
Expand Up @@ -538,4 +538,64 @@ public function testAdd() {
$result = Cache::add('test_add_key', 'test data 2', 'default');
$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);
}

}

0 comments on commit 175503f

Please sign in to comment.