ResolveFactoryClassPassTest.php 2.8 KB

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