File: ConfigurationTest.php

package info (click to toggle)
php-league-config 1.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 508 kB
  • sloc: php: 482; ruby: 45; makefile: 18; javascript: 15; xml: 15
file content (288 lines) | stat: -rw-r--r-- 9,530 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php

declare(strict_types=1);

/*
 * This file is part of the league/config package.
 *
 * (c) Colin O'Dell <colinodell@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace League\Config\Tests;

use League\Config\Configuration;
use League\Config\Exception\UnknownOptionException;
use League\Config\Exception\ValidationException;
use Nette\Schema\Expect;
use PHPUnit\Framework\TestCase;

final class ConfigurationTest extends TestCase
{
    public function testAddSchema(): void
    {
        $config = new Configuration();

        try {
            $config->get('foo');
            $this->fail('An exception should be thrown since no options have been defined yet');
        } catch (\Throwable $t) {
            $this->assertInstanceOf(UnknownOptionException::class, $t);
            \assert($t instanceof UnknownOptionException);
            $this->assertSame('foo', $t->getPath());
        }

        $config->addSchema('foo', Expect::string()->default('bar'));
        $this->assertSame('bar', $config->get('foo'));

        $config->addSchema('a', Expect::string()->required());

        // Even though 'a' requires data, reading 'foo' should still work
        $config->get('foo');

        // But reading 'a' should fail
        try {
            $config->get('a');
            $this->fail('A validation exception should be thrown since the "a" schema doesn\'t pass validation');
        } catch (\Throwable $t) {
            $this->assertInstanceOf(ValidationException::class, $t);
        }

        // Overwrite the previous schema we added
        $config->addSchema('a', Expect::int(42));

        $this->assertSame(42, $config->get('a'));
    }

    public function testGet(): void
    {
        $config = new Configuration([
            'foo' => Expect::string('bar'),
            'required_number' => Expect::int()->required(),
            'default_string' => Expect::string()->default('default'),
            'nested' => Expect::structure([
                'lucky_number' => Expect::int()->default(42),
            ]),
            'array' => Expect::arrayOf('mixed')->deprecated(),
        ]);

        $config->merge([
            'required_number' => 3,
            'array' => ['a' => 1, 'b' => 2],
        ]);

        // Test getting a single scalar element
        $this->assertSame(3, $config->get('required_number'));

        // Test getting a single scalar element with a default value
        $this->assertSame('bar', $config->get('foo'));

        // Test getting a single array element
        $this->assertSame(['a' => 1, 'b' => 2], $config->get('array'));

        // Test getting a nested array element by path
        $this->assertSame(2, $config->get('array/b'));
        $this->assertSame(2, $config->get('array.b'));

        // Test getting a nested structure element by path
        $this->assertSame(42, $config->get('nested/lucky_number'));
        $this->assertSame(42, $config->get('nested.lucky_number'));
    }

    public function testGetNonExistentPath(): void
    {
        $this->expectException(UnknownOptionException::class);
        $this->expectExceptionMessageMatches('/does-not-exist/');

        (new Configuration())->get('does-not-exist');
    }

    public function testGetWhenSchemaValidationFails(): void
    {
        $config = new Configuration();
        $config->addSchema('foo', Expect::int()->required());
        $config->addSchema('bar', Expect::int());

        try {
            $config->get('foo');
            $this->fail('A validation exception should have been thrown');
        } catch (\Throwable $t) {
            $this->assertInstanceOf(ValidationException::class, $t);
            $this->assertCount(1, $t->getMessages());
            $this->assertStringContainsString("item 'foo' is missing", $t->getMessages()[0]);
        }

        try {
            $config->set('bar', 'not an integer value');
            $config->get('bar');
            $this->fail('A validation exception should have been thrown');
        } catch (\Throwable $t) {
            $this->assertInstanceOf(ValidationException::class, $t);
            $this->assertCount(1, $t->getMessages());
            $this->assertStringContainsString("item 'bar' expects to be int", $t->getMessages()[0]);
        }
    }

    public function testExists(): void
    {
        $config = new Configuration([
            'str' => Expect::string(''),
            'int' => Expect::int(0),
            'arr' => Expect::array([]),
            'null' => Expect::null(),
            'nested' => Expect::structure([
                'foo' => Expect::type('int')->nullable(),
            ]),
        ]);

        $this->assertTrue($config->exists('str'));
        $this->assertTrue($config->exists('int'));
        $this->assertTrue($config->exists('arr'));
        $this->assertTrue($config->exists('null'));
        $this->assertTrue($config->exists('nested'));
        $this->assertTrue($config->exists('nested/foo'));

        $this->assertFalse($config->exists('does-not-exist'));
        $this->assertFalse($config->exists('arr.0'));
        $this->assertFalse($config->exists('nested/bar'));
        $this->assertFalse($config->exists('nested/foo/bar'));

        // Re-test 'str' to check the cache
        $this->assertTrue($config->exists('str'));

        // Test with an empty string
        $this->assertFalse($config->exists(''));
    }

    public function testSet(): void
    {
        $config = new Configuration([
            'foo' => Expect::type('int|string'),
        ]);

        $config->set('foo', 'bar');
        $this->assertSame('bar', $config->get('foo'));

        $config->set('foo', 42);
        $this->assertSame(42, $config->get('foo'));

        $config->set('foo', new \DateTimeImmutable());
        $this->expectException(ValidationException::class);
        $this->assertSame('bar', $config->get('foo'));
    }

    public function testSetNested(): void
    {
        $config = new Configuration([
            'a' => Expect::structure([
                'b' => Expect::structure([
                    'c' => Expect::string('d'),
                ]),
            ]),
        ]);

        $config->set('a/b/c', 'e');
        $this->assertSame('e', $config->get('a/b/c'));
        $this->assertSame(['c' => 'e'], $config->get('a/b'));
        $this->assertSame(['b' => ['c' => 'e']], $config->get('a'));

        $config->set('a/b', ['c' => 'f']);
        $this->assertSame('f', $config->get('a/b/c'));
        $this->assertSame(['c' => 'f'], $config->get('a/b'));
        $this->assertSame(['b' => ['c' => 'f']], $config->get('a'));

        $config->set('a', ['b' => ['c' => 'g']]);
        $this->assertSame('g', $config->get('a/b/c'));
        $this->assertSame(['c' => 'g'], $config->get('a/b'));
        $this->assertSame(['b' => ['c' => 'g']], $config->get('a'));
    }

    public function testSetInvalidType(): void
    {
        $this->expectException(ValidationException::class);
        $this->expectExceptionMessageMatches("/item 'foo' expects to be int/");

        $config = new Configuration(['foo' => Expect::int()]);

        $config->set('foo', 'bar');
        $config->get('foo');
    }

    public function testSetUndefinedKeyDoesNotThrowException(): void
    {
        $config = new Configuration(['foo' => Expect::int(42)]);

        $config->set('bar', 3);
        $this->assertSame(42, $config->get('foo'));
    }

    public function testSetNestedWhenOptionNotNested(): void
    {
        $this->expectException(UnknownOptionException::class);
        $this->expectExceptionMessageMatches('/cannot be indexed into/');

        $config = new Configuration(['foo' => Expect::int(42)]);

        $config->set('foo', 3);
        $config->set('foo.bar', 42);
    }

    public function testMerge(): void
    {
        $config = new Configuration([
            'foo' => Expect::int(1),
            'bar' => Expect::int(1),
            'baz' => Expect::structure([
                'a' => Expect::int(1),
                'b' => Expect::int(1),
                'c' => Expect::int(1),
            ]),
        ]);

        $config->set('foo', 1);
        $config->merge([
            'foo' => 2,
            'baz' => [
                'b' => 2,
            ],
        ]);

        $this->assertSame(2, $config->get('foo'));
        $this->assertSame(1, $config->get('bar'));
        $this->assertSame(1, $config->get('baz/a'));
        $this->assertSame(2, $config->get('baz/b'));
        $this->assertSame(1, $config->get('baz/c'));

        $config->merge([
            'foo' => 3,
            'baz' => [
                'c' => 3,
            ],
        ]);

        $this->assertSame(3, $config->get('foo'));
        $this->assertSame(1, $config->get('bar'));
        $this->assertSame(1, $config->get('baz/a'));
        $this->assertSame(2, $config->get('baz/b'));
        $this->assertSame(3, $config->get('baz/c'));
    }

    public function testReader(): void
    {
        $config = new Configuration(['foo' => Expect::string('bar')]);

        $reader = $config->reader();

        $this->assertSame('bar', $config->get('foo'));
        $this->assertSame('bar', $reader->get('foo'));
        $this->assertTrue($reader->exists('foo'));

        $config->set('foo', 'baz');

        $this->assertSame('baz', $config->get('foo'));
        $this->assertSame('baz', $reader->get('foo'));
        $this->assertTrue($reader->exists('foo'));
    }
}