forked from pvolyntsev/yii-cache-tag-dependency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DependencyTest.php
133 lines (107 loc) · 4.31 KB
/
DependencyTest.php
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
<?php
use \TaggedCache\Dependency;
use \TaggedCache\Tag;
class TaggedCache_DependencyTest extends CTestCase
{
/**
* Test if new tags have no value
*/
public function testUnknownTagVersionHaveNoValue()
{
/** @var \CCache $cache */
$cache = \Yii::app()->cache;
$unknownTag = new Tag(uniqid());
$this->assertFalse($unknownTag->get(), 'Unknown tag must be FALSE');
}
/**
* Test if previously saved tags have old values
*/
public function testKnownTagVersionHasOldValue()
{
/** @var \CCache $cache */
$cache = \Yii::app()->cache;
$oldTagKey = uniqid();
$oldTag = new Tag($oldTagKey);
$oldTagVersion = microtime();
$oldTag->set($oldTagVersion)->save();
$unknownTag = new Tag(uniqid());
$knownTag = new Tag($oldTagKey); // new Tag instance with the same key as $oldTag
$this->assertFalse($unknownTag->get(), 'Unknown tag must be FALSE');
$this->assertEquals($oldTagVersion, $knownTag->get(), 'Known tag must have the OLD value');
}
/**
* Test if tags not changed if used in several Dependency instances
*/
public function testCacheTagVersionsNotChangedIfSavedAgain()
{
/** @var \CCache $cache */
$cache = \Yii::app()->cache;
$key12 = 'key|'.uniqid().'|12';
$value12 = 'value|'.uniqid().'|12';
$key1 = 'key|'.uniqid().'|1';
$value1 = 'value|'.uniqid().'|1';
$key2 = 'key|'.uniqid().'|2';
$value2 = 'value|'.uniqid().'|2';
#1 save one tag
$tag1 = new Tag(uniqid());
$tag2 = new Tag(uniqid());
$this->assertFalse($tag1->get(), 'Not saved tag must be FALSE');
$this->assertFalse($tag2->get(), 'Not saved tag must be FALSE');
$cache->set($key1, $value1, 20, new Dependency(array($tag1)));
$tag1Version = $tag1->get();
$this->assertInternalType('string', $tag1Version, 'Saved tag must have VERSION after save');
$this->assertFalse($tag2->get(), 'Not saved tag must be FALSE');
$cache->set($key2, $value2, 20, new Dependency(array($tag2)));
$tag2Version = $tag2->get();
$this->assertEquals($tag1Version, $tag1->get(), 'Version of tag must not changed if tag not deleted');
$this->assertInternalType('string', $tag2Version, 'Saved tag must have VERSION after save');
#2 save two tags
$cache->set($key12, $value12, 20, new Dependency(array($tag1, $tag2)));
$this->assertEquals($tag1Version, $tag1->get(), 'Version of tag must not changed if tag not deleted');
$this->assertEquals($tag2Version, $tag2->get(), 'Version of tag must not changed if tag not deleted');
}
/**
* Test if tags have new value if tag deleted and used again
*/
public function testCacheTagVersionsMustUpdatedIfTagDeleted()
{
/** @var \CCache $cache */
$cache = \Yii::app()->cache;
$key1 = 'key|'.uniqid().'|1';
$value1 = 'value|'.uniqid().'|1';
#1 save tag
$tag1 = new Tag(uniqid());
$cache->set($key1, $value1, 20, new Dependency(array($tag1)));
$tag1Version = $tag1->get();
#2 delete tag
$tag1->delete();
$cache->set($key1, $value1, 20, new Dependency(array($tag1)));
$this->assertInternalType('string', $tag1->get(), 'Saved tag must have VERSION after save');
$this->assertNotEquals($tag1Version, $tag1->get(), 'Deleted tag must have NEW value if deleted and used again');
}
public function testDependencyChangedIfTagDeleted()
{
/** @var \CCache $cache */
$cache = \Yii::app()->cache;
$key12 = 'key|'.microtime().'|12';
$value12 = 'value|'.microtime().'|12';
$key1 = 'key|'.microtime().'|1';
$value1 = 'value|'.microtime().'|1';
$key2 = 'key|'.microtime().'|2';
$value2 = 'value|'.microtime().'|2';
$tag1 = new Tag(uniqid());
$tag2 = new Tag(uniqid());
$cache->set($key12, $value12, 20, new Dependency(array($tag1, $tag2)));
$cache->set($key1, $value1, 20, new Dependency(array($tag1)));
$cache->set($key2, $value2, 20, new Dependency(array($tag2)));
#1 correct values saved
$this->assertEquals($value12, $cache->get($key12), 'Incorrect value in cache');
$this->assertEquals($value1, $cache->get($key1), 'Incorrect value in cache');
$this->assertEquals($value2, $cache->get($key2), 'Incorrect value in cache');
#2 delete tag 1
$tag1->delete();
$this->assertFalse($cache->get($key12), 'Cached value must become invalid if tag deleted');
$this->assertFalse($cache->get($key1), 'Cached value must become invalid if tag deleted');
$this->assertEquals($value2, $cache->get($key2), 'Cached value must become invalid if not used tag which was deleted');
}
}