From 428cbe4b0327fc0f0bce70b0fe2050c1f2cc8417 Mon Sep 17 00:00:00 2001 From: Marcus Nyeholt Date: Fri, 15 Feb 2013 10:24:47 +1100 Subject: [PATCH] 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. --- control/injector/Injector.php | 3 +++ tests/injector/InjectorTest.php | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/control/injector/Injector.php b/control/injector/Injector.php index cfe06843e..324f3be59 100644 --- a/control/injector/Injector.php +++ b/control/injector/Injector.php @@ -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); } } diff --git a/tests/injector/InjectorTest.php b/tests/injector/InjectorTest.php index 297ba3af2..904aaff88 100644 --- a/tests/injector/InjectorTest.php +++ b/tests/injector/InjectorTest.php @@ -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); } }