|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\DependencyInjection\Tests\Compiler; |
| 13 | + |
| 14 | +use Symfony\Component\DependencyInjection\Compiler\ResolveFactoryClassPass; |
| 15 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 16 | +use Symfony\Component\DependencyInjection\Definition; |
| 17 | +use Symfony\Component\DependencyInjection\Reference; |
| 18 | + |
| 19 | +class ResolveFactoryClassPassTest extends \PHPUnit_Framework_TestCase |
| 20 | +{ |
| 21 | + public function testProcess() |
| 22 | + { |
| 23 | + $container = new ContainerBuilder(); |
| 24 | + |
| 25 | + $factory = $container->register('factory', 'Foo\Bar'); |
| 26 | + $factory->setFactory(array(null, 'create')); |
| 27 | + |
| 28 | + $pass = new ResolveFactoryClassPass(); |
| 29 | + $pass->process($container); |
| 30 | + |
| 31 | + $this->assertSame(array('Foo\Bar', 'create'), $factory->getFactory()); |
| 32 | + } |
| 33 | + |
| 34 | + public function testInlinedDefinitionFactoryIsProcessed() |
| 35 | + { |
| 36 | + $container = new ContainerBuilder(); |
| 37 | + |
| 38 | + $factory = $container->register('factory'); |
| 39 | + $factory->setFactory(array((new Definition('Baz\Qux'))->setFactory(array(null, 'getInstance')), 'create')); |
| 40 | + |
| 41 | + $pass = new ResolveFactoryClassPass(); |
| 42 | + $pass->process($container); |
| 43 | + |
| 44 | + $this->assertSame(array('Baz\Qux', 'getInstance'), $factory->getFactory()[0]->getFactory()); |
| 45 | + } |
| 46 | + |
| 47 | + public function provideFulfilledFactories() |
| 48 | + { |
| 49 | + return array( |
| 50 | + array(array('Foo\Bar', 'create')), |
| 51 | + array(array(new Reference('foo'), 'create')), |
| 52 | + array(array(new Definition('Baz'), 'create')), |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @dataProvider provideFulfilledFactories |
| 58 | + */ |
| 59 | + public function testIgnoresFulfilledFactories($factory) |
| 60 | + { |
| 61 | + $container = new ContainerBuilder(); |
| 62 | + $definition = new Definition(); |
| 63 | + $definition->setFactory($factory); |
| 64 | + |
| 65 | + $container->setDefinition('factory', $definition); |
| 66 | + |
| 67 | + $pass = new ResolveFactoryClassPass(); |
| 68 | + $pass->process($container); |
| 69 | + |
| 70 | + $this->assertSame($factory, $container->getDefinition('factory')->getFactory()); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException |
| 75 | + * @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? |
| 76 | + */ |
| 77 | + public function testNotAnyClassThrowsException() |
| 78 | + { |
| 79 | + $container = new ContainerBuilder(); |
| 80 | + |
| 81 | + $factory = $container->register('factory'); |
| 82 | + $factory->setFactory(array(null, 'create')); |
| 83 | + |
| 84 | + $pass = new ResolveFactoryClassPass(); |
| 85 | + $pass->process($container); |
| 86 | + } |
| 87 | +} |
0 commit comments