You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
4.7 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-11
  8. *
  9. * Unit tests for RedisManager class
  10. */
  11. require_once dirname(__FILE__) . '/../../Registry.php';
  12. require_once dirname(__FILE__) . '/../../Config.php';
  13. require_once dirname(__FILE__) . '/../../redis/RedisManager.php';
  14. require_once dirname(__FILE__) . '/../../exception/InitializationException.php';
  15. require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
  16. class RedisManagerTest extends PHPUnit_Framework_TestCase
  17. {
  18. private $connections;
  19. public function run(PHPUnit_Framework_TestResult $result = NULL)
  20. {
  21. $this->setPreserveGlobalState(false);
  22. return parent::run($result);
  23. }
  24. public function setUp()
  25. {
  26. $this->getMock('Redis');
  27. $conf = Config::getInstance();
  28. if ($conf->offsetExists('Redis')) {
  29. $conf->offsetUnset('Redis');
  30. }
  31. $class = new ReflectionClass('RedisManager');
  32. $this->connections = $class->getProperty('connections');
  33. $this->connections->setAccessible(true);
  34. $this->connections->setValue(null, array());
  35. set_new_overload(array($this, 'newCallback'));
  36. }
  37. /**
  38. * @expectedExceptionMessage Trying to get property of non-object
  39. * @TODO: line 34: $config = Config::get('Redis')->$name; - check for Redis config existence. Agafonov: 'check added'
  40. */
  41. public function testConnectNoAnyConfig()
  42. {
  43. //$this->setExpectedException('GeneralException');
  44. $this->setExpectedException('GeneralException');
  45. RedisManager::connect();
  46. }
  47. /**
  48. * @expected Exception GeneralException
  49. * @expected ExceptionMessage Connection parameters must be an array
  50. */
  51. public function testConnectWrongPersistantConfig()
  52. {
  53. $this->setExpectedException('GeneralException');
  54. Config::set('Redis', array('new' => 'some'));
  55. RedisManager::connect('new');
  56. }
  57. /**
  58. * @expected Exception GeneralException
  59. * @expected ExceptionMessage Connection parameters must be an array
  60. */
  61. public function testConnectDefaultConfig()
  62. {
  63. $this->setExpectedException('GeneralException');
  64. Config::set('Redis', array('default' => 'some'));
  65. RedisManager::connect();
  66. }
  67. /**
  68. * @expected Exception GeneralException
  69. * @expected ExceptionMessage Failed to connect to Redis server at
  70. */
  71. public function testConnectFailedConnection()
  72. {
  73. $this->setExpectedException('GeneralException');
  74. Config::set('Redis', array('new' => array('host' => 'error', 'port' => 2023, 'database' => 'some')));
  75. RedisManager::connect('new');
  76. }
  77. /**
  78. * @expected Exception GeneralException
  79. * @expected ExceptionMessage Failed to select Redis database with index
  80. */
  81. public function testConnectEstablishedConnectionNoDb()
  82. {
  83. $this->setExpectedException('GeneralException');
  84. Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => 'some')));
  85. RedisManager::connect('new');
  86. }
  87. public function testConnectionGood()
  88. {
  89. // $this->setExpectedException('GeneralException');
  90. Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => true)));
  91. $redis = RedisManager::connect('new');
  92. $this->assertInstanceOf('RedisMock', $redis);
  93. }
  94. public function testConnectWithDebug()
  95. {
  96. if (!defined('DEBUG')) {
  97. define('DEBUG', true);
  98. }
  99. $this->getMock('RedisDebug');
  100. Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => true)));
  101. $redis = RedisManager::connect('new');
  102. $this->assertInstanceOf('RedisDebugMock', $redis);
  103. }
  104. public function testConnectWithConfigArray()
  105. {
  106. $config = array('host' => true, 'port' => 2023, 'database' => true);
  107. $redis = RedisManager::connect('nsew', $config);
  108. $this->assertInstanceOf('RedisMock', $redis);
  109. }
  110. protected function newCallback($className)
  111. {
  112. switch ($className) {
  113. case 'Redis':
  114. return 'RedisMock';
  115. case 'RedisDebug':
  116. return 'RedisDebugMock';
  117. default:
  118. return $className;
  119. }
  120. }
  121. public function tearDown()
  122. {
  123. unset_new_overload();
  124. }
  125. }
  126. class RedisMock
  127. {
  128. public function connect($host, $port)
  129. {
  130. if ($host === true) {
  131. return true;
  132. }
  133. return false;
  134. }
  135. public function select($dbname)
  136. {
  137. if ($dbname === true) {
  138. return true;
  139. }
  140. return false;
  141. }
  142. }
  143. class RedisDebugMock extends RedisMock
  144. {
  145. }