Completed
Push — authenticator-refactor ( 0a18bb...b9e528 )
by Simon
08:12
created

ConfigTest::testStaticLookup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 14
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\Config\Config;
7
use SilverStripe\Dev\SapphireTest;
8
9
class ConfigTest extends SapphireTest
10
{
11
12
    public function testNest()
13
    {
14
        // Check basic config
15
        $this->assertEquals(3, Config::inst()->get(ConfigTest\TestNest::class, 'foo'));
16
        $this->assertEquals(5, Config::inst()->get(ConfigTest\TestNest::class, 'bar'));
17
18
        // Test nest copies data
19
        Config::nest();
20
        $this->assertEquals(3, Config::inst()->get(ConfigTest\TestNest::class, 'foo'));
21
        $this->assertEquals(5, Config::inst()->get(ConfigTest\TestNest::class, 'bar'));
22
23
        // Test nested data can be updated
24
        Config::modify()->merge(ConfigTest\TestNest::class, 'foo', 4);
25
        $this->assertEquals(4, Config::inst()->get(ConfigTest\TestNest::class, 'foo'));
26
        $this->assertEquals(5, Config::inst()->get(ConfigTest\TestNest::class, 'bar'));
27
28
        // Test unnest restores data
29
        Config::unnest();
30
        $this->assertEquals(3, Config::inst()->get(ConfigTest\TestNest::class, 'foo'));
31
        $this->assertEquals(5, Config::inst()->get(ConfigTest\TestNest::class, 'bar'));
32
    }
33
34
    public function testUpdateStatic()
35
    {
36
        $this->assertEquals(
37
            ['test_1'],
38
            Config::inst()->get(ConfigTest\First::class, 'first')
39
        );
40
        $this->assertEquals(
41
            [
42
                'test_1',
43
                'test_2'
44
            ],
45
            Config::inst()->get(ConfigTest\Second::class, 'first')
46
        );
47
        $this->assertEquals(
48
            [ 'test_2' ],
49
            Config::inst()->get(ConfigTest\Second::class, 'first', true)
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50
        );
51
        $this->assertEquals(
52
            [
53
                'test_1',
54
                'test_2',
55
                'test_3'
56
            ],
57
            Config::inst()->get(ConfigTest\Third::class, 'first')
58
        );
59
        $this->assertEquals(
60
            [ 'test_3' ],
61
            Config::inst()->get(ConfigTest\Third::class, 'first', true)
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
        );
63
64
        Config::modify()->merge(ConfigTest\First::class, 'first', array('test_1_2'));
65
        Config::modify()->merge(ConfigTest\Third::class, 'first', array('test_3_2'));
66
        Config::modify()->merge(ConfigTest\Fourth::class, 'first', array('test_4'));
67
68
        $this->assertEquals(
69
            ['test_1', 'test_1_2'],
70
            Config::inst()->get(ConfigTest\First::class, 'first')
71
        );
72
        $this->assertEquals(
73
            ['test_1', 'test_1_2'],
74
            Config::inst()->get(ConfigTest\First::class, 'first', true)
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
        );
76
77
        Config::modify()->merge(ConfigTest\Fourth::class, 'second', array('test_4'));
78
        Config::modify()->merge(ConfigTest\Third::class, 'second', array('test_3_2'));
79
80
        $this->assertEquals(
81
            ['test_1', 'test_3', 'test_3_2', 'test_4'],
82
            Config::inst()->get(ConfigTest\Fourth::class, 'second')
83
        );
84
        $this->assertEquals(
85
            ['test_4'],
86
            Config::inst()->get(ConfigTest\Fourth::class, 'second', true)
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
        );
88
        $this->assertEquals(
89
            ['test_1', 'test_3', 'test_3_2'],
90
            Config::inst()->get(ConfigTest\Third::class, 'second')
91
        );
92
        $this->assertEquals(
93
            ['test_3', 'test_3_2'],
94
            Config::inst()->get(ConfigTest\Third::class, 'second', true)
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
        );
96
97
        Config::modify()->remove(ConfigTest\Third::class, 'second');
98
        $this->assertEquals(
99
            ['test_1'],
100
            Config::inst()->get(ConfigTest\Third::class, 'second')
101
        );
102
        $this->assertTrue(
103
            Config::inst()->exists(ConfigTest\Third::class, 'second')
104
        );
105
        $this->assertEquals(
106
            null,
107
            Config::inst()->get(ConfigTest\Third::class, 'second', true)
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
        );
109
        $this->assertFalse(
110
            Config::inst()->exists(ConfigTest\Third::class, 'second', true)
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
111
        );
112
        Config::modify()->merge(ConfigTest\Third::class, 'second', ['test_3_2']);
113
        $this->assertEquals(
114
            ['test_1', 'test_3_2'],
115
            Config::inst()->get(ConfigTest\Third::class, 'second')
116
        );
117
    }
118
119
    public function testUpdateWithFalsyValues()
120
    {
121
        // Booleans
122
        $this->assertTrue(Config::inst()->get(ConfigTest\First::class, 'bool'));
123
        Config::modify()->merge(ConfigTest\First::class, 'bool', false);
124
        $this->assertFalse(Config::inst()->get(ConfigTest\First::class, 'bool'));
125
        Config::modify()->merge(ConfigTest\First::class, 'bool', true);
126
        $this->assertTrue(Config::inst()->get(ConfigTest\First::class, 'bool'));
127
128
        // Integers
129
        $this->assertEquals(42, Config::inst()->get(ConfigTest\First::class, 'int'));
130
        Config::modify()->merge(ConfigTest\First::class, 'int', 0);
131
        $this->assertEquals(0, Config::inst()->get(ConfigTest\First::class, 'int'));
132
        Config::modify()->merge(ConfigTest\First::class, 'int', 42);
133
        $this->assertEquals(42, Config::inst()->get(ConfigTest\First::class, 'int'));
134
135
        // Strings
136
        $this->assertEquals('value', Config::inst()->get(ConfigTest\First::class, 'string'));
137
        Config::modify()->merge(ConfigTest\First::class, 'string', '');
138
        $this->assertEquals('', Config::inst()->get(ConfigTest\First::class, 'string'));
139
        Config::modify()->merge(ConfigTest\First::class, 'string', 'value');
140
        $this->assertEquals('value', Config::inst()->get(ConfigTest\First::class, 'string'));
141
142
        // Nulls
143
        $this->assertEquals('value', Config::inst()->get(ConfigTest\First::class, 'nullable'));
144
        Config::modify()->merge(ConfigTest\First::class, 'nullable', null);
145
        $this->assertNull(Config::inst()->get(ConfigTest\First::class, 'nullable'));
146
        Config::modify()->merge(ConfigTest\First::class, 'nullable', 'value');
147
        $this->assertEquals('value', Config::inst()->get(ConfigTest\First::class, 'nullable'));
148
    }
149
150
    public function testSetsFalsyDefaults()
151
    {
152
        $this->assertFalse(Config::inst()->get(ConfigTest\First::class, 'default_false'));
153
        // Technically the same as an undefined config key
154
        $this->assertNull(Config::inst()->get(ConfigTest\First::class, 'default_null'));
155
        $this->assertEquals(0, Config::inst()->get(ConfigTest\First::class, 'default_zero'));
156
        $this->assertEquals('', Config::inst()->get(ConfigTest\First::class, 'default_empty_string'));
157
    }
158
159
    public function testUninheritedStatic()
160
    {
161
        $this->assertEquals(Config::inst()->get(ConfigTest\First::class, 'third', Config::UNINHERITED), 'test_1');
162
        $this->assertEquals(Config::inst()->get(ConfigTest\Fourth::class, 'third', Config::UNINHERITED), null);
163
164
        Config::modify()->merge(ConfigTest\First::class, 'first', array('test_1b'));
165
        Config::modify()->merge(ConfigTest\Second::class, 'first', array('test_2b'));
166
167
        // Check that it can be applied to parent and subclasses, and queried directly
168
        $this->assertContains(
169
            'test_1b',
170
            Config::inst()->get(ConfigTest\First::class, 'first', Config::UNINHERITED)
171
        );
172
        $this->assertContains(
173
            'test_2b',
174
            Config::inst()->get(ConfigTest\Second::class, 'first', Config::UNINHERITED)
175
        );
176
177
        // But it won't affect subclasses - this is *uninherited* static
178
        $this->assertNotContains(
179
            'test_2b',
180
            Config::inst()->get(ConfigTest\Third::class, 'first', Config::UNINHERITED)
181
        );
182
        $this->assertNull(Config::inst()->get(ConfigTest\Fourth::class, 'first', Config::UNINHERITED));
183
184
        // Subclasses that don't have the static explicitly defined should allow definition, also
185
        // This also checks that set can be called after the first uninherited get()
186
        // call (which can be buggy due to caching)
187
        Config::modify()->merge(ConfigTest\Fourth::class, 'first', array('test_4b'));
188
        $this->assertContains('test_4b', Config::inst()->get(ConfigTest\Fourth::class, 'first', Config::UNINHERITED));
189
    }
190
191
    public function testCombinedStatic()
192
    {
193
        $this->assertEquals(
194
            ['test_1', 'test_2', 'test_3'],
195
            ConfigTest\Combined3::config()->get('first')
196
        );
197
198
        // Test that unset values are ignored
199
        $this->assertEquals(
200
            ['test_1', 'test_3'],
201
            ConfigTest\Combined3::config()->get('second')
202
        );
203
    }
204
205
    public function testMerges()
206
    {
207
        $result = Priority::mergeArray(
208
            ['A' => 1, 'B' => 2, 'C' => 3],
209
            ['C' => 4, 'D' => 5]
210
        );
211
        $this->assertEquals(
212
            ['A' => 1, 'B' => 2, 'C' => 3, 'D' => 5],
213
            $result
214
        );
215
216
        $result = Priority::mergeArray(
217
            ['C' => 4, 'D' => 5],
218
            ['A' => 1, 'B' => 2, 'C' => 3]
219
        );
220
        $this->assertEquals(
221
            ['A' => 1, 'B' => 2, 'C' => 4, 'D' => 5],
222
            $result
223
        );
224
225
        $result = Priority::mergeArray(
226
            [ 'C' => [4, 5, 6], 'D' => 5 ],
227
            [ 'A' => 1, 'B' => 2, 'C' => [1, 2, 3] ]
228
        );
229
        $this->assertEquals(
230
            ['A' => 1, 'B' => 2, 'C' => [1, 2, 3, 4, 5, 6], 'D' => 5],
231
            $result
232
        );
233
234
        $result = Priority::mergeArray(
235
            ['A' => 1, 'B' => 2, 'C' => [1, 2, 3]],
236
            ['C' => [4, 5, 6], 'D' => 5]
237
        );
238
        $this->assertEquals(
239
            ['A' => 1, 'B' => 2, 'C' => [4, 5, 6, 1, 2, 3], 'D' => 5],
240
            $result
241
        );
242
243
        $result = Priority::mergeArray(
244
            ['A' => 1, 'B' => 2, 'C' => ['Foo' => 1, 'Bar' => 2], 'D' => 3],
245
            ['C' => ['Bar' => 3, 'Baz' => 4]]
246
        );
247
        $this->assertEquals(
248
            ['A' => 1, 'B' => 2, 'C' => ['Foo' => 1, 'Bar' => 2, 'Baz' => 4], 'D' => 3],
249
            $result
250
        );
251
252
        $result = Priority::mergeArray(
253
            ['C' => ['Bar' => 3, 'Baz' => 4]],
254
            ['A' => 1, 'B' => 2, 'C' => ['Foo' => 1, 'Bar' => 2], 'D' => 3]
255
        );
256
        $this->assertEquals(
257
            ['A' => 1, 'B' => 2, 'C' => ['Foo' => 1, 'Bar' => 3, 'Baz' => 4], 'D' => 3],
258
            $result
259
        );
260
    }
261
262
    public function testForClass()
263
    {
264
        $config = ConfigTest\DefinesFoo::config();
265
        // Set values
266
        $this->assertTrue(isset($config->not_foo));
267
        $this->assertFalse(empty($config->not_foo));
268
        $this->assertEquals(1, $config->not_foo);
269
270
        // Unset values
271
        $this->assertFalse(isset($config->bar));
272
        $this->assertTrue(empty($config->bar));
273
        $this->assertNull($config->bar);
274
    }
275
}
276