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.

168 lines
4.3 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/GeneralException.php';
  15. class RedisManagerTest extends PHPUnit_Framework_TestCase
  16. {
  17. private $connections;
  18. public function run(PHPUnit_Framework_TestResult $result = NULL)
  19. {
  20. $this->setPreserveGlobalState(false);
  21. return parent::run($result);
  22. }
  23. public function setUp()
  24. {
  25. $this->getMock('Redis');
  26. $conf = Config::getInstance();
  27. if ($conf->offsetExists('Redis')) {
  28. $conf->offsetUnset('Redis');
  29. }
  30. $class = new ReflectionClass('RedisManager');
  31. $this->connections = $class->getProperty('connections');
  32. $this->connections->setAccessible(true);
  33. $this->connections->setValue(null, array());
  34. set_new_overload(array($this, 'newCallback'));
  35. }
  36. /**
  37. * @group Redis
  38. */
  39. public function testConnectNoAnyConfig()
  40. {
  41. $this->setExpectedException('GeneralException', 'Redis config no existence');
  42. RedisManager::connect();
  43. }
  44. /**
  45. * @group Redis
  46. */
  47. public function testConnectWrongPersistantConfig()
  48. {
  49. Config::set('Redis', array('new' => 'some'));
  50. $this->setExpectedException('GeneralException', 'Connection parameters must be an array');
  51. RedisManager::connect('new');
  52. }
  53. /**
  54. * @group Redis
  55. */
  56. public function testConnectDefaultConfig()
  57. {
  58. Config::set('Redis', array('default' => 'some'));
  59. $this->setExpectedException('GeneralException', 'Connection parameters must be an array');
  60. RedisManager::connect();
  61. }
  62. /**
  63. * @group Redis
  64. */
  65. public function testConnectFailedConnection()
  66. {
  67. Config::set('Redis', array('new' => array('host' => 'error', 'port' => 2023, 'database' => 'some')));
  68. $this->setExpectedException('GeneralException', 'Failed to connect to Redis server at');
  69. RedisManager::connect('new');
  70. }
  71. /**
  72. * @group Redis
  73. */
  74. public function testConnectEstablishedConnectionNoDb()
  75. {
  76. Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => 'some')));
  77. $this->setExpectedException('GeneralException', 'Failed to select Redis database with index');
  78. RedisManager::connect('new');
  79. }
  80. /**
  81. * @group Redis
  82. */
  83. public function testConnectionGood()
  84. {
  85. Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => true)));
  86. $redis = RedisManager::connect('new');
  87. $this->assertInstanceOf('RedisMock', $redis);
  88. }
  89. /**
  90. * @runInSeparateProcess
  91. * @group Redis
  92. */
  93. public function testConnectWithDebug()
  94. {
  95. if (!defined('DEBUG')) {
  96. define('DEBUG', true);
  97. }
  98. $this->getMock('RedisDebug');
  99. Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => true)));
  100. $redis = RedisManager::connect('new');
  101. $this->assertInstanceOf('RedisDebugMock', $redis);
  102. }
  103. /**
  104. * @group Redis
  105. */
  106. public function testConnectWithConfigArray()
  107. {
  108. $config = array('host' => true, 'port' => 2023, 'database' => true);
  109. $redis = RedisManager::connect('nsew', $config);
  110. $this->assertInstanceOf('RedisMock', $redis);
  111. }
  112. protected function newCallback($className)
  113. {
  114. switch ($className) {
  115. case 'Redis':
  116. return 'RedisMock';
  117. case 'RedisDebug':
  118. return 'RedisDebugMock';
  119. default:
  120. return $className;
  121. }
  122. }
  123. public function tearDown()
  124. {
  125. unset_new_overload();
  126. }
  127. }
  128. class RedisMock
  129. {
  130. public function connect($host, $port)
  131. {
  132. if ($host === true) {
  133. return true;
  134. }
  135. return false;
  136. }
  137. public function select($dbname)
  138. {
  139. if ($dbname === true) {
  140. return true;
  141. }
  142. return false;
  143. }
  144. }
  145. class RedisDebugMock extends RedisMock
  146. {
  147. }