Passed
Push — master ( 4d0668...60ad61 )
by Petr
07:54
created

DualTest::testNotExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace StorageTests;
4
5
6
use kalanis\kw_cache\CacheException;
7
use kalanis\kw_cache\Interfaces\ICache;
8
use kalanis\kw_cache\Storage as CStor;
9
use kalanis\kw_storage\Interfaces\IStorage;
10
use kalanis\kw_storage\StorageException;
11
12
13
class DualTest extends AStorageTest
14
{
15
    /**
16
     * @throws CacheException
17
     * @throws StorageException
18
     */
19
    public function testRun(): void
20
    {
21
        $storage = $this->getStorage(new \MockStorage());
22
        $lib = new CStor\Dual($storage, $storage);
23
        $lib->init('');
24
25
        $this->assertFalse($lib->exists());
26
        $this->assertEquals('', $lib->get());
27
        // simple write
28
        $this->assertTrue($lib->set(static::TESTING_CONTENT));
29
        $this->assertEquals(static::TESTING_CONTENT, $lib->get());
30
        $this->assertTrue($lib->exists());
31
        // retry reload
32
        $storage->write(ICache::EXT_RELOAD, 'DUMMY'); // new reload key
33
        $this->assertTrue($lib->set(static::TESTING_CONTENT . static::TESTING_CONTENT));
34
        // clear all
35
        $lib->clear();
36
        $this->assertFalse($lib->exists());
37
    }
38
39
    /**
40
     * @throws CacheException
41
     */
42
    public function testNotSet(): void
43
    {
44
        $lib = new CStor\Dual($this->getStorage(new \MockFailedStorage()));
45
        $lib->init('');
46
47
        $this->assertFalse($lib->exists());
48
        $this->assertEquals('', $lib->get());
49
        $this->assertFalse($lib->set(static::TESTING_CONTENT));
50
        $this->assertFalse($lib->exists());
51
    }
52
53
    /**
54
     * @throws CacheException
55
     */
56
    public function testCannotSet(): void
57
    {
58
        $lib = new CStor\Dual($this->getStorage(new \MockKillingStorage2()));
59
        $lib->init('');
60
        $this->expectException(CacheException::class);
61
        $lib->set('lkjhgfdsa');
62
    }
63
64
    /**
65
     * @throws CacheException
66
     */
67
    public function testNotExists(): void
68
    {
69
        $lib = new CStor\Dual($this->getStorage(new \MockKillingStorage3()), $this->getStorage(new \MockFailedStorage()));
70
        $lib->init('');
71
        $this->expectException(CacheException::class);
72
        $lib->exists();
73
    }
74
75
    /**
76
     * @throws CacheException
77
     */
78
    public function testNotGet(): void
79
    {
80
        $lib = new CStor\Dual($this->getStorage(new \MockKillingStorage2()), $this->getStorage(new \MockFailedStorage()));
81
        $lib->init('');
82
        $this->expectException(CacheException::class);
83
        $lib->get();
84
    }
85
86
    /**
87
     * @throws CacheException
88
     */
89
    public function testNotClear(): void
90
    {
91
        $lib = new CStor\Dual($this->getStorage(new \MockKillingStorage2()));
92
        $lib->init('');
93
        $this->expectException(CacheException::class);
94
        $lib->clear();
95
    }
96
97
    public function testCompare(): void
98
    {
99
        // one target
100
        $lib = new CompareDual($this->getStorage(new \MockStorage()));
101
        $this->assertEquals($lib->getStorage(), $lib->getReload());
102
103
        // multiple targets
104
        $lib = new CompareDual($this->getStorage(new \MockStorage()), $this->getStorage('volume'));
105
        $this->assertNotEquals($lib->getStorage(), $lib->getReload());
106
    }
107
}
108
109
110
class CompareDual extends CStor\Dual
111
{
112
    public function getStorage(): IStorage
113
    {
114
        return $this->cacheStorage;
115
    }
116
117
    public function getReload(): IStorage
118
    {
119
        return $this->reloadStorage;
120
    }
121
}
122