Passed
Pull Request — master (#36)
by Alexander
02:03
created

TagDependencyTest::testInvalidTag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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