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.

167 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. Config::set('PROFILER', true);
  96. Config::set('PROFILER_DETAILS', true);
  97. $this->getMock('RedisDebug');
  98. Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => true)));
  99. $redis = RedisManager::connect('new');
  100. $this->assertInstanceOf('RedisDebugMock', $redis);
  101. }
  102. /**
  103. * @group Redis
  104. */
  105. public function testConnectWithConfigArray()
  106. {
  107. $config = array('host' => true, 'port' => 2023, 'database' => true);
  108. $redis = RedisManager::connect('nsew', $config);
  109. $this->assertInstanceOf('RedisMock', $redis);
  110. }
  111. protected function newCallback($className)
  112. {
  113. switch ($className) {
  114. case 'Redis':
  115. return 'RedisMock';
  116. case 'RedisDebug':
  117. return 'RedisDebugMock';
  118. default:
  119. return $className;
  120. }
  121. }
  122. public function tearDown()
  123. {
  124. unset_new_overload();
  125. }
  126. }
  127. class RedisMock
  128. {
  129. public function connect($host, $port)
  130. {
  131. if ($host === true) {
  132. return true;
  133. }
  134. return false;
  135. }
  136. public function select($dbname)
  137. {
  138. if ($dbname === true) {
  139. return true;
  140. }
  141. return false;
  142. }
  143. }
  144. class RedisDebugMock extends RedisMock
  145. {
  146. }