LegacyContainerBuilderTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. /**
  15. * @group legacy
  16. */
  17. class LegacyContainerBuilderTest extends TestCase
  18. {
  19. public function testCreateServiceFactoryMethod()
  20. {
  21. $builder = new ContainerBuilder();
  22. $builder->register('bar', 'stdClass');
  23. $builder->register('foo1', 'Bar\FooClass')->setFactoryClass('Bar\FooClass')->setFactoryMethod('getInstance')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')));
  24. $builder->setParameter('value', 'bar');
  25. $this->assertTrue($builder->get('foo1')->called, '->createService() calls the factory method to create the service instance');
  26. $this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar')), $builder->get('foo1')->arguments, '->createService() passes the arguments to the factory method');
  27. }
  28. public function testCreateServiceFactoryService()
  29. {
  30. $builder = new ContainerBuilder();
  31. $builder->register('baz_service')->setFactoryService('baz_factory')->setFactoryMethod('getInstance');
  32. $builder->register('baz_factory', 'BazClass');
  33. $this->assertInstanceOf('BazClass', $builder->get('baz_service'));
  34. }
  35. }