Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Amrouche Hamza committed Apr 6, 2019
1 parent 9b8397b commit 66f6e8e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Expand Up @@ -158,7 +158,15 @@ private function autowireCalls(\ReflectionClass $reflectionClass, bool $isRoot):
if ($method instanceof \ReflectionFunctionAbstract) {
$reflectionMethod = $method;
} else {
$reflectionMethod = $this->getReflectionMethod(new Definition($reflectionClass->name), $method);
$definition = new Definition($reflectionClass->name);
try {
$reflectionMethod = $this->getReflectionMethod($definition, $method);
} catch (RuntimeException $e) {
if ($definition->getFactory()) {
continue;
}
throw $e;
}
}

$arguments = $this->autowireMethod($reflectionMethod, $arguments);
Expand Down
Expand Up @@ -26,6 +26,7 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Tests\Fixtures\includes\FooVariadic;
use Symfony\Component\DependencyInjection\TypedReference;
use Symfony\Component\HttpKernel\HttpKernelInterface;

require_once __DIR__.'/../Fixtures/includes/autowiring_classes.php';

Expand Down Expand Up @@ -545,6 +546,18 @@ public function testSetterInjection()
);
}

public function testWithNonExistingSetterAndAutowiring()
{
$container = new ContainerBuilder();

$definition = $container->register(HttpKernelInterface::class, HttpKernelInterface::class)->setAutowired(true);
$definition->addMethodCall('setLogger');
$this->expectException(RuntimeException::class);
(new ResolveClassPass())->process($container);
(new AutowireRequiredMethodsPass())->process($container);
(new AutowirePass())->process($container);
}

public function testExplicitMethodInjection()
{
$container = new ContainerBuilder();
Expand Down
Expand Up @@ -16,11 +16,14 @@
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
use Symfony\Component\DependencyInjection\Tests\Fixtures\ParentNotExists;
use Symfony\Component\DependencyInjection\TypedReference;
use Symfony\Component\HttpKernel\HttpKernelInterface;

require_once __DIR__.'/../Fixtures/includes/autowiring_classes.php';

Expand Down Expand Up @@ -112,6 +115,23 @@ public function testScalarSetter()
$this->assertEquals([['setDefaultLocale', ['fr']]], $definition->getMethodCalls());
}

public function testWithNonExistingSetterAndBinding()
{
$container = new ContainerBuilder();

$bindings = [
'$c' => (new Definition('logger'))->setFactory('logger'),
];

$definition = $container->register(HttpKernelInterface::class, HttpKernelInterface::class);
$definition->addMethodCall('setLogger');
$definition->setBindings($bindings);
$this->expectException(RuntimeException::class);

$pass = new ResolveBindingsPass();
$pass->process($container);
}

public function testTupleBinding()
{
$container = new ContainerBuilder();
Expand Down

0 comments on commit 66f6e8e

Please sign in to comment.