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.

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