ContainerBuilder example

use Shopware\Core\Framework\Framework;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/** * @internal */
class BusinessEventRegisterCompilerPassTest extends TestCase
{
    public function testEventsGetAdded(): void
    {
        $container = new ContainerBuilder();
        $container->register(BusinessEventRegistry::class)
            ->setPublic(true);

        $container->addCompilerPass(new BusinessEventRegisterCompilerPass([Framework::class]), PassConfig::TYPE_BEFORE_OPTIMIZATION, 0);

        $container->compile(false);
        static::assertSame([Framework::class]$container->get(BusinessEventRegistry::class)->getClasses());
    }
}
use Symfony\Component\DependencyInjection\Definition;

/** * @internal * * @covers \Shopware\Core\Framework\DependencyInjection\CompilerPass\RemoveOldFlowStorerCompilerPass */
class RemoveOldFlowStorerCompilerPassTest extends TestCase
{
    public function testProcess(): void
    {
        $builder = new ContainerBuilder();

        $definition = new Definition(\stdClass::class);
        $definition->addTag('flow.storer');
        $definition->addTag('not-affected');

        $builder->setDefinition('not-affected', $definition);

        $definition = new Definition(ShopNameStorer::class);
        $definition->addTag('flow.storer');
        $definition->addTag('not-affected');
        $builder->setDefinition(ShopNameStorer::class$definition);

        
 $rendered->getContent());
    }

    public function getContainer(): ContainerInterface
    {
        $container = new ContainerBuilder();
        $container->set('request_stack', new RequestStack());
        $container->set('event_dispatcher', new EventDispatcher());

        $placeholder = $this->createMock(SeoUrlPlaceholderHandlerInterface::class);
        $placeholder->method('replace')->willReturnArgument(0);

        $container->set(SeoUrlPlaceholderHandlerInterface::class$placeholder);

        return $container;
    }

    
use Symfony\Component\DependencyInjection\ContainerBuilder;

