Browse Source

added setExpectedException and check empty($search)

master
Vyacheslav Agafonov 13 years ago
parent
commit
69169590da
  1. 6
      redis/RedisDebug.php
  2. 10
      redis/RedisManager.php
  3. 4
      tests/redis/RedisDebugTest.php
  4. 33
      tests/redis/RedisManagerTest.php

6
redis/RedisDebug.php

@ -25,6 +25,12 @@ class RedisDebug
{ {
$command = $this->r_implode(', ', $arguments); $command = $this->r_implode(', ', $arguments);
$profiler = Profiler::getInstance()->profilerCommand('Redis->' . $name, $command); $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); $data = call_user_func_array(array($this->redis, $name), $arguments);
$profiler->end(); $profiler->end();
return $data; return $data;

10
redis/RedisManager.php

@ -31,11 +31,15 @@ class RedisManager
{ {
if (!isset(self::$connections[$name])) { if (!isset(self::$connections[$name])) {
if (!$config) { if (!$config) {
if (gettype(Config::get('Redis')) != 'object') {
throw new GeneralException('config non-object');
}
$config = Config::get('Redis')->$name; $config = Config::get('Redis')->$name;
} }
if (!is_array($config)) { 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'; $host = isset($config['host']) ? $config['host'] : 'localhost';
@ -50,11 +54,11 @@ class RedisManager
$connection = new RedisDebug($connection); $connection = new RedisDebug($connection);
} }
if (!$connection->connect($host, $port)) { 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 ($database) {
if (!$connection->select($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; self::$connections[$name] = $connection;

4
tests/redis/RedisDebugTest.php

@ -22,6 +22,7 @@ class RedisDebugTest extends PHPUnit_Framework_TestCase
public function run(PHPUnit_Framework_TestResult $result = NULL) public function run(PHPUnit_Framework_TestResult $result = NULL)
{ {
$this->setPreserveGlobalState(false); $this->setPreserveGlobalState(false);
return parent::run($result); 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 * @expectedExceptionMessage call_user_func_array() expects parameter 1 to be a valid callback
* @runInSeparateProcess * @runInSeparateProcess
*/ */
@ -94,8 +94,10 @@ class RedisDebugTest extends PHPUnit_Framework_TestCase
if (!defined('DEBUG')) { if (!defined('DEBUG')) {
define('DEBUG', true); define('DEBUG', true);
} }
$mock = $this->getMock('Redis', array('connect')); $mock = $this->getMock('Redis', array('connect'));
$redisDebug = new RedisDebug($mock); $redisDebug = new RedisDebug($mock);
$this->setExpectedException('GeneralException');
$this->assertNull($redisDebug->nothing('localhost', 4322)); $this->assertNull($redisDebug->nothing('localhost', 4322));
} }
} }

33
tests/redis/RedisManagerTest.php

@ -13,9 +13,9 @@
require_once dirname(__FILE__) . '/../../Registry.php'; require_once dirname(__FILE__) . '/../../Registry.php';
require_once dirname(__FILE__) . '/../../Config.php'; require_once dirname(__FILE__) . '/../../Config.php';
require_once dirname(__FILE__) . '/../../redis/RedisManager.php'; require_once dirname(__FILE__) . '/../../redis/RedisManager.php';
require_once dirname(__FILE__) . '/../../exception/InitializationException.php';
require_once dirname(__FILE__) . '/../../exception/GeneralException.php'; require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
class RedisManagerTest extends PHPUnit_Framework_TestCase 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 * @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() public function testConnectNoAnyConfig()
{ {
//$this->setExpectedException('GeneralException');
$this->setExpectedException('GeneralException');
RedisManager::connect(); 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() public function testConnectWrongPersistantConfig()
{ {
$this->setExpectedException('GeneralException');
Config::set('Redis', array('new' => 'some')); Config::set('Redis', array('new' => 'some'));
RedisManager::connect('new'); 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() public function testConnectDefaultConfig()
{ {
$this->setExpectedException('GeneralException');
Config::set('Redis', array('default' => 'some')); Config::set('Redis', array('default' => 'some'));
RedisManager::connect(); 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() public function testConnectFailedConnection()
{ {
$this->setExpectedException('GeneralException');
Config::set('Redis', array('new' => array('host' => 'error', 'port' => 2023, 'database' => 'some'))); Config::set('Redis', array('new' => array('host' => 'error', 'port' => 2023, 'database' => 'some')));
RedisManager::connect('new'); 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() public function testConnectEstablishedConnectionNoDb()
{ {
$this->setExpectedException('GeneralException');
Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => 'some'))); Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => 'some')));
RedisManager::connect('new'); RedisManager::connect('new');
} }
public function testConnectionGood() public function testConnectionGood()
{ {
// $this->setExpectedException('GeneralException');
Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => true))); Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => true)));
$redis = RedisManager::connect('new'); $redis = RedisManager::connect('new');
$this->assertInstanceOf('RedisMock', $redis); $this->assertInstanceOf('RedisMock', $redis);
} }
/**
* @runInSeparateProcess
*/
public function testConnectWithDebug() public function testConnectWithDebug()
{ {
if (!defined('DEBUG')) { if (!defined('DEBUG')) {

Loading…
Cancel
Save