Passed
Push — master ( 33fccf...01de27 )
by Petr
07:15
created

MockKillingStorage   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 45
rs 10
c 1
b 0
f 0
wmc 9
1
<?php
2
3
use kalanis\kw_storage\Interfaces\IStorage;
4
use kalanis\kw_storage\StorageException;
5
use PHPUnit\Framework\TestCase;
6
7
8
/**
9
 * Class CommonTestClass
10
 * The structure for mocking and configuration seems so complicated, but it's necessary to let it be totally idiot-proof
11
 */
12
class CommonTestClass extends TestCase
13
{
14
}
15
16
17
class MockStorage implements IStorage
18
{
19
    protected $data = [];
20
21
    public function check(string $key): bool
22
    {
23
        return true;
24
    }
25
26
    public function exists(string $key): bool
27
    {
28
        return isset($this->data[$key]);
29
    }
30
31
    public function load(string $key)
32
    {
33
        return $this->exists($key) ? $this->data[$key] : null ;
34
    }
35
36
    public function save(string $key, $data, ?int $timeout = null): bool
37
    {
38
        $this->data[$key] = $data;
39
        return true;
40
    }
41
42
    public function remove(string $key): bool
43
    {
44
        if ($this->exists($key)) {
45
            unset($this->data[$key]);
46
        }
47
        return true;
48
    }
49
50
    public function lookup(string $key): Traversable
51
    {
52
        yield from [];
53
    }
54
55
    public function increment(string $key): bool
56
    {
57
        $this->data[$key] = $this->exists($key) ? $this->data[$key] + 1 : 1 ;
58
        return true;
59
    }
60
61
    public function decrement(string $key): bool
62
    {
63
        $this->data[$key] = $this->exists($key) ? $this->data[$key] - 1 : 0 ;
64
        return true;
65
    }
66
67
    public function removeMulti(array $keys): array
68
    {
69
        $result = [];
70
        foreach ($keys as $index => $key) {
71
            $result[$index] = $this->remove($key);
72
        }
73
        return $result;
74
    }
75
}
76
77
78
class MockFailedStorage implements IStorage
79
{
80
    public function check(string $key): bool
81
    {
82
        return false;
83
    }
84
85
    public function exists(string $key): bool
86
    {
87
        return false;
88
    }
89
90
    public function load(string $key)
91
    {
92
        return null;
93
    }
94
95
    public function save(string $key, $data, ?int $timeout = null): bool
96
    {
97
        return false;
98
    }
99
100
    public function remove(string $key): bool
101
    {
102
        return false;
103
    }
104
105
    public function lookup(string $key): Traversable
106
    {
107
        yield from [];
108
    }
109
110
    public function increment(string $key): bool
111
    {
112
        return false;
113
    }
114
115
    public function decrement(string $key): bool
116
    {
117
        return false;
118
    }
119
120
    public function removeMulti(array $keys): array
121
    {
122
        return [];
123
    }
124
}
125
126
127
class MockKillingStorage implements IStorage
128
{
129
    public function check(string $key): bool
130
    {
131
        return false;
132
    }
133
134
    public function exists(string $key): bool
135
    {
136
        return false;
137
    }
138
139
    public function load(string $key)
140
    {
141
        throw new StorageException('mock fail');
142
    }
143
144
    public function save(string $key, $data, ?int $timeout = null): bool
145
    {
146
        throw new StorageException('mock fail');
147
    }
148
149
    public function remove(string $key): bool
150
    {
151
        throw new StorageException('mock fail');
152
    }
153
154
    public function lookup(string $key): Traversable
155
    {
156
        throw new StorageException('mock fail');
157
    }
158
159
    public function increment(string $key): bool
160
    {
161
        throw new StorageException('mock fail');
162
    }
163
164
    public function decrement(string $key): bool
165
    {
166
        throw new StorageException('mock fail');
167
    }
168
169
    public function removeMulti(array $keys): array
170
    {
171
        throw new StorageException('mock fail');
172
    }
173
}
174