Skip to content

Commit

Permalink
Merge pull request #67 from atk4/feature/no-special-treatment-for-inj…
Browse files Browse the repository at this point in the history
…ected-arrays

Feature/no special treatment for injected arrays
  • Loading branch information
romaninsh authored Nov 30, 2017
2 parents 8315bf1 + d50b6ee commit e900b66
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/DIContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
73 changes: 66 additions & 7 deletions tests/SeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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']);
Expand All @@ -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']]);
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -386,6 +407,22 @@ 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
Expand All @@ -405,6 +442,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 {
Expand Down

0 comments on commit e900b66

Please sign in to comment.