CurrencyRule example

/** * @internal * * @covers \Shopware\Core\System\Currency\Rule\CurrencyRule */
#[Package('buyers-experience')] class CurrencyRuleTest extends TestCase
{
    public function testGetName(): void
    {
        $rule = new CurrencyRule();

        static::assertEquals('currency', $rule->getName());
    }

    public function testGetConstraints(): void
    {
        $rule = new CurrencyRule();

        static::assertEquals([
            'operator' => RuleConstraints::uuidOperators(false),
            'currencyIds' => RuleConstraints::uuids(),
        ],
new PriceDefinitionField('test', 'test'),
            $encoded['test']
        );

        static::assertEquals($definition$decoded);
    }

    public static function serializerProvider()
    {
        $rule = new AndRule([
            new OrRule([
                new CurrencyRule(CurrencyRule::OPERATOR_EQ, [Defaults::CURRENCY]),
            ]),
            new CurrencyRule(CurrencyRule::OPERATOR_EQ, [Defaults::CURRENCY]),
        ]);

        yield 'Test quantity price definition' => [
            new QuantityPriceDefinition(100, new TaxRuleCollection([new TaxRule(19, 50)new TaxRule(7, 50)]), 3),
        ];

        yield 'Test absolute price definition' => [
            new AbsolutePriceDefinition(20, $rule),
        ];

        

        $this->ruleRepository = $this->getContainer()->get('rule.repository');
        $this->conditionRepository = $this->getContainer()->get('rule_condition.repository');
        $this->context = Context::createDefaultContext();
    }

    public function testValidateWithMissingValue(): void
    {
        try {
            $this->conditionRepository->create([
                [
                    'type' => (new CurrencyRule())->getName(),
                    'ruleId' => Uuid::randomHex(),
                ],
            ]$this->context);
            static::fail('Exception was not thrown');
        } catch (WriteException $stackException) {
            $errors = iterator_to_array($stackException->getErrors());
            static::assertCount(2, $errors);

            static::assertEquals('/0/value/currencyIds', $errors[0]['source']['pointer']);
            static::assertEquals(NotBlank::IS_BLANK_ERROR, $errors[0]['code']);

            
'id' => $id,
            'name' => 'test rule',
            'priority' => 1,
            'conditions' => [
                [
                    'type' => (new AndRule())->getName(),
                    'children' => [
                        [
                            'type' => (new OrRule())->getName(),
                            'children' => [
                                [
                                    'type' => (new CurrencyRule())->getName(),
                                    'value' => [
                                        'currencyIds' => [
                                            $currencyId,
                                            $currencyId2,
                                        ],
                                        'operator' => CurrencyRule::OPERATOR_EQ,
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],


        static::assertInstanceOf(LineItem::class$lineItem);
        $scope = new LineItemScope($lineItem$context);
        foreach ($rules as $rule) {
            // test combination with currency rule to validate currency list prices+
            /** @var list<string> $currencyIds */
            $currencyIds = array_values($ids->getList(['currency']));

            $wrapper = new AndRule([
                new CurrencyRule(CurrencyRule::OPERATOR_EQ, $currencyIds),
                $rule,
            ]);

            static::assertTrue($wrapper->match($scope));
        }
    }

    private function createLineItemWithListPrice(?float $listPriceAmount): LineItem
    {
        $listPrice = $listPriceAmount === null ? null : ListPrice::createFromUnitPrice(400, $listPriceAmount);
        $listPriceAmount ??= 99.99;

        
'name' => 'Next two days',
            ],
            [
                'rule' => (new GoodsPriceRule())->assign(['amount' => 5000, 'operator' => GoodsPriceRule::OPERATOR_GTE]),
                'name' => 'Cart >= 5000',
            ],
            [
                'rule' => (new CustomerGroupRule())->assign(['customerGroupIds' => [TestDefaults::FALLBACK_CUSTOMER_GROUP]]),
                'name' => 'Default group',
            ],
            [
                'rule' => (new CurrencyRule())->assign(['currencyIds' => [Defaults::CURRENCY]]),
                'name' => 'Default currency',
            ],
        ];

        $payload = [];
        for ($i = 0; $i < 20; ++$i) {
            $rules = \array_slice($poolrandom_int(0, \count($pool) - 2)random_int(1, 2));

            $classes = array_column($rules, 'rule');
            $names = array_column($rules, 'name');

            
$currencyId2 = Uuid::randomHex();

        $data = [
            'id' => $id,
            'name' => 'test rule',
            'priority' => 1,
            'conditions' => [
                [
                    'type' => (new OrRule())->getName(),
                    'children' => [
                        [
                            'type' => (new CurrencyRule())->getName(),
                            'value' => [
                                'currencyIds' => [
                                    $currencyId1,
                                    $currencyId2,
                                ],
                                'operator' => CurrencyRule::OPERATOR_EQ,
                            ],
                        ],
                    ],
                ],
            ],
        ];
Home | Imprint | This part of the site doesn't use cookies.