Passed
Push — master ( 380356...2e1f1e )
by Petr
10:45
created

DualTest::testNotSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace CacheTests;
4
5
6
use kalanis\kw_cache\Cache;
7
use kalanis\kw_cache\CacheException;
8
use kalanis\kw_cache\Interfaces\ICache;
9
use kalanis\kw_cache\Simple;
10
11
12
class DualTest extends ACacheTest
13
{
14
    /**
15
     * @throws CacheException
16
     */
17
    public function testRun(): void
18
    {
19
        $storage = new Simple\Variable();
20
        $lib = new Cache\Dual(new Simple\Variable(), $storage);
21
        $lib->init('');
22
23
        $this->assertFalse($lib->exists());
24
        $this->assertEquals('', $lib->get());
25
        // simple write
26
        $this->assertTrue($lib->set(static::TESTING_CONTENT));
27
        $this->assertEquals(static::TESTING_CONTENT, $lib->get());
28
        $this->assertTrue($lib->exists());
29
        // retry reload
30
        $storage->set('DUMMY'); // new reload key
31
        $this->assertTrue($lib->set(static::TESTING_CONTENT . static::TESTING_CONTENT));
32
        // clear all
33
        $lib->clear();
34
        $this->assertFalse($lib->exists());
35
    }
36
37
    /**
38
     * @throws CacheException
39
     */
40
    public function testNotSet(): void
41
    {
42
        $lib = new CacheDual(new MockCacheFail());
43
        $lib->init('');
44
45
        $this->assertFalse($lib->exists());
46
        $this->assertEquals('', $lib->get());
47
        $this->assertFalse($lib->set(static::TESTING_CONTENT));
48
        $this->assertFalse($lib->exists());
49
    }
50
51
    /**
52
     * @throws CacheException
53
     */
54
    public function testFailSet(): void
55
    {
56
        $lib = new Cache\Dual(new Simple\Variable(), new MockCacheKill());
57
        $this->expectException(CacheException::class);
58
        $lib->set(static::TESTING_CONTENT . static::TESTING_CONTENT);
59
    }
60
61
    public function testCompare(): void
62
    {
63
        // one target
64
        $lib = new CompareDual(new Simple\Memory());
65
        $this->assertEquals($lib->getStorage(), $lib->getReload());
66
67
        // multiple targets
68
        $lib = new CompareDual(new Simple\Memory(), new Simple\Variable());
69
        $this->assertNotEquals($lib->getStorage(), $lib->getReload());
70
    }
71
}
72
73
74
class CacheDual extends Cache\Dual
75
{
76
    public function setReload(ICache $reloadCache): void
77
    {
78
        $this->reloadCache = $reloadCache;
79
    }
80
}
81
82
83
class CompareDual extends Cache\Dual
84
{
85
    public function getStorage(): ICache
86
    {
87
        return $this->storageCache;
88
    }
89
90
    public function getReload(): ICache
91
    {
92
        return $this->reloadCache;
93
    }
94
}
95