From 089ff0ba318e9e865ce4e6d0d5c1131fec156fe1 Mon Sep 17 00:00:00 2001 From: Bob McKee Date: Thu, 24 Sep 2009 16:09:03 -0400 Subject: [PATCH] fix for bug where magic bind method would not work if binder class had dependencies. --- framework/Injector/lib/Horde/Injector.php | 3 ++- .../Injector/test/Horde/Injector/InjectorTest.php | 28 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/framework/Injector/lib/Horde/Injector.php b/framework/Injector/lib/Horde/Injector.php index af0368dde..d31b8d00a 100644 --- a/framework/Injector/lib/Horde/Injector.php +++ b/framework/Injector/lib/Horde/Injector.php @@ -75,8 +75,9 @@ class Horde_Injector implements Horde_Injector_Scope if ($reflectionClass->getConstructor()) { $this->_addBinder($interface, $reflectionClass->newInstanceArgs($args)); + } else { + $this->_addBinder($interface, $reflectionClass->newInstance()); } - $this->_addBinder($interface, $reflectionClass->newInstance()); return $this->_getBinder($interface); } diff --git a/framework/Injector/test/Horde/Injector/InjectorTest.php b/framework/Injector/test/Horde/Injector/InjectorTest.php index 1bf61da7a..701a24cc7 100644 --- a/framework/Injector/test/Horde/Injector/InjectorTest.php +++ b/framework/Injector/test/Horde/Injector/InjectorTest.php @@ -30,6 +30,17 @@ class Horde_Injector_InjectorTest extends PHPUnit_Framework_TestCase $this->assertType('Horde_Injector_Binder_Mock', $injector->bindMock('BOUND_INTERFACE')); $this->assertType('Horde_Injector_Binder_Mock', $injector->getBinder('BOUND_INTERFACE')); } + + public function testShouldProvideMagicFactoryMethodForBinderAdditionWhereBinderHasDependencies() + { + $injector = new Horde_Injector(new Horde_Injector_TopLevel()); + + // binds a Horde_Injector_Binder_Mock object + $this->assertType('Horde_Injector_Binder_MockWithDependencies', + $injector->bindMockWithDependencies('BOUND_INTERFACE', 'PARAMETER1')); + $this->assertType('Horde_Injector_Binder_MockWithDependencies', + $injector->getBinder('BOUND_INTERFACE')); + } /** * @expectedException BadMethodCallException @@ -241,3 +252,20 @@ class Horde_Injector_Binder_Mock implements Horde_Injector_Binder return $otherBinder === $this; } } + +class Horde_Injector_Binder_MockWithDependencies implements Horde_Injector_Binder +{ + private $_interface; + + public function __construct($parameter1) {} + + public function create(Horde_Injector $injector) + { + return $injector; + } + + public function equals(Horde_Injector_Binder $otherBinder) + { + return $otherBinder === $this; + } +} -- 2.11.0