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.

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