CacheTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace yiiunit\extensions\mongodb;
  3. use Yii;
  4. use yii\mongodb\Cache;
  5. class CacheTest extends TestCase
  6. {
  7. /**
  8. * @var string test cache collection name.
  9. */
  10. protected static $cacheCollection = '_test_cache';
  11. protected function tearDown()
  12. {
  13. $this->dropCollection(static::$cacheCollection);
  14. parent::tearDown();
  15. }
  16. /**
  17. * Creates test cache instance.
  18. * @return Cache cache instance.
  19. */
  20. protected function createCache()
  21. {
  22. return Yii::createObject([
  23. 'class' => Cache::className(),
  24. 'db' => $this->getConnection(),
  25. 'cacheCollection' => static::$cacheCollection,
  26. 'gcProbability' => 0,
  27. ]);
  28. }
  29. // Tests:
  30. public function testSet()
  31. {
  32. $cache = $this->createCache();
  33. $key = 'test_key';
  34. $value = 'test_value';
  35. $this->assertTrue($cache->set($key, $value), 'Unable to set value!');
  36. $this->assertEquals($value, $cache->get($key), 'Unable to set value correctly!');
  37. $newValue = 'test_new_value';
  38. $this->assertTrue($cache->set($key, $newValue), 'Unable to update value!');
  39. $this->assertEquals($newValue, $cache->get($key), 'Unable to update value correctly!');
  40. }
  41. public function testAdd()
  42. {
  43. $cache = $this->createCache();
  44. $key = 'test_key';
  45. $value = 'test_value';
  46. $this->assertTrue($cache->add($key, $value), 'Unable to add value!');
  47. $this->assertEquals($value, $cache->get($key), 'Unable to add value correctly!');
  48. $newValue = 'test_new_value';
  49. $this->assertTrue($cache->add($key, $newValue), 'Unable to re-add value!');
  50. $this->assertEquals($value, $cache->get($key), 'Original value is lost!');
  51. }
  52. /**
  53. * @depends testSet
  54. */
  55. public function testDelete()
  56. {
  57. $cache = $this->createCache();
  58. $key = 'test_key';
  59. $value = 'test_value';
  60. $cache->set($key, $value);
  61. $this->assertTrue($cache->delete($key), 'Unable to delete key!');
  62. $this->assertEquals(false, $cache->get($key), 'Value is not deleted!');
  63. }
  64. /**
  65. * @depends testSet
  66. */
  67. public function testFlush()
  68. {
  69. $cache = $this->createCache();
  70. $cache->set('key1', 'value1');
  71. $cache->set('key2', 'value2');
  72. $this->assertTrue($cache->flush(), 'Unable to flush cache!');
  73. $collection = $cache->db->getCollection($cache->cacheCollection);
  74. $rows = $this->findAll($collection);
  75. $this->assertCount(0, $rows, 'Unable to flush records!');
  76. }
  77. /**
  78. * @depends testSet
  79. */
  80. public function testGc()
  81. {
  82. $cache = $this->createCache();
  83. $cache->set('key1', 'value1');
  84. $cache->set('key2', 'value2');
  85. $collection = $cache->db->getCollection($cache->cacheCollection);
  86. list($row) = $this->findAll($collection);
  87. $collection->update(['_id' => $row['_id']], ['expire' => time() - 10]);
  88. $cache->gc(true);
  89. $rows = $this->findAll($collection);
  90. $this->assertCount(1, $rows, 'Unable to collect garbage!');
  91. }
  92. /**
  93. * @depends testSet
  94. */
  95. public function testGetExpired()
  96. {
  97. $cache = $this->createCache();
  98. $key = 'test_key';
  99. $value = 'test_value';
  100. $cache->set($key, $value);
  101. $collection = $cache->db->getCollection($cache->cacheCollection);
  102. list($row) = $this->findAll($collection);
  103. $collection->update(['_id' => $row['_id']], ['expire' => time() - 10]);
  104. $this->assertEquals(false, $cache->get($key), 'Expired key value returned!');
  105. }
  106. }