Passed
Branch master (e8fd46)
by Alexey
03:15
created

ConfigTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 378
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 378
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 3
1
<?php
2
3
use Venta\Config\Config;
4
use PHPUnit\Framework\TestCase;
5
6
class ConfigTest extends TestCase
7
{
8
9
//    /**
10
//     * @test
11
//     */
12
//    public function canCheckIfHasKey()
13
//    {
14
//        $config = new Config(['key' => 'value']);
15
//
16
//        $this->assertTrue($config->has('key'));
17
//        $this->assertFalse($config->has('other key'));
18
//    }
19
20
    /**
21
     * @test
22
     */
23
    public function canCheckIssetOnNullValue()
24
    {
25
        $config = new Config(['key' => null]);
26
27
        $this->assertTrue($config->has('key'));
28
        $this->assertTrue(isset($config->key));
29
        $this->assertTrue(isset($config['key']));
30
    }
31
32
    /**
33
     * @test
34
     */
35
    public function canClone()
36
    {
37
        $array = [
38
            'key' => 'value',
39
            'another' => [
40
                'name' => 'qwerty',
41
            ],
42
        ];
43
        $config = new Config($array);
44
        $another = $config->get('another');
45
        $clone = clone $config;
46
47
        $this->assertSame($config->toArray(), $clone->toArray());
48
        $this->assertNotSame($another, $clone->get('another'));
49
    }
50
51
    /**
52
     * @test
53
     */
54
    public function canConvertArrayToSelf()
55
    {
56
        $array = [
57
            'key' => 'value',
58
            'another' => [
59
                'name' => 'qwerty',
60
            ],
61
        ];
62
        $config = new Config($array);
63
64
        $this->assertInstanceOf(Config::class, $config->get('another'));
65
        $this->assertSame('qwerty', $config->get('another')->get('name'));
66
    }
67
68
69
    /**
70
     * @test
71
     */
72
//    public function canCount()
73
//    {
74
//        $config = new Config(['key' => 'value']);
75
//
76
//        $this->assertCount(1, $config);
77
//    }
78
79
    /**
80
     * @test
81
     */
82
//    public function canGetSetValues()
83
//    {
84
//        $config = new Config();
85
//        $config->set('key', 'value');
86
//
87
//        $this->assertSame('value', $config->get('key'));
88
//    }
89
90
    /**
91
     * @test
92
     */
93
    public function canHandleIsset()
94
    {
95
        $config = new Config(['key' => 'value']);
96
97
        $this->assertTrue(isset($config->key));
98
        $this->assertFalse(isset($config->other_key));
99
    }
100
101
    /**
102
     * @test
103
     */
104
    public function canHandleIssetArraySyntax()
105
    {
106
        $config = new Config(['key' => 'value']);
107
108
        $this->assertTrue(isset($config['key']));
109
        $this->assertFalse(isset($config['other_key']));
110
    }
111
112
    /**
113
     * @test
114
     */
115
    public function canHandleMultidimensionalArray()
116
    {
117
        $array = [
118
            'key' => 'value',
119
            'another' => [
120
                'name' => 'qwerty',
121
            ],
122
        ];
123
        $config = new Config($array);
124
125
        $this->assertSame($array, $config->toArray());
126
    }
127
128
    /**
129
     * @test
130
     */
131
    public function canHandleNonAssociativeArray()
132
    {
133
        $array = ['value1', 'value2'];
134
        $config = new Config($array);
135
136
        $this->assertSame($array, $config->toArray());
137
    }
138
139
    /**
140
     * @test
141
     */
142
    public function canIterate()
143
    {
144
        $keys = ['key', 'another'];
145
        $values = ['value', 'val'];
146
        $config = new Config(array_combine($keys, $values));
147
148
        foreach ($config as $key => $value) {
149
            $this->assertContains($key, $keys);
150
            $this->assertContains($value, $values);
151
        }
152
    }
153
154
    /**
155
     * @test
156
     */
157
//    public function canLockForModification()
158
//    {
159
//        $config = new Config();
160
//        $this->assertFalse($config->isLocked());
161
//        $config->lock();
162
//        $this->assertTrue($config->isLocked());
163
//    }
164
165
    /**
166
     * @test
167
     */
168
//    public function canLockRecursively()
169
//    {
170
//        $array = [
171
//            'key' => 'value',
172
//            'another' => [
173
//                'name' => 'qwerty',
174
//            ],
175
//        ];
176
//        $config = new Config($array);
177
//        $config->lock();
178
//
179
//        $this->assertTrue($config->isLocked());
180
//        $this->assertTrue($config->get('another')->isLocked());
181
//    }
182
183
    /**
184
     * @test
185
     */
186
    public function canMerge()
187
    {
188
        $array = [
189
            'key' => 'value',
190
            'map' => [
191
                'name' => 'qwerty',
192
            ],
193
            'vector' => [
194
                'value 1',
195
                'value 2',
196
                'value 3',
197
            ],
198
            'this' => 'will be overwritten',
199
            'another' => [
200
                'config' => 'instance',
201
            ],
202
        ];
203
        $config = new Config($array);
204
        $merge = [
205
            'map' => [
206
                'name' => 'zxcv',
207
                'other' => 'poiuyt',
208
            ],
209
            'vector' => [
210
                'value 4',
211
                'value 5',
212
            ],
213
            'this' => 'is overwritten',
214
            'another' => 'plain string',
215
        ];
216
        $result = $config->merge(new Config($merge));
217
218
        $this->assertSame('value', $result->get('key'));
219
        $this->assertSame('zxcv', $result->get('map')->get('name'));
220
        $this->assertSame('poiuyt', $result->get('map')->get('other'));
221
        $this->assertSame(['value 1', 'value 2', 'value 3', 'value 4', 'value 5',], $result->get('vector')->toArray());
222
        $this->assertSame('is overwritten', $result->get('this'));
223
        $this->assertSame('plain string', $result->get('another'));
224
    }
225
226
    /**
227
     * @test
228
     */
229
//    public function canPushValue()
230
//    {
231
//        $config = new Config();
232
//        $config->push('value1');
233
//        $config->push('value2');
234
//
235
//        $this->assertSame(['value1', 'value2'], $config->toArray());
236
//    }
237
238
    /**
239
     * @test
240
     */
241
//    public function canPushValueToArray()
242
//    {
243
//        $config = new Config();
244
//        $config[] = 'value1';
245
//        $config[] = 'value2';
246
//
247
//        $this->assertSame(['value1', 'value2'], $config->toArray());
248
//    }
249
250
//    /**
251
//     * @test
252
//     */
253
//    public function canSetThroughConstructor()
254
//    {
255
//        $config = new Config(['key' => 'value']);
256
//
257
//        $this->assertSame('value', $config->get('key'));
258
//    }
259
260
    /**
261
     * @test
262
     */
263
//    public function canSetUsingArraySyntax()
264
//    {
265
//        $config = new Config();
266
//        $config['key'] = 'value';
267
//
268
//        $this->assertSame('value', $config['key']);
269
//    }
270
271
    /**
272
     * @test
273
     */
274
//    public function canSetUsingProperties()
275
//    {
276
//        $config = new Config();
277
//        $config->key = 'value';
278
//
279
//        $this->assertSame('value', $config->key);
280
//    }
281
282
    /**
283
     * @test
284
     */
285
//    public function canUnsetKey()
286
//    {
287
//        $config = new Config();
288
//        $config['key'] = 'value';
289
//
290
//        $this->assertTrue(isset($config['key']));
291
//        unset($config['key']);
292
//        $this->assertFalse(isset($config['key']));
293
//    }
294
295
    /**
296
     * @test
297
     * @expectedException RuntimeException
298
     * @expectedExceptionMessage Config is locked for modifications.
299
     */
300
    public function throwsExceptionOnLockedConfigModification()
301
    {
302
        $config = new Config();
303
        $config->lock();
304
        $config->set('abc', 'def');
305
    }
306
307
    /**
308
     * @test
309
     * @expectedException RuntimeException
310
     * @expectedExceptionMessage Config is locked for modifications.
311
     */
312
    public function throwsExceptionOnLockedConfigModificationAsArray()
313
    {
314
        $config = new Config();
315
        $config->lock();
316
        $config['abc'] = 'def';
317
    }
318
319
    /**
320
     * @test
321
     * @expectedException RuntimeException
322
     * @expectedExceptionMessage Config is locked for modifications.
323
     */
324
    public function throwsExceptionOnLockedConfigPropertyModification()
325
    {
326
        $config = new Config();
327
        $config->lock();
328
        $config->abc = 'def';
329
    }
330
331
    /**
332
     * @test
333
     * @expectedException RuntimeException
334
     * @expectedExceptionMessage Config is locked for modifications.
335
     */
336
    public function throwsExceptionOnLockedConfigPush()
337
    {
338
        $config = new Config();
339
        $config->lock();
340
        $config->push('value');
341
    }
342
343
    /**
344
     * @test
345
     * @expectedException RuntimeException
346
     * @expectedExceptionMessage Config is locked for modifications.
347
     */
348
    public function throwsExceptionOnLockedConfigPushToArray()
349
    {
350
        $config = new Config();
351
        $config->lock();
352
        $config[] = 'value';
353
    }
354
355
    /**
356
     * @test
357
     * @expectedException RuntimeException
358
     * @expectedExceptionMessage Config is locked for modifications.
359
     */
360
    public function throwsExceptionOnLockedConfigUnset()
361
    {
362
        $config = new Config();
363
        $config->lock();
364
        unset($config['key']);
365
    }
366
367
//    /**
368
//     * @test
369
//     */
370
//    public function nameIsAttachedToConfig()
371
//    {
372
//        $array = [
373
//            'key' => 'value',
374
//            'another' => [
375
//                'name' => 'qwerty',
376
//            ],
377
//        ];
378
//
379
//        $config = new Config($array);
380
//        $this->assertEquals('root', $config->getName());
381
//        $this->assertEquals('another', $config->get('another')->getName());
382
//    }
383
}
384