Skip to content

Commit

Permalink
Fix memcached tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Sep 6, 2013
1 parent ef59d04 commit c2171e8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
15 changes: 10 additions & 5 deletions lib/Cake/Cache/Engine/MemcachedEngine.php
Expand Up @@ -12,6 +12,12 @@
* @since CakePHP(tm) v 2.5.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Cache\Engine;

use Cake\Cache\CacheEngine;
use Cake\Error;
use Cake\Utility\Inflector;
use \Memcached;

/**
* Memcached storage engine for cache. Memcached has some limitations in the amount of
Expand All @@ -23,7 +29,6 @@
* (if memcached extension compiled with --enable-igbinary)
* Compressed keys can also be incremented/decremented
*
* @package Cake.Cache.Engine
*/
class MemcachedEngine extends CacheEngine {

Expand Down Expand Up @@ -55,7 +60,7 @@ class MemcachedEngine extends CacheEngine {
*
* @param array $settings array of setting for the engine
* @return boolean True if the engine has been successfully initialized, false if not
* @throws CacheException when you try use authentication without Memcached compiled with SASL support
* @throws Cake\Error\Exception when you try use authentication without Memcached compiled with SASL support
*/
public function init($settings = array()) {
if (!class_exists('Memcached')) {
Expand Down Expand Up @@ -100,7 +105,7 @@ public function init($settings = array()) {

if ($this->settings['login'] !== null && $this->settings['password'] !== null) {
if (!method_exists($this->_Memcached, 'setSaslAuthData')) {
throw new CacheException(
throw new Error\Exception(
__d('cake_dev', 'Memcached extension is not build with SASL support')
);
}
Expand Down Expand Up @@ -192,7 +197,7 @@ public function read($key) {
* @param string $key Identifier for the data
* @param integer $offset How much to increment
* @return New incremented value, false otherwise
* @throws CacheException when you try to increment with compress = true
* @throws Cake\Error\Exception when you try to increment with compress = true
*/
public function increment($key, $offset = 1) {
return $this->_Memcached->increment($key, $offset);
Expand All @@ -204,7 +209,7 @@ public function increment($key, $offset = 1) {
* @param string $key Identifier for the data
* @param integer $offset How much to subtract
* @return New decremented value, false otherwise
* @throws CacheException when you try to decrement with compress = true
* @throws Cake\Error\Exception when you try to decrement with compress = true
*/
public function decrement($key, $offset = 1) {
return $this->_Memcached->decrement($key, $offset);
Expand Down
53 changes: 30 additions & 23 deletions lib/Cake/Test/TestCase/Cache/Engine/MemcachedEngineTest.php
@@ -1,7 +1,5 @@
<?php
/**
* MemcachedEngineTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
Expand All @@ -13,18 +11,19 @@
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Cache.Engine
* @since CakePHP(tm) v 2.5.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Cache\Engine;

App::uses('Cache', 'Cache');
App::uses('MemcachedEngine', 'Cache/Engine');
use Cake\Cache\Cache;
use Cake\Cache\Engine\MemcachedEngine;
use Cake\TestSuite\TestCase;
use \Memcached;

/**
* Class TestMemcachedEngine
*
* @package Cake.Test.Case.Cache.Engine
*/
class TestMemcachedEngine extends MemcachedEngine {

Expand All @@ -51,9 +50,8 @@ public function getMemcached() {
/**
* MemcachedEngineTest class
*
* @package Cake.Test.Case.Cache.Engine
*/
class MemcachedEngineTest extends CakeTestCase {
class MemcachedEngineTest extends TestCase {

/**
* setUp method
Expand All @@ -64,11 +62,22 @@ public function setUp() {
parent::setUp();
$this->skipIf(!class_exists('Memcached'), 'Memcached is not installed or configured properly.');

Cache::config('memcached', array(
'engine' => 'Memcached',
$this->_configCache();
}

/**
* Helper method for testing.
*
* @return void
*/
protected function _configCache($settings = []) {
$defaults = [
'className' => 'Memcached',
'prefix' => 'cake_',
'duration' => 3600
));
];
Cache::drop('memcached');
Cache::config('memcached', array_merge($defaults, $settings));
}

/**
Expand All @@ -79,9 +88,12 @@ public function setUp() {
public function tearDown() {
parent::tearDown();
Cache::drop('memcached');
Cache::drop('memcached2');
Cache::drop('memcached_groups');
Cache::drop('memcached_helper');
Cache::config('default');
Cache::drop('compressed_memcached');
Cache::drop('long_memcached');
Cache::drop('short_memcached');
}

/**
Expand Down Expand Up @@ -154,7 +166,7 @@ public function testSaslAuthException() {
);

$this->setExpectedException(
'CacheException', 'Memcached extension is not build with SASL support'
'Cake\Error\Exception', 'Memcached extension is not build with SASL support'
);
$Memcached->init($settings);
}
Expand Down Expand Up @@ -237,7 +249,7 @@ public function testParseServerStringUnix() {
* @return void
*/
public function testReadAndWriteCache() {
Cache::set(array('duration' => 1), null, 'memcached');
$this->_configCache(['duration' => 1]);

$result = Cache::read('test', 'memcached');
$expecting = '';
Expand All @@ -260,7 +272,7 @@ public function testReadAndWriteCache() {
* @return void
*/
public function testExpiry() {
Cache::set(array('duration' => 1), 'memcached');
$this->_configCache(['duration' => 1]);

$result = Cache::read('test', 'memcached');
$this->assertFalse($result);
Expand All @@ -273,7 +285,7 @@ public function testExpiry() {
$result = Cache::read('other_test', 'memcached');
$this->assertFalse($result);

Cache::set(array('duration' => "+1 second"), 'memcached');
$this->_configCache(['duration' => '+1 second']);

$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 'memcached');
Expand All @@ -283,12 +295,10 @@ public function testExpiry() {
$result = Cache::read('other_test', 'memcached');
$this->assertFalse($result);

Cache::config('memcached', array('duration' => '+1 second'));

$result = Cache::read('other_test', 'memcached');
$this->assertFalse($result);

Cache::config('memcached', array('duration' => '+29 days'));
$this->_configCache(['duration' => '+29 days']);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('long_expiry_test', $data, 'memcached');
$this->assertTrue($result);
Expand All @@ -297,8 +307,6 @@ public function testExpiry() {
$result = Cache::read('long_expiry_test', 'memcached');
$expecting = $data;
$this->assertEquals($expecting, $result);

Cache::config('memcached', array('duration' => 3600));
}

/**
Expand Down Expand Up @@ -441,7 +449,6 @@ public function testConfigurationConflict() {
'duration' => '+1 seconds',
'servers' => array('127.0.0.1:11211'),
));
Cache::config('some_file', array('engine' => 'File'));

$this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcached'));
$this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcached'));
Expand Down Expand Up @@ -492,7 +499,7 @@ public function testClear() {
* @return void
*/
public function testZeroDuration() {
Cache::config('memcached', array('duration' => 0));
$this->_configCache(['duration' => 0]);
$result = Cache::write('test_key', 'written!', 'memcached');

$this->assertTrue($result);
Expand Down

0 comments on commit c2171e8

Please sign in to comment.