/tests/ARedisSetTest.php

https://github.com/phpnode/YiiRedis · PHP · 166 lines · 131 code · 15 blank · 20 comment · 4 complexity · f1437f8993a8ac33a98ed4b751e9a3f7 MD5 · raw file

  1. <?php
  2. require_once("common.php");
  3. /**
  4. * Tests for the {@link ARedisSet} class
  5. * @author Charles Pick
  6. * @package packages.redis.tests
  7. */
  8. class ARedisSetTest 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 ARedisSet("TestSet:".uniqid(),$redis);
  20. $this->assertTrue($set->add("fish"));
  21. $this->assertTrue($set->add("chips"));
  22. $this->assertEquals(2, $set->getCount());
  23. $this->assertTrue($set->contains("fish"));
  24. $this->assertTrue($set->contains("chips"));
  25. $this->assertTrue($set->remove("fish"));
  26. $this->assertTrue($set->remove("chips"));
  27. $this->assertFalse($set->contains("fish"));
  28. $this->assertFalse($set->contains("chips"));
  29. $this->assertTrue($set->add("fish"));
  30. $this->assertTrue($set->add("chips"));
  31. $this->assertTrue(in_array($set->random(),array("fish", "chips")));
  32. $this->assertTrue(in_array($set->pop(),array("fish", "chips")));
  33. $this->assertTrue(in_array($set->pop(),array("fish", "chips")));
  34. $this->assertFalse($set->contains("fish"));
  35. $this->assertFalse($set->contains("chips"));
  36. }
  37. public function testDiff() {
  38. $redis = $this->getConnection();
  39. $set1 = new ARedisSet("TestSet1:".uniqid(),$redis);
  40. $set2 = new ARedisSet("TestSet2:".uniqid(),$redis);
  41. $this->assertTrue($set1->add("1"));
  42. $this->assertTrue($set2->add("1"));
  43. $this->assertTrue($set1->add("5"));
  44. $this->assertTrue($set2->add("10"));
  45. $this->assertTrue($set1->add("20"));
  46. $this->assertTrue($set2->add("20"));
  47. $this->assertEquals(array(5),$set1->diff($set2->name));
  48. $this->assertEquals(array(10),$set2->diff($set1->name));
  49. $newSet = $set1->diffStore("TestSet3:".uniqid(),$set2);
  50. $this->assertEquals(array(5),$newSet->getData());
  51. $newSet->clear();
  52. $set1->clear();
  53. $set2->clear();
  54. }
  55. public function testInter() {
  56. $redis = $this->getConnection();
  57. $set1 = new ARedisSet("TestSet1:".uniqid(),$redis);
  58. $set2 = new ARedisSet("TestSet2:".uniqid(),$redis);
  59. $this->assertTrue($set1->add("1"));
  60. $this->assertTrue($set2->add("1"));
  61. $this->assertTrue($set1->add("5"));
  62. $this->assertTrue($set2->add("10"));
  63. $this->assertTrue($set1->add("20"));
  64. $this->assertTrue($set2->add("20"));
  65. $this->assertEquals(array(1, 20),$set1->inter($set2->name));
  66. $this->assertEquals(array(1, 20),$set2->inter($set1->name));
  67. $newSet = $set1->interStore("TestSet3:".uniqid(),$set2);
  68. $this->assertEquals(array(1, 20),$newSet->getData());
  69. $newSet->clear();
  70. $set1->clear();
  71. $set2->clear();
  72. }
  73. public function testUnion() {
  74. $redis = $this->getConnection();
  75. $set1 = new ARedisSet("TestSet1:".uniqid(),$redis);
  76. $set2 = new ARedisSet("TestSet2:".uniqid(),$redis);
  77. $this->assertTrue($set1->add("1"));
  78. $this->assertTrue($set2->add("1"));
  79. $this->assertTrue($set1->add("5"));
  80. $this->assertTrue($set2->add("10"));
  81. $this->assertTrue($set1->add("20"));
  82. $this->assertTrue($set2->add("20"));
  83. $sorted = $set1->union($set2);
  84. sort($sorted);
  85. $this->assertEquals(array(1, 5, 10, 20),$sorted);
  86. $newSet = $set1->unionStore("TestSet3:".uniqid(),$set2);
  87. $sorted = $newSet->getData();
  88. sort($sorted);
  89. $this->assertEquals(array(1, 5, 10, 20),$sorted);
  90. $newSet->clear();
  91. $set1->clear();
  92. $set2->clear();
  93. }
  94. public function testMove() {
  95. $redis = $this->getConnection();
  96. $set1 = new ARedisSet("TestSet1:".uniqid(),$redis);
  97. $set2 = new ARedisSet("TestSet2:".uniqid(),$redis);
  98. $this->assertTrue($set1->add("1"));
  99. $this->assertTrue($set2->add("1"));
  100. $this->assertTrue($set1->add("5"));
  101. $this->assertTrue($set1->add("10"));
  102. $this->assertTrue($set1->move($set2,"1"));
  103. $this->assertFalse($set1->move($set2,"1"));
  104. $this->assertTrue($set1->move($set2,"5"));
  105. $this->assertTrue($set1->move($set2,"10"));
  106. $this->assertEquals(0, $set1->getCount());
  107. $this->assertEquals(3, $set2->getCount());
  108. $set1->clear();
  109. $set2->clear();
  110. }
  111. public function testInterfaces() {
  112. $redis = $this->getConnection();
  113. $set = new ARedisSet("TestSet:".uniqid(),$redis);
  114. $this->assertEquals(0,count($set));
  115. $set[] = "test";
  116. $set[] = "test2";
  117. $this->assertEquals(2,count($set));
  118. foreach($set as $item) {
  119. $this->assertTrue($item == "test" || $item == "test2");
  120. }
  121. unset($set[0]);
  122. $this->assertEquals(1, count($set));
  123. $set->clear();
  124. }
  125. /**
  126. * Sets the redis connection to use with this test
  127. * @param ARedisConnection $connection the connection
  128. */
  129. public function setConnection($connection)
  130. {
  131. $this->_connection = $connection;
  132. }
  133. /**
  134. * Gets the redis connection to use with this test
  135. * @return ARedisConnection the redis connection
  136. */
  137. public function getConnection()
  138. {
  139. if ($this->_connection === null) {
  140. $this->_connection = Yii::createComponent(
  141. array(
  142. "class" => "packages.redis.ARedisConnection",
  143. "hostname" => REDIS_HOSTNAME,
  144. "port" => REDIS_PORT,
  145. "database" => REDIS_DATABASE,
  146. "password" => REDIS_PASSWORD
  147. ));
  148. }
  149. return $this->_connection;
  150. }
  151. }