ARedisCacheTest.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. require_once("common.php");
  3. /**
  4. * Tests for the {@link ARedisCache} class
  5. * @author Charles Pick
  6. * @package packages.redis.tests
  7. */
  8. class ARedisCacheTest extends CTestCase {
  9. /**
  10. * Holds the redis cache component
  11. * @var ARedisConnection
  12. */
  13. protected $_connection;
  14. /**
  15. * Tests the basic functionality
  16. */
  17. public function testBasics() {
  18. $cache = new ARedisCache();
  19. $cache->setConnection($this->getConnection());
  20. $cacheKey = "TEST_KEY:".uniqid();
  21. $this->assertEquals(false,$cache->get($cacheKey));
  22. $cache->set($cacheKey,"test");
  23. $this->assertEquals("test",$cache->get($cacheKey));
  24. $cache->delete($cacheKey);
  25. $this->assertEquals(false,$cache->get($cacheKey));
  26. // test expire
  27. $cache->set($cacheKey,"test",1);
  28. $this->assertEquals("test",$cache->get($cacheKey));
  29. sleep(2);
  30. $this->assertEquals(false,$cache->get($cacheKey));
  31. }
  32. /**
  33. * Sets the redis connection to use with this test
  34. * @param ARedisConnection $connection the connection
  35. */
  36. public function setConnection($connection)
  37. {
  38. $this->_connection = $connection;
  39. }
  40. /**
  41. * Gets the redis connection to use with this test
  42. * @return ARedisConnection the redis connection
  43. */
  44. public function getConnection()
  45. {
  46. if ($this->_connection === null) {
  47. $this->_connection = Yii::createComponent(
  48. array(
  49. "class" => "packages.redis.ARedisConnection",
  50. "hostname" => REDIS_HOSTNAME,
  51. "port" => REDIS_PORT,
  52. "database" => REDIS_DATABASE,
  53. "password" => REDIS_PASSWORD
  54. ));
  55. }
  56. return $this->_connection;
  57. }
  58. }