MockVariables::getCookie()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace FilteredTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_input\Entries;
8
use kalanis\kw_input\Filtered;
9
use kalanis\kw_input\Inputs;
10
use kalanis\kw_input\Interfaces;
11
12
13
class VariablesTest extends CommonTestClass
14
{
15
    public function testBasics(): void
16
    {
17
        $input = new Inputs();
18
        $input->setSource($this->cliDataset()); // direct cli
19
20
        $source = new MockSource();
21
        $source->setRemotes($this->entryDataset(), null, $this->cliDataset());
22
        $variables = new MockVariables($input->setSource($source)->loadEntries());
23
24
        $this->assertNotEmpty($variables->getCli());
25
        $this->assertNotEmpty($variables->getGet());
26
        $this->assertEmpty($variables->getPost());
27
        $this->assertEmpty($variables->getSession());
28
        $this->assertEmpty($variables->getCookie());
29
        $this->assertNotEmpty($variables->getFiles()); // seems strange, but there are files from Cli
30
        $this->assertEmpty($variables->getServer());
31
        $this->assertEmpty($variables->getEnv());
32
        $this->assertNotEmpty($variables->getBasic());
33
        $this->assertEmpty($variables->getSystem());
34
        $this->assertEmpty($variables->getExternal());
35
36
        $entries = $variables->getInArray(null, [Interfaces\IEntry::SOURCE_GET]);
37
        $this->assertNotEmpty($entries);
38
39
        $entry = reset($entries);
40
        $this->assertEquals('foo', key($entries));
41
        $this->assertEquals('foo', $entry->getKey());
42
        $this->assertEquals('val1', $entry->getValue());
43
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $entry->getSource());
44
45
        $entry = next($entries);
46
        $this->assertEquals('bar', key($entries));
47
        $this->assertEquals('bar', $entry->getKey());
48
        $this->assertEquals(['bal1', 'bal2'], $entry->getValue());
49
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $entry->getSource());
50
51
        $entry = next($entries);
52
        $this->assertEquals('baz', key($entries));
53
        $this->assertEquals('baz', $entry->getKey());
54
        $this->assertEquals(true, $entry->getValue());
55
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $entry->getSource());
56
57
        $entry = next($entries);
58
        $this->assertEquals('aff', key($entries));
59
        $this->assertEquals('aff', $entry->getKey());
60
        $this->assertEquals(42, $entry->getValue());
61
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $entry->getSource());
62
    }
63
64
    public function testFiles(): void
65
    {
66
        $source = new MockSource();
67
        $source->setRemotes($this->entryDataset(), null, null, $this->fileDataset());
68
69
        $variables = new MockVariables((new Inputs())->setSource($source)->loadEntries());
70
71
        $this->assertEmpty($variables->getCli());
72
        $this->assertNotEmpty($variables->getGet());
73
        $this->assertEmpty($variables->getPost());
74
        $this->assertEmpty($variables->getSession());
75
        $this->assertNotEmpty($variables->getFiles());
76
        $this->assertEmpty($variables->getCookie());
77
        $this->assertEmpty($variables->getServer());
78
        $this->assertEmpty($variables->getEnv());
79
        $this->assertNotEmpty($variables->getBasic());
80
        $this->assertEmpty($variables->getSystem());
81
        $this->assertEmpty($variables->getExternal());
82
83
        $entries = $variables->getInArray(null, [Interfaces\IEntry::SOURCE_FILES]);
84
        $this->assertNotEmpty($entries);
85
86
        $entry = reset($entries);
87
        $this->assertEquals('files', key($entries));
88
        $this->assertEquals('files', $entry->getKey());
89
        $this->assertEquals('facepalm.jpg', $entry->getValue());
90
        $this->assertEquals(Interfaces\IEntry::SOURCE_FILES, $entry->getSource());
91
92
        $entry = next($entries);
93
        $this->assertEquals('download[file1]', key($entries));
94
        $this->assertEquals('download[file1]', $entry->getKey());
95
        $this->assertEquals('MyFile.txt', $entry->getValue());
96
        $this->assertEquals(Interfaces\IEntry::SOURCE_FILES, $entry->getSource());
97
98
        $entry = next($entries);
99
        $this->assertEquals('download[file2]', key($entries));
100
        $this->assertEquals('download[file2]', $entry->getKey());
101
        $this->assertEquals('MyFile.jpg', $entry->getValue());
102
        $this->assertEquals(Interfaces\IEntry::SOURCE_FILES, $entry->getSource());
103
    }
