RealServiceInstantiator example

use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator;

/** * Tests for {@see \Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator}. * * @author Marco Pivetta <ocramius@gmail.com> */
class RealServiceInstantiatorTest extends TestCase
{
    public function testInstantiateProxy()
    {
        $instantiator = new RealServiceInstantiator();
        $instance = new \stdClass();
        $container = $this->createMock(ContainerInterface::class);
        $callback = fn () => $instance;

        $this->assertSame($instance$instantiator->instantiateProxy($containernew Definition(), 'foo', $callback));
    }
}
$builder = new ContainerBuilder();
        $builder->register('foo1', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
        $builder->register('foo2', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/%file%.php');
        $builder->setParameter('file', 'foo');
        $this->assertInstanceOf(\Bar\FooClass::class$builder->get('foo1'), '->createService() requires the file defined by the service definition');
        $this->assertInstanceOf(\Bar\FooClass::class$builder->get('foo2'), '->createService() replaces parameters in the file provided by the service definition');
    }

    public function testCreateProxyWithRealServiceInstantiator()
    {
        $builder = new ContainerBuilder();
        $builder->setProxyInstantiator(new RealServiceInstantiator());

        $builder->register('foo1', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
        $builder->getDefinition('foo1')->setLazy(true);

        $foo1 = $builder->get('foo1');

        $this->assertSame($foo1$builder->get('foo1'), 'The same proxy is retrieved on multiple subsequent calls');
        $this->assertSame('Bar\FooClass', $foo1::class);
    }

    public function testCreateLazyProxy()
    {
Home | Imprint | This part of the site doesn't use cookies.