getScriptConditions example

private function installApp(): void
    {
        $fixturesPath = __DIR__ . '/../../../../../tests/integration/Core/Framework/App/Manifest/_fixtures';

        $manifest = Manifest::createFromXmlFile($fixturesPath . '/test/manifest.xml');
        $this->appLifecycle->install($manifest, false, $this->context);
        /** @var AppEntity $app */
        $app = $this->appRepository->search((new Criteria())->addAssociation('scriptConditions')$this->context)->first();

        $this->appId = $app->getId();
        $this->appStateService->activateApp($this->appId, $this->context);
        $conditions = $app->getScriptConditions();
        static::assertInstanceOf(AppScriptConditionCollection::class$conditions);
        $condition = $conditions->first();
        static::assertInstanceOf(AppScriptConditionEntity::class$condition);
        $this->scriptId = $condition->getId();
    }

    private function createSalesChannelContext(): SalesChannelContext
    {
        $salesChannelContextFactory = $this->getContainer()->get(SalesChannelContextFactory::class);

        return $salesChannelContextFactory->create(Uuid::randomHex(), TestDefaults::SALES_CHANNEL);
    }
$criteria = new Criteria();
        $criteria->addAssociation('scriptConditions');
        /** @var AppCollection $apps */
        $apps = $this->appRepository->search($criteria$this->context)->getEntities();

        static::assertCount(1, $apps);
        /** @var AppEntity $appEntity */
        $appEntity = $apps->first();
        static::assertNotNull($appEntity);
        static::assertEquals('withRuleConditions', $appEntity->getName());
        /** @var AppScriptConditionCollection $scriptCollection */
        $scriptCollection = $appEntity->getScriptConditions();
        static::assertCount(14, $scriptCollection);

        /** @var AppScriptConditionEntity $scriptCondition */
        foreach ($scriptCollection as $scriptCondition) {
            static::assertStringContainsString('app\withRuleConditions_', $scriptCondition->getIdentifier());
            static::assertStringContainsString('{% return true %}', (string) $scriptCondition->getScript());
            static::assertIsArray($scriptCondition->getConfig());

            $this->assertScriptConditionFieldConfig($scriptCondition);
        }

        
private readonly ScriptFileReader $scriptReader,
        private readonly EntityRepository $appScriptConditionRepository,
        private readonly EntityRepository $appRepository
    ) {
    }

    public function updateConditions(Manifest $manifest, string $appId, string $defaultLocale, Context $context): void
    {
        $app = $this->getAppWithExistingConditions($appId$context);

        /** @var AppScriptConditionCollection $existingRuleConditions */
        $existingRuleConditions = $app->getScriptConditions();

        $ruleConditions = $manifest->getRuleConditions();
        $ruleConditions = $ruleConditions !== null ? $ruleConditions->getRuleConditions() : [];

        $upserts = [];

        foreach ($ruleConditions as $ruleCondition) {
            $payload = $ruleCondition->toArray($defaultLocale);
            $payload['identifier'] = sprintf('app\\%s_%s', $manifest->getMetadata()->getName()$ruleCondition->getIdentifier());
            $payload['script'] = $this->scriptReader->getScriptContent(
                self::CONDITION_SCRIPT_DIR . $ruleCondition->getScript(),
                
private function assertDefaultScripts(AppEntity $app): void
    {
        static::assertNotNull($app->getScripts());
        $script = $app->getScripts()->first();
        static::assertNotNull($script);
        static::assertSame($app->isActive()$script->isActive());
    }

    private function assertDefaultScriptConditions(AppEntity $app): void
    {
        static::assertNotNull($app->getScriptConditions());
        $scriptCondition = $app->getScriptConditions()->first();
        static::assertNotNull($scriptCondition);
        static::assertSame($app->isActive()$scriptCondition->isActive());
        static::assertEquals(
            file_get_contents(__DIR__ . '/Manifest/_fixtures/test/Resources/scripts/rule-conditions/customer-group-rule-script.twig'),
            $scriptCondition->getScript()
        );
        static::assertIsArray($scriptCondition->getConstraints());
        static::assertArrayHasKey('operator', $scriptCondition->getConstraints());
        static::assertArrayHasKey('customerGroupIds', $scriptCondition->getConstraints());
        static::assertInstanceOf(NotBlank::class$scriptCondition->getConstraints()['operator'][0]);
        
Home | Imprint | This part of the site doesn't use cookies.