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.

166 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('DEBUG', 1);
  96. $this->getMock('RedisDebug');
  97. Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => true)));
  98. $redis = RedisManager::connect('new');
  99. $this->assertInstanceOf('RedisDebugMock', $redis);
  100. }
  101. /**
  102. * @group 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. }