/** * @internal * * @covers \Shopware\Elasticsearch\DependencyInjection\ElasticsearchMigrationCompilerPass */
class ElasticsearchMigrationCompilerPassTest extends TestCase
{
    public function testCompilerPass(): void
    {
        $container = new ContainerBuilder();
        $container->register(MigrationSource::class D '.core.V6_5', MigrationSource::class)->setPublic(true);

        $container->addCompilerPass(new ElasticsearchMigrationCompilerPass());
        $container->compile();

        $calls = $container->getDefinition(MigrationSource::class D '.core.V6_5')->getMethodCalls();
        static::assertCount(1, $calls);

        static::assertSame('addDirectory', $calls[0][0]);
        static::assertStringContainsString('Migration/V6_5', $calls[0][1][0]);
        static::assertSame('Shopware\Elasticsearch\Migration\V6_5', $calls[0][1][1]);
    }
'policy' => 'system_config',
                    'reset' => '5 minutes',
                    'limits' => [
                        [
                            'domain' => 'test.limit',
                            'interval' => '10 seconds',
                        ],
                    ],
                ],
            ],
        ];
        $container = new ContainerBuilder(new ParameterBag($config));
        $container->register(RateLimiter::class);

        $rateLimiterCompilerPass = new RateLimiterCompilerPass();
        $rateLimiterCompilerPass->process($container);

        $this->rateLimiterDef = $container->getDefinition('shopware.rate_limiter');
    }

    public function testSystemServiceConfigReference(): void
    {
        static::assertEquals('registerLimiterFactory', $this->rateLimiterDef->getMethodCalls()[0][0]);

        
use Symfony\Component\DependencyInjection\Definition;

/** * @internal */
class IncrementerGatewayCompilerPassTest extends TestCase
{
    use IntegrationTestBehaviour;

    public function testProcess(): void
    {
        $container = new ContainerBuilder();
        $container->setParameter('shopware.increment', [
            'user_activity' => [
                'type' => 'mysql',
            ],
            'message_queue' => [
                'type' => 'redis',
                'config' => ['url' => 'redis://test'],
            ],
            'another_pool' => [
                'type' => 'array',
            ],
        ]);
$this->controller = new AuthControllerTestClass(
            $this->accountLoginPageLoader,
            $passwordRecoveryPageLoader,
            $resetPasswordRoute,
            $this->loginRoute,
            $logoutRoute,
            $cartFacade,
            $recoverPasswordRoute,
            $this->salesChannelContextService,
        );

        $containerBuilder = new ContainerBuilder();
        $containerBuilder->set('request_stack', new RequestStack());
        $this->controller->setContainer($containerBuilder);
    }

    public function testAccountRegister(): void
    {
        $context = Generator::createSalesChannelContext();
        $context->assign(['customer' => null]);
        $request = new Request();
        $request->attributes->set('_route', 'frontend.account.login.page');
        $dataBag = new RequestDataBag();
        
use Symfony\Component\DependencyInjection\ContainerBuilder;

/** * @internal * * @covers \Shopware\Core\Installer\Installer */
class InstallerTest extends TestCase
{
    public function testBuild(): void
    {
        $container = new ContainerBuilder();
        $container->registerExtension(new FrameworkExtension());

        $installer = new Installer();
        $installer->build($container);

        static::assertSame(
            [
                'de' => 'de-DE',
                'en' => 'en-GB',
                'us' => 'en-US',
                'cs' => 'cs-CZ',
                
/** * @internal */
trait InstallerControllerTestTrait
{
    /** * @param array<string, object> $services */
    private function getInstallerContainer(Environment $twig, array $services = []): ContainerInterface
    {
        $container = new ContainerBuilder();
        $container->set('twig', $twig);
        $requestStack = new RequestStack();
        $requestStack->push(new Request([][]['_route' => 'installer.language-selection']));
        $container->set('request_stack', $requestStack);
        $container->setParameter('shopware.installer.supportedLanguages', ['en' => 'en-GB', 'de' => 'de-DE']);
        $container->setParameter('kernel.shopware_version', Kernel::SHOPWARE_FALLBACK_VERSION);

        foreach ($services as $id => $service) {
            $container->set($id$service);
        }

        
use Symfony\Component\DependencyInjection\Reference;

/** * @internal * * @covers \Shopware\Core\Framework\DependencyInjection\CompilerPass\AutoconfigureCompilerPass */
class AutoconfigureCompilerPassTest extends TestCase
{
    public function testAutoConfigure(): void
    {
        $container = new ContainerBuilder();

        $container->addCompilerPass(new AutoconfigureCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 1000);
        $container->setDefinition('product', (new Definition(ProductDefinition::class))->setPublic(true)->setAutoconfigured(true)->setAutowired(true));

        $container->compile(true);

        static::assertTrue($container->hasDefinition('product'));
        static::assertTrue($container->getDefinition('product')->hasTag('shopware.entity.definition'));
    }

    public function testAliasing(): void
    {
$this->expectException(KernelPluginLoaderException::class);
        $this->expectExceptionMessage('Failed to load plugin "SwagTest". Reason: Plugin class "SwagTest\SwagTestFake" must extend "Shopware\Core\Framework\Plugin"');
        $loader->initializePlugins(TEST_PROJECT_DIR);
    }

    public function testBuildNoInitShouldNotChangeContainer(): void
    {
        $activePluginData = $this->getActivePlugin()->jsonSerialize();
        $loader = new StaticKernelPluginLoader($this->classLoader, null, [$activePluginData]);

        $emptyContainer = new ContainerBuilder();
        $container = new ContainerBuilder();

        static::assertEquals($emptyContainer$container);
        $loader->build($container);

        static::assertEquals($emptyContainer$container);
    }

    public function testBuildInactivePluginShouldNotChangeContainer(): void
    {
        $pluginData = $this->getInstalledInactivePlugin()->jsonSerialize();
        
use Shopware\Core\Framework\Test\DependencyInjection\fixtures\TestEvent;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/** * @internal */
class ActionEventCompilerPassTest extends TestCase
{
    public function testProcess(): void
    {
        $container = new ContainerBuilder();

        $container->register(CustomerDefinition::class, CustomerDefinition::class);
        $container->register(OrderDefinition::class, OrderDefinition::class);

        $container->register(DefinitionInstanceRegistry::class, DefinitionInstanceRegistry::class)
            ->addArgument(new Reference('service_container'))
            ->addArgument([])
            ->addArgument([]);

        $container->register(BusinessEventRegistry::class, BusinessEventRegistry::class)
            ->addArgument(new Reference(DefinitionInstanceRegistry::class));

        
use Twig\Loader\FilesystemLoader;

/** * @internal * * @covers \Shopware\Core\Framework\DependencyInjection\CompilerPass\TwigLoaderConfigCompilerPass */
class TwigLoaderConfigCompilerPassTest extends TestCase
{
    public function testDevModeNoPluginsOrApps(): void
    {
        $container = new ContainerBuilder();

        $container->register('twig.loader.native_filesystem', FilesystemLoader::class);
        $container->setParameter('kernel.bundles_metadata', []);
        $container->setParameter('kernel.environment', 'dev');
        $container->setParameter('kernel.project_dir', '/project');

        $connectionMock = $this->createMock(Connection::class);

        $container->set(Connection::class$connectionMock);

        $connectionMock->expects(static::once())->method('fetchAllAssociative')->willReturn([]);

        
$updateController = new UpdateController(
            $this->createMock(ApiClient::class),
            $this->createMock(WriteableCheck::class),
            $this->createMock(LicenseCheck::class),
            $this->createMock(ExtensionCompatibility::class),
            $eventDispatcher,
            $this->createMock(SystemConfigService::class),
            $this->createMock(AbstractExtensionLifecycle::class),
            '6.1.0'
        );

        $container = new ContainerBuilder();
        $service = $this->createMock(Kernel::class);
        $service->method('getContainer')->willReturn($container);
        $container->set('kernel', $service);
        $container->set('event_dispatcher', $eventDispatcher);

        $updateController->setContainer($container);

        $updateController->deactivatePlugins(new Request(), Context::createDefaultContext());

        static::assertCount(2, $events);
        static::assertInstanceOf(UpdatePrePrepareEvent::class$events[0]);
        

        }
    }

    /** * Gets a new ContainerBuilder instance used to build the service container. * * @return ContainerBuilder */
    protected function getContainerBuilder()
    {
        return new ContainerBuilder(
            new ParameterBag(
                $this->getKernelParameters()
            )
        );
    }

    /** * Returns the kernel parameters. * * @return array An array of kernel parameters */
    
Home | Imprint | This part of the site doesn't use cookies.