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. 17
      tests/redis/RedisManagerTest.php

6
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;

10
redis/RedisManager.php

@ -31,11 +31,15 @@ class RedisManager
{
if (!isset(self::$connections[$name])) {
if (!$config) {
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;

4
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));
}
}

17
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,12 +46,13 @@ 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();
}
@ -61,6 +62,7 @@ class RedisManagerTest extends PHPUnit_Framework_TestCase
*/
public function testConnectWrongPersistantConfig()
{
$this->setExpectedException('GeneralException');
Config::set('Redis', array('new' => 'some'));
RedisManager::connect('new');
}
@ -71,6 +73,7 @@ class RedisManagerTest extends PHPUnit_Framework_TestCase
*/
public function testConnectDefaultConfig()
{
$this->setExpectedException('GeneralException');
Config::set('Redis', array('default' => 'some'));
RedisManager::connect();
}
@ -81,6 +84,7 @@ class RedisManagerTest extends PHPUnit_Framework_TestCase
*/
public function testConnectFailedConnection()
{
$this->setExpectedException('GeneralException');
Config::set('Redis', array('new' => array('host' => 'error', 'port' => 2023, 'database' => 'some')));
RedisManager::connect('new');
}
@ -91,20 +95,21 @@ class RedisManagerTest extends PHPUnit_Framework_TestCase
*/
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')) {

Loading…
Cancel
Save