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