104
105
    public function testObject(): void
106
    {
107
        $input = new Inputs();
108
        $input->setSource($this->cliDataset()); // direct cli
109
110
        $source = new MockSource();
111
        $source->setRemotes($this->entryDataset());
112
        $variables = new MockVariables($input->setSource($source)->loadEntries());
113
114
        $this->assertNotEmpty($variables->getGet());
115
116
        /** @var Interfaces\IEntry[] $entries */
117
        $entries = $variables->getInArray(null, [Interfaces\IEntry::SOURCE_GET]);
118
        $input = new Filtered\FilterAdapter($variables, [Interfaces\IEntry::SOURCE_GET]);
119
        $this->assertEquals(4, $input->count());
120
        $this->assertNotEmpty(count($entries));
121
122
        $this->assertTrue(isset($input['foo']));
123
        $this->assertEquals('foo', $input['foo']->getKey());
0 ignored issues
show
Bug introduced by
The method getKey() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
        $this->assertEquals('foo', $input['foo']->/** @scrutinizer ignore-call */ getKey());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124
        $this->assertEquals('val1', $input['foo']->getValue());
125
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $input['foo']->getSource());
126
127
        $this->assertTrue($input->offsetExists('bar'));
128
        $this->assertEquals('bar', $input->offsetGet('bar')->getKey());
129
        $this->assertEquals(['bal1', 'bal2'], $input->offsetGet('bar')->getValue());
130
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $input->offsetGet('bar')->getSource());
131
132
        $this->assertTrue(isset($input->baz));
0 ignored issues
show
Bug Best Practice introduced by
The property baz does not exist on kalanis\kw_input\Filtered\FilterAdapter. Since you implemented __get, consider adding a @property annotation.
Loading history...
133
        $this->assertEquals('baz', $input->baz->getKey());
0 ignored issues
show
Bug introduced by
The method getKey() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

133
        $this->assertEquals('baz', $input->baz->/** @scrutinizer ignore-call */ getKey());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
        $this->assertEquals(true, $input->baz->getValue());
135
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $input->baz->getSource());
136
137
        $this->assertTrue($input->offsetExists('aff'));
138
        $this->assertEquals('aff', $input->offsetGet('aff')->getKey());
139
        $this->assertEquals(42, $input->offsetGet('aff')->getValue());
140
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $input->offsetGet('aff')->getSource());
141
142
        $this->assertFalse($input->offsetExists('uhb'));
143
        $input->offsetSet('uhb', 'feaht');
144
        $this->assertEquals('feaht', $input->offsetGet('uhb')->getValue());
145
        $this->assertEquals(Interfaces\IEntry::SOURCE_EXTERNAL, $input->offsetGet('uhb')->getSource());
146
147
        $entry = $input->offsetGet('aff');
148
        unset($input['aff']);
149
        $this->assertFalse(isset($input['aff']));
150
        $input[$entry->getKey()] = $entry;
151
        $this->assertTrue($input->offsetExists('aff'));
152
        $input[$entry->getKey()] = 'tfc';
153
        $this->assertEquals('tfc', $input->offsetGet('aff'));
154
155
        $entry = $input->baz;
156
        unset($input->baz);
157
        $this->assertTrue(empty($input->baz));
158
        $input->{$entry->getKey()} = $entry;
159
        $this->assertTrue(isset($input->baz));
160
    }
