Passed
Pull Request — master (#277)
by Kirill
03:11
created

InjectableConfigTest::testUnsetError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Core;
13
14
use ArrayIterator;
15
use PHPUnit\Framework\TestCase;
16
use Spiral\Core\Exception\ConfigException;
17
use Spiral\Core\Traits\Config\AliasTrait;
18
use Spiral\Tests\Core\Fixtures\TestConfig;
19
20
class InjectableConfigTest extends TestCase
21
{
22
    use AliasTrait;
0 ignored issues
show
Deprecated Code introduced by
The trait Spiral\Core\Traits\Config\AliasTrait has been deprecated: to be removed in future releases. ( Ignorable by Annotation )

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

22
    use /** @scrutinizer ignore-deprecated */ AliasTrait;

This trait has been deprecated. The supplier of the trait has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the trait will be removed and what other trait to use instead.

Loading history...
23
24
    protected $config = [
25
        'aliases' => [
26
            'default' => 'value',
27
            'value'   => 'another',
28
            'another' => 'test',
29
        ],
30
    ];
31
32
    public function testArrayAccess(): void
33
    {
34
        $config = new TestConfig([
35
            'key' => 'value',
36
        ]);
37
38
        $this->assertArrayHasKey('key', $config);
39
        $this->assertEquals('value', $config['key']);
40
41
        $this->assertArrayNotHasKey('otherKey', $config);
42
    }
43
44
    public function testToArray(): void
45
    {
46
        $config = new TestConfig([
47
            'keyA' => 'value',
48
            'keyB' => 'valueB',
49
        ]);
50
51
        $this->assertEquals([
52
            'keyA' => 'value',
53
            'keyB' => 'valueB',
54
        ], $config->toArray());
55
    }
56
57
    public function testIteration(): void
58
    {
59
        $config = new TestConfig([
60
            'keyA' => 'value',
61
            'keyB' => 'valueB',
62
        ]);
63
64
        $iterator = $config->getIterator();
65
        $this->assertInstanceOf(ArrayIterator::class, $iterator);
66
        $this->assertSame($iterator->getArrayCopy(), $config->toArray());
67
    }
68
69
    public function testWriteError(): void
70
    {
71
        $excepted = 'Unable to change configuration data, configs are treated as immutable by default';
72
        $this->expectExceptionMessage($excepted);
73
74
        $this->expectException(ConfigException::class);
75
        $config = new TestConfig([
76
            'keyA' => 'value',
77
            'keyB' => 'valueB',
78
        ]);
79
80
        $config['keyA'] = 'abc';
81
    }
82
83
    public function testUnsetError(): void
84
    {
85
        $excepted = 'Unable to change configuration data, configs are treated as immutable by default';
86
        $this->expectExceptionMessage($excepted);
87
88
        $config = new TestConfig([
89
            'keyA' => 'value',
90
            'keyB' => 'valueB',
91
        ]);
92
93
        unset($config['keyA']);
94
    }
95
96
    public function testGetError(): void
97
    {
98
        $this->expectException(ConfigException::class);
99
        $this->expectExceptionMessage("Undefined configuration key 'keyC'");
100
101
        $config = new TestConfig([
102
            'keyA' => 'value',
103
            'keyB' => 'valueB',
104
        ]);
105
106
        $config['keyC'];
107
    }
108
109
    /**
110
     * @covers \Spiral\Core\InjectableConfig::__set_state()
111
     */
112
    public function testSerialize(): void
113
    {
114
        $config = new TestConfig([
115
            'keyA' => 'value',
116
            'keyB' => 'valueB',
117
        ]);
118
119
        $serialized = serialize($config);
120
        $this->assertEquals($config, unserialize($serialized));
121
122
        $this->assertEquals($config, TestConfig::__set_state([
123
            'config' => [
124
                'keyA' => 'value',
125
                'keyB' => 'valueB',
126
            ],
127
        ]));
128
    }
129
130
    public function testAliases(): void
131
    {
132
        $this->assertEquals('test', $this->resolveAlias('default'));
133
        $this->assertEquals('test', $this->resolveAlias('value'));
134
    }
135
}
136