From 69169590dafd5fc522ea2d168223f428fb78699d Mon Sep 17 00:00:00 2001 From: Vyacheslav Agafonov Date: Wed, 30 Nov 2011 13:29:48 +0400 Subject: [PATCH] added setExpectedException and check empty($search) --- redis/RedisDebug.php | 6 ++++++ redis/RedisManager.php | 12 ++++++++---- tests/redis/RedisDebugTest.php | 4 +++- tests/redis/RedisManagerTest.php | 33 +++++++++++++++++++-------------- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/redis/RedisDebug.php b/redis/RedisDebug.php index 692f0ea..5875210 100644 --- a/redis/RedisDebug.php +++ b/redis/RedisDebug.php @@ -25,6 +25,12 @@ class RedisDebug { $command = $this->r_implode(', ', $arguments); $profiler = Profiler::getInstance()->profilerCommand('Redis->' . $name, $command); + + $search = array_search($name, get_class_methods($this->redis)); + + if (empty($search)) { + throw new GeneralException('undefined method:'.$name); + } $data = call_user_func_array(array($this->redis, $name), $arguments); $profiler->end(); return $data; diff --git a/redis/RedisManager.php b/redis/RedisManager.php index 4c95802..3bfc30d 100644 --- a/redis/RedisManager.php +++ b/redis/RedisManager.php @@ -31,11 +31,15 @@ class RedisManager { if (!isset(self::$connections[$name])) { if (!$config) { - $config = Config::get('Redis')->$name; + + if (gettype(Config::get('Redis')) != 'object') { + throw new GeneralException('config non-object'); + } + $config = Config::get('Redis')->$name; } if (!is_array($config)) { - throw new Exception('Connection parameters must be an array'); + throw new GeneralException('Connection parameters must be an array'); } $host = isset($config['host']) ? $config['host'] : 'localhost'; @@ -50,11 +54,11 @@ class RedisManager $connection = new RedisDebug($connection); } if (!$connection->connect($host, $port)) { - throw new Exception('Failed to connect to Redis server at ' . $host . ':' . $port); + throw new GeneralException('Failed to connect to Redis server at ' . $host . ':' . $port); } if ($database) { if (!$connection->select($database)) { - throw new Exception('Failed to select Redis database with index ' . $database); + throw new GeneralException('Failed to select Redis database with index ' . $database); } } self::$connections[$name] = $connection; diff --git a/tests/redis/RedisDebugTest.php b/tests/redis/RedisDebugTest.php index 8b1fc5f..752c51f 100644 --- a/tests/redis/RedisDebugTest.php +++ b/tests/redis/RedisDebugTest.php @@ -22,6 +22,7 @@ class RedisDebugTest extends PHPUnit_Framework_TestCase public function run(PHPUnit_Framework_TestResult $result = NULL) { $this->setPreserveGlobalState(false); + return parent::run($result); } @@ -85,7 +86,6 @@ class RedisDebugTest extends PHPUnit_Framework_TestCase } /** - * @expectedException GeneralException * @expectedExceptionMessage call_user_func_array() expects parameter 1 to be a valid callback * @runInSeparateProcess */ @@ -94,8 +94,10 @@ class RedisDebugTest extends PHPUnit_Framework_TestCase if (!defined('DEBUG')) { define('DEBUG', true); } + $mock = $this->getMock('Redis', array('connect')); $redisDebug = new RedisDebug($mock); + $this->setExpectedException('GeneralException'); $this->assertNull($redisDebug->nothing('localhost', 4322)); } } \ No newline at end of file diff --git a/tests/redis/RedisManagerTest.php b/tests/redis/RedisManagerTest.php index 9f56dbf..5edf851 100644 --- a/tests/redis/RedisManagerTest.php +++ b/tests/redis/RedisManagerTest.php @@ -13,9 +13,9 @@ require_once dirname(__FILE__) . '/../../Registry.php'; require_once dirname(__FILE__) . '/../../Config.php'; require_once dirname(__FILE__) . '/../../redis/RedisManager.php'; +require_once dirname(__FILE__) . '/../../exception/InitializationException.php'; require_once dirname(__FILE__) . '/../../exception/GeneralException.php'; - class RedisManagerTest extends PHPUnit_Framework_TestCase { @@ -46,65 +46,70 @@ class RedisManagerTest extends PHPUnit_Framework_TestCase } /** - * @expectedException GeneralException * @expectedExceptionMessage Trying to get property of non-object - * @TODO: line 34: $config = Config::get('Redis')->$name; - check for Redis config existence + * @TODO: line 34: $config = Config::get('Redis')->$name; - check for Redis config existence. Agafonov: 'check added' */ public function testConnectNoAnyConfig() { + //$this->setExpectedException('GeneralException'); + $this->setExpectedException('GeneralException'); RedisManager::connect(); } /** - * @expectedException GeneralException - * @expectedExceptionMessage Connection parameters must be an array + * @expected Exception GeneralException + * @expected ExceptionMessage Connection parameters must be an array */ public function testConnectWrongPersistantConfig() { + $this->setExpectedException('GeneralException'); Config::set('Redis', array('new' => 'some')); RedisManager::connect('new'); } /** - * @expectedException GeneralException - * @expectedExceptionMessage Connection parameters must be an array + * @expected Exception GeneralException + * @expected ExceptionMessage Connection parameters must be an array */ public function testConnectDefaultConfig() { + $this->setExpectedException('GeneralException'); Config::set('Redis', array('default' => 'some')); RedisManager::connect(); } /** - * @expectedException GeneralException - * @expectedExceptionMessage Failed to connect to Redis server at + * @expected Exception GeneralException + * @expected ExceptionMessage Failed to connect to Redis server at */ public function testConnectFailedConnection() { + $this->setExpectedException('GeneralException'); Config::set('Redis', array('new' => array('host' => 'error', 'port' => 2023, 'database' => 'some'))); RedisManager::connect('new'); } /** - * @expectedException GeneralException - * @expectedExceptionMessage Failed to select Redis database with index + * @expected Exception GeneralException + * @expected ExceptionMessage Failed to select Redis database with index */ public function testConnectEstablishedConnectionNoDb() { + $this->setExpectedException('GeneralException'); Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => 'some'))); RedisManager::connect('new'); } + public function testConnectionGood() { + // $this->setExpectedException('GeneralException'); Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => true))); $redis = RedisManager::connect('new'); $this->assertInstanceOf('RedisMock', $redis); } - /** - * @runInSeparateProcess - */ + public function testConnectWithDebug() { if (!defined('DEBUG')) {