2011-12-22 03:17:41 +01:00
|
|
|
<?php
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
namespace SilverStripe\Core\Tests\Config;
|
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
use SilverStripe\Config\MergeStrategy\Priority;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Object;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class ConfigTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
public function testNest()
|
|
|
|
{
|
|
|
|
// Check basic config
|
|
|
|
$this->assertEquals(3, Config::inst()->get(ConfigTest\TestNest::class, 'foo'));
|
|
|
|
$this->assertEquals(5, Config::inst()->get(ConfigTest\TestNest::class, 'bar'));
|
|
|
|
|
|
|
|
// Test nest copies data
|
|
|
|
Config::nest();
|
|
|
|
$this->assertEquals(3, Config::inst()->get(ConfigTest\TestNest::class, 'foo'));
|
|
|
|
$this->assertEquals(5, Config::inst()->get(ConfigTest\TestNest::class, 'bar'));
|
|
|
|
|
|
|
|
// Test nested data can be updated
|
2017-02-22 04:15:08 +01:00
|
|
|
Config::modify()->merge(ConfigTest\TestNest::class, 'foo', 4);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(4, Config::inst()->get(ConfigTest\TestNest::class, 'foo'));
|
|
|
|
$this->assertEquals(5, Config::inst()->get(ConfigTest\TestNest::class, 'bar'));
|
|
|
|
|
|
|
|
// Test unnest restores data
|
|
|
|
Config::unnest();
|
|
|
|
$this->assertEquals(3, Config::inst()->get(ConfigTest\TestNest::class, 'foo'));
|
|
|
|
$this->assertEquals(5, Config::inst()->get(ConfigTest\TestNest::class, 'bar'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateStatic()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2017-02-22 04:15:08 +01:00
|
|
|
['test_1'],
|
|
|
|
Config::inst()->get(ConfigTest\First::class, 'first')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
'test_1',
|
|
|
|
'test_2'
|
|
|
|
],
|
|
|
|
Config::inst()->get(ConfigTest\Second::class, 'first')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
[ 'test_2' ],
|
|
|
|
Config::inst()->get(ConfigTest\Second::class, 'first', true)
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-02-22 04:15:08 +01:00
|
|
|
[
|
|
|
|
'test_1',
|
|
|
|
'test_2',
|
|
|
|
'test_3'
|
|
|
|
],
|
|
|
|
Config::inst()->get(ConfigTest\Third::class, 'first')
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-02-22 04:15:08 +01:00
|
|
|
[ 'test_3' ],
|
|
|
|
Config::inst()->get(ConfigTest\Third::class, 'first', true)
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
Config::modify()->merge(ConfigTest\First::class, 'first', array('test_1_2'));
|
|
|
|
Config::modify()->merge(ConfigTest\Third::class, 'first', array('test_3_2'));
|
|
|
|
Config::modify()->merge(ConfigTest\Fourth::class, 'first', array('test_4'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
2017-02-22 04:15:08 +01:00
|
|
|
['test_1', 'test_1_2'],
|
|
|
|
Config::inst()->get(ConfigTest\First::class, 'first')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
['test_1', 'test_1_2'],
|
|
|
|
Config::inst()->get(ConfigTest\First::class, 'first', true)
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
Config::modify()->merge(ConfigTest\Fourth::class, 'second', array('test_4'));
|
|
|
|
Config::modify()->merge(ConfigTest\Third::class, 'second', array('test_3_2'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
2017-02-22 04:15:08 +01:00
|
|
|
['test_1', 'test_3', 'test_3_2', 'test_4'],
|
|
|
|
Config::inst()->get(ConfigTest\Fourth::class, 'second')
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-02-22 04:15:08 +01:00
|
|
|
['test_4'],
|
|
|
|
Config::inst()->get(ConfigTest\Fourth::class, 'second', true)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
['test_1', 'test_3', 'test_3_2'],
|
|
|
|
Config::inst()->get(ConfigTest\Third::class, 'second')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
['test_3', 'test_3_2'],
|
|
|
|
Config::inst()->get(ConfigTest\Third::class, 'second', true)
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
Config::modify()->remove(ConfigTest\Third::class, 'second');
|
|
|
|
$this->assertEquals(
|
|
|
|
['test_1'],
|
|
|
|
Config::inst()->get(ConfigTest\Third::class, 'second')
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
Config::inst()->exists(ConfigTest\Third::class, 'second')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
null,
|
|
|
|
Config::inst()->get(ConfigTest\Third::class, 'second', true)
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
Config::inst()->exists(ConfigTest\Third::class, 'second', true)
|
|
|
|
);
|
|
|
|
Config::modify()->merge(ConfigTest\Third::class, 'second', ['test_3_2']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2017-02-22 04:15:08 +01:00
|
|
|
['test_1', 'test_3_2'],
|
|
|
|
Config::inst()->get(ConfigTest\Third::class, 'second')
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateWithFalsyValues()
|
|
|
|
{
|
|
|
|
// Booleans
|
|
|
|
$this->assertTrue(Config::inst()->get(ConfigTest\First::class, 'bool'));
|
2017-02-22 04:15:08 +01:00
|
|
|
Config::modify()->merge(ConfigTest\First::class, 'bool', false);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertFalse(Config::inst()->get(ConfigTest\First::class, 'bool'));
|
2017-02-22 04:15:08 +01:00
|
|
|
Config::modify()->merge(ConfigTest\First::class, 'bool', true);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue(Config::inst()->get(ConfigTest\First::class, 'bool'));
|
|
|
|
|
|
|
|
// Integers
|
|
|
|
$this->assertEquals(42, Config::inst()->get(ConfigTest\First::class, 'int'));
|
2017-02-22 04:15:08 +01:00
|
|
|
Config::modify()->merge(ConfigTest\First::class, 'int', 0);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(0, Config::inst()->get(ConfigTest\First::class, 'int'));
|
2017-02-22 04:15:08 +01:00
|
|
|
Config::modify()->merge(ConfigTest\First::class, 'int', 42);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(42, Config::inst()->get(ConfigTest\First::class, 'int'));
|
|
|
|
|
|
|
|
// Strings
|
|
|
|
$this->assertEquals('value', Config::inst()->get(ConfigTest\First::class, 'string'));
|
2017-02-22 04:15:08 +01:00
|
|
|
Config::modify()->merge(ConfigTest\First::class, 'string', '');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals('', Config::inst()->get(ConfigTest\First::class, 'string'));
|
2017-02-22 04:15:08 +01:00
|
|
|
Config::modify()->merge(ConfigTest\First::class, 'string', 'value');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals('value', Config::inst()->get(ConfigTest\First::class, 'string'));
|
|
|
|
|
|
|
|
// Nulls
|
|
|
|
$this->assertEquals('value', Config::inst()->get(ConfigTest\First::class, 'nullable'));
|
2017-02-22 04:15:08 +01:00
|
|
|
Config::modify()->merge(ConfigTest\First::class, 'nullable', null);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertNull(Config::inst()->get(ConfigTest\First::class, 'nullable'));
|
2017-02-22 04:15:08 +01:00
|
|
|
Config::modify()->merge(ConfigTest\First::class, 'nullable', 'value');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals('value', Config::inst()->get(ConfigTest\First::class, 'nullable'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetsFalsyDefaults()
|
|
|
|
{
|
|
|
|
$this->assertFalse(Config::inst()->get(ConfigTest\First::class, 'default_false'));
|
|
|
|
// Technically the same as an undefined config key
|
|
|
|
$this->assertNull(Config::inst()->get(ConfigTest\First::class, 'default_null'));
|
|
|
|
$this->assertEquals(0, Config::inst()->get(ConfigTest\First::class, 'default_zero'));
|
|
|
|
$this->assertEquals('', Config::inst()->get(ConfigTest\First::class, 'default_empty_string'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUninheritedStatic()
|
|
|
|
{
|
|
|
|
$this->assertEquals(Config::inst()->get(ConfigTest\First::class, 'third', Config::UNINHERITED), 'test_1');
|
|
|
|
$this->assertEquals(Config::inst()->get(ConfigTest\Fourth::class, 'third', Config::UNINHERITED), null);
|
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
Config::modify()->merge(ConfigTest\First::class, 'first', array('test_1b'));
|
|
|
|
Config::modify()->merge(ConfigTest\Second::class, 'first', array('test_2b'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Check that it can be applied to parent and subclasses, and queried directly
|
|
|
|
$this->assertContains(
|
|
|
|
'test_1b',
|
|
|
|
Config::inst()->get(ConfigTest\First::class, 'first', Config::UNINHERITED)
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'test_2b',
|
|
|
|
Config::inst()->get(ConfigTest\Second::class, 'first', Config::UNINHERITED)
|
|
|
|
);
|
|
|
|
|
|
|
|
// But it won't affect subclasses - this is *uninherited* static
|
|
|
|
$this->assertNotContains(
|
|
|
|
'test_2b',
|
|
|
|
Config::inst()->get(ConfigTest\Third::class, 'first', Config::UNINHERITED)
|
|
|
|
);
|
|
|
|
$this->assertNull(Config::inst()->get(ConfigTest\Fourth::class, 'first', Config::UNINHERITED));
|
|
|
|
|
|
|
|
// Subclasses that don't have the static explicitly defined should allow definition, also
|
|
|
|
// This also checks that set can be called after the first uninherited get()
|
|
|
|
// call (which can be buggy due to caching)
|
2017-02-22 04:15:08 +01:00
|
|
|
Config::modify()->merge(ConfigTest\Fourth::class, 'first', array('test_4b'));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertContains('test_4b', Config::inst()->get(ConfigTest\Fourth::class, 'first', Config::UNINHERITED));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCombinedStatic()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2017-02-22 04:15:08 +01:00
|
|
|
['test_1', 'test_2', 'test_3'],
|
|
|
|
ConfigTest\Combined3::config()->get('first')
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
// Test that unset values are ignored
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2017-02-22 04:15:08 +01:00
|
|
|
['test_1', 'test_3'],
|
|
|
|
ConfigTest\Combined3::config()->get('second')
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMerges()
|
|
|
|
{
|
2017-02-22 04:15:08 +01:00
|
|
|
$result = Priority::mergeArray(
|
|
|
|
['A' => 1, 'B' => 2, 'C' => 3],
|
|
|
|
['C' => 4, 'D' => 5]
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
['A' => 1, 'B' => 2, 'C' => 3, 'D' => 5],
|
|
|
|
$result
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
$result = Priority::mergeArray(
|
|
|
|
['C' => 4, 'D' => 5],
|
|
|
|
['A' => 1, 'B' => 2, 'C' => 3]
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
['A' => 1, 'B' => 2, 'C' => 4, 'D' => 5],
|
|
|
|
$result
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
$result = Priority::mergeArray(
|
|
|
|
[ 'C' => [4, 5, 6], 'D' => 5 ],
|
|
|
|
[ 'A' => 1, 'B' => 2, 'C' => [1, 2, 3] ]
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
['A' => 1, 'B' => 2, 'C' => [1, 2, 3, 4, 5, 6], 'D' => 5],
|
|
|
|
$result
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
$result = Priority::mergeArray(
|
|
|
|
['A' => 1, 'B' => 2, 'C' => [1, 2, 3]],
|
|
|
|
['C' => [4, 5, 6], 'D' => 5]
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
['A' => 1, 'B' => 2, 'C' => [4, 5, 6, 1, 2, 3], 'D' => 5],
|
|
|
|
$result
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
$result = Priority::mergeArray(
|
|
|
|
['A' => 1, 'B' => 2, 'C' => ['Foo' => 1, 'Bar' => 2], 'D' => 3],
|
|
|
|
['C' => ['Bar' => 3, 'Baz' => 4]]
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2017-02-22 04:15:08 +01:00
|
|
|
['A' => 1, 'B' => 2, 'C' => ['Foo' => 1, 'Bar' => 2, 'Baz' => 4], 'D' => 3],
|
|
|
|
$result
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
$result = Priority::mergeArray(
|
|
|
|
['C' => ['Bar' => 3, 'Baz' => 4]],
|
|
|
|
['A' => 1, 'B' => 2, 'C' => ['Foo' => 1, 'Bar' => 2], 'D' => 3]
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2017-02-22 04:15:08 +01:00
|
|
|
['A' => 1, 'B' => 2, 'C' => ['Foo' => 1, 'Bar' => 3, 'Baz' => 4], 'D' => 3],
|
|
|
|
$result
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testStaticLookup()
|
|
|
|
{
|
|
|
|
$this->assertEquals(Object::static_lookup(ConfigTest\DefinesFoo::class, 'foo'), 1);
|
|
|
|
$this->assertEquals(Object::static_lookup(ConfigTest\DefinesFoo::class, 'bar'), null);
|
|
|
|
|
|
|
|
$this->assertEquals(Object::static_lookup(ConfigTest\DefinesBar::class, 'foo'), null);
|
|
|
|
$this->assertEquals(Object::static_lookup(ConfigTest\DefinesBar::class, 'bar'), 2);
|
|
|
|
|
|
|
|
$this->assertEquals(Object::static_lookup(ConfigTest\DefinesFooAndBar::class, 'foo'), 3);
|
|
|
|
$this->assertEquals(Object::static_lookup(ConfigTest\DefinesFooAndBar::class, 'bar'), 3);
|
|
|
|
|
|
|
|
$this->assertEquals(Object::static_lookup(ConfigTest\DefinesFooDoesntExtendObject::class, 'foo'), 4);
|
|
|
|
$this->assertEquals(Object::static_lookup(ConfigTest\DefinesFooDoesntExtendObject::class, 'bar'), null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testForClass()
|
|
|
|
{
|
|
|
|
$config = ConfigTest\DefinesFoo::config();
|
|
|
|
// Set values
|
|
|
|
$this->assertTrue(isset($config->not_foo));
|
|
|
|
$this->assertFalse(empty($config->not_foo));
|
|
|
|
$this->assertEquals(1, $config->not_foo);
|
|
|
|
|
|
|
|
// Unset values
|
|
|
|
$this->assertFalse(isset($config->bar));
|
|
|
|
$this->assertTrue(empty($config->bar));
|
|
|
|
$this->assertNull($config->bar);
|
|
|
|
}
|
2011-12-22 03:17:41 +01:00
|
|
|
}
|