FlatContainerTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 334
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 334
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testDelimiter() 0 7 1
A testGet() 0 6 1
A testArrayGet() 0 6 1
A getData() 0 50 1
A testSet() 0 7 1
A testArraySet() 0 7 1
A setData() 0 59 1
A testHas() 0 5 1
A testArrayIsset() 0 5 1
B hasData() 0 41 1
A testRemove() 0 6 1
A testArrayUnset() 0 6 1
B removeData() 0 44 1
1
<?php
2
3
namespace Graze\DataStructure\Container;
4
5
use PHPUnit_Framework_TestCase as TestCase;
6
7
class FlatContainerTest extends TestCase
8
{
9
    public function testDelimiter()
10
    {
11
        $cont = new FlatContainer();
12
        $this->assertEquals('.', $cont->getDelimiter());
13
        $this->assertSame($cont, $cont->setDelimiter('->'));
14
        $this->assertEquals('->', $cont->getDelimiter());
15
    }
16
17
    /**
18
     * @dataProvider getData
19
     *
20
     * @param array  $base
21
     * @param string $key
22
     * @param mixed  $expected
23
     */
24
    public function testGet(array $base, $key, $expected)
25
    {
26
        $cont = new FlatContainer($base);
27
28
        $this->assertEquals($expected, $cont->get($key));
29
    }
30
31
    /**
32
     * @dataProvider getData
33
     *
34
     * @param array  $base
35
     * @param string $key
36
     * @param mixed  $expected
37
     */
38
    public function testArrayGet(array $base, $key, $expected)
39
    {
40
        $cont = new FlatContainer($base);
41
42
        $this->assertEquals($expected, $cont[$key]);
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    public function getData()
49
    {
50
        return [
51
            [
52
                ['first' => 'one', 'second' => 'two'],
53
                'first',
54
                'one',
55
            ],
56
            [
57
                ['first' => 'one', 'second' => ['a' => 'b', 'c' => 'd']],
58
                'second',
59
                ['a' => 'b', 'c' => 'd'],
60
            ],
61
            [
62
                ['first' => 'one', 'second' => ['a' => 'b', 'c' => 'd']],
63
                'second.a',
64
                'b',
65
            ],
66
            [
67
                ['first' => 'one', 'second' => ['a' => 'b', 'c' => 'd']],
68
                'nope',
69
                null,
70
            ],
71
            [
72
                ['first' => 'one', 'second' => ['a' => 'b', 'c' => 'd']],
73
                'second.nope',
74
                null,
75
            ],
76
            [
77
                ['first' => 'one', 'second' => ['a' => 'b', 'c' => 'd']],
78
                'first.nope',
79
                null,
80
            ],
81
            [
82
                ['indexed' => ['first', 'second']],
83
                'indexed.0',
84
                'first',
85
            ],
86
            [
87
                ['top' => new Container(['a' => 'b', 'c' => 'd'])],
88
                'top.a',
89
                'b',
90
            ],
91
            [
92
                ['top' => new Container(['a' => ['c' => 'd']])],
93
                'top.a.c',
94
                'd',
95
            ],
96
        ];
97
    }
98
99
    /**
100
     * @dataProvider setData
101
     *
102
     * @param array  $base
103
     * @param string $key
104
     * @param mixed  $value
105
     * @param array  $expected
106
     */
107
    public function testSet(array $base, $key, $value, array $expected)
108
    {
109
        $cont = new FlatContainer($base);
110
        $cont->set($key, $value);
111
112
        $this->assertEquals($expected, $cont->getAll());
113
    }
114
115
    /**
116
     * @dataProvider setData
117
     *
118
     * @param array  $base
119
     * @param string $key
120
     * @param mixed  $value
121
     * @param array  $expected
122
     */
123
    public function testArraySet(array $base, $key, $value, array $expected)
124
    {
125
        $cont = new FlatContainer($base);
126
        $cont[$key] = $value;
127
128
        $this->assertEquals($expected, $cont->getAll());
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    public function setData()
135
    {
136
        return [
137
            [
138
                [],
139
                'simple',
140
                'value',
141
                ['simple' => 'value'],
142
            ],
143
            [
144
                [],
145
                'key.child',
146
                'value',
147
                ['key' => ['child' => 'value']],
148
            ],
149
            [
150
                ['key' => ['child' => 'one']],
151
                'key.child',
152
                'value',
153
                ['key' => ['child' => 'value']],
154
            ],
155
            [
156
                ['key' => ['child' => 'one']],
157
                'key.second',
158
                'value',
159
                ['key' => ['child' => 'one', 'second' => 'value']],
160
            ],
161
            [
162
                ['key' => 'node'],
163
                'key.second',
164
                'value',
165
                ['key' => ['second' => 'value']],
166
            ],
167
            [
168
                ['child' => new Container(['node' => 'child'])],
169
                'child.node',
170
                'value',
171
                ['child' => new Container(['node' => 'value'])],
172
            ],
173
            [
174
                ['child' => new Container(['node' => 'child'])],
175
                'child.other',
176
                'value',
177
                ['child' => new Container(['node' => 'child', 'other' => 'value'])],
178
            ],
179
            [
180
                ['child' => new Container(['node' => 'child'])],
181
                'child.other.more',
182
                'value',
183
                ['child' => new Container(['node' => 'child', 'other' => ['more' => 'value']])],
184
            ],
185
            [
186
                ['child' => new ImmutableContainer(['node' => 'child'])],
187
                'child.other.more',
188
                'value',
189
                ['child' => new ImmutableContainer(['node' => 'child', 'other' => ['more' => 'value']])],
190
            ],
191
        ];
192
    }
193
194
    /**
195
     * @dataProvider hasData
196
     *
197
     * @param array  $base
198
     * @param string $key
199
     * @param bool   $expected
200
     */
201
    public function testHas(array $base, $key, $expected)
202
    {
203
        $cont = new FlatContainer($base);
204
        $this->assertEquals($expected, $cont->has($key));
205
    }
206
207
    /**
208
     * @dataProvider hasData
209
     *
210
     * @param array  $base
211
     * @param string $key
212
     * @param bool   $expected
213
     */
214
    public function testArrayIsset(array $base, $key, $expected)
215
    {
216
        $cont = new FlatContainer($base);
217
        $this->assertEquals($expected, isset($cont[$key]));
218
    }
219
220
    /**
221
     * @return array
222
     */
223
    public function hasData()
224
    {
225
        return [
226
            [
227
                [], 'key', false,
228
            ],
229
            [
230
                [], 'key.nope', false,
231
            ],
232
            [
233
                ['key' => 'value'], 'key', true,
234
            ],
235
            [
236
                ['key' => 'value'], 'nope', false,
237
            ],
238
            [
239
                ['key' => ['child' => 'value']], 'key', true,
240
            ],
241
            [
242
                ['key' => ['child' => 'value']], 'key.child', true,
243
            ],
244
            [
245
                ['key' => ['value']], 'key.value', false,
246
            ],
247
            [
248
                ['key' => ['value']], 'key.0', true,
249
            ],
250
            [
251
                ['key' => ['child' => 'value']], 'key.nope', false,
252
            ],
253
            [
254
                ['key' => new Container(['child' => 'value'])], 'key.child', true,
255
            ],
256
            [
257
                ['key' => new Container(['child' => 'value'])], 'key.nope', false,
258
            ],
259
            [
260
                ['key' => new Container(['child' => 'value'])], 'key', true,
261
            ],
262
        ];
263
    }
264
265
    /**
266
     * @dataProvider removeData
267
     *
268
     * @param array  $base
269
     * @param string $key
270
     * @param array  $expected
271
     */
272
    public function testRemove(array $base, $key, array $expected)
273
    {
274
        $cont = new FlatContainer($base);
275
        $cont->remove($key);
276
        $this->assertEquals($expected, $cont->getAll());
277
    }
278
279
    /**
280
     * @dataProvider removeData
281
     *
282
     * @param array  $base
283
     * @param string $key
284
     * @param array  $expected
285
     */
286
    public function testArrayUnset(array $base, $key, array $expected)
287
    {
288
        $cont = new FlatContainer($base);
289
        unset($cont[$key]);
290
        $this->assertEquals($expected, $cont->getAll());
291
    }
292
293
    /**
294
     * @return array
295
     */
296
    public function removeData()
297
    {
298
        return [
299
            [
300
                [], 'key', [],
301
            ],
302
            [
303
                [], 'key.nope', [],
304
            ],
305
            [
306
                ['key' => 'value'], 'key', [],
307
            ],
308
            [
309
                ['key' => 'value'], 'nope', ['key' => 'value'],
310
            ],
311
            [
312
                ['key' => ['child' => 'value']], 'key', [],
313
            ],
314
            [
315
                ['key' => ['child' => 'value']], 'key.child', ['key' => []],
316
            ],
317
            [
318
                ['key' => ['value']], 'key.value', ['key' => ['value']],
319
            ],
320
            [
321
                ['key' => ['value']], 'key.0', ['key' => []],
322
            ],
323
            [
324
                ['key' => ['child' => 'value']], 'key.nope', ['key' => ['child' => 'value']],
325
            ],
326
            [
327
                ['key' => new Container(['child' => 'value'])], 'key', [],
328
            ],
329
            [
330
                ['key' => new Container(['child' => 'value'])], 'key.child', ['key' => new Container([])],
331
            ],
332
            [
333
                ['key' => new Container(['child' => 'value', 'other' => 'thing'])], 'key.child', ['key' => new Container(['other' => 'thing'])],
334
            ],
335
            [
336
                ['key' => new ImmutableContainer(['child' => 'value'])], 'key.child', ['key' => new ImmutableContainer([])],
337
            ],
338
        ];
339
    }
340
}
341