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.

165 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. * @expectedException GeneralException
  38. * @expectedExceptionMessage Trying to get property of non-object
  39. * @TODO: line 34: $config = Config::get('Redis')->$name; - check for Redis config existence
  40. */
  41. public function testConnectNoAnyConfig()
  42. {
  43. RedisManager::connect();
  44. }
  45. /**
  46. * @expectedException GeneralException
  47. * @expectedExceptionMessage Connection parameters must be an array
  48. */
  49. public function testConnectWrongPersistantConfig()
  50. {
  51. Config::set('Redis', array('new' => 'some'));
  52. RedisManager::connect('new');
  53. }
  54. /**
  55. * @expectedException GeneralException
  56. * @expectedExceptionMessage Connection parameters must be an array
  57. */
  58. public function testConnectDefaultConfig()
  59. {
  60. Config::set('Redis', array('default' => 'some'));
  61. RedisManager::connect();
  62. }
  63. /**
  64. * @expectedException GeneralException
  65. * @expectedExceptionMessage Failed to connect to Redis server at
  66. */
  67. public function testConnectFailedConnection()
  68. {
  69. Config::set('Redis', array('new' => array('host' => 'error', 'port' => 2023, 'database' => 'some')));
  70. RedisManager::connect('new');
  71. }
  72. /**
  73. * @expectedException GeneralException
  74. * @expectedExceptionMessage Failed to select Redis database with index
  75. */
  76. public function testConnectEstablishedConnectionNoDb()
  77. {
  78. Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => 'some')));
  79. RedisManager::connect('new');
  80. }
  81. public function testConnectionGood()
  82. {
  83. Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => true)));
  84. $redis = RedisManager::connect('new');
  85. $this->assertInstanceOf('RedisMock', $redis);
  86. }
  87. /**
  88. * @runInSeparateProcess
  89. */
  90. public function testConnectWithDebug()
  91. {
  92. if (!defined('DEBUG')) {
  93. define('DEBUG', true);
  94. }
  95. $this->getMock('RedisDebug');
  96. Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => true)));
  97. $redis = RedisManager::connect('new');
  98. $this->assertInstanceOf('RedisDebugMock', $redis);
  99. }
  100. public function testConnectWithConfigArray()
  101. {
  102. $config = array('host' => true, 'port' => 2023, 'database' => true);
  103. $redis = RedisManager::connect('nsew', $config);
  104. $this->assertInstanceOf('RedisMock', $redis);
  105. }
  106. protected function newCallback($className)
  107. {
  108. switch ($className) {
  109. case 'Redis':
  110. return 'RedisMock';
  111. case 'RedisDebug':
  112. return 'RedisDebugMock';
  113. default:
  114. return $className;
  115. }
  116. }
  117. public function tearDown()
  118. {
  119. unset_new_overload();
  120. }
  121. }
  122. class RedisMock
  123. {
  124. public function connect($host, $port)
  125. {
  126. if ($host === true) {
  127. return true;
  128. }
  129. return false;
  130. }
  131. public function select($dbname)
  132. {
  133. if ($dbname === true) {
  134. return true;
  135. }
  136. return false;
  137. }
  138. }
  139. class RedisDebugMock extends RedisMock
  140. {
  141. }