LineItemScope example

$this->lineItem = (new LineItem('A', 'product', 'A', 2))
            ->setPrice(new CalculatedPrice(100, 200, new CalculatedTaxCollection()new TaxRuleCollection(), 2));
    }

    public function testRuleWithExactAmountMatch(): void
    {
        $rule = (new LineItemWithQuantityRule())->assign(['id' => 'A', 'quantity' => 2, 'operator' => Rule::OPERATOR_EQ]);

        $context = $this->createMock(SalesChannelContext::class);

        static::assertTrue(
            $rule->match(new LineItemScope($this->lineItem, $context))
        );

        $cart = new Cart('test');
        $cart->add($this->lineItem);
        static::assertTrue(
            $rule->match(new CartRuleScope($cart$context))
        );
    }

    public function testRuleWithExactAmountNotMatch(): void
    {
        
new CalculatedPrice(100, 200, new CalculatedTaxCollection()new TaxRuleCollection())
            );
    }

    public function testRuleWithExactAmountMatch(): void
    {
        $rule = (new LineItemUnitPriceRule())->assign(['amount' => 100, 'operator' => Rule::OPERATOR_EQ]);

        $context = $this->createMock(SalesChannelContext::class);

        static::assertTrue(
            $rule->match(new LineItemScope($this->lineItem, $context))
        );

        $cart = new Cart('test');
        $cart->add($this->lineItem);
        static::assertTrue(
            $rule->match(new CartRuleScope($cart$context))
        );
    }

    public function testRuleWithExactAmountNotMatch(): void
    {
        

        if (!$scope instanceof CartRuleScope) {
            return false;
        }

        $goods = new LineItemCollection($scope->getCart()->getLineItems()->filterGoodsFlat());
        $filter = $this->filter;
        if ($filter !== null) {
            $context = $scope->getSalesChannelContext();

            $goods = $goods->filter(static function DLineItem $lineItem) use ($filter$context) {
                $scope = new LineItemScope($lineItem$context);

                return $filter->match($scope);
            });
        }

        return RuleComparison::numeric($goods->getPrices()->sum()->getTotalPrice()$this->amount, $this->operator);
    }

    public function getConstraints(): array
    {
        return [
            
static::assertEquals(new NotNull()$isActiveConstraint[0]);
        static::assertEquals(new Type('bool')$isActiveConstraint[1]);
    }

    public function testReturnsFalseWhenProvidingIncorrectScope(): void
    {
        $salesChannelContext = $this->createMock(SalesChannelContext::class);
        $lineItem = new LineItem('random-id', 'line-item');

        $isActiveCustomerRule = new IsActiveRule(true);

        $scope = new LineItemScope($lineItem$salesChannelContext);

        static::assertFalse($isActiveCustomerRule->match($scope));
    }

    /** * @return \Traversable<list<mixed>> */
    public static function getCustomerScopeTestData(): \Traversable
    {
        yield 'match / operator yes / active customer' => [true, true, true, false];
        yield 'match / operator no / deactivated customer' => [false, false, true, false];
        
public function testMatchesWithLineItemScope(
        array $states,
        string $operator,
        string $productState,
        bool $expected
    ): void {
        $this->rule->assign([
            'operator' => $operator,
            'productState' => $productState,
        ]);

        $match = $this->rule->match(new LineItemScope(
            $this->createLineItemWithStates($states),
            $this->createMock(SalesChannelContext::class)
        ));

        static::assertSame($expected$match);
    }

    /** * @dataProvider caseDataProvider * * @param array<int, string> $states */
$notEqualsRule = new PromotionLineItemRule(Rule::OPERATOR_NEQ, ['id']);
        static::assertFalse($notEqualsRule->match(new CheckoutRuleScope($this->createMock(SalesChannelContext::class))));
    }

    /** * @dataProvider lineItemScopeCases * * @param list<string>|null $ids */
    public function testMatchesInLineItemScope(?array $ids, LineItem $lineItem, bool $expected): void
    {
        $scope = new LineItemScope($lineItem$this->createMock(SalesChannelContext::class));

        $equalsRule = new PromotionLineItemRule(Rule::OPERATOR_EQ, $ids);
        static::assertSame($expected$equalsRule->match($scope));

        $notEqualsRule = new PromotionLineItemRule(Rule::OPERATOR_NEQ, $ids);
        static::assertSame(!$expected$notEqualsRule->match($scope));
    }

    public static function lineItemScopeCases(): \Generator
    {
        yield 'Line item id in configured ids' => [
            [
'operator' => Rule::OPERATOR_EQ,
                ],
            ],
        ]$this->context);

        static::assertNotNull($this->conditionRepository->search(new Criteria([$id])$this->context)->get($id));
    }

    public function testLineItemNoMatchWithoutTags(): void
    {
        $match = $this->createLineItemTagRule([Uuid::randomHex()])->match(
            new LineItemScope($this->createLineItem()$this->createMock(SalesChannelContext::class))
        );

        static::assertFalse($match);
    }

    public function testLineItemMatchUnequalsTags(): void
    {
        $match = $this->createLineItemTagRule([Uuid::randomHex()], Rule::OPERATOR_NEQ)->match(
            new LineItemScope($this->createLineItem()$this->createMock(SalesChannelContext::class))
        );

        
 void {
        $this->rule->assign([
            'amount' => $height,
            'operator' => $operator,
        ]);

        $lineItem = $this->createLineItemWithHeight($lineItemHeight);
        if ($lineItemWithoutDeliveryInfo) {
            $lineItem = $this->createLineItem();
        }

        $match = $this->rule->match(new LineItemScope(
            $lineItem,
            $this->createMock(SalesChannelContext::class)
        ));

        static::assertSame($expected$match);
    }

    /** * @return \Traversable<string, array<string|int|bool|null>> */
    public static function getMatchingRuleTestData(): \Traversable
    {
 void {
        $this->rule->assign([
            'amount' => $stock,
            'operator' => $operator,
        ]);

        $lineItem = $this->createLineItemWithPrice(LineItem::PRODUCT_LINE_ITEM_TYPE, $lineItemPrice);
        if ($lineItemWithoutPrice) {
            $lineItem = $this->createLineItem();
        }

        $match = $this->rule->match(new LineItemScope(
            $lineItem,
            $this->createMock(SalesChannelContext::class)
        ));

        static::assertSame($expected$match);
    }

    /** * @return \Traversable<string, array<string|int|bool|null>> */
    public static function getMatchingRuleTestData(): \Traversable
    {
public function testIfMatchesCorrectWithLineItemScope(
        array $categoryIds,
        string $operator,
        array $lineItemCategoryIds,
        bool $expected
    ): void {
        $this->rule->assign([
            'categoryIds' => $categoryIds,
            'operator' => $operator,
        ]);

        $match = $this->rule->match(new LineItemScope(
            $this->createLineItemWithCategories($lineItemCategoryIds),
            $this->createMock(SalesChannelContext::class)
        ));

        static::assertSame($expected$match);
    }

    public static function getLineItemScopeTestData(): \Generator
    {
        yield 'single product / equal / match category id' => [['1', '2'], Rule::OPERATOR_EQ, ['1'], true];
        yield 'single product / equal / no match' => [['1', '2'], Rule::OPERATOR_EQ, ['3'], false];
        

    public function isMatching(LineItemGroupDefinition $groupDefinition, LineItem $item, SalesChannelContext $context): bool
    {
        // no rules mean OK         if ($groupDefinition->getRules()->count() <= 0) {
            return true;
        }

        // if we have rules, make sure         // they are connected using an OR condition         $scope = new LineItemScope($item$context);

        foreach ($groupDefinition->getRules() as $rule) {
            $rootCondition = $rule->getPayload();

            // if any rule matches, return OK             if ($rootCondition instanceof Rule && $rootCondition->match($scope)) {
                return true;
            }
        }

        return false;
    }
/** * This test verifies that our rule works correctly * with all the different operators and values. * * @dataProvider getMatchValues */
    public function testRuleMatching(bool $expected, ?string $itemReleased, ?string $ruleDate, string $operator): void
    {
        $this->rule->assign(['lineItemReleaseDate' => $ruleDate, 'operator' => $operator]);

        $isMatching = $this->rule->match(new LineItemScope(
            $this->createLineItemWithReleaseDate($itemReleased),
            $this->createMock(SalesChannelContext::class)
        ));

        static::assertSame($expected$isMatching);
    }

    public function testItemWithoutReleaseDateIsFalse(): void
    {
        $scope = new LineItemScope(
            $this->createLineItem(),
            
 void {
        $this->rule->assign([
            'amount' => $amount,
            'operator' => $operator,
        ]);

        $lineItem = $this->createLineItemWithLength($lineItemAmount);
        if ($lineItemWithoutDeliveryInfo) {
            $lineItem = $this->createLineItem();
        }

        $match = $this->rule->match(new LineItemScope(
            $lineItem,
            $this->createMock(SalesChannelContext::class)
        ));

        static::assertSame($expected$match);
    }

    /** * @return \Traversable<string, array<string|int|bool|null>> */
    public static function getMatchingRuleTestData(): \Traversable
    {
'operator' => Rule::OPERATOR_EQ,
                ],
            ],
        ]$this->context);

        static::assertNotNull($this->conditionRepository->search(new Criteria([$id])$this->context)->get($id));
    }

    public function testNotMatchesWithoutId(): void
    {
        $matches = $this->getLineItemRule()->match(
            new LineItemScope(
                $this->createLineItem(),
                $this->createMock(SalesChannelContext::class)
            )
        );

        static::assertFalse($matches);
    }

    public function testMatchesWithReferencedId(): void
    {
        $matches = $this->getLineItemRule()->match(
            
 void {
        $this->rule->assign([
            'amount' => $percentage,
            'operator' => $operator,
        ]);

        $lineItem = $this->createLineItemWithListPrice($price$listPrice);
        if ($lineItemWithoutPrice) {
            $lineItem = $this->createLineItem();
        }

        $match = $this->rule->match(new LineItemScope(
            $lineItem,
            $this->createMock(SalesChannelContext::class)
        ));

        static::assertSame($expected$match);
    }

    /** * @return \Traversable<string, array<string|int|bool|null>> */
    public static function getMatchingRuleTestData(): \Traversable
    {
Home | Imprint | This part of the site doesn't use cookies.