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; |
|
|
|
|
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
|
|
|
|
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.