ARedisRecordTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. require_once("common.php");
  3. /**
  4. * Tests for the {@link ARedisRecord} class
  5. * @author Charles Pick
  6. * @package packages.redis.tests
  7. */
  8. class ARedisRecordTest 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. $model = new ExampleRedisRecord();
  20. $model->name = "Test";
  21. $this->assertTrue($model->save());
  22. $this->assertGreaterThan(0,$model->id);
  23. $model2 = new ExampleRedisRecord();
  24. $model2->name = "Test 2";
  25. $this->assertTrue($model2->save());
  26. $this->assertGreaterThan(0,$model2->id);
  27. $this->assertTrue(is_object(ExampleRedisRecord::model()->findByPk($model->id)));
  28. $this->assertTrue(is_object(ExampleRedisRecord::model()->findByPk($model2->id)));
  29. $this->assertGreaterThan(1,$model->count());
  30. $this->assertTrue($model->delete());
  31. $this->assertTrue($model2->delete());
  32. $startTime = microtime(true);
  33. $ids = array();
  34. for($i = 0; $i < 1000; $i++) {
  35. $model = new ExampleRedisRecord();
  36. $model->name = "Bulk Test Item ".$i;
  37. $model->address = "Some test address ".$i;
  38. $this->assertTrue($model->save());
  39. $ids[] = $model->id;
  40. }
  41. $totalTime = (microtime(true) - $startTime);
  42. echo "Inserted 1000 redis records in ".$totalTime." seconds\n";
  43. $startTime = microtime(true);
  44. $n = 0;
  45. foreach($model->getRedisSet() as $key) {
  46. $n++;
  47. }
  48. $this->assertGreaterThanOrEqual(999,$n);
  49. $totalTime = (microtime(true) - $startTime);
  50. echo "Traversed 1000 redis record keys in ".$totalTime." seconds\n";
  51. $startTime = microtime(true);
  52. $models = ExampleRedisRecord::model()->findAllByPk($ids);
  53. echo "Found 1000 redis records in ".(microtime(true) - $startTime)." seconds\n";
  54. $this->assertEquals(1000,count($models));
  55. foreach($models as $model) {
  56. $this->assertTrue($model->delete());
  57. }
  58. }
  59. /**
  60. * Sets the redis connection to use with this test
  61. * @param ARedisConnection $connection the connection
  62. */
  63. public function setConnection($connection)
  64. {
  65. $this->_connection = $connection;
  66. }
  67. /**
  68. * Gets the redis connection to use with this test
  69. * @return ARedisConnection the redis connection
  70. */
  71. public function getConnection()
  72. {
  73. if ($this->_connection === null) {
  74. $this->_connection = Yii::createComponent(
  75. array(
  76. "class" => "packages.redis.ARedisConnection",
  77. "hostname" => REDIS_HOSTNAME,
  78. "port" => REDIS_PORT,
  79. "database" => REDIS_DATABASE,
  80. "password" => REDIS_PASSWORD
  81. ));
  82. }
  83. return $this->_connection;
  84. }
  85. }
  86. /**
  87. * An example of a redis record
  88. * @author Charles Pick
  89. * @package packages.redis.tests
  90. */
  91. class ExampleRedisRecord extends ARedisRecord {
  92. /**
  93. * The primary key
  94. * @var integer
  95. */
  96. public $id;
  97. /**
  98. * The name attribute
  99. * @var string
  100. */
  101. public $name;
  102. /**
  103. * The address attribute, with a default value
  104. * @var string
  105. */
  106. public $address = "Not Yet Added";
  107. /**
  108. * Gets the static model
  109. * @param string $className the model class to instantiate
  110. * @return ExampleRedisRecord the model
  111. */
  112. public static function model($className = __CLASS__) {
  113. return parent::model($className);
  114. }
  115. }