Passed
Push — master ( e74ce9...744fa2 )
by Petr
10:42
created

ExEntry   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 7
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
1
<?php
2
3
use kalanis\kw_input\Entries;
4
use kalanis\kw_input\Filtered;
5
use kalanis\kw_input\Input;
6
use kalanis\kw_input\Inputs;
7
use kalanis\kw_input\Interfaces;
8
9
10
class FilteredTest extends CommonTestClass
11
{
12
    public function testBasics()
13
    {
14
        $input = new MockInputs();
15
        $input->setSource($this->cliDataset()); // direct cli
16
        $variables = new Filtered\Variables($input);
17
18
        $source = new MockSource();
19
        $source->setRemotes($this->entryDataset(), null, $this->cliDataset());
20
        $input->setSource($source)->loadEntries();
21
22
        $this->assertNotEmpty(iterator_to_array($input->getCli()));
23
        $this->assertNotEmpty(iterator_to_array($input->getGet()));
24
        $this->assertEmpty(iterator_to_array($input->getPost()));
25
        $this->assertEmpty(iterator_to_array($input->getSession()));
26
        $this->assertEmpty(iterator_to_array($input->getCookie()));
27
        $this->assertNotEmpty(iterator_to_array($input->getFiles())); // seems strange, but there are files from Cli
28
        $this->assertEmpty(iterator_to_array($input->getServer()));
29
        $this->assertEmpty(iterator_to_array($input->getEnv()));
30
        $this->assertNotEmpty(iterator_to_array($input->getBasic()));
31
        $this->assertEmpty(iterator_to_array($input->getSystem()));
32
        $this->assertEmpty(iterator_to_array($input->getExternal()));
33
34
        $entries = $variables->getInArray(null, [Interfaces\IEntry::SOURCE_GET]);
35
        $this->assertNotEmpty($entries);
36
37
        $entry = reset($entries);
38
        $this->assertEquals('foo', key($entries));
39
        $this->assertEquals('foo', $entry->getKey());
40
        $this->assertEquals('val1', $entry->getValue());
41
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $entry->getSource());
42
43
        $entry = next($entries);
44
        $this->assertEquals('bar', key($entries));
45
        $this->assertEquals('bar', $entry->getKey());
46
        $this->assertEquals(['bal1', 'bal2'], $entry->getValue());
47
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $entry->getSource());
48
49
        $entry = next($entries);
50
        $this->assertEquals('baz', key($entries));
51
        $this->assertEquals('baz', $entry->getKey());
52
        $this->assertEquals(true, $entry->getValue());
53
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $entry->getSource());
54
55
        $entry = next($entries);
56
        $this->assertEquals('aff', key($entries));
57
        $this->assertEquals('aff', $entry->getKey());
58
        $this->assertEquals(42, $entry->getValue());
59
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $entry->getSource());
60
    }
61
62
    public function testFiles()