161
}
162
163
164
class MockSource implements Interfaces\ISource
165
{
166
    protected ?array $mockCli = null;
167
    protected ?array $mockGet = null;
168
    protected ?array $mockPost = null;
169
    protected ?array $mockFiles = null;
170
    protected ?array $mockCookie = null;
171
    protected ?array $mockSession = null;
172
173
    public function setRemotes(?array $get, ?array $post = null, ?array $cli = null, ?array $files = null, ?array $cookie = null, ?array $session = null): self
174
    {
175
        $this->mockCli = $cli;
176
        $this->mockGet = $get;
177
        $this->mockPost = $post;
178
        $this->mockFiles = $files;
179
        $this->mockCookie = $cookie;
180
        $this->mockSession = $session;
181
        return $this;
182
    }
183
184
    public function cli(): ?array
185
    {
186
        return $this->mockCli;
187
    }
188
189
    public function get(): ?array
190
    {
191
        return $this->mockGet;
192
    }
193
194
    public function post(): ?array
195
    {
196
        return $this->mockPost;
197
    }
198
199
    public function files(): ?array
200
    {
201
        return $this->mockFiles;
202
    }
203
204
    public function cookie(): ?array
205
    {
206
        return $this->mockCookie;
207
    }
208
209
    public function session(): ?array
210
    {
211
        return $this->mockSession;
212
    }
213
214
    public function server(): ?array
215
    {
216
        $content = null;
217
        return $content;
218
    }
219
220
    public function env(): ?array
221
    {
222
        $content = null;
223
        return $content;
224
    }
225
226
    public function external(): ?array
227
    {
228
        $content = null;
229
        return $content;
230
    }
231
232
    public function inputRawPaths(): ?array
233
    {
234
        return [];
235
    }
236
}
237
238
239
class MockVariables extends Filtered\Variables
240
{
241
    public function getBasic(): array
242
    {
243
        return $this->getInArray(null, [
244
            Interfaces\IEntry::SOURCE_CLI,
245
            Interfaces\IEntry::SOURCE_GET,
246
            Interfaces\IEntry::SOURCE_POST,
247
        ]);
248
    }
249
250
    public function getSystem(): array
251
    {
252
        return $this->getInArray(null, [
253
            Interfaces\IEntry::SOURCE_SERVER,
254
            Interfaces\IEntry::SOURCE_ENV,
255
        ]);
256
    }
257
258
    public function getCli(): array
259
    {
260
        return $this->getInArray(null, [Interfaces\IEntry::SOURCE_CLI]);
261
    }
262
263
    public function getGet(): array
264
    {
265
        return $this->getInArray(null, [Interfaces\IEntry::SOURCE_GET]);
266
    }
267
268
    public function getPost(): array
269
    {
270
        return $this->getInArray(null, [Interfaces\IEntry::SOURCE_POST]);
271
    }
272
273
    public function getSession(): array
274
    {
275
        return $this->getInArray(null, [Interfaces\IEntry::SOURCE_SESSION]);
276
    }
277
278
    public function getCookie(): array
279
    {
280
        return $this->getInArray(null, [Interfaces\IEntry::SOURCE_COOKIE]);
281
    }
282
283
    public function getFiles(): array
284
    {
285
        return $this->getInArray(null, [Interfaces\IEntry::SOURCE_FILES]);
286
    }
287
288
    public function getServer(): array
289
    {
290
        return $this->getInArray(null, [Interfaces\IEntry::SOURCE_SERVER]);
291
    }
292
293
    public function getEnv(): array
294
    {
295
        return $this->getInArray(null, [Interfaces\IEntry::SOURCE_ENV]);
296
    }
297
298
    public function getExternal(): array
299
    {
300
        return $this->getInArray(null, [Interfaces\IEntry::SOURCE_EXTERNAL]);
301
    }
302
}
303
304
305
class ExEntry extends Entries\Entry
306
{
307
    public static function init(string $source, string $key, $value = null): Entries\Entry
308
    {
309
        $lib = new self();
310
        $lib->setEntry($source, $key, $value);
311
        return $lib;
312
    }
313
}
314