ServiceLocator example

'bar' => $bar,
        ]));

        $ret = $tester->execute(['tags' => $tagsToInvalidate]);

        $this->assertSame(Command::FAILURE, $ret);
    }

    private function createCommand(array $services): CachePoolInvalidateTagsCommand
    {
        return new CachePoolInvalidateTagsCommand(
            new ServiceLocator(array_map(fn ($service) => fn () => $service$services))
        );
    }
}
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\DependencyInjection\ServiceLocator;

class WorkflowDumpCommandTest extends TestCase
{
    /** * @dataProvider provideCompletionSuggestions */
    public function testComplete(array $input, array $expectedSuggestions)
    {
        $application = new Application();
        $application->add(new WorkflowDumpCommand(new ServiceLocator([])));

        $tester = new CommandCompletionTester($application->find('workflow:dump'));
        $suggestions = $tester->complete($input, 2);
        $this->assertSame($expectedSuggestions$suggestions);
    }

    public static function provideCompletionSuggestions(): iterable
    {
        yield 'option --dump-format' => [['--dump-format', '']['puml', 'mermaid', 'dot']];
    }
}
use Symfony\Component\HttpKernel\DependencyInjection\RegisterControllerArgumentLocatorsPass;

class ServiceValueResolverTest extends TestCase
{
    /** * In Symfony 7, keep this test case but remove the call to supports(). * * @group legacy */
    public function testDoNotSupportWhenControllerDoNotExists()
    {
        $resolver = new ServiceValueResolver(new ServiceLocator([]));
        $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
        $request = $this->requestWithAttributes(['_controller' => 'my_controller']);

        $this->assertSame([]$resolver->resolve($request$argument));
        $this->assertFalse($resolver->supports($request$argument));
    }

    public function testExistingController()
    {
        $resolver = new ServiceValueResolver(new ServiceLocator([
            'App\\Controller\\Mine::method' => fn () => new ServiceLocator([
                
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
use Symfony\Component\Messenger\Transport\Sender\SendersLocator;
use Symfony\Component\Messenger\Worker;

class FailureIntegrationTest extends TestCase
{
    public function testRequeueMechanism()
    {
        $transport1 = new DummyFailureTestSenderAndReceiver();
        $transport2 = new DummyFailureTestSenderAndReceiver();
        $failureTransport = new DummyFailureTestSenderAndReceiver();
        $sendersLocatorFailureTransport = new ServiceLocator([
            'transport1' => fn () => $failureTransport,
            'transport2' => fn () => $failureTransport,
        ]);

        $transports = [
            'transport1' => $transport1,
            'transport2' => $transport2,
            'the_failure_transport' => $failureTransport,
        ];

        $locator = $this->createMock(ContainerInterface::class);
        
->method('sanitize')
            ->with('foobar')
            ->willReturn('foo');

        $barSanitizer = $this->createMock(HtmlSanitizerInterface::class);
        $barSanitizer->expects($this->once())
            ->method('sanitize')
            ->with('foobar')
            ->willReturn('bar');

        return array_merge(parent::getExtensions()[
            new HtmlSanitizerExtension(new ServiceLocator([
                'foo' => fn () => $fooSanitizer,
                'bar' => fn () => $barSanitizer,
            ]), 'foo'),
        ]);
    }

    public function testSanitizer()
    {
        $form = $this->factory->createBuilder(FormType::class['data' => null])
            ->add('data', TextType::class['sanitize_html' => true])
            ->getForm()
        ;
++$count;
                }

                return $count;
            });
        } elseif ($value instanceof ServiceLocatorArgument) {
            $refs = $types = [];
            foreach ($value->getValues() as $k => $v) {
                $refs[$k] = [$v, null];
                $types[$k] = $v instanceof TypedReference ? $v->getType() : '?';
            }
            $value = new ServiceLocator($this->resolveServices(...)$refs$types);
        } elseif ($value instanceof Reference) {
            $value = $this->doGet((string) $value$value->getInvalidBehavior()$inlineServices$isConstructorArgument);
        } elseif ($value instanceof Definition) {
            $value = $this->createService($value$inlineServices$isConstructorArgument);
        } elseif ($value instanceof Parameter) {
            $value = $this->getParameter((string) $value);
        } elseif ($value instanceof Expression) {
            $value = $this->getExpressionLanguage()->evaluate($value['container' => $this]);
        } elseif ($value instanceof AbstractArgument) {
            throw new RuntimeException($value->getTextWithContext());
        }

        

        return $container->services['Symfony\\Component\\DependencyInjection\\Tests\\Dumper\\Rot13EnvVarProcessor'] = new \Symfony\Component\DependencyInjection\Tests\Dumper\Rot13EnvVarProcessor();
    }

    /** * Gets the public 'container.env_var_processors_locator' shared service. * * @return \Symfony\Component\DependencyInjection\ServiceLocator */
    protected static function getContainer_EnvVarProcessorsLocatorService($container)
    {
        return $container->services['container.env_var_processors_locator'] = new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($container->getService ??= $container->getService(...)[
            'rot13' => ['services', 'Symfony\\Component\\DependencyInjection\\Tests\\Dumper\\Rot13EnvVarProcessor', 'getRot13EnvVarProcessorService', false],
        ][
            'rot13' => '?',
        ]);
    }

    public function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null
    {
        if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name$this->parameters))) {
            throw new ParameterNotFoundException($name);
        }
        

        ;
        $security = new Security($container);
        $response = $security->logout();

        $this->assertInstanceOf(Response::class$response);
        $this->assertEquals('a custom response', $response->getContent());
    }