63
    {
64
        $source = new MockSource();
65
        $source->setRemotes($this->entryDataset(), null, null, $this->fileDataset());
66
67
        $input = new MockInputs();
68
        $input->setSource($source)->loadEntries();
69
        $variables = new Filtered\Variables($input);
70
71
        $this->assertEmpty(iterator_to_array($input->getCli()));
72
        $this->assertNotEmpty(iterator_to_array($input->getGet()));
73
        $this->assertEmpty(iterator_to_array($input->getPost()));
74
        $this->assertEmpty(iterator_to_array($input->getSession()));
75
        $this->assertNotEmpty(iterator_to_array($input->getFiles()));
76
        $this->assertEmpty(iterator_to_array($input->getCookie()));
77
        $this->assertEmpty(iterator_to_array($input->getServer()));
78
        $this->assertEmpty(iterator_to_array($input->getEnv()));
79
        $this->assertNotEmpty(iterator_to_array($input->getBasic()));
80
        $this->assertEmpty(iterator_to_array($input->getSystem()));
81
        $this->assertEmpty(iterator_to_array($input->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()
106
    {
107
        $input = new MockInputs();
108
        $input->setSource($this->cliDataset()); // direct cli
109
110
        $source = new MockSource();
111
        $source->setRemotes($this->entryDataset());
112
        $input->setSource($source)->loadEntries();
113
        $variables = new Filtered\Variables($input);
114
115
        $this->assertNotEmpty(iterator_to_array($input->getGet()));
116
117
        /** @var Input $entries */
118
        $entries = $variables->getInObject(null, [Interfaces\IEntry::SOURCE_GET]);
119
        $this->assertNotEmpty(iterator_to_array($entries->getIterator()));
120
        $this->assertNotEmpty(count($entries));
121
122
        $this->assertTrue(isset($entries['foo']));
123
        $this->assertEquals('foo', $entries['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', $entries['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', $entries['foo']->getValue());
125
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $entries['foo']->getSource());
126
127
        $this->assertTrue($entries->offsetExists('bar'));
128
        $this->assertEquals('bar', $entries->offsetGet('bar')->getKey());
129
        $this->assertEquals(['bal1', 'bal2'], $entries->offsetGet('bar')->getValue());
130
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $entries->offsetGet('bar')->getSource());
131
132
        $this->assertTrue(isset($entries->baz));
0 ignored issues
show
Bug Best Practice introduced by
The property baz does not exist on kalanis\kw_input\Input. Since you implemented __get, consider adding a @property annotation.
Loading history...
133
        $this->assertEquals('baz', $entries->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', $entries->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, $entries->baz->getValue());
135
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $entries->baz->getSource());
136
137
        $this->assertTrue($entries->offsetExists('aff'));
138
        $this->assertEquals('aff', $entries->offsetGet('aff')->getKey());
139
        $this->assertEquals(42, $entries->offsetGet('aff')->getValue());
140
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $entries->offsetGet('aff')->getSource());
141
142
        $this->assertFalse($entries->offsetExists('uhb'));
143
        $entries->offsetSet('uhb', 'feaht');
144
        $this->assertEquals('feaht', $entries->offsetGet('uhb')->getValue());
145
        $this->assertEquals(Interfaces\IEntry::SOURCE_EXTERNAL, $entries->offsetGet('uhb')->getSource());
146
147
        $entry = $entries->offsetGet('aff');
148
        unset($entries['aff']);
149
        $this->assertFalse(isset($entries['aff']));
150
        $entries[$entry->getKey()] = $entry;
151
        $this->assertTrue($entries->offsetExists('aff'));
152
        $entries[$entry->getKey()] = 'tfc';
153
        $this->assertEquals('tfc', $entries->offsetGet('aff')->getValue());
154
155
        $entry = $entries->baz;
156
        unset($entries->baz);
157
        $this->assertTrue(empty($entries->baz));
158
        $entries->{$entry->getKey()} = $entry;
159
        $this->assertTrue(isset($entries->baz));
160
    }
161
162
    public function testEntries()
163
    {
164
        $variables = new Filtered\EntryArrays([
165
            ExEntry::init(Interfaces\IEntry::SOURCE_GET, 'foo', 'val1'),
166
            ExEntry::init(Interfaces\IEntry::SOURCE_GET, 'bar', ['bal1', 'bal2']),
167
            ExEntry::init(Interfaces\IEntry::SOURCE_GET, 'baz', true),
168
            ExEntry::init(Interfaces\IEntry::SOURCE_GET, 'aff', 42),
169
            ExEntry::init(Interfaces\IEntry::SOURCE_EXTERNAL, 'uhb', 'feaht'),
170
        ]);
171
172
        /** @var Input $entries */
173
        $entries = $variables->getInObject(null, [Interfaces\IEntry::SOURCE_GET]);
174
        $this->assertNotEmpty(iterator_to_array($entries->getIterator()));
175
        $this->assertNotEmpty(count($entries));
176
177
        $this->assertTrue(isset($entries['foo']));
178
        $this->assertEquals('foo', $entries['foo']->getKey());
179
        $this->assertEquals('val1', $entries['foo']->getValue());
180
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $entries['foo']->getSource());
181
182
        $this->assertTrue($entries->offsetExists('bar'));
183
        $this->assertEquals('bar', $entries->offsetGet('bar')->getKey());
184
        $this->assertEquals(['bal1', 'bal2'], $entries->offsetGet('bar')->getValue());
185
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $entries->offsetGet('bar')->getSource());
186
187
        $this->assertTrue(isset($entries->baz));
0 ignored issues
show
Bug Best Practice introduced by
The property baz does not exist on kalanis\kw_input\Input. Since you implemented __get, consider adding a @property annotation.
Loading history...
188
        $this->assertEquals('baz', $entries->baz->getKey());
189
        $this->assertEquals(true, $entries->baz->getValue());
190
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $entries->baz->getSource());
191
192
        $this->assertTrue($entries->offsetExists('aff'));
193
        $this->assertEquals('aff', $entries->offsetGet('aff')->getKey());
