Skip to content
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.

Commit

Permalink
property bindings should work together with type hints, e.g. for stub…
Browse files Browse the repository at this point in the history
…bles\lang\SecureString
  • Loading branch information
mikey179 committed Aug 10, 2014
1 parent c626012 commit b4e4398
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
4.1.3 (2014-08-10)
------------------

* fixed bug that property bindings did not work together with type hints, e.g. for `stubbles\lang\SecureString`


4.1.2 (2014-08-10)
------------------

Expand Down
4 changes: 4 additions & 0 deletions src/main/php/ioc/Injector.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ private function getParamType(ReflectionMethod $method, ReflectionParameter $par
{
$paramClass = $param->getClass();
if (null !== $paramClass) {
if ($method->hasAnnotation('Property') || $param->hasAnnotation('Property')) {
return BindingIndex::getPropertyType();
}

return $paramClass->getName();
}

Expand Down
69 changes: 58 additions & 11 deletions src/test/php/ioc/binding/PropertyBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,30 @@
* @package stubbles
*/
namespace stubbles\ioc\binding;
use stubbles\ioc\Binder;
use stubbles\lang;
use stubbles\lang\Properties;
use stubbles\lang\SecureString;
/**
* Class used for tests.
*
* @since 4.1.3
*/
class Example
{
public $password;
/**
* constructor
*
* @param \stubbles\lang\SecureString $password
* @Inject
* @Property('example.password')
*/
public function __construct(SecureString $password)
{
$this->password = $password;
}
}
/**
* Test for stubbles\ioc\binding\PropertyBinding.
*
Expand Down Expand Up @@ -47,18 +69,19 @@ public function setUp()
->disableOriginalConstructor()
->getMock();
$this->mockMode = $this->getMock('stubbles\lang\Mode');
$this->propertyBinding = new PropertyBinding(new Properties(['PROD' => ['foo.bar' => 'baz',
'baz' => __CLASS__ . '.class'
],
'config' => ['foo.bar' => 'default',
'other' => 'someValue',
'baz' => 'stubbles\lang\Properties.class'
]
]
),
$this->mockMode
$this->propertyBinding = new PropertyBinding(
new Properties(['PROD' => ['foo.bar' => 'baz',
'baz' => __CLASS__ . '.class'
],
'config' => ['foo.bar' => 'default',
'other' => 'someValue',
'baz' => 'stubbles\lang\Properties.class'
]
]
),
$this->mockMode

);
);
}

/**
Expand Down Expand Up @@ -186,4 +209,28 @@ public function returnsParsedValuesForCommonProperties()
$this->propertyBinding->getInstance($this->mockInjector, 'baz')
);
}

/**
* @test
* @since 4.1.3
*/
public function propertyBindingUsedWhenParamHasTypeHintButIsAnnotated()
{
$this->mockMode->expects($this->any())
->method('name')
->will($this->returnValue('PROD'));
$binder = new Binder();
$properties = new Properties(
['config' => ['example.password' => 'somePassword']]
);
$binder->bindProperties($properties, $this->mockMode);
$example = $binder->getInjector()->getInstance('stubbles\ioc\binding\Example');
$this->assertInstanceOf('stubbles\lang\SecureString', $example->password);
// ensure all references are removed to clean up environment
// otherwise all *SecureStringTests will fail
$properties = null;
$example->password = null;
$binder = null;
gc_collect_cycles();
}
}

0 comments on commit b4e4398

Please sign in to comment.