createLineItem example

float $lineItemHeight,
        bool $expected,
        bool $lineItemWithoutDeliveryInfo = false
    ): 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>> */
float $lineItemAmount,
        bool $expected,
        bool $lineItemWithoutDeliveryInfo = false
    ): 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>> */


    protected static function createLineItemWithDeliveryInfo(
        bool $freeDelivery,
        int $quantity = 1,
        ?float $weight = 50.0,
        ?float $height = null,
        ?float $width = null,
        ?float $length = null,
        int $stock = 9999
    ): LineItem {
        return self::createLineItem(LineItem::PRODUCT_LINE_ITEM_TYPE, $quantity)->setDeliveryInformation(
            new DeliveryInformation(
                $stock,
                $weight,
                $freeDelivery,
                null,
                (new DeliveryTime())->assign([
                    'min' => 1,
                    'max' => 3,
                    'unit' => 'weeks',
                    'name' => '1-3 weeks',
                ]),
                
return $output;
    }

    public static function transformFlatToNested(OrderLineItemCollection $lineItems): LineItemCollection
    {
        $lineItems->sortByPosition();
        $index = [];
        $root = new LineItemCollection();

        foreach ($lineItems as $id => $lineItem) {
            if (!\array_key_exists($id$index)) {
                $index[$id] = self::createLineItem($lineItem);
            }

            $currentLineItem = $index[$id];

            self::updateLineItem($currentLineItem$lineItem$id);

            if ($lineItem->getParentId() === null) {
                $root->add($currentLineItem);

                continue;
            }

            
static::assertFalse($rule->match($this->createMock(RuleScope::class)));
    }

    /** * @param float[] $prices * * @dataProvider provideLineItemTestCases */
    public function testMatchWithCartRuleScope(string $operator, array $prices, float $total, bool $matches): void
    {
        $cart = new Cart('test-token');
        $cart->setLineItems(new LineItemCollection(array_map(fn (float $price): LineItem => $this->createLineItem($price)$prices)));

        $ruleScope = new CartRuleScope($cartstatic::createMock(SalesChannelContext::class));

        $rule = new CartTotalPurchasePriceRule();
        $rule->assign([
            'operator' => $operator,
            'amount' => $total,
        ]);

        static::assertEquals($matches$rule->match($ruleScope));
    }

    
'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))
        );

        


        $lineItems = $this->createLineItems();
        $cart->addLineItems($lineItems);

        return $cart;
    }

    private function createLineItems(): LineItemCollection
    {
        $lineItems = new LineItemCollection();
        $lineItem = $this->createLineItem();
        $lineItems->add($lineItem);

        return $lineItems;
    }

    private function createLineItem(): LineItem
    {
        $lineItem = new LineItem('testid', LineItem::PRODUCT_LINE_ITEM_TYPE);

        $deliveryInformation = $this->createDeliveryInformation();
        $lineItem->setDeliveryInformation($deliveryInformation);

        
$rule->assign(['count' => 0, 'operator' => Rule::OPERATOR_EQ]);

        static::assertTrue($rule->match(new CartRuleScope($this->createCart(new LineItemCollection())$this->createMock(SalesChannelContext::class))));
    }

    public function testRuleMatchesWithTwoLineItems(): void
    {
        $rule = new LineItemsInCartCountRule();
        $rule->assign(['count' => 2, 'operator' => Rule::OPERATOR_EQ]);

        $lineItemCollection = new LineItemCollection([
            $this->createLineItem(),
            $this->createLineItem(),
        ]);
        $cart = $this->createCart($lineItemCollection);

        static::assertTrue($rule->match(new CartRuleScope($cart$this->createMock(SalesChannelContext::class))));
    }

    public function testRuleDoesNotMatchOnUnequalsWithTwoLineItems(): void
    {
        $rule = new LineItemsInCartCountRule();
        $rule->assign(['count' => 2, 'operator' => Rule::OPERATOR_NEQ]);

        


        return array_map(static fn ($case) => [$case]$cases);
    }

    /** * @param array<string> $properties * @param array<string> $options */
    private static function createLineItemWithVariantOptions(array $properties = [], array $options = []): LineItem
    {
        $lineItem = self::createLineItem();

        $lineItem->setPayloadValue('propertyIds', $properties);
        $lineItem->setPayloadValue('optionIds', $options);

        return $lineItem;
    }
}


        $shippingMethod->setPrices(new ShippingMethodPriceCollection([$price]));

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

        $context->expects(static::atLeastOnce())->method('getContext')->willReturn($baseContext);
        $context->expects(static::atLeastOnce())->method('getRuleIds')->willReturn([]);
        $context->expects(static::atLeastOnce())->method('getShippingMethod')->willReturn($shippingMethod);

        $firstLineItem = $this->createLineItem(
            new DeliveryInformation(10, 12.0, false, null, $this->deliveryTime),
            new CalculatedPrice(
                10,
                10,
                new CalculatedTaxCollection([new CalculatedTax(5, 19, 5)]),
                new TaxRuleCollection([new TaxRule(19)])
            )
        );

        $secondLineItem = $this->createLineItem(
            new DeliveryInformation(10, 12.0, false, null, $this->deliveryTime),
            
$allLineItemsRule = new MatchAllLineItemsRule([], null, 'product');
        $allLineItemsRule->assign(['minimumShouldMatch' => 2]);
        $allLineItemsRule->addRule($lineItemRule);

        $lineItemCollection = new LineItemCollection([
            $this->createLineItemWithCategories($categoryIdsProductA),
            $this->createLineItemWithCategories($categoryIdsProductB),
            $this->createLineItemWithCategories($categoryIdsProductC),
        ]);

        $promotionLineItem = $this->createLineItem(LineItem::PROMOTION_LINE_ITEM_TYPE, 1, 'PROMO')->setPayloadValue('promotionId', 'A');
        $lineItemCollection->add($promotionLineItem);

        $cart = $this->createCart($lineItemCollection);

        $match = $allLineItemsRule->match(new CartRuleScope(
            $cart,
            $this->createMock(SalesChannelContext::class)
        ));

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

    

        return [
            'rule yes / clearance sale yes' => [true, true, true],
            'rule yes / clearance sale no' => [true, false, false],
            'rule no / clearance sale no' => [false, false, true],
            'rule no / clearance sale yes' => [false, true, true],
        ];
    }

    private function createLineItemWithClearance(bool $clearanceSaleEnabled): LineItem
    {
        return $this->createLineItem()->setPayloadValue('isCloseout', $clearanceSaleEnabled);
    }
}
float $lineItemWeight,
        bool $expected,
        bool $lineItemWithoutDeliveryInfo = false
    ): void {
        $this->rule->assign([
            'amount' => $weight,
            'operator' => $operator,
        ]);

        $lineItem = $this->createLineItemWithWeight($lineItemWeight);
        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>> */

            ],
        ]$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(
            new LineItemScope(
                

        return [
            'rule yes / newcomer yes' => [true, true, true],
            'rule yes / newcomer no' => [true, false, false],
            'rule no / newcomer yes' => [false, true, true],
            'rule no / newcomer no' => [false, false, true],
        ];
    }

    private function createLineItemWithIsNewMarker(bool $isNew): LineItem
    {
        return $this->createLineItem()->setPayloadValue('isNew', $isNew);
    }
}
Home | Imprint | This part of the site doesn't use cookies.