Completed
Push — master ( 23299f...b8bcfc )
by butschster
22s queued 19s
created

InjectableConfigTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 63
c 1
b 0
f 0
dl 0
loc 130
rs 10
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testArrayAccess() 0 10 1
A testSerialize() 0 14 1
A testWriteError() 0 12 1
A testIteration() 0 10 1
A testGetError() 0 11 1
A testUnsetError() 0 11 1
A testCircleReference() 0 14 1
A testToArray() 0 11 1
A testAliases() 0 4 1
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\Exception\Container\ContainerException;
18
use Spiral\Core\Traits\Config\AliasTrait;
19
use Spiral\Tests\Core\Fixtures\TestConfig;
20
21
class InjectableConfigTest extends TestCase
22
{
23
    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

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

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

139
        self::/** @scrutinizer ignore-call */ 
140
              expectException(ContainerException::class);
Loading history...
140
        self::expectExceptionMessage('Circle reference detected for alias `foo`');
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCa...xpectExceptionMessage() is not static, but was called statically. ( Ignorable by Annotation )

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

140
        self::/** @scrutinizer ignore-call */ 
141
              expectExceptionMessage('Circle reference detected for alias `foo`');
Loading history...
141
142
        $config = new TestConfig([
143
            'key' => 'value',
144
            'aliases' => [
145
                'foo' => 'bar',
146
                'bar' => 'foo'
147
            ]
148
        ]);
149
150
        $config->resolveAlias('foo');
151
    }
152
}
153