PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ZendTest/Cache/Storage/Adapter/RedisTest.php

https://bitbucket.org/saifshuvo/zf2
PHP | 264 lines | 206 code | 46 blank | 12 comment | 10 complexity | 3eba05ffff73fdcc4358b20acb7ed3bf MD5 | raw file
  1. <?php
  2. namespace ZendTest\Cache\Storage\Adapter;
  3. use Zend\Cache;
  4. use Redis as RedisResource;
  5. class RedisTest extends CommonAdapterTest
  6. {
  7. /**
  8. *
  9. * @var Cache\Storage\Adapter\RedisOptions
  10. */
  11. protected $_options;
  12. /**
  13. *
  14. * @var Cache\Storage\Adapter\Redis
  15. */
  16. protected $_storage;
  17. public function setUp()
  18. {
  19. if (!defined('TESTS_ZEND_CACHE_REDIS_ENABLED') || !TESTS_ZEND_CACHE_REDIS_ENABLED) {
  20. $this->markTestSkipped("Skipped by TestConfiguration (TESTS_ZEND_CACHE_REDIS_ENABLED)");
  21. }
  22. if (!extension_loaded('redis')) {
  23. $this->markTestSkipped("Redis extension is not loaded");
  24. }
  25. $this->_options = new Cache\Storage\Adapter\RedisOptions(array(
  26. 'resource_id' => __CLASS__,
  27. ));
  28. if (defined('TESTS_ZEND_CACHE_REDIS_HOST') && defined('TESTS_ZEND_CACHE_REDIS_PORT')) {
  29. $this->_options->getResourceManager()->setServer(__CLASS__, array(
  30. TESTS_ZEND_CACHE_REDIS_HOST, TESTS_ZEND_CACHE_REDIS_PORT, 1
  31. ));
  32. } elseif (defined('TESTS_ZEND_CACHE_REDIS_HOST')) {
  33. $this->_options->getResourceManager()->setServer(__CLASS__, array(
  34. TESTS_ZEND_CACHE_REDIS_HOST
  35. ));
  36. }
  37. if (defined('TESTS_ZEND_CACHE_REDIS_DATABASE')) {
  38. $this->_options->getResourceManager()->setDatabase(__CLASS__,
  39. TESTS_ZEND_CACHE_REDIS_DATABASE
  40. );
  41. }
  42. if (defined('TESTS_ZEND_CACHE_REDIS_PASSWORD')) {
  43. $this->_options->getResourceManager()->setPassword(__CLASS__,
  44. TESTS_ZEND_CACHE_REDIS_PASSWORD
  45. );
  46. }
  47. $this->_storage = new Cache\Storage\Adapter\Redis();
  48. $this->_storage->setOptions($this->_options);
  49. $this->_storage->flush();
  50. parent::setUp();
  51. }
  52. public function tearDown()
  53. {
  54. if ($this->_storage) {
  55. $this->_storage->flush();
  56. }
  57. parent::tearDown();
  58. }
  59. /* Redis */
  60. public function testRedisCacheStore()
  61. {
  62. $key = 'singleKey';
  63. //assure that there's nothing under key
  64. $this->_storage->removeItem($key);
  65. $this->assertNull($this->_storage->getItem($key));
  66. $this->_storage->setItem($key, serialize(array('test', array('one', 'two'))));
  67. $this->assertCount(2, unserialize($this->_storage->getItem($key)), 'Get item should return array of two elements');
  68. $expectedVals = array(
  69. 'key1' => 'val1',
  70. 'key2' => 'val2',
  71. 'key3' => array('val3', 'val4'),
  72. );
  73. $this->_storage->setItems($expectedVals);
  74. $this->assertCount(
  75. 3,
  76. $this->_storage->getItems(array_keys($expectedVals)),
  77. 'Multiple set/get items didnt save correct amount of rows'
  78. );
  79. }
  80. public function testRedisSerializer()
  81. {
  82. $this->_storage->addPlugin(new \Zend\Cache\Storage\Plugin\Serializer());
  83. $value = array('test', 'of', 'array');
  84. $this->_storage->setItem('key', $value);
  85. $this->assertCount(count($value), $this->_storage->getItem('key'), 'Problem with Redis serialization');
  86. }
  87. public function testRedisSetInt()
  88. {
  89. $key = 'key';
  90. $this->assertTrue($this->_storage->setItem($key, 123));
  91. $this->assertEquals('123', $this->_storage->getItem($key), 'Integer should be cast to string');
  92. }
  93. public function testRedisSetDouble()
  94. {
  95. $key = 'key';
  96. $this->assertTrue($this->_storage->setItem($key, 123.12));
  97. $this->assertEquals('123.12', $this->_storage->getItem($key), 'Integer should be cast to string');
  98. }
  99. public function testRedisSetNull()
  100. {
  101. $key = 'key';
  102. $this->assertTrue($this->_storage->setItem($key, null));
  103. $this->assertEquals('', $this->_storage->getItem($key), 'Null should be cast to string');
  104. }
  105. public function testRedisSetBoolean()
  106. {
  107. $key = 'key';
  108. $this->assertTrue($this->_storage->setItem($key, true));
  109. $this->assertEquals('1', $this->_storage->getItem($key), 'Boolean should be cast to string');
  110. $this->assertTrue($this->_storage->setItem($key, false));
  111. $this->assertEquals('', $this->_storage->getItem($key), 'Boolean should be cast to string');
  112. }
  113. public function testGetCapabilitiesTtl()
  114. {
  115. $host = defined('TESTS_ZEND_CACHE_REDIS_HOST') ? TESTS_ZEND_CACHE_REDIS_HOST : '127.0.0.1';
  116. $port = defined('TESTS_ZEND_CACHE_REDIS_PORT') ? TESTS_ZEND_CACHE_REDIS_PORT : 6379;
  117. $redisResource = new RedisResource();
  118. $redisResource->connect($host, $port);
  119. $info = $redisResource->info();
  120. $mayorVersion = (int)$info['redis_version'];
  121. $this->assertEquals($mayorVersion, $this->_options->getResourceManager()->getMayorVersion($this->_options->getResourceId()));
  122. $capabilities = $this->_storage->getCapabilities();
  123. if ($mayorVersion < 2) {
  124. $this->assertEquals(0, $capabilities->getMinTtl(), 'Redis version < 2.0.0 does not support key expiration');
  125. } else {
  126. $this->assertEquals(1, $capabilities->getMinTtl(), 'Redis version > 2.0.0 supports key expiration');
  127. }
  128. }
  129. /* ResourceManager */
  130. public function testSocketConnection()
  131. {
  132. $socket = '/tmp/redis.sock';
  133. $this->_options->getResourceManager()->setServer($this->_options->getResourceId(), $socket);
  134. $normalized = $this->_options->getResourceManager()->getServer($this->_options->getResourceId());
  135. $this->assertEquals($socket, $normalized['host'], 'Host should equal to socket {$socket}');
  136. $this->_storage = null;
  137. }
  138. public function testGetSetDatabase()
  139. {
  140. $this->assertTrue($this->_storage->setItem('key', 'val'));
  141. $this->assertEquals('val', $this->_storage->getItem('key'));
  142. $databaseNumber = 1;
  143. $resourceManager = $this->_options->getResourceManager();
  144. $resourceManager->setDatabase($this->_options->getResourceId(), $databaseNumber);
  145. $this->assertNull($this->_storage->getItem('key'), 'No value should be found because set was done on different database than get');
  146. $this->assertEquals($databaseNumber, $resourceManager->getDatabase($this->_options->getResourceId()), 'Incorrect database was returned');
  147. }
  148. public function testGetSetPassword()
  149. {
  150. $pass = 'super secret';
  151. $this->_options->getResourceManager()->setPassword($this->_options->getResourceId(), $pass);
  152. $this->assertEquals(
  153. $pass,
  154. $this->_options->getResourceManager()->getPassword($this->_options->getResourceId()),
  155. 'Password was not correctly set'
  156. );
  157. }
  158. /* RedisOptions */
  159. public function testGetSetNamespace()
  160. {
  161. $namespace = 'testNamespace';
  162. $this->_options->setNamespace($namespace);
  163. $this->assertEquals($namespace, $this->_options->getNamespace(), 'Namespace was not set correctly');
  164. }
  165. public function testGetSetNamespaceSeparator()
  166. {
  167. $separator = '/';
  168. $this->_options->setNamespaceSeparator($separator);
  169. $this->assertEquals($separator, $this->_options->getNamespaceSeparator(), 'Separator was not set correctly');
  170. }
  171. public function testGetSetResourceManager()
  172. {
  173. $resourceManager = new \Zend\Cache\Storage\Adapter\RedisResourceManager();
  174. $options = new \Zend\Cache\Storage\Adapter\RedisOptions();
  175. $options->setResourceManager($resourceManager);
  176. $this->assertInstanceOf(
  177. 'Zend\\Cache\\Storage\\Adapter\\RedisResourceManager',
  178. $options->getResourceManager(),
  179. 'Wrong resource manager retuned, it should of type RedisResourceManager'
  180. );
  181. $this->assertEquals($resourceManager, $options->getResourceManager());
  182. }
  183. public function testGetSetResourceId()
  184. {
  185. $resourceId = '1';
  186. $options = new \Zend\Cache\Storage\Adapter\RedisOptions();
  187. $options->setResourceId($resourceId);
  188. $this->assertEquals($resourceId, $options->getResourceId(), 'Resource id was not set correctly');
  189. }
  190. public function testGetSetPersistentId()
  191. {
  192. $persistentId = '1';
  193. $this->_options->setPersistentId($persistentId);
  194. $this->assertEquals($persistentId, $this->_options->getPersistentId(), 'Persistent id was not set correctly');
  195. }
  196. public function testOptionsGetSetLibOptions()
  197. {
  198. $options = array('serializer', RedisResource::SERIALIZER_PHP);
  199. $this->_options->setLibOptions($options);
  200. $this->assertEquals($options, $this->_options->getLibOptions(), 'Lib Options were not set correctly through RedisOptions');
  201. }
  202. public function testGetSetServer()
  203. {
  204. $server = array(
  205. 'host' => '127.0.0.1',
  206. 'port' => 6379,
  207. 'timeout' => 0,
  208. );
  209. $this->_options->setServer($server);
  210. $this->assertEquals($server, $this->_options->getServer(), 'Server was not set correctly through RedisOptions');
  211. }
  212. public function testOptionsGetSetDatabase()
  213. {
  214. $database = 1;
  215. $this->_options->setDatabase($database);
  216. $this->assertEquals($database, $this->_options->getDatabase(), 'Database not set correctly using RedisOptions');
  217. }
  218. }