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.

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