FIX issue with Injector::create not passing args

If creating an object using Injector::create() and constructor arguments
are passed through, in some cases where the object being created had a yml
configuration set for it, the passed in constructor arguments weren't being
passed through to the instantiation of the object.
This commit is contained in:
Marcus Nyeholt 2013-02-15 10:24:47 +11:00
parent 3dab996c90
commit 428cbe4b03
2 changed files with 16 additions and 0 deletions

View File

@ -773,6 +773,9 @@ class Injector {
if (isset($this->specs[$name])) {
$spec = $this->specs[$name];
$this->updateSpecConstructor($spec);
if ($constructorArgs) {
$spec['constructor'] = $constructorArgs;
}
return $this->instantiate($spec, $name);
}
}

View File

@ -523,6 +523,15 @@ class InjectorTest extends SapphireTest {
$this->assertInstanceOf('OtherTestObject', $item->property->property);
}
public function testCreateConfiggedObjectWithCustomConstructorArgs() {
// need to make sure that even if the config defines some constructor params,
// that we take our passed in constructor args instead
$injector = new Injector(array('locator' => 'InjectorTestConfigLocator'));
$item = $injector->create('ConfigConstructor', 'othervalue');
$this->assertEquals($item->property, 'othervalue');
}
}
class InjectorTestConfigLocator extends SilverStripeServiceConfigurationLocator implements TestOnly {
@ -531,6 +540,10 @@ class InjectorTestConfigLocator extends SilverStripeServiceConfigurationLocator
return array('class' => 'ConstructableObject', 'constructor' => array('%$OtherTestObject'));
}
if ($name == 'ConfigConstructor') {
return array('class' => 'ConstructableObject', 'constructor' => array('value'));
}
return parent::locateConfigFor($name);
}
}