| 
									
										
										
										
											2011-10-28 14:47:24 +04:00
										 |  |  | <?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'; | 
					
						
							| 
									
										
										
										
											2011-11-30 13:29:48 +04:00
										 |  |  | require_once dirname(__FILE__) . '/../../exception/InitializationException.php'; | 
					
						
							| 
									
										
										
										
											2011-11-25 14:59:39 +04:00
										 |  |  | require_once dirname(__FILE__) . '/../../exception/GeneralException.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 14:47:24 +04:00
										 |  |  | 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')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @expectedExceptionMessage Trying to get property of non-object | 
					
						
							| 
									
										
										
										
											2011-11-30 13:29:48 +04:00
										 |  |  |      * @TODO: line 34: $config = Config::get('Redis')->$name; - check for Redis config existence. Agafonov: 'check added' | 
					
						
							| 
									
										
										
										
											2011-10-28 14:47:24 +04:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function testConnectNoAnyConfig() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-11-30 13:29:48 +04:00
										 |  |  |        //$this->setExpectedException('GeneralException');
 | 
					
						
							|  |  |  |         $this->setExpectedException('GeneralException'); | 
					
						
							| 
									
										
										
										
											2011-10-28 14:47:24 +04:00
										 |  |  |         RedisManager::connect(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2011-11-30 13:29:48 +04:00
										 |  |  |      * @expected Exception GeneralException | 
					
						
							|  |  |  |      * @expected ExceptionMessage Connection parameters must be an array | 
					
						
							| 
									
										
										
										
											2011-10-28 14:47:24 +04:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function testConnectWrongPersistantConfig() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-11-30 13:29:48 +04:00
										 |  |  |         $this->setExpectedException('GeneralException'); | 
					
						
							| 
									
										
										
										
											2011-10-28 14:47:24 +04:00
										 |  |  |         Config::set('Redis', array('new' => 'some')); | 
					
						
							|  |  |  |         RedisManager::connect('new'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2011-11-30 13:29:48 +04:00
										 |  |  |      * @expected Exception GeneralException | 
					
						
							|  |  |  |      * @expected ExceptionMessage Connection parameters must be an array | 
					
						
							| 
									
										
										
										
											2011-10-28 14:47:24 +04:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function testConnectDefaultConfig() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-11-30 13:29:48 +04:00
										 |  |  |         $this->setExpectedException('GeneralException'); | 
					
						
							| 
									
										
										
										
											2011-10-28 14:47:24 +04:00
										 |  |  |         Config::set('Redis', array('default' => 'some')); | 
					
						
							|  |  |  |         RedisManager::connect(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2011-11-30 13:29:48 +04:00
										 |  |  |      * @expected Exception GeneralException | 
					
						
							|  |  |  |      * @expected ExceptionMessage Failed to connect to Redis server at | 
					
						
							| 
									
										
										
										
											2011-10-28 14:47:24 +04:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function testConnectFailedConnection() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-11-30 13:29:48 +04:00
										 |  |  |          $this->setExpectedException('GeneralException'); | 
					
						
							| 
									
										
										
										
											2011-10-28 14:47:24 +04:00
										 |  |  |         Config::set('Redis', array('new' => array('host' => 'error', 'port' => 2023, 'database' => 'some'))); | 
					
						
							|  |  |  |         RedisManager::connect('new'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2011-11-30 13:29:48 +04:00
										 |  |  |      * @expected Exception GeneralException | 
					
						
							|  |  |  |      * @expected ExceptionMessage Failed to select Redis database with index | 
					
						
							| 
									
										
										
										
											2011-10-28 14:47:24 +04:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function testConnectEstablishedConnectionNoDb() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-11-30 13:29:48 +04:00
										 |  |  |         $this->setExpectedException('GeneralException'); | 
					
						
							| 
									
										
										
										
											2011-10-28 14:47:24 +04:00
										 |  |  |         Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => 'some'))); | 
					
						
							|  |  |  |         RedisManager::connect('new'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-30 13:29:48 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 14:47:24 +04:00
										 |  |  |     public function testConnectionGood() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-11-30 13:29:48 +04:00
										 |  |  |        // $this->setExpectedException('GeneralException');
 | 
					
						
							| 
									
										
										
										
											2011-10-28 14:47:24 +04:00
										 |  |  |         Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => true))); | 
					
						
							|  |  |  |         $redis = RedisManager::connect('new'); | 
					
						
							|  |  |  |         $this->assertInstanceOf('RedisMock', $redis); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-30 13:29:48 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-28 14:47:24 +04:00
										 |  |  |     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 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } |