Completed
Push — master ( deca00...5388ff )
by Sam
24s
created

ConfigTest::testCombinedStatic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Core\Tests\Config;
4
5
use SilverStripe\Config\MergeStrategy\Priority;
6
use SilverStripe\Core\Object;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Dev\SapphireTest;
9
10
class ConfigTest extends SapphireTest
11
{
12
13
    public function testNest()
14
    {
15
        // Check basic config
16
        $this->assertEquals(3, Config::inst()->get(ConfigTest\TestNest::class, 'foo'));
17
        $this->assertEquals(5, Config::inst()->get(ConfigTest\TestNest::class, 'bar'));
18
19
        // Test nest copies data
20
        Config::nest();
21
        $this->assertEquals(3, Config::inst()->get(ConfigTest\TestNest::class, 'foo'));
22
        $this->assertEquals(5, Config::inst()->get(ConfigTest\TestNest::class, 'bar'));
23
24
        // Test nested data can be updated
25
        Config::modify()->merge(ConfigTest\TestNest::class, 'foo', 4);
26
        $this->assertEquals(4, Config::inst()->get(ConfigTest\TestNest::class, 'foo'));
27
        $this->assertEquals(5, Config::inst()->get(ConfigTest\TestNest::class, 'bar'));
28
29
        // Test unnest restores data
30
        Config::unnest();
31
        $this->assertEquals(3, Config::inst()->get(ConfigTest\TestNest::class, 'foo'));
32
        $this->assertEquals(5, Config::inst()->get(ConfigTest\TestNest::class, 'bar'));
33
    }
34
35
    public function testUpdateStatic()
36
    {
37
        $this->assertEquals(
38
            ['test_1'],
39
            Config::inst()->get(ConfigTest\First::class, 'first')
40
        );
41
        $this->assertEquals(
42
            [
43
                'test_1',
44
                'test_2'
45
            ],
46
            Config::inst()->get(ConfigTest\Second::class, 'first')
47
        );
48
        $this->assertEquals(
49
            [ 'test_2' ],
50
            Config::inst()->get(ConfigTest\Second::class, 'first', true)
51
        );
52
        $this->assertEquals(
53
            [
54
                'test_1',
55
                'test_2',
56
                'test_3'
57
            ],
58
            Config::inst()->get(ConfigTest\Third::class, 'first')
59
        );
60
        $this->assertEquals(
61
            [ 'test_3' ],
62
            Config::inst()->get(ConfigTest\Third::class, 'first', true)
63
        );
64
65
        Config::modify()->merge(ConfigTest\First::class, 'first', array('test_1_2'));
66
        Config::modify()->merge(ConfigTest\Third::class, 'first', array('test_3_2'));
67
        Config::modify()->merge(ConfigTest\Fourth::class, 'first', array('test_4'));
68
69
        $this->assertEquals(
70
            ['test_1', 'test_1_2'],
71
            Config::inst()->get(ConfigTest\First::class, 'first')
72
        );
73
        $this->assertEquals(
74
            ['test_1', 'test_1_2'],
75
            Config::inst()->get(ConfigTest\First::class, 'first', true)
76
        );
77
78
        Config::modify()->merge(ConfigTest\Fourth::class, 'second', array('test_4'));
79
        Config::modify()->merge(ConfigTest\Third::class, 'second', array('test_3_2'));
80
81
        $this->assertEquals(
82
            ['test_1', 'test_3', 'test_3_2', 'test_4'],
83
            Config::inst()->get(ConfigTest\Fourth::class, 'second')
84
        );
85
        $this->assertEquals(
86
            ['test_4'],
87
            Config::inst()->get(ConfigTest\Fourth::class, 'second', true)
88
        );
89
        $this->assertEquals(
90
            ['test_1', 'test_3', 'test_3_2'],
91
            Config::inst()->get(ConfigTest\Third::class, 'second')
92
        );
93
        $this->assertEquals(
94
            ['test_3', 'test_3_2'],
95
            Config::inst()->get(ConfigTest\Third::class, 'second', true)
96
        );
97
98
        Config::modify()->remove(ConfigTest\Third::class, 'second');
99
        $this->assertEquals(
100
            ['test_1'],
101
            Config::inst()->get(ConfigTest\Third::class, 'second')
102
        );
103
        $this->assertTrue(
104
            Config::inst()->exists(ConfigTest\Third::class, 'second')
105
        );
106
        $this->assertEquals(
107
            null,
108
            Config::inst()->get(ConfigTest\Third::class, 'second', true)
109
        );
110
        $this->assertFalse(
111
            Config::inst()->exists(ConfigTest\Third::class, 'second', true)
112
        );
113
        Config::modify()->merge(ConfigTest\Third::class, 'second', ['test_3_2']);
114
        $this->assertEquals(
115
            ['test_1', 'test_3_2'],
116
            Config::inst()->get(ConfigTest\Third::class, 'second')
117
        );
118
    }
119
120
    public function testUpdateWithFalsyValues()
121
    {
122
        // Booleans
123
        $this->assertTrue(Config::inst()->get(ConfigTest\First::class, 'bool'));
124
        Config::modify()->merge(ConfigTest\First::class, 'bool', false);
125
        $this->assertFalse(Config::inst()->get(ConfigTest\First::class, 'bool'));
126
        Config::modify()->merge(ConfigTest\First::class, 'bool', true);
127
        $this->assertTrue(Config::inst()->get(ConfigTest\First::class, 'bool'));
128
129
        // Integers
130
        $this->assertEquals(42, Config::inst()->get(ConfigTest\First::class, 'int'));
131
        Config::modify()->merge(ConfigTest\First::class, 'int', 0);
132
        $this->assertEquals(0, Config::inst()->get(ConfigTest\First::class, 'int'));
133
        Config::modify()->merge(ConfigTest\First::class, 'int', 42);
134
        $this->assertEquals(42, Config::inst()->get(ConfigTest\First::class, 'int'));
135
136
        // Strings
137
        $this->assertEquals('value', Config::inst()->get(ConfigTest\First::class, 'string'));
138
        Config::modify()->merge(ConfigTest\First::class, 'string', '');
139
        $this->assertEquals('', Config::inst()->get(ConfigTest\First::class, 'string'));
140
        Config::modify()->merge(ConfigTest\First::class, 'string', 'value');
141
        $this->assertEquals('value', Config::inst()->get(ConfigTest\First::class, 'string'));
142
143
        // Nulls
144
        $this->assertEquals('value', Config::inst()->get(ConfigTest\First::class, 'nullable'));
145
        Config::modify()->merge(ConfigTest\First::class, 'nullable', null);
146
        $this->assertNull(Config::inst()->get(ConfigTest\First::class, 'nullable'));
147
        Config::modify()->merge(ConfigTest\First::class, 'nullable', 'value');
148
        $this->assertEquals('value', Config::inst()->get(ConfigTest\First::class, 'nullable'));
149
    }
150
151
    public function testSetsFalsyDefaults()
152
    {
153
        $this->assertFalse(Config::inst()->get(ConfigTest\First::class, 'default_false'));
154
        // Technically the same as an undefined config key
155
        $this->assertNull(Config::inst()->get(ConfigTest\First::class, 'default_null'));
156
        $this->assertEquals(0, Config::inst()->get(ConfigTest\First::class, 'default_zero'));
157
        $this->assertEquals('', Config::inst()->get(ConfigTest\First::class, 'default_empty_string'));
158
    }
159
160
    public function testUninheritedStatic()
161
    {
162
        $this->assertEquals(Config::inst()->get(ConfigTest\First::class, 'third', Config::UNINHERITED), 'test_1');
163
        $this->assertEquals(Config::inst()->get(ConfigTest\Fourth::class, 'third', Config::UNINHERITED), null);
164
165
        Config::modify()->merge(ConfigTest\First::class, 'first', array('test_1b'));
166
        Config::modify()->merge(ConfigTest\Second::class, 'first', array('test_2b'));
167
168
        // Check that it can be applied to parent and subclasses, and queried directly
169
        $this->assertContains(
170
            'test_1b',
171
            Config::inst()->get(ConfigTest\First::class, 'first', Config::UNINHERITED)
172
        );
173
        $this->assertContains(
174
            'test_2b',
175
            Config::inst()->get(ConfigTest\Second::class, 'first', Config::UNINHERITED)
176
        );
177
178
        // But it won't affect subclasses - this is *uninherited* static
179
        $this->assertNotContains(
180
            'test_2b',
181
            Config::inst()->get(ConfigTest\Third::class, 'first', Config::UNINHERITED)
182
        );
183
        $this->assertNull(Config::inst()->get(ConfigTest\Fourth::class, 'first', Config::UNINHERITED));
184
185
        // Subclasses that don't have the static explicitly defined should allow definition, also
186
        // This also checks that set can be called after the first uninherited get()
187
        // call (which can be buggy due to caching)
188
        Config::modify()->merge(ConfigTest\Fourth::class, 'first', array('test_4b'));
189
        $this->assertContains('test_4b', Config::inst()->get(ConfigTest\Fourth::class, 'first', Config::UNINHERITED));
190
    }
191
192
    public function testCombinedStatic()
193
    {
194
        $this->assertEquals(
195
            ['test_1', 'test_2', 'test_3'],
196
            ConfigTest\Combined3::config()->get('first')
197
        );
198
199
        // Test that unset values are ignored
200
        $this->assertEquals(
201
            ['test_1', 'test_3'],
202
            ConfigTest\Combined3::config()->get('second')
203
        );
204
    }
205
206
    public function testMerges()
207
    {
208
        $result = Priority::mergeArray(
209
            ['A' => 1, 'B' => 2, 'C' => 3],
210
            ['C' => 4, 'D' => 5]
211
        );
212
        $this->assertEquals(
213
            ['A' => 1, 'B' => 2, 'C' => 3, 'D' => 5],
214
            $result
215
        );
216
217
        $result = Priority::mergeArray(
218
            ['C' => 4, 'D' => 5],
219
            ['A' => 1, 'B' => 2, 'C' => 3]
220
        );
221
        $this->assertEquals(
222
            ['A' => 1, 'B' => 2, 'C' => 4, 'D' => 5],
223
            $result
224
        );
225
226
        $result = Priority::mergeArray(
227
            [ 'C' => [4, 5, 6], 'D' => 5 ],
228
            [ 'A' => 1, 'B' => 2, 'C' => [1, 2, 3] ]
229
        );
230
        $this->assertEquals(
231
            ['A' => 1, 'B' => 2, 'C' => [1, 2, 3, 4, 5, 6], 'D' => 5],
232
            $result
233
        );
234
235
        $result = Priority::mergeArray(
236
            ['A' => 1, 'B' => 2, 'C' => [1, 2, 3]],
237
            ['C' => [4, 5, 6], 'D' => 5]
238
        );
239
        $this->assertEquals(
240
            ['A' => 1, 'B' => 2, 'C' => [4, 5, 6, 1, 2, 3], 'D' => 5],
241
            $result
242
        );
243
244
        $result = Priority::mergeArray(
245
            ['A' => 1, 'B' => 2, 'C' => ['Foo' => 1, 'Bar' => 2], 'D' => 3],
246
            ['C' => ['Bar' => 3, 'Baz' => 4]]
247
        );
248
        $this->assertEquals(
249
            ['A' => 1, 'B' => 2, 'C' => ['Foo' => 1, 'Bar' => 2, 'Baz' => 4], 'D' => 3],
250
            $result
251
        );
252
253
        $result = Priority::mergeArray(
254
            ['C' => ['Bar' => 3, 'Baz' => 4]],
255
            ['A' => 1, 'B' => 2, 'C' => ['Foo' => 1, 'Bar' => 2], 'D' => 3]
256
        );
257
        $this->assertEquals(
258
            ['A' => 1, 'B' => 2, 'C' => ['Foo' => 1, 'Bar' => 3, 'Baz' => 4], 'D' => 3],
259
            $result
260
        );
261
    }
262
263
    public function testStaticLookup()
264
    {
265
        $this->assertEquals(Object::static_lookup(ConfigTest\DefinesFoo::class, 'foo'), 1);
266
        $this->assertEquals(Object::static_lookup(ConfigTest\DefinesFoo::class, 'bar'), null);
267
268
        $this->assertEquals(Object::static_lookup(ConfigTest\DefinesBar::class, 'foo'), null);
269
        $this->assertEquals(Object::static_lookup(ConfigTest\DefinesBar::class, 'bar'), 2);
270
271
        $this->assertEquals(Object::static_lookup(ConfigTest\DefinesFooAndBar::class, 'foo'), 3);
272
        $this->assertEquals(Object::static_lookup(ConfigTest\DefinesFooAndBar::class, 'bar'), 3);
273
274
        $this->assertEquals(Object::static_lookup(ConfigTest\DefinesFooDoesntExtendObject::class, 'foo'), 4);
275
        $this->assertEquals(Object::static_lookup(ConfigTest\DefinesFooDoesntExtendObject::class, 'bar'), null);
276
    }
277
278
    public function testForClass()
279
    {
280
        $config = ConfigTest\DefinesFoo::config();
281
        // Set values
282
        $this->assertTrue(isset($config->not_foo));
283
        $this->assertFalse(empty($config->not_foo));
284
        $this->assertEquals(1, $config->not_foo);
285
286
        // Unset values
287
        $this->assertFalse(isset($config->bar));
288
        $this->assertTrue(empty($config->bar));
289
        $this->assertNull($config->bar);
290
    }
291
}
292