    private function createContainer(string $serviceId, object $serviceObject): ContainerInterface
    {
        return new ServiceLocator([$serviceId => fn () => $serviceObject]);
    }
}
usleep(30000);
                $item->expiresAfter(3600);

                return 123;
            }
        };

        $container = new Container();
        $container->set('computation_service', $computationService);
        $container->set('cache_pool', $pool);

        $reverseContainer = new ReverseContainer($containernew ServiceLocator([]));

        $msg = EarlyExpirationMessage::create($reverseContainer$computationService$item$pool);

        $handler = new EarlyExpirationHandler($reverseContainer);

        $handler($msg);

        $this->assertSame(123, $pool->get('foo', $this->fail(...), 0.0, $metadata));

        $this->assertGreaterThan(25, $metadata['ctime']);
        $this->assertGreaterThan(time()$metadata['expiry']);
    }
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\NotTaggedControllerValueResolver;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;

class NotTaggedControllerValueResolverTest extends TestCase
{
    /** * @group legacy */
    public function testDoSupportWhenControllerDoNotExists()
    {
        $resolver = new NotTaggedControllerValueResolver(new ServiceLocator([]));
        $argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null);
        $request = $this->requestWithAttributes(['_controller' => 'my_controller']);

        $this->assertTrue($resolver->supports($request$argument));
    }

    /** * In Symfony 7, keep this test case but remove the call to supports(). * * @group legacy */
    
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\DependencyInjection\ServiceLocator;

class ContainerCommandLoaderTest extends TestCase
{
    public function testHas()
    {
        $loader = new ContainerCommandLoader(new ServiceLocator([
            'foo-service' => fn () => new Command('foo'),
            'bar-service' => fn () => new Command('bar'),
        ])['foo' => 'foo-service', 'bar' => 'bar-service']);

        $this->assertTrue($loader->has('foo'));
        $this->assertTrue($loader->has('bar'));
        $this->assertFalse($loader->has('baz'));
    }

    public function testGet()
    {
        


    /** * Gets the public 'bar' shared service. * * @return \stdClass */
    protected static function getBarService($container)
    {
        $container->services['bar'] = $instance = new \stdClass();

        $instance->locator = new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($container->getService ??= $container->getService(...)[
            'foo1' => ['services', 'foo1', 'getFoo1Service', false],
            'foo2' => ['privates', 'foo2', 'getFoo2Service', false],
            'foo3' => [false, 'foo3', 'getFoo3Service', false],
            'foo4' => ['privates', 'foo4', NULL, 'BOOM'],
            'foo5' => ['services', 'foo5', NULL, false],
        ][
            'foo1' => '?',
            'foo2' => '?',
            'foo3' => '?',
            'foo4' => '?',
            'foo5' => '?',
        ]);

        return $container->services['Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber();
    }

    /** * Gets the public 'foo_service' shared autowired service. * * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber */
    protected static function getFooServiceService($container)
    {
        return $container->services['foo_service'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber((new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($container->getService ??= $container->getService(...)[
            'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => ['privates', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition', 'getCustomDefinitionService', false],
            'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => ['services', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber', 'getTestServiceSubscriberService', false],
            'bar' => ['services', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber', 'getTestServiceSubscriberService', false],
            'baz' => ['privates', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition', 'getCustomDefinitionService', false],
            'late_alias' => ['services', 'late_alias', 'getLateAliasService', false],
        ][
            'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition',
            'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber',
            'bar' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition',
            'baz' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition',
            'late_alias' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestDefinition1',
        ]))
public static function provideCompletionSuggestions()
    {
        yield 'event' => [[''][MessageEvent::class, 'console.command']];
        yield 'event for other dispatcher' => [['--dispatcher', 'other_event_dispatcher', '']['other_event', 'App\OtherEvent']];
        yield 'dispatcher' => [['--dispatcher=']['event_dispatcher', 'other_event_dispatcher']];
        yield 'format' => [['--format=']['txt', 'xml', 'json', 'md']];
    }

    private function createCommandCompletionTester(): CommandCompletionTester
    {
        $dispatchers = new ServiceLocator([
            'event_dispatcher' => function D) {
                $dispatcher = new EventDispatcher();
                $dispatcher->addListener(MessageEvent::class, 'var_dump');
                $dispatcher->addListener('console.command', 'var_dump');

                return $dispatcher;
            },
            'other_event_dispatcher' => function D) {
                $dispatcher = new EventDispatcher();
                $dispatcher->addListener('other_event', 'var_dump');
                $dispatcher->addListener('App\OtherEvent', 'var_dump');

                

        return $container->services['bar'] = new \stdClass((new \stdClass())(new \stdClass()));
    }

    /** * Gets the public 'baz' shared service. * * @return \stdClass */
    protected static function getBazService($container)
    {
        return $container->services['baz'] = new \stdClass(new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($container->getService ??= $container->getService(...)[
            'foo' => [false, 'foo', 'getFooService', false],
        ][
            'foo' => '?',
        ]));
    }

    /** * Gets the private 'foo' service. * * @return \stdClass */
    
Home | Imprint | This part of the site doesn't use cookies.