From c2171e80e8aee91c1478cde21cde6ef53a67a635 Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 5 Sep 2013 00:09:39 -0400 Subject: [PATCH] Fix memcached tests. --- lib/Cake/Cache/Engine/MemcachedEngine.php | 15 ++++-- .../Cache/Engine/MemcachedEngineTest.php | 53 +++++++++++-------- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/lib/Cake/Cache/Engine/MemcachedEngine.php b/lib/Cake/Cache/Engine/MemcachedEngine.php index ad41f1d680f..ebceb939f81 100755 --- a/lib/Cake/Cache/Engine/MemcachedEngine.php +++ b/lib/Cake/Cache/Engine/MemcachedEngine.php @@ -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 @@ -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 { @@ -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')) { @@ -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') ); } @@ -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); @@ -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); diff --git a/lib/Cake/Test/TestCase/Cache/Engine/MemcachedEngineTest.php b/lib/Cake/Test/TestCase/Cache/Engine/MemcachedEngineTest.php index 7ca8e41555d..8971ff01fcd 100755 --- a/lib/Cake/Test/TestCase/Cache/Engine/MemcachedEngineTest.php +++ b/lib/Cake/Test/TestCase/Cache/Engine/MemcachedEngineTest.php @@ -1,7 +1,5 @@ @@ -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 { @@ -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 @@ -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)); } /** @@ -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'); } /** @@ -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); } @@ -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 = ''; @@ -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); @@ -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'); @@ -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); @@ -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)); } /** @@ -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')); @@ -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);