added setExpectedException and check empty($search)
This commit is contained in:
@ -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;
|
||||||
|
@ -31,11 +31,15 @@ class RedisManager
|
|||||||
{
|
{
|
||||||
if (!isset(self::$connections[$name])) {
|
if (!isset(self::$connections[$name])) {
|
||||||
if (!$config) {
|
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)) {
|
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;
|
||||||
|
@ -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));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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
|
* @expected Exception GeneralException
|
||||||
* @expectedExceptionMessage Connection parameters must be an array
|
* @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
|
* @expected Exception GeneralException
|
||||||
* @expectedExceptionMessage Connection parameters must be an array
|
* @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
|
* @expected Exception GeneralException
|
||||||
* @expectedExceptionMessage Failed to connect to Redis server at
|
* @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
|
* @expected Exception GeneralException
|
||||||
* @expectedExceptionMessage Failed to select Redis database with index
|
* @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')) {
|
||||||
|
Reference in New Issue
Block a user