ARedisHashTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. require_once("common.php");
  3. /**
  4. * Tests for the {@link ARedisHash} class
  5. * @author Charles Pick
  6. * @package packages.redis.tests
  7. */
  8. class ARedisHashTest extends CTestCase {
  9. /**
  10. * Holds the redis connection
  11. * @var ARedisConnection
  12. */
  13. protected $_connection;
  14. /**
  15. * Tests the basic functionality
  16. */
  17. public function testBasics() {
  18. $redis = $this->getConnection();
  19. $set = new ARedisHash("TestHash:".uniqid(),$redis);
  20. $this->assertTrue($set->add("oranges", 2.40));
  21. $this->assertTrue($set->add("apples", 1.40));
  22. $this->assertTrue($set->add("strawberries", 3));
  23. $this->assertEquals(3, $set->getCount());
  24. $this->assertTrue($set->add("carrots",0.4));
  25. $this->assertEquals(4, $set->getCount());
  26. $this->assertTrue($set->remove("carrots"));
  27. $this->assertFalse($set->remove("carrots"));
  28. $this->assertEquals(3, $set->getCount());
  29. $set->clear();
  30. $this->assertEquals(0, $set->getCount());
  31. }
  32. public function testBehaviors() {
  33. $component = new CComponent;
  34. $hash = new ARedisHash("testAttribute".uniqid(),$this->getConnection());
  35. $component->attachBehavior("testAttribute",$hash);
  36. $this->assertTrue(isset($component->testAttribute));
  37. $this->assertTrue($component->testAttribute->add("test",true));
  38. $this->assertTrue((bool) $component->testAttribute['test']);
  39. $component->testAttribute->clear();
  40. }
  41. public function testInterfaces() {
  42. $redis = $this->getConnection();
  43. $set = new ARedisHash("TestHash:".uniqid(),$redis);
  44. $this->assertEquals(0,count($set));
  45. $set["test"] = 24;
  46. $set["test2"] = 12;
  47. $this->assertEquals(2,count($set));
  48. foreach($set as $item => $value) {
  49. $this->assertTrue($item == "test" || $item == "test2");
  50. }
  51. unset($set["test"]);
  52. $this->assertEquals(1, count($set));
  53. $set->clear();
  54. }
  55. /**
  56. * Sets the redis connection to use with this test
  57. * @param ARedisConnection $connection the connection
  58. */
  59. public function setConnection($connection)
  60. {
  61. $this->_connection = $connection;
  62. }
  63. /**
  64. * Gets the redis connection to use with this test
  65. * @return ARedisConnection the redis connection
  66. */
  67. public function getConnection()
  68. {
  69. if ($this->_connection === null) {
  70. $this->_connection = Yii::createComponent(
  71. array(
  72. "class" => "packages.redis.ARedisConnection",
  73. "hostname" => REDIS_HOSTNAME,
  74. "port" => REDIS_PORT,
  75. "database" => REDIS_DATABASE,
  76. "password" => REDIS_PASSWORD
  77. ));
  78. }
  79. return $this->_connection;
  80. }
  81. }