4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests\Compiler;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\Compiler\ResolveFactoryClassPass;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17 use Symfony\Component\DependencyInjection\Definition;
18 use Symfony\Component\DependencyInjection\Reference;
20 class ResolveFactoryClassPassTest extends TestCase
22 public function testProcess()
24 $container = new ContainerBuilder();
26 $factory = $container->register('factory', 'Foo\Bar');
27 $factory->setFactory(array(null, 'create'));
29 $pass = new ResolveFactoryClassPass();
30 $pass->process($container);
32 $this->assertSame(array('Foo\Bar', 'create'), $factory->getFactory());
35 public function testInlinedDefinitionFactoryIsProcessed()
37 $container = new ContainerBuilder();
39 $factory = $container->register('factory');
40 $factory->setFactory(array((new Definition('Baz\Qux'))->setFactory(array(null, 'getInstance')), 'create'));
42 $pass = new ResolveFactoryClassPass();
43 $pass->process($container);
45 $this->assertSame(array('Baz\Qux', 'getInstance'), $factory->getFactory()[0]->getFactory());
48 public function provideFulfilledFactories()
51 array(array('Foo\Bar', 'create')),
52 array(array(new Reference('foo'), 'create')),
53 array(array(new Definition('Baz'), 'create')),
58 * @dataProvider provideFulfilledFactories
60 public function testIgnoresFulfilledFactories($factory)
62 $container = new ContainerBuilder();
63 $definition = new Definition();
64 $definition->setFactory($factory);
66 $container->setDefinition('factory', $definition);
68 $pass = new ResolveFactoryClassPass();
69 $pass->process($container);
71 $this->assertSame($factory, $container->getDefinition('factory')->getFactory());
75 * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
76 * @expectedExceptionMessage The "factory" service is defined to be created by a factory, but is missing the factory class. Did you forget to define the factory or service class?
78 public function testNotAnyClassThrowsException()
80 $container = new ContainerBuilder();
82 $factory = $container->register('factory');
83 $factory->setFactory(array(null, 'create'));
85 $pass = new ResolveFactoryClassPass();
86 $pass->process($container);