Redis classes tested
This commit is contained in:
99
tests/redis/RedisDebugTest.php
Normal file
99
tests/redis/RedisDebugTest.php
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @copyright NetMonsters <team@netmonsters.ru>
|
||||||
|
* @link http://netmonsters.ru
|
||||||
|
* @package Majestic
|
||||||
|
* @subpackage UnitTests
|
||||||
|
* @since 2011-10-11
|
||||||
|
*
|
||||||
|
* Unit tests for RedisDebug class
|
||||||
|
*/
|
||||||
|
|
||||||
|
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
|
||||||
|
require_once dirname(__FILE__) . '/../../util/profiler/CommandProfiler.php';
|
||||||
|
require_once dirname(__FILE__) . '/../../util/profiler/Profiler.php';
|
||||||
|
require_once dirname(__FILE__) . '/../../redis/RedisDebug.php';
|
||||||
|
|
||||||
|
class RedisDebugTest extends PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
public function run(PHPUnit_Framework_TestResult $result = NULL)
|
||||||
|
{
|
||||||
|
$this->setPreserveGlobalState(false);
|
||||||
|
return parent::run($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException GeneralException
|
||||||
|
*/
|
||||||
|
public function testConstructException()
|
||||||
|
{
|
||||||
|
$redisDebug = new RedisDebug('redis');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testConstructGood()
|
||||||
|
{
|
||||||
|
$mock = $this->getMock('Redis');
|
||||||
|
$redisDebug = new RedisDebug($mock);
|
||||||
|
|
||||||
|
$class = new ReflectionClass('RedisDebug');
|
||||||
|
$redis = $class->getProperty('redis');
|
||||||
|
$redis->setAccessible(true);
|
||||||
|
$redis = $redis->getValue($redisDebug);
|
||||||
|
|
||||||
|
$this->assertSame($mock, $redis);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
*/
|
||||||
|
public function testCallSimpleParams()
|
||||||
|
{
|
||||||
|
if (!defined('DEBUG')) {
|
||||||
|
define('DEBUG', true);
|
||||||
|
}
|
||||||
|
$mock = $this->getMock('Redis', array('connect'));
|
||||||
|
|
||||||
|
$mock->expects($this->once())
|
||||||
|
->method('connect')
|
||||||
|
->with($this->equalTo('localhost'), $this->equalTo(4322))
|
||||||
|
->will($this->returnValue(true));
|
||||||
|
|
||||||
|
$redisDebug = new RedisDebug($mock);
|
||||||
|
$this->assertTrue($redisDebug->connect('localhost', 4322));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
*/
|
||||||
|
public function testCallArrayParam()
|
||||||
|
{
|
||||||
|
if (!defined('DEBUG')) {
|
||||||
|
define('DEBUG', true);
|
||||||
|
}
|
||||||
|
$mock = $this->getMock('Redis', array('connect'));
|
||||||
|
|
||||||
|
$mock->expects($this->once())
|
||||||
|
->method('connect')
|
||||||
|
->with($this->equalTo(array('localhost', 4322)))
|
||||||
|
->will($this->returnValue(true));
|
||||||
|
|
||||||
|
$redisDebug = new RedisDebug($mock);
|
||||||
|
$this->assertTrue($redisDebug->connect(array('localhost', 4322)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Exception
|
||||||
|
* @runInSeparateProcess
|
||||||
|
*/
|
||||||
|
public function testCallUndefinedMethod()
|
||||||
|
{
|
||||||
|
if (!defined('DEBUG')) {
|
||||||
|
define('DEBUG', true);
|
||||||
|
}
|
||||||
|
$mock = $this->getMock('Redis', array('connect'));
|
||||||
|
$redisDebug = new RedisDebug($mock);
|
||||||
|
$this->assertNull($redisDebug->nothing('localhost', 4322));
|
||||||
|
}
|
||||||
|
}
|
164
tests/redis/RedisManagerTest.php
Normal file
164
tests/redis/RedisManagerTest.php
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @copyright NetMonsters <team@netmonsters.ru>
|
||||||
|
* @link http://netmonsters.ru
|
||||||
|
* @package Majestic
|
||||||
|
* @subpackage UnitTests
|
||||||
|
* @since 2011-10-11
|
||||||
|
*
|
||||||
|
* Unit tests for RedisManager class
|
||||||
|
*/
|
||||||
|
|
||||||
|
require_once dirname(__FILE__) . '/../../Registry.php';
|
||||||
|
require_once dirname(__FILE__) . '/../../Config.php';
|
||||||
|
require_once dirname(__FILE__) . '/../../redis/RedisManager.php';
|
||||||
|
|
||||||
|
class RedisManagerTest extends PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
private $connections;
|
||||||
|
|
||||||
|
|
||||||
|
public function run(PHPUnit_Framework_TestResult $result = NULL)
|
||||||
|
{
|
||||||
|
$this->setPreserveGlobalState(false);
|
||||||
|
return parent::run($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$this->getMock('Redis');
|
||||||
|
|
||||||
|
$conf = Config::getInstance();
|
||||||
|
if ($conf->offsetExists('Redis')) {
|
||||||
|
$conf->offsetUnset('Redis');
|
||||||
|
}
|
||||||
|
|
||||||
|
$class = new ReflectionClass('RedisManager');
|
||||||
|
$this->connections = $class->getProperty('connections');
|
||||||
|
$this->connections->setAccessible(true);
|
||||||
|
$this->connections->setValue(null, array());
|
||||||
|
|
||||||
|
set_new_overload(array($this, 'newCallback'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Exception
|
||||||
|
* @expectedExceptionMessage Trying to get property of non-object
|
||||||
|
* @TODO: line 34: $config = Config::get('Redis')->$name; - check for Redis config existence
|
||||||
|
*/
|
||||||
|
public function testConnectNoAnyConfig()
|
||||||
|
{
|
||||||
|
RedisManager::connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Exception
|
||||||
|
* @expectedExceptionMessage Connection parameters must be an array
|
||||||
|
*/
|
||||||
|
public function testConnectWrongPersistantConfig()
|
||||||
|
{
|
||||||
|
Config::set('Redis', array('new' => 'some'));
|
||||||
|
RedisManager::connect('new');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Exception
|
||||||
|
* @expectedExceptionMessage Connection parameters must be an array
|
||||||
|
*/
|
||||||
|
public function testConnectDefaultConfig()
|
||||||
|
{
|
||||||
|
Config::set('Redis', array('default' => 'some'));
|
||||||
|
RedisManager::connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Exception
|
||||||
|
* @expectedExceptionMessage Failed to connect to Redis server at
|
||||||
|
*/
|
||||||
|
public function testConnectFailedConnection()
|
||||||
|
{
|
||||||
|
Config::set('Redis', array('new' => array('host' => 'error', 'port' => 2023, 'database' => 'some')));
|
||||||
|
RedisManager::connect('new');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Exception
|
||||||
|
* @expectedExceptionMessage Failed to select Redis database with index
|
||||||
|
*/
|
||||||
|
public function testConnectEstablishedConnectionNoDb()
|
||||||
|
{
|
||||||
|
Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => 'some')));
|
||||||
|
RedisManager::connect('new');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testConnectionGood()
|
||||||
|
{
|
||||||
|
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')) {
|
||||||
|
define('DEBUG', true);
|
||||||
|
}
|
||||||
|
$this->getMock('RedisDebug');
|
||||||
|
|
||||||
|
Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => true)));
|
||||||
|
$redis = RedisManager::connect('new');
|
||||||
|
$this->assertInstanceOf('RedisDebugMock', $redis);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testConnectWithConfigArray()
|
||||||
|
{
|
||||||
|
$config = array('host' => true, 'port' => 2023, 'database' => true);
|
||||||
|
$redis = RedisManager::connect('nsew', $config);
|
||||||
|
$this->assertInstanceOf('RedisMock', $redis);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function newCallback($className)
|
||||||
|
{
|
||||||
|
switch ($className) {
|
||||||
|
case 'Redis':
|
||||||
|
return 'RedisMock';
|
||||||
|
case 'RedisDebug':
|
||||||
|
return 'RedisDebugMock';
|
||||||
|
default:
|
||||||
|
return $className;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tearDown()
|
||||||
|
{
|
||||||
|
unset_new_overload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class RedisMock
|
||||||
|
{
|
||||||
|
public function connect($host, $port)
|
||||||
|
{
|
||||||
|
if ($host === true) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function select($dbname)
|
||||||
|
{
|
||||||
|
if ($dbname === true) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class RedisDebugMock extends RedisMock
|
||||||
|
{
|
||||||
|
}
|
Reference in New Issue
Block a user