Skip to content

Commit

Permalink
Check same passed parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
sapsan4eg committed Apr 2, 2016
1 parent ed0ad37 commit e38a6e9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/Inject.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected static function getParameters(\ReflectionMethod $method, array $parame

try {
foreach ($method->getParameters() as $parameter) {
if (isset($parameters[$parameter->getName()]))
if (self::sameParameter($parameter, $parameters))
$arguments[$parameter->getName()] = $parameters[$parameter->getName()];
elseif (null != $parameter->getClass() && self::container()->isInjected($parameter->getClass()->name, $method->getDocComment()))
$arguments[$parameter->getName()] = self::instantiation(self::container()->getServiceName($parameter->getClass()->name, $method->getDocComment()));
Expand All @@ -105,6 +105,28 @@ protected static function getParameters(\ReflectionMethod $method, array $parame
return $arguments;
}

/**
* @param \ReflectionParameter $parameter
* @param null|array $parameters
* @return bool
*/
protected static function sameParameter(\ReflectionParameter $parameter, $parameters = null)
{
if (! isset($parameters[$parameter->getName()]))
return false;

if (null == $parameter->getClass() && is_object($parameters[$parameter->getName()]))
return false;

if (null != $parameter->getClass() && ! is_object($parameters[$parameter->getName()]))
return false;

if (null != $parameter->getClass() && ! $parameter->getClass()->isInstance($parameters[$parameter->getName()]))
return false;

return true;
}

/**
* @param object $class
* @param array|null $properties
Expand Down
10 changes: 10 additions & 0 deletions tests/InjectExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,14 @@ public function testExceptionClassMustImplements()
$class->bind("INext", "Start");
$class->isInjected("INext");
}

/**
* @expectedException \Sixx\DependencyInjection\Exceptions\InjectException
* @expectedExceptionMessage Required parameter [c] in SimpleParameter::__construct is not specified.
*/
public function testExceptionRequiredParameterDifferent()
{
Inject::bind("INext", "Next");
Inject::instantiation("SimpleParameter", ["c" => new Next()]);
}
}
7 changes: 7 additions & 0 deletions tests/InjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ public function testCheckAllInInjected()
$this->assertInstanceOf("Starter", Inject::method("ChildClass", "getStarter", ["c" => "fff"]));
$this->assertInstanceOf("Starter", $class->starter);
}

public function testCheckInjectParameter()
{
Inject::instantiation("SimpleParameter", ["c" => 1]);
Inject::instantiation("SimpleParameter", ["c" => 1, "d" => new Next()]);
Inject::instantiation("SimpleParameter", ["c" => 1, "d" => 2]);
}
}
10 changes: 10 additions & 0 deletions tests/TestClasses/Classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,13 @@ interface IStart
{

}

class SimpleParameter
{
public $d;

public function __construct($c, \INext $d)
{
$this->d = $d;
}
}

0 comments on commit e38a6e9

Please sign in to comment.