Skip to content

Commit

Permalink
#110 - instanceOf now inherits from base class
Browse files Browse the repository at this point in the history
  • Loading branch information
TRPB committed Jun 10, 2016
1 parent 5913c4c commit c8692fd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
7 changes: 4 additions & 3 deletions Dice.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ class Dice {
* @param string $name The name of the class to add the rule for
* @param array $rule The container can be fully configured using rules provided by associative arrays. See {@link https://r.je/dice.html#example3} for a description of the rules.
*/
public function addRule($name, array $rule) {
$this->rules[ltrim(strtolower($name), '\\')] = array_merge_recursive($this->getRule($name), $rule);
}
public function addRule($name, array $rule) {
if (isset($rule['instanceOf']) && (!array_key_exists('inherit', $rule) || $rule['inherit'] === true )) $rule = array_merge_recursive($this->getRule($rule['instanceOf']), $rule);
$this->rules[ltrim(strtolower($name), '\\')] = array_merge_recursive($this->getRule($name), $rule);
}

/**
* Returns the rule that will be applied to the class $name when calling create()
Expand Down
12 changes: 0 additions & 12 deletions tests/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,6 @@ public function testObjectGraphCreation() {
$this->assertInstanceOf('F', $a->b->c->e->f);
}


public function testAssignSharedNamed() {
$rule = [];
$rule['shared'] = true;
$rule['instanceOf'] = function() {
return Baz77::create();
};
$this->dice->addRule('$SharedBaz', $rule);
}



public function testSharedNamed() {
$rule = [];
$rule['shared'] = true;
Expand Down
37 changes: 37 additions & 0 deletions tests/NamedInstancesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function testMultipleSharedInstancesByNameMixed() {
$rule = [];
$rule['instanceOf'] = 'Y';
$rule['shared'] = true;
$rule['inherit'] = false;
$rule['constructParams'][] = 'SecondY';

$this->dice->addRule('[Y2]', $rule);
Expand Down Expand Up @@ -102,6 +103,42 @@ public function testMultipleSubstitutions() {
$this->assertEquals('second', $twodep->y2b->name);
}

public function testNamedInstanceCallWithInheritance() {
$rule1 = [];
$rule1['call'] = [
['callMe', [1, 3] ],
['callMe', [3, 4] ]
];

$this->dice->addRule('Y', $rule1);

$rule2 = [];
$rule2['instanceOf'] = 'Y';
$rule2['constructParams'] = ['Foo'];

$this->dice->addRule('$MyInstance', $rule2);

$this->assertEquals(array_merge_recursive($rule1, $rule2), $this->dice->getRule('$MyInstance'));

}

public function testNamedInstanceCallWithoutInheritance() {
$rule1 = [];
$rule1['call'] = [
['callMe', [1, 3] ],
['callMe', [3, 4] ]
];

$this->dice->addRule('Y', $rule1);

$rule2 = [];
$rule2['instanceOf'] = 'Y';
$rule2['inherit'] = false;
$rule2['constructParams'] = ['Foo'];

$this->dice->addRule('$MyInstance', $rule2);

$this->assertEquals($rule2, $this->dice->getRule('$MyInstance'));
}

}
3 changes: 0 additions & 3 deletions tests/TestData/NamedInstances.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,3 @@ public function __construct(Y2 $y2a, Y2 $y2b) {
$this->y2b = $y2b;
}
}



0 comments on commit c8692fd

Please sign in to comment.