1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/config project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\Test\Config; |
10
|
|
|
|
11
|
|
|
use Assert\AssertionFailedException; |
12
|
|
|
use Daikon\Config\ArrayConfigLoader; |
13
|
|
|
use Daikon\Config\ConfigLoaderInterface; |
14
|
|
|
use Daikon\Config\ConfigProvider; |
15
|
|
|
use Daikon\Config\ConfigProviderInterface; |
16
|
|
|
use Daikon\Config\ConfigProviderParams; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
|
19
|
|
|
final class ConfigProviderTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
private const CONFIG_FIXTURE = [ |
22
|
|
|
'project' => [ |
23
|
|
|
'environment' => 'development' |
24
|
|
|
], |
25
|
|
|
'auth' => [ |
26
|
|
|
'simple.credentials' => [ |
27
|
|
|
'username' => 'superuser', |
28
|
|
|
'password' => 'p455w0rd' |
29
|
|
|
] |
30
|
|
|
], |
31
|
|
|
'labels' => [ |
32
|
|
|
'foo' => ['label' => ['snafu' => ['value' => 'hello']]], |
33
|
|
|
'bar' => ['label' => ['fnord' => ['value' => 'eris' ]]] |
34
|
|
|
] |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @dataProvider provideSut |
39
|
|
|
* @param ConfigProviderInterface $sut |
40
|
|
|
*/ |
41
|
1 |
|
public function testHas(ConfigProviderInterface $sut): void |
42
|
|
|
{ |
43
|
1 |
|
$this->assertTrue($sut->has('settings')); |
44
|
1 |
|
$this->assertTrue($sut->has('settings.project')); |
45
|
1 |
|
$this->assertTrue($sut->has('settings.project.environment')); |
46
|
1 |
|
$this->assertTrue($sut->has('settings.auth.simple.credentials.username')); |
47
|
|
|
|
48
|
1 |
|
$this->assertFalse($sut->has('foobar')); |
49
|
1 |
|
$this->assertFalse($sut->has('settings.foobar')); |
50
|
1 |
|
$this->assertFalse($sut->has('settings.auth.simple.foobar')); |
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @dataProvider provideSut |
55
|
|
|
* @param ConfigProviderInterface $sut |
56
|
|
|
*/ |
57
|
1 |
|
public function testGet(ConfigProviderInterface $sut): void |
58
|
|
|
{ |
59
|
1 |
|
$this->assertEquals(self::CONFIG_FIXTURE, $sut->get('settings')); |
60
|
1 |
|
$this->assertEquals( |
61
|
1 |
|
self::CONFIG_FIXTURE['project'], |
62
|
1 |
|
$sut->get('settings.project') |
63
|
|
|
); |
64
|
1 |
|
$this->assertEquals('development', $sut->get('settings.project.environment')); |
65
|
1 |
|
$this->assertEquals('superuser', $sut->get('settings.auth.simple.credentials.username')); |
66
|
1 |
|
$this->assertEquals( |
67
|
1 |
|
self::CONFIG_FIXTURE['auth']['simple.credentials'], |
68
|
1 |
|
$sut->get('settings.auth.simple.credentials') |
69
|
|
|
); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @dataProvider provideSut |
74
|
|
|
* @param ConfigProviderInterface $sut |
75
|
|
|
*/ |
76
|
1 |
|
public function testGetWildcardExpansion(ConfigProviderInterface $sut): void |
77
|
|
|
{ |
78
|
1 |
|
$this->assertEquals(['hello', 'eris'], $sut->get('settings.labels.*.label.*.value')); |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @dataProvider provideSut |
83
|
|
|
* @param ConfigProviderInterface $sut |
84
|
|
|
*/ |
85
|
1 |
|
public function testGetWithDefault(ConfigProviderInterface $sut): void |
86
|
|
|
{ |
87
|
1 |
|
$this->assertEquals('development', $sut->get('settings.project.environment', 'bar')); |
88
|
1 |
|
$this->assertEquals('bar', $sut->get('foo', 'bar')); |
89
|
1 |
|
$this->assertNull($sut->get('foo')); |
90
|
1 |
|
} |
91
|
|
|
|
92
|
1 |
|
public function testInterpolation(): void |
93
|
|
|
{ |
94
|
1 |
|
$sut = new ConfigProvider(new ConfigProviderParams([ |
95
|
|
|
'settings' => [ |
96
|
1 |
|
'loader' => ArrayConfigLoader::class, |
97
|
|
|
'sources' => [ |
98
|
|
|
'project' => [ |
99
|
|
|
'secret' => 'c4ntgu35th15' |
100
|
|
|
], |
101
|
|
|
'auth' => [ |
102
|
|
|
'simple' => [ |
103
|
|
|
'username' => 'superuser', |
104
|
|
|
'password' => '${settings.project.secret}' |
105
|
|
|
] |
106
|
|
|
] |
107
|
|
|
] |
108
|
|
|
] |
109
|
|
|
])); |
110
|
1 |
|
$this->assertEquals('c4ntgu35th15', $sut->get('settings.auth.simple.password')); |
111
|
1 |
|
} |
112
|
|
|
|
113
|
1 |
|
public function testLocationAndSourceInterpolation(): void |
114
|
|
|
{ |
115
|
1 |
|
$loaderMock = $this->getMockBuilder(ConfigLoaderInterface::class) |
116
|
1 |
|
->onlyMethods(['load']) |
117
|
1 |
|
->getMock(); |
118
|
1 |
|
$loaderMock->expects($this->once()) |
119
|
1 |
|
->method('load') |
120
|
1 |
|
->with( |
121
|
1 |
|
$this->equalTo(['foo/dev/bar']), |
122
|
1 |
|
$this->equalTo(['some_value.yaml']) |
123
|
|
|
); |
124
|
1 |
|
$sut = new ConfigProvider(new ConfigProviderParams([ |
125
|
|
|
'settings' => [ |
126
|
1 |
|
'loader' => ArrayConfigLoader::class, |
127
|
|
|
'sources' => [ |
128
|
|
|
'project' => ['environment' => 'dev'], |
129
|
|
|
'some_setting' => 'some_value' |
130
|
|
|
] |
131
|
|
|
], |
132
|
|
|
'connections' => [ |
133
|
1 |
|
'loader' => $loaderMock, |
134
|
|
|
'locations' => ['foo/${settings.project.environment}/bar'], |
135
|
|
|
'sources' => ['${settings.some_setting}.yaml'] |
136
|
|
|
] |
137
|
|
|
])); |
138
|
1 |
|
$sut->get('connections'); |
139
|
1 |
|
} |
140
|
|
|
|
141
|
1 |
|
public function testInvalidSourceInterpolation(): void |
142
|
|
|
{ |
143
|
1 |
|
$this->expectException(AssertionFailedException::class); |
144
|
1 |
|
$sut = new ConfigProvider(new ConfigProviderParams([ |
145
|
|
|
'settings' => [ |
146
|
1 |
|
'loader' => $this->createMock(ConfigLoaderInterface::class), |
147
|
|
|
'sources' => ['foo/${settings.auth.name}/bar'] |
148
|
|
|
] |
149
|
|
|
])); |
150
|
1 |
|
$sut->get('settings'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function provideSut(): array |
154
|
|
|
{ |
155
|
|
|
$configProvider = new ConfigProvider( |
156
|
|
|
new ConfigProviderParams([ |
157
|
|
|
'settings' => [ |
158
|
|
|
'loader' => ArrayConfigLoader::class, |
159
|
|
|
'sources' => self::CONFIG_FIXTURE |
160
|
|
|
] |
161
|
|
|
]) |
162
|
|
|
); |
163
|
|
|
return [[$configProvider]]; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|