From 162c6bb1e039dbc7e754a9e6f35a23dfac35af4b Mon Sep 17 00:00:00 2001 From: Romans Malinovskis Date: Wed, 29 Nov 2017 23:43:08 +0000 Subject: [PATCH 1/2] Refactor array merging when injecting --- src/DIContainerTrait.php | 5 ++- tests/SeedTest.php | 75 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 70 insertions(+), 10 deletions(-) diff --git a/src/DIContainerTrait.php b/src/DIContainerTrait.php index f50b7d24..def69ad7 100644 --- a/src/DIContainerTrait.php +++ b/src/DIContainerTrait.php @@ -55,9 +55,8 @@ public function setDefaults($properties = [], $passively = false) if ($passively && $this->$key !== null) { continue; } - if (is_array($val)) { - $this->$key = array_merge(isset($this->$key) && is_array($this->$key) ? $this->$key : [], $val); - } elseif ($val !== null) { + + if ($val !== null) { $this->$key = $val; } } else { diff --git a/tests/SeedTest.php b/tests/SeedTest.php index de27d219..02f918dd 100644 --- a/tests/SeedTest.php +++ b/tests/SeedTest.php @@ -128,7 +128,7 @@ public function testMerge3() public function testMerge4() { // array values don't overwrite but rather merge - $o = new SeedDITestMock(); + $o = new ViewTestMock(); $o->foo = ['red']; $oo = $this->mergeSeeds(['foo'=>['green']], $o); @@ -147,9 +147,9 @@ public function testMerge4() public function testMerge5() { // works even if more arguments present - $o = new SeedDITestMock(); + $o = new ViewTestMock(); $o->foo = ['red']; - $oo = $this->mergeSeeds(['foo'=>['xx']], ['foo'=>['green']], $o, ['foo'=>5]); + $oo = $this->mergeSeeds(['foo'=>['xx']], ['foo'=>['green']], $o); $this->assertSame($o, $oo); $this->assertEquals($oo->foo, ['red', 'green', 'xx']); @@ -161,16 +161,37 @@ public function testMerge5() $this->assertSame($o, $oo); $this->assertEquals($oo->foo, 'xx'); + } + public function testMerge5b() + { // and even if multiple objects are found - $o = new SeedDITestMock(); + $o = new ViewTestMock(); $o->foo = ['red']; - $o2 = new SeedDITestMock(); + $o2 = new ViewTestMock(); $o2->foo = ['yellow']; $oo = $this->mergeSeeds(['foo'=>['xx']], $o, ['foo'=>['green']], $o2, ['foo'=>['cyan']]); $this->assertSame($o, $oo); - $this->assertEquals($oo->foo, ['red', 'xx']); + $this->assertEquals(['red', 'xx'], $oo->foo); + } + + public function testMerge6() + { + $oo = $this->mergeSeeds(['4'=>'four'], ['5'=>'five']); + $this->assertEquals($oo, ['4'=>'four', '5'=>'five']); + + $oo = $this->mergeSeeds(['4'=>['four']], ['5'=>['five']]); + $this->assertEquals($oo, ['4'=>['four'], '5'=>['five']]); + + $oo = $this->mergeSeeds(['x'=>['four']], ['x'=>['five']]); + $this->assertEquals($oo, ['x'=>['four']]); + + $oo = $this->mergeSeeds(['4'=>['four']], ['4'=>['five']]); + $this->assertEquals($oo, ['4'=>['four']]); + + $oo = $this->mergeSeeds(['4'=>['200']], ['4'=>['201']]); + $this->assertEquals($oo, ['4'=>['200']]); } /** @@ -302,7 +323,7 @@ public function testMerge() $s1 = $this->factory([new SeedDITestMock(), 'foo'=>['red']], ['foo'=>['big'], 'foo'=>'default']); $this->assertEquals(['red'], $s1->foo); - $o = new SeedDITestMock(); + $o = new ViewTestMock(); $o->foo = ['xx']; $s1 = $this->factory([$o, 'foo'=>['red']], ['foo'=>['big'], 'foo'=>'default']); $this->assertEquals(['xx', 'red'], $s1->foo); @@ -386,6 +407,24 @@ public function testNonDIInject() $this->assertTrue($s1 instanceof SeedDITestMock); $this->assertEquals(['hello'], $s1->args); } + + /** + * Test seed property merging + */ + public function testPropertyMerging() + { + $s1 = $this->factory( + ['atk4/core/tests/SeedDITestMock', 'foo'=>['Button', 'icon'=>'red']], + ['foo'=>['Label', 'red']]); + + $this->assertEquals(['Button', 'icon'=>'red'], $s1->foo); + + $s1->setDefaults(['foo'=>['Message', 'detail'=>'blah']]); + + $this->assertEquals(['Message', 'detail'=>'blah'], $s1->foo); + } + + } class SeedTestMock @@ -405,6 +444,28 @@ class SeedDITestMock extends SeedTestMock use DIContainerTrait; } +class ViewTestMock extends SeedTestMock +{ + use DIContainerTrait { + setDefaults as _setDefaults; + } + public $def = null; + + public function setDefaults($properties = [], $passively = false) + { + if ($properties['foo']) { + + if ($passively) { + $this->foo = array_merge($properties['foo'], $this->foo); + } else { + $this->foo = array_merge($this->foo, $properties['foo']); + } + unset($properties['foo']); + } + return $this->_setDefaults($properties, $passively); + } +} + class SeedDefTestMock extends SeedTestMock { use DIContainerTrait { From d50b6ee86f1aa30aa6c296917081752e7207929f Mon Sep 17 00:00:00 2001 From: Romans Malinovskis Date: Wed, 29 Nov 2017 23:43:33 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- tests/SeedTest.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/SeedTest.php b/tests/SeedTest.php index 02f918dd..bdb28779 100644 --- a/tests/SeedTest.php +++ b/tests/SeedTest.php @@ -409,13 +409,13 @@ public function testNonDIInject() } /** - * Test seed property merging + * Test seed property merging. */ public function testPropertyMerging() { $s1 = $this->factory( - ['atk4/core/tests/SeedDITestMock', 'foo'=>['Button', 'icon'=>'red']], - ['foo'=>['Label', 'red']]); + ['atk4/core/tests/SeedDITestMock', 'foo'=>['Button', 'icon'=>'red']], + ['foo'=> ['Label', 'red']]); $this->assertEquals(['Button', 'icon'=>'red'], $s1->foo); @@ -423,8 +423,6 @@ public function testPropertyMerging() $this->assertEquals(['Message', 'detail'=>'blah'], $s1->foo); } - - } class SeedTestMock @@ -454,7 +452,6 @@ class ViewTestMock extends SeedTestMock public function setDefaults($properties = [], $passively = false) { if ($properties['foo']) { - if ($passively) { $this->foo = array_merge($properties['foo'], $this->foo); } else { @@ -462,6 +459,7 @@ public function setDefaults($properties = [], $passively = false) } unset($properties['foo']); } + return $this->_setDefaults($properties, $passively); } }