ARedisCounterTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. require_once("common.php");
  3. /**
  4. * Tests for the {@link ARedisCounter} class
  5. * @author Charles Pick
  6. * @package packages.redis.tests
  7. */
  8. class ARedisCounterTest 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. $counter = new ARedisCounter("TestCounter:".uniqid(),$redis);
  20. $this->assertEquals(0,$counter->getValue());
  21. $this->assertEquals(1,$counter->increment());
  22. $this->assertEquals(11,$counter->increment(10));
  23. $this->assertEquals(11,$counter->getValue());
  24. $this->assertEquals(10,$counter->decrement());
  25. $this->assertEquals(5,$counter->decrement(5));
  26. $this->assertEquals(0,$counter->decrement(5));
  27. $counter->clear();
  28. $this->assertFalse($redis->getClient()->exists($counter->name));
  29. }
  30. /**
  31. * Sets the redis connection to use with this test
  32. * @param ARedisConnection $connection the connection
  33. */
  34. public function setConnection($connection)
  35. {
  36. $this->_connection = $connection;
  37. }
  38. /**
  39. * Gets the redis connection to use with this test
  40. * @return ARedisConnection the redis connection
  41. */
  42. public function getConnection()
  43. {
  44. if ($this->_connection === null) {
  45. $this->_connection = Yii::createComponent(
  46. array(
  47. "class" => "packages.redis.ARedisConnection",
  48. "hostname" => REDIS_HOSTNAME,
  49. "port" => REDIS_PORT,
  50. "database" => REDIS_DATABASE,
  51. "password" => REDIS_PASSWORD
  52. ));
  53. }
  54. return $this->_connection;
  55. }
  56. }