194
        $this->assertEquals(42, $entries->offsetGet('aff')->getValue());
195
        $this->assertEquals(Interfaces\IEntry::SOURCE_GET, $entries->offsetGet('aff')->getSource());
196
197
        $this->assertFalse($entries->offsetExists('uhb'));
198
        $entries->offsetSet('uhb', 'feaht');
199
        $this->assertEquals('feaht', $entries->offsetGet('uhb')->getValue());
200
        $this->assertEquals(Interfaces\IEntry::SOURCE_EXTERNAL, $entries->offsetGet('uhb')->getSource());
201
    }
202
203
    public function testSimpleArray()
204
    {
205
        $variables = new Filtered\SimpleArrays([
206
            'foo' => 'val1',
207
            'bar' => ['bal1', 'bal2'],
208
            'baz' => true,
209
            'aff' => 42,
210
        ], Interfaces\IEntry::SOURCE_POST);
211
212
        /** @var Input $entries */
213
        $entries = $variables->getInObject(null, [Interfaces\IEntry::SOURCE_GET]); // sources have no meaning here
214
        $this->assertNotEmpty(iterator_to_array($entries->getIterator()));
215
        $this->assertNotEmpty(count($entries));
216
217
        $this->assertTrue(isset($entries['foo']));
218
        $this->assertEquals('foo', $entries['foo']->getKey());
219
        $this->assertEquals('val1', $entries['foo']->getValue());
220
        $this->assertEquals(Interfaces\IEntry::SOURCE_POST, $entries['foo']->getSource());
221
222
        $this->assertTrue($entries->offsetExists('bar'));
223
        $this->assertEquals('bar', $entries->offsetGet('bar')->getKey());
224
        $this->assertEquals(['bal1', 'bal2'], $entries->offsetGet('bar')->getValue());
225
        $this->assertEquals(Interfaces\IEntry::SOURCE_POST, $entries->offsetGet('bar')->getSource());
226
227
        $this->assertTrue(isset($entries->baz));
0 ignored issues
show
Bug Best Practice introduced by
The property baz does not exist on kalanis\kw_input\Input. Since you implemented __get, consider adding a @property annotation.
Loading history...
228
        $this->assertEquals('baz', $entries->baz->getKey());
229
        $this->assertEquals(true, $entries->baz->getValue());
230
        $this->assertEquals(Interfaces\IEntry::SOURCE_POST, $entries->baz->getSource());
231
232
        $this->assertTrue($entries->offsetExists('aff'));
233
        $this->assertEquals('aff', $entries->offsetGet('aff')->getKey());
234
        $this->assertEquals(42, $entries->offsetGet('aff')->getValue());
235
        $this->assertEquals(Interfaces\IEntry::SOURCE_POST, $entries->offsetGet('aff')->getSource());
236
    }
237
238
    public function testArrayAccess()
239
    {
240
        $variables = new Filtered\ArrayAccessed(new ArrayObject([
241
            'foo' => 'val1',
242
            'bar' => ['bal1', 'bal2'],
243
            'baz' => true,
244
            'aff' => 42,
245
        ]), Interfaces\IEntry::SOURCE_CLI);
246
247
        /** @var Input $entries */
248
        $entries = $variables->getInObject(null, [Interfaces\IEntry::SOURCE_GET]); // sources have no meaning here
249
        $this->assertNotEmpty(iterator_to_array($entries->getIterator()));
250
        $this->assertNotEmpty(count($entries));
251
252
        $this->assertTrue(isset($entries['foo']));
253
        $this->assertEquals('foo', $entries['foo']->getKey());
254
        $this->assertEquals('val1', $entries['foo']->getValue());
255
        $this->assertEquals(Interfaces\IEntry::SOURCE_CLI, $entries['foo']->getSource());
256
257
        $this->assertTrue($entries->offsetExists('bar'));
258
        $this->assertEquals('bar', $entries->offsetGet('bar')->getKey());
259
        $this->assertEquals(['bal1', 'bal2'], $entries->offsetGet('bar')->getValue());
260
        $this->assertEquals(Interfaces\IEntry::SOURCE_CLI, $entries->offsetGet('bar')->getSource());
261
262
        $this->assertTrue(isset($entries->baz));
0 ignored issues
show
Bug Best Practice introduced by
The property baz does not exist on kalanis\kw_input\Input. Since you implemented __get, consider adding a @property annotation.
Loading history...
263
        $this->assertEquals('baz', $entries->baz->getKey());
264
        $this->assertEquals(true, $entries->baz->getValue());
265
        $this->assertEquals(Interfaces\IEntry::SOURCE_CLI, $entries->baz->getSource());
266
267
        $this->assertTrue($entries->offsetExists('aff'));
268
        $this->assertEquals('aff', $entries->offsetGet('aff')->getKey());
269
        $this->assertEquals(42, $entries->offsetGet('aff')->getValue());
270
        $this->assertEquals(Interfaces\IEntry::SOURCE_CLI, $entries->offsetGet('aff')->getSource());
271
    }
