setLineItems example


    public function testCartScopeMatching(bool $expected, array $itemPropertyIds, array $identifiers, string $operator): void
    {
        $lineItem = new LineItem(Uuid::randomHex(), LineItem::PRODUCT_LINE_ITEM_TYPE, null, 1);
        $lineItem->setPayloadValue('propertyIds', $itemPropertyIds);
        $lineItems = new LineItemCollection([$lineItem]);

        $cart = new Cart(Uuid::randomHex());
        $cart->setLineItems($lineItems);

        $context = $this->createMock(SalesChannelContext::class);
        $scope = new CartRuleScope($cart$context);

        $this->rule->assign(['identifiers' => $identifiers, 'operator' => $operator]);

        static::assertSame(
            $expected,
            $this->rule->match($scope)
        );
    }

    
$salesChannelContext,
            new CartBehavior(),
        );
    }

    /** * @dataProvider getLineItemsThatResultInAnEmptyDelivery */
    public function testLineItemResultInAnEmptyDelivery(LineItemCollection $lineItems): void
    {
        $cart = new Cart('cart-token');
        $cart->setLineItems($lineItems);

        $deliveries = (new DeliveryBuilder())->buildByUsingShippingMethod(
            $cart,
            new ShippingMethodEntity(),
            $this->createMock(SalesChannelContext::class),
        );

        static::assertEquals(0, $deliveries->count());
    }

    /** * @return iterable<array{0: LineItemCollection}> */

    public function testCartScopeMatching(bool $expected, array $itemOptionIds, array $identifiers, string $operator): void
    {
        $lineItem = new LineItem(Uuid::randomHex(), LineItem::PRODUCT_LINE_ITEM_TYPE, null, 1);
        $lineItem->setPayloadValue('optionIds', $itemOptionIds);
        $lineItems = new LineItemCollection([$lineItem]);

        $cart = new Cart(Uuid::randomHex());
        $cart->setLineItems($lineItems);

        $context = $this->createMock(SalesChannelContext::class);
        $scope = new CartRuleScope($cart$context);

        $this->rule->assign(['identifiers' => $identifiers, 'operator' => $operator]);

        static::assertSame(
            $expected,
            $this->rule->match($scope)
        );
    }

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

    public function testMatchCartItemScopeMatchesWithEquals(): void
    {
        $rule = new LineItemOfTypeRule(Rule::OPERATOR_EQ, 'shirt');

        $lineItem = new LineItem(Uuid::randomHex(), 'shirt');

        $cart = new Cart(Uuid::randomHex());
        $cart->setLineItems(new LineItemCollection([$lineItem]));
        $scope = new CartRuleScope($cartstatic::createMock(SalesChannelContext::class));

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

    public function testMatchCartItemScopeNotMatchesWithEquals(): void
    {
        $rule = new LineItemOfTypeRule(Rule::OPERATOR_EQ, 'jeans');

        $lineItem = new LineItem(Uuid::randomHex(), 'shirt');

        
/** * @dataProvider calculationProvider */
    public function testCalculation(LineItem $item, ?CalculatedPrice $expected): void
    {
        $processor = $this->getContainer()->get(ContainerCartProcessor::class);

        $context = $this->getContainer()->get(SalesChannelContextFactory::class)
            ->create(Uuid::randomHex(), TestDefaults::SALES_CHANNEL);

        $cart = new Cart('test');
        $cart->setLineItems(new LineItemCollection([$item]));

        $new = new Cart('after');
        $processor->process(new CartDataCollection()$cart$new$contextnew CartBehavior());

        if ($expected === null) {
            static::assertFalse($new->has($item->getId()));

            return;
        }

        static::assertTrue($new->has($item->getId()));

        
private function createOrder(): OrderEntity
    {
        $order = new OrderEntity();

        $lineItems = new OrderLineItemCollection();
        $lineItems->add($this->buildOrderLineItem($this->ids->get('id1'), LineItem::PRODUCT_LINE_ITEM_TYPE, 1));
        $lineItems->add($this->buildOrderLineItem($this->ids->get('id2'), LineItem::PRODUCT_LINE_ITEM_TYPE, 2));
        $lineItems->add($this->buildOrderLineItem(null, LineItem::PRODUCT_LINE_ITEM_TYPE, 2));
        $lineItems->add($this->buildOrderLineItem($this->ids->get('discount'), LineItem::DISCOUNT_LINE_ITEM, 1));

        $order->setLineItems($lineItems);

        return $order;
    }

    private function buildOrderLineItem(?string $id, string $type, int $qty): OrderLineItemEntity
    {
        $orderLineItemEntity = new OrderLineItemEntity();
        $orderLineItemEntity->setId(Uuid::randomHex());
        $orderLineItemEntity->setReferencedId($id);
        $orderLineItemEntity->setType($type);
        $orderLineItemEntity->setIdentifier($id ?? Uuid::randomHex());
        

    public function testProcessor(array $items, ?CalculatedPrice $expected): void
    {
        $processor = $this->getContainer()->get(DiscountCartProcessor::class);

        $context = $this->getContainer()->get(SalesChannelContextFactory::class)
            ->create(Uuid::randomHex(), TestDefaults::SALES_CHANNEL);

        $cart = new Cart('test');
        $cart->setLineItems(new LineItemCollection($items));

        $new = new Cart('after');
        $new->setLineItems(
            (new LineItemCollection($items))->filter(fn (LineItem $item) => $item->getType() !== LineItem::DISCOUNT_LINE_ITEM)
        );

        $processor->process(new CartDataCollection()$cart$new$contextnew CartBehavior());

        if ($expected === null) {
            static::assertFalse($new->has(self::DISCOUNT_ID));

            
use IntegrationTestBehaviour;

    /** * @dataProvider buildPackagesProvider * * @param LineItem[] $items * @param array<string> $expected */
    public function testBuildPackages(array $items, array $expected): void
    {
        $cart = new Cart('test');
        $cart->setLineItems(new LineItemCollection($items));

        $packager = $this->getContainer()->get(CartScopeDiscountPackager::class);

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

        $discount = new DiscountLineItem('test', new QuantityPriceDefinition(10, new TaxRuleCollection([]), 1)[
            'discountScope' => 'scope',
            'discountType' => 'type',
            'filter' => [],
        ], null);

        
/** * @internal * * @covers \Shopware\Core\Checkout\Cart\Facade\CartFacade */
class CartFacadeTest extends TestCase
{
    public function testPublicApiAvailable(): void
    {
        $cart = new Cart('foo');
        $cart->setLineItems(new LineItemCollection([
            new LineItem('item', 'item'),
            new LineItem('discount', 'discount'),
            new LineItem('product', 'product'),
        ]));

        $cart->addState('within-test');

        $cart->setPrice(new CartPrice(
            100,
            100,
            100,
            
foreach ($this->ids->prefixed('product-state-') as $id) {
            $this->cart->getLineItems()->add(new LineItem(Uuid::randomHex(), LineItem::PRODUCT_LINE_ITEM_TYPE, $id));
        }

        $this->getContainer()->get(ProductCartProcessor::class)->collect($this->cart->getData()$this->cart, $this->context, new CartBehavior());
    }

    #[Bench\BeforeMethods(['setupWithLogin'])]     #[Bench\Assert('mode(variant.time.avg) < 150ms +/- 20ms')]     public function bench_order_10_physical_products(): void
    {
        $this->cart->setLineItems($this->cart->getLineItems()->filter(fn (LineItem $lineItem): bool => \in_array($lineItem->getReferencedId()$this->ids->prefixed('product-state-physical-'), true)));
        $this->getContainer()->get(CartOrderRoute::class)->order($this->cart, $this->context, new RequestDataBag());
    }

    #[Bench\BeforeMethods(['setupWithLogin'])]     #[Bench\Assert('mode(variant.time.avg) < 170ms +/- 20ms')]     public function bench_order_10_digital_products(): void
    {
        $this->cart->setLineItems($this->cart->getLineItems()->filter(fn (LineItem $lineItem): bool => \in_array($lineItem->getReferencedId()$this->ids->prefixed('product-state-digital-'), true)));
        $this->getContainer()->get(CartOrderRoute::class)->order($this->cart, $this->context, new RequestDataBag());
    }
}
 $this->context);

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

    /** * @dataProvider matchProvider */
    public function testMatch(LineItem $lineItem, LineItemWithQuantityRule $rule, bool $shouldMatch): void
    {
        $cart = new Cart('test');
        $cart->setLineItems(new LineItemCollection([$lineItem]));

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

        static::assertSame(
            $shouldMatch,
            $rule->match(new CartRuleScope($cart$context))
        );
    }

    public static function matchProvider(): \Generator
    {
        
$mock = $this->createMock(ProductGateway::class);
        $mock
            ->method('get')
            ->willReturn($priceDefinitions);

        return $mock;
    }

    public static function createCart(): Cart
    {
        $cart = new Cart('test');
        $cart->setLineItems(
            new LineItemCollection([
                (new LineItem('A', 'product', 'A', 27))
                    ->setPrice(new CalculatedPrice(10, 270, new CalculatedTaxCollection()new TaxRuleCollection(), 27)),
                (new LineItem('B', 'test', 'B', 5))
                    ->setGood(false)
                    ->setPrice(new CalculatedPrice(0, 0, new CalculatedTaxCollection()new TaxRuleCollection())),
            ])
        );
        $cart->setPrice(
            new CartPrice(
                275.0,
                
$result = $this->orderRoute
            ->load($event->getStoreApiRequest()$salesChannelContext$criteria);

        $order = $result->getOrders()->first();

        if (!$order instanceof OrderEntity) {
            throw new NotFoundHttpException();
        }

        $page = AccountOrderDetailPage::createFrom($this->genericLoader->load($request$salesChannelContext));
        $page->setLineItems($order->getNestedLineItems());
        $page->setOrder($order);

        if ($page->getMetaInformation()) {
            $page->getMetaInformation()->setRobots('noindex,follow');
        }

        $this->eventDispatcher->dispatch(
            new AccountOrderDetailPageLoadedEvent($page$salesChannelContext$request)
        );

        return $page;
    }
$this->salesChannelContext->getShippingMethod()
        );
        $originalCart = new Cart('original');
        $calculatedCart = new Cart('calculated');

        $lineItem = new LineItem('test', LineItem::PRODUCT_LINE_ITEM_TYPE);
        $lineItem->setDeliveryInformation(new DeliveryInformation(5, 0, false));
        $lineItem->setPrice(new CalculatedPrice(5.0, 5.0, new CalculatedTaxCollection([
            new CalculatedTax(5, 19, 5),
        ])new TaxRuleCollection()));

        $calculatedCart->setLineItems(new LineItemCollection([$lineItem]));

        $cartBehavior = new CartBehavior();

        static::assertCount(0, $calculatedCart->getDeliveries());

        $deliveryProcessor->process($cartDataCollection$originalCart$calculatedCart$this->salesChannelContext, $cartBehavior);

        // Deliveries were built         static::assertCount(1, $calculatedCart->getDeliveries());

        // Price was recalculated
$order = new OrderEntity();

        yield 'order without line items' => [$order[]];

        $order = new OrderEntity();

        $lineItem = new OrderLineItemEntity();
        $lineItem->setGood(true);
        $lineItem->setId(Uuid::randomHex());

        $order->setLineItems(new OrderLineItemCollection([$lineItem]));

        yield 'order without downloadable line items' => [$order[]];

        $order = new OrderEntity();

        $lineItem = new OrderLineItemEntity();
        $lineItem->setId(Uuid::randomHex());
        $lineItem->setGood(true);
        $lineItem->setStates([State::IS_DOWNLOAD]);

        $downloadId = Uuid::randomHex();
        
Home | Imprint | This part of the site doesn't use cookies.