1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Cache\Tests\Dependency; |
4
|
|
|
|
5
|
|
|
require_once __DIR__ . '/../MockHelper.php'; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Cache\ArrayCache; |
8
|
|
|
use Yiisoft\Cache\Cache; |
9
|
|
|
use Yiisoft\Cache\CacheInterface; |
10
|
|
|
use Yiisoft\Cache\Dependency\TagDependency; |
11
|
|
|
use Yiisoft\Cache\Exception\InvalidArgumentException; |
12
|
|
|
use Yiisoft\Cache\Tests\MockHelper; |
13
|
|
|
|
14
|
|
|
class TagDependencyTest extends DependencyTestCase |
15
|
|
|
{ |
16
|
|
|
protected function tearDown(): void |
17
|
|
|
{ |
18
|
|
|
MockHelper::resetMocks(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected function createCache(): CacheInterface |
22
|
|
|
{ |
23
|
|
|
// isChanged of TagDependency needs cache access. |
24
|
|
|
// Using real cache. |
25
|
|
|
return new Cache(new ArrayCache()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testInvalidateByTag(): void |
29
|
|
|
{ |
30
|
|
|
$cache = $this->getCache(); |
31
|
|
|
$cache->set('item_42_price', 13, null, new TagDependency('item_42')); |
32
|
|
|
$cache->set('item_42_total', 26, null, new TagDependency('item_42')); |
33
|
|
|
|
34
|
|
|
$this->assertSame(13, $cache->get('item_42_price')); |
35
|
|
|
$this->assertSame(26, $cache->get('item_42_total')); |
36
|
|
|
|
37
|
|
|
TagDependency::invalidate($cache, 'item_42'); |
38
|
|
|
|
39
|
|
|
// keys are invalidated but are still there |
40
|
|
|
$this->assertTrue($cache->has('item_42_price')); |
41
|
|
|
$this->assertTrue($cache->has('item_42_total')); |
42
|
|
|
|
43
|
|
|
$this->assertNull($cache->get('item_42_price')); |
44
|
|
|
$this->assertNull($cache->get('item_42_total')); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testEmptyTags(): void |
48
|
|
|
{ |
49
|
|
|
$cache = $this->getCache(); |
50
|
|
|
$dependency = new TagDependency([]); |
51
|
|
|
$cache->set('item_42_price', 13, null, $dependency); |
52
|
|
|
$this->assertSame(13, $cache->get('item_42_price')); |
53
|
|
|
$this->assertSame([], $this->getInaccessibleProperty($dependency, 'data')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testInvalidTag(): void |
57
|
|
|
{ |
58
|
|
|
$this->expectException(InvalidArgumentException::class); |
59
|
|
|
MockHelper::$mock_json_encode = false; |
60
|
|
|
$cache = $this->getCache(); |
61
|
|
|
$dependency = new TagDependency(['test']); |
62
|
|
|
$dependency->isChanged($cache); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|