Skip to content

Commit

Permalink
Fixed some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Oct 9, 2013
1 parent 432f42b commit b404ac3
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 62 deletions.
4 changes: 1 addition & 3 deletions src/DI/Definition/ClassDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
class ClassDefinition implements Definition
{

/**
* Entry name (most of the time, same as $classname)
* @var string
Expand Down Expand Up @@ -202,7 +201,7 @@ public function merge(Definition $definition)
{
if (!$definition instanceof ClassDefinition) {
throw new DefinitionException("DI definition conflict: there are 2 different definitions for '"
. $definition->getName() . "' that are incompatible, they are not of the same type");
. $definition->getName() . "' that are incompatible, they are not of the same type");
}

// The latter prevails
Expand Down Expand Up @@ -265,5 +264,4 @@ public static function isMergeable()
{
return true;
}

}
17 changes: 11 additions & 6 deletions src/DI/Definition/Source/ArrayDefinitionSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,22 @@ public function getDefinition($name)
return null;
}

$value = $this->definitions[$name];
$definition = $this->definitions[$name];

if ($value instanceof Definition) {
return $value;
if ($definition instanceof DefinitionHelper) {
$definition = $definition->getDefinition($name);
}

if ($value instanceof DefinitionHelper) {
return $value->getDefinition($name);
if (! $definition instanceof Definition) {
$definition = new ValueDefinition($name, $definition);
}

return new ValueDefinition($name, $value);
// If it's a class, merge definitions from parent classes and interfaces
if ($definition instanceof ClassDefinition) {
$this->mergeWithParents($name, $definition);
}

return $definition;
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/DI/Definition/Source/CombinedDefinitionSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/
class CombinedDefinitionSource implements DefinitionSource
{

/**
* Sub-sources
* @var DefinitionSource[]
Expand Down Expand Up @@ -85,5 +84,4 @@ public function addSource($source)
{
$this->subSources[] = $source;
}

}
36 changes: 13 additions & 23 deletions src/DI/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,7 @@ public function injectOnInstance(ClassDefinition $classDefinition, $instance)
*/
private function injectConstructor(ReflectionClass $classReflection, MethodInjection $constructorInjection = null)
{
$constructorReflection = $classReflection->getConstructor();

// No constructor
if (!$constructorReflection) {
return $classReflection->newInstance();
}

$args = $this->prepareMethodParameters(
$constructorInjection,
$constructorReflection,
$classReflection->getName()
);
$args = $this->prepareMethodParameters($constructorInjection, $classReflection->getConstructor());

return $classReflection->newInstanceArgs($args);
}
Expand All @@ -125,7 +114,7 @@ private function injectMethodsAndProperties($instance, ClassDefinition $classDef
}

/**
* Inject dependencies through methods
* Inject dependencies through methods.
*
* @param object $object Object to inject dependencies into
* @param MethodInjection $methodInjection
Expand All @@ -135,25 +124,25 @@ private function injectMethodsAndProperties($instance, ClassDefinition $classDef
*/
private function injectMethod($object, MethodInjection $methodInjection)
{
$classReflection = new ReflectionClass($object);
$methodReflection = $classReflection->getMethod($methodInjection->getMethodName());
$methodReflection = new ReflectionMethod($object, $methodInjection->getMethodName());

$args = $this->prepareMethodParameters(
$methodInjection,
$methodReflection,
$classReflection->getName()
);
$args = $this->prepareMethodParameters($methodInjection, $methodReflection);

$methodReflection->invokeArgs($object, $args);
}

private function prepareMethodParameters(MethodInjection $methodInjection, ReflectionMethod $methodReflection, $className)
private function prepareMethodParameters(MethodInjection $methodInjection = null, ReflectionMethod $methodReflection = null)
{
if (!$methodReflection) {
return array();
}

// Check the number of parameters match
$nbRequiredParameters = $methodReflection->getNumberOfRequiredParameters();
$parameterInjections = $methodInjection->getParameters();
$parameterInjections = $methodInjection ? $methodInjection->getParameters() : array();
if (count($parameterInjections) < $nbRequiredParameters) {
throw new DefinitionException("The constructor of $className takes $nbRequiredParameters"
$className = $methodReflection->getDeclaringClass()->getName();
throw new DefinitionException("$className::{$methodReflection->name} takes $nbRequiredParameters"
. " parameters, " . count($parameterInjections) . " defined or guessed");
}

Expand All @@ -172,6 +161,7 @@ private function prepareMethodParameters(MethodInjection $methodInjection, Refle
$args[] = $this->getParameterDefaultValue($reflectionParameters[$index], $methodReflection);
continue;
}
$className = $methodReflection->getDeclaringClass()->getName();
throw new DefinitionException("The parameter '" . $reflectionParameters[$index]->getName()
. "' of $className::{$methodReflection->name} has no value defined or guessable");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
abstract class BaseClass
{

/**
* @Inject
* @var Dependency
Expand Down Expand Up @@ -49,5 +48,4 @@ public function setProperty2(Dependency $property2)
{
$this->property2 = $property2;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
*/
class SubClass extends BaseClass
{

/**
* @Inject
* @var Dependency
*/
public $property4;

}
44 changes: 20 additions & 24 deletions tests/IntegrationTests/DI/InheritanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,36 +72,32 @@ public static function containerProvider()
$builder->useReflection(false);
$builder->useAnnotations(false);
$containerFullArrayDefinitions = $builder->build();
$containerFullArrayDefinitions->addDefinitions(
array(
BaseClass::class => Entry::object(SubClass::class)
->withProperty('property1', Entry::link(Dependency::class))
->withProperty('property4', Entry::link(Dependency::class))
->withConstructor(Entry::link(Dependency::class))
->withMethod('setProperty2', Entry::link(Dependency::class)),
SubClass::class => Entry::object()
->withProperty('property1', Entry::link(Dependency::class))
->withProperty('property4', Entry::link(Dependency::class))
->withConstructor(Entry::link(Dependency::class))
->withMethod('setProperty2', Entry::link(Dependency::class)),
)
);
$containerFullArrayDefinitions->addDefinitions([
BaseClass::class => Entry::object(SubClass::class)
->withProperty('property1', Entry::link(Dependency::class))
->withProperty('property4', Entry::link(Dependency::class))
->withConstructor(Entry::link(Dependency::class))
->withMethod('setProperty2', Entry::link(Dependency::class)),
SubClass::class => Entry::object()
->withProperty('property1', Entry::link(Dependency::class))
->withProperty('property4', Entry::link(Dependency::class))
->withConstructor(Entry::link(Dependency::class))
->withMethod('setProperty2', Entry::link(Dependency::class)),
]);

// Test with a container using array configuration
$builder = new ContainerBuilder();
$builder->useReflection(false);
$builder->useAnnotations(false);
$containerInheritanceDefinitions = $builder->build();
$containerInheritanceDefinitions->addDefinitions(
array(
BaseClass::class => Entry::object(SubClass::class)
->withProperty('property1', Entry::link(Dependency::class))
->withConstructor(Entry::link(Dependency::class))
->withMethod('setProperty2', Entry::link(Dependency::class)),
SubClass::class => Entry::object()
->withProperty('property4', Entry::link(Dependency::class)),
)
);
$containerInheritanceDefinitions->addDefinitions([
BaseClass::class => Entry::object(SubClass::class)
->withProperty('property1', Entry::link(Dependency::class))
->withConstructor(Entry::link(Dependency::class))
->withMethod('setProperty2', Entry::link(Dependency::class)),
SubClass::class => Entry::object()
->withProperty('property4', Entry::link(Dependency::class)),
]);

return array(
array($containerAnnotations),
Expand Down

0 comments on commit b404ac3

Please sign in to comment.