272
}
273
274
275
class MockSource implements Interfaces\ISource
276
{
277
    protected $mockCli;
278
    protected $mockGet;
279
    protected $mockPost;
280
    protected $mockFiles;
281
    protected $mockCookie;
282
    protected $mockSession;
283
284
    public function setRemotes(?array $get, ?array $post = null, ?array $cli = null, ?array $files = null, ?array $cookie = null, ?array $session = null): self
285
    {
286
        $this->mockCli = $cli;
287
        $this->mockGet = $get;
288
        $this->mockPost = $post;
289
        $this->mockFiles = $files;
290
        $this->mockCookie = $cookie;
291
        $this->mockSession = $session;
292
        return $this;
293
    }
294
295
    public function cli(): ?array
296
    {
297
        return $this->mockCli;
298
    }
299
300
    public function get(): ?array
301
    {
302
        return $this->mockGet;
303
    }
304
305
    public function post(): ?array
306
    {
307
        return $this->mockPost;
308
    }
309
310
    public function files(): ?array
311
    {
312
        return $this->mockFiles;
313
    }
314
315
    public function cookie(): ?array
316
    {
317
        return $this->mockCookie;
318
    }
319
320
    public function session(): ?array
321
    {
322
        return $this->mockSession;
323
    }
324
325
    public function server(): ?array
326
    {
327
        $content = null;
328
        return $content;
329
    }
330
331
    public function env(): ?array
332
    {
333
        $content = null;
334
        return $content;
335
    }
336
337
    public function external(): ?array
338
    {
339
        $content = null;
340
        return $content;
341
    }
342
}
343
344
345
class MockInputs extends Inputs
346
{
347
    public function getBasic(): Traversable
348
    {
349
        return $this->getIn(null, [
350
            Interfaces\IEntry::SOURCE_CLI,
351
            Interfaces\IEntry::SOURCE_GET,
352
            Interfaces\IEntry::SOURCE_POST,
353
        ]);
354
    }
355
356
    public function getSystem(): Traversable
357
    {
358
        return $this->getIn(null, [
359
            Interfaces\IEntry::SOURCE_SERVER,
360
            Interfaces\IEntry::SOURCE_ENV,
361
        ]);
362
    }
363
364
    public function getCli(): Traversable
365
    {
366
        return $this->getIn(null, [Interfaces\IEntry::SOURCE_CLI]);
367
    }
368
369
    public function getGet(): Traversable
370
    {
371
        return $this->getIn(null, [Interfaces\IEntry::SOURCE_GET]);
372
    }
373
374
    public function getPost(): Traversable
375
    {
376
        return $this->getIn(null, [Interfaces\IEntry::SOURCE_POST]);
377
    }
378
379
    public function getSession(): Traversable
380
    {
381
        return $this->getIn(null, [Interfaces\IEntry::SOURCE_SESSION]);
382
    }
383
384
    public function getCookie(): Traversable
385
    {
386
        return $this->getIn(null, [Interfaces\IEntry::SOURCE_COOKIE]);
387
    }
388
389
    public function getFiles(): Traversable
390
    {
391
        return $this->getIn(null, [Interfaces\IEntry::SOURCE_FILES]);
392
    }
393
394
    public function getServer(): Traversable
395
    {
396
        return $this->getIn(null, [Interfaces\IEntry::SOURCE_SERVER]);
397
    }
398
399
    public function getEnv(): Traversable
400
    {
401
        return $this->getIn(null, [Interfaces\IEntry::SOURCE_ENV]);
402
    }
403
404
    public function getExternal(): Traversable
405
    {
406
        return $this->getIn(null, [Interfaces\IEntry::SOURCE_EXTERNAL]);
407
    }
408
}
409
410
411
class ExEntry extends Entries\Entry
412
{
413
    public static function init(string $source, string $key, $value = null): Entries\Entry
414
    {
415
        $lib = new self();
416
        $lib->setEntry($source, $key, $value);
417
        return $lib;
418
    }
419
}
420