Skip to content

Commit c76df67

Browse files
authored
Merge pull request #144 from anlutro/fix-get-default-array
fix behavior of Settings::get when $key is an array and defaults are set
2 parents 74a743f + 9e46f01 commit c76df67

File tree

5 files changed

+62
-13
lines changed

5 files changed

+62
-13
lines changed

src/ArrayUtil.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ protected static function getArray(array $input, $keys, $default = null)
6161
$output = array();
6262

6363
foreach ($keys as $key) {
64-
static::set($output, $key, static::get($input, $key, $default));
64+
if ($default) {
65+
$keyDefault = static::get($default, $key);
66+
} else {
67+
$keyDefault = null;
68+
}
69+
static::set($output, $key, static::get($input, $key, $keyDefault));
6570
}
6671

6772
return $output;

src/SettingStore.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,10 @@ public function setCache($cache, $ttl = null, $forgetOnWrite = null)
114114
*/
115115
public function get($key, $default = null)
116116
{
117-
if ($default === NULL && !is_array($key)) {
117+
if ($default === NULL) {
118118
$default = ArrayUtil::get($this->defaults, $key);
119+
} elseif (is_array($key) && is_array($default)) {
120+
$default = array_merge(ArrayUtil::get($this->defaults, $key, []), $default);
119121
}
120122

121123
$this->load();

tests/functional/AbstractFunctionalTest.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,68 @@
77

88
abstract class AbstractFunctionalTest extends TestCase
99
{
10-
protected abstract function createStore(array $data = array());
10+
protected $defaults;
11+
12+
protected abstract function createStore(array $data = null);
13+
14+
protected function getStore(array $data = null)
15+
{
16+
$store = $this->createStore($data);
17+
if ($this->defaults) {
18+
$store->setDefaults($this->defaults);
19+
}
20+
return $store;
21+
}
1122

1223
public function tearDown(): void
1324
{
1425
m::close();
26+
$this->defaults = [];
1527
}
1628

1729
protected function assertStoreEquals($store, $expected, $message = '')
1830
{
1931
$this->assertEquals($expected, $store->all(), $message);
2032
$store->save();
21-
$store = $this->createStore();
33+
$store = $this->getStore();
2234
$this->assertEquals($expected, $store->all(), $message);
2335
}
2436

2537
protected function assertStoreKeyEquals($store, $key, $expected, $message = '')
2638
{
2739
$this->assertEquals($expected, $store->get($key), $message);
2840
$store->save();
29-
$store = $this->createStore();
41+
$store = $this->getStore();
3042
$this->assertEquals($expected, $store->get($key), $message);
3143
}
3244

3345
/** @test */
3446
public function store_is_initially_empty()
3547
{
36-
$store = $this->createStore();
48+
$store = $this->getStore();
3749
$this->assertEquals(array(), $store->all());
3850
}
3951

4052
/** @test */
4153
public function written_changes_are_saved()
4254
{
43-
$store = $this->createStore();
55+
$store = $this->getStore();
4456
$store->set('foo', 'bar');
4557
$this->assertStoreKeyEquals($store, 'foo', 'bar');
4658
}
4759

4860
/** @test */
4961
public function nested_keys_are_nested()
5062
{
51-
$store = $this->createStore();
63+
$store = $this->getStore();
5264
$store->set('foo.bar', 'baz');
5365
$this->assertStoreEquals($store, array('foo' => array('bar' => 'baz')));
5466
}
5567

5668
/** @test */
5769
public function cannot_set_nested_key_on_non_array_member()
5870
{
59-
$store = $this->createStore();
71+
$store = $this->getStore();
6072
$store->set('foo', 'bar');
6173
$this->expectException('UnexpectedValueException');
6274
$this->expectExceptionMessage('Non-array segment encountered');
@@ -66,7 +78,7 @@ public function cannot_set_nested_key_on_non_array_member()
6678
/** @test */
6779
public function can_forget_key()
6880
{
69-
$store = $this->createStore();
81+
$store = $this->getStore();
7082
$store->set('foo', 'bar');
7183
$store->set('bar', 'baz');
7284
$this->assertStoreEquals($store, array('foo' => 'bar', 'bar' => 'baz'));
@@ -78,7 +90,7 @@ public function can_forget_key()
7890
/** @test */
7991
public function can_forget_nested_key()
8092
{
81-
$store = $this->createStore();
93+
$store = $this->getStore();
8294
$store->set('foo.bar', 'baz');
8395
$store->set('foo.baz', 'bar');
8496
$store->set('bar.foo', 'baz');
@@ -119,9 +131,18 @@ public function can_forget_nested_key()
119131
/** @test */
120132
public function can_forget_all()
121133
{
122-
$store = $this->createStore(array('foo' => 'bar'));
134+
$store = $this->getStore(array('foo' => 'bar'));
123135
$this->assertStoreEquals($store, array('foo' => 'bar'));
124136
$store->forgetAll();
125137
$this->assertStoreEquals($store, array());
126138
}
139+
140+
/** @test */
141+
public function defaults_are_respected()
142+
{
143+
$this->defaults = ['foo' => 'default', 'bar' => 'default'];
144+
$store = $this->getStore(array('foo' => 'bar'));
145+
$this->assertStoreEquals($store, ['foo' => 'bar']);
146+
$this->assertStoreKeyEquals($store, ['foo', 'bar'], ['foo' => 'bar', 'bar' => 'default']);
147+
}
127148
}

tests/functional/DatabaseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function tearDown(): void
2929
parent::tearDown();
3030
}
3131

32-
protected function createStore(array $data = array())
32+
protected function createStore(array $data = null)
3333
{
3434
if ($data) {
3535
$store = $this->createStore();

tests/unit/ArrayUtilTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function getReturnsCorrectValue(array $data, $key, $expected)
1717
public function getGetData()
1818
{
1919
return array(
20+
// $data, $key, $expected
2021
array(array(), 'foo', null),
2122
array(array('foo' => 'bar'), 'foo', 'bar'),
2223
array(array('foo' => 'bar'), 'bar', null),
@@ -47,6 +48,26 @@ public function getGetData()
4748
);
4849
}
4950

51+
/**
52+
* @test
53+
* @dataProvider getGetWithDefaultsData
54+
*/
55+
public function getWithDefaultsReturnsCorrectValue(array $data, $key, $default, $expected)
56+
{
57+
$this->assertEquals($expected, ArrayUtil::get($data, $key, $default));
58+
}
59+
60+
public function getGetWithDefaultsData()
61+
{
62+
return [
63+
// $data, $key, $default, $expected
64+
[[], 'foo', 'default', 'default'],
65+
[['foo' => 'value'], 'foo', 'default', 'value'],
66+
[[], ['foo'], ['foo' => 'default'], ['foo' => 'default']],
67+
[['foo' => 'value'], ['foo'], ['foo' => 'default'], ['foo' => 'value']],
68+
];
69+
}
70+
5071
/**
5172
* @test
5273
* @dataProvider getSetData

0 commit comments

Comments
 (0)