createProductItem example


    public function testShouldFindGroupsWithRule(): void
    {
        $cart = $this->buildCart(0);

        $item1 = $this->createProductItem(10, 10);
        $item2 = $this->createProductItem(20, 10);
        $item3 = $this->createProductItem(50, 10);

        $item1->setReferencedId($item1->getId());
        $item2->setReferencedId($item2->getId());
        $item3->setReferencedId($item3->getId());

        $item1->setQuantity(10);
        $item2->setQuantity(10);
        $item3->setQuantity(10);

        


    /** * This test verifies that we correctly assign the * provided list of our cart items and return it in the getter. * * @group promotions */
    public function testCartItemsAreCorrectlyAdded(): void
    {
        $cartItems = new LineItemFlatCollection();
        $product = $this->createProductItem(29, 19);
        $cartItems->add($product);

        $package = new DiscountPackage(new LineItemQuantityCollection());
        $package->setCartItems($cartItems);

        static::assertEquals(1, $package->getCartItems()->count());
    }

    /** * This test verifies that we dont get an exception * when requesting the price without any items. * We have to get 0,00 in this case. * * @group promotions */
public function testMatchesForMultipleRules(): void
    {
        $minPrice = 50;
        $minQuantity = 3;

        $productHighQuantity1Id = Uuid::randomBytes();
        $productHighQuantity2Id = Uuid::randomBytes();
        $productLowQuantity1Id = Uuid::randomBytes();
        $productLowQuantity2Id = Uuid::randomBytes();

        // create our test product line items         $productHighQuantityHighPrice = $this->createProductItem($minPrice, 0);
        $productHighQuantityHighPrice->setId($productHighQuantity1Id);
        $productHighQuantityHighPrice->setReferencedId($productHighQuantity1Id);
        $productHighQuantityHighPrice->setQuantity($minQuantity);

        $productHighQuantityLowPrice = $this->createProductItem($minPrice - 0.1, 0);
        $productHighQuantityLowPrice->setId($productHighQuantity2Id);
        $productHighQuantityLowPrice->setReferencedId($productHighQuantity2Id);
        $productHighQuantityLowPrice->setQuantity($minQuantity);

        $productLowQuantityHighPrice = $this->createProductItem($minPrice, 0);
        $productLowQuantityHighPrice->setId($productLowQuantity1Id);
        
protected function setUp(): void
    {
        $this->matcher = new AnyRuleLineItemMatcher();
        $this->context = Generator::createSalesChannelContext();
    }

    /** * @dataProvider lineItemProvider */
    public function testMatching(bool $withRules, bool $diffrentId, bool $expected): void
    {
        $lineItem = $this->createProductItem(50, 10);
        $lineItem->setReferencedId($lineItem->getId());

        $ruleCollection = new RuleCollection();

        if ($withRules === true) {
            $matchId = $diffrentId === true ? Uuid::randomHex() : $lineItem->getId();

            $ruleCollection->add($this->buildRuleEntity(
                $this->getProductsRule([$matchId])
            ));
        }

        
static::assertEquals(2, $collection->count());
    }

    /** * This test verifies that our object collects the * calculated prices from all existing packages. * * @group promotions */
    public function testAffectedPricesFromAllPackages(): void
    {
        $product1 = $this->createProductItem(29, 19);
        $product2 = $this->createProductItem(14, 19);

        $package1 = new DiscountPackage(new LineItemQuantityCollection());
        $package1->setCartItems(new LineItemFlatCollection([$product1]));

        $package2 = new DiscountPackage(new LineItemQuantityCollection());
        $package2->setCartItems(new LineItemFlatCollection([$product2]));

        $collection = new DiscountPackageCollection([$package1$package2]);

        static::assertEquals(2, $collection->getAffectedPrices()->count());
    }


    /** * This test verifies that our sorting works correctly. * We add 3 items with different item prices and test that * the sorted list comes in the correct order. * * @group lineitemgroup */
    public function testSortPriceASC(): void
    {
        $p1 = $this->createProductItem(50.0, 0);
        $p2 = $this->createProductItem(23.5, 0);
        $p3 = $this->createProductItem(150.0, 0);

        $items = new LineItemFlatCollection();
        $items->add($p1);
        $items->add($p2);
        $items->add($p3);

        $sortedItems = $this->sorter->sort($items);

        static::assertEquals($p2->getId()$sortedItems->getElements()[0]->getId());
        
/** * This test verifies that we have finished building our * package, as soon as we have reached a gross item price of 100.0. * This is archived with 4 items. * * @group lineitemgroup */
    public function testPackageDoneWhenSumReached(): void
    {
        $items = new LineItemFlatCollection();
        $items->add($this->createProductItem(25.0, 19));
        $items->add($this->createProductItem(25.0, 19));
        $items->add($this->createProductItem(25.0, 19));
        $items->add($this->createProductItem(25.0, 19));
        $items->add($this->createProductItem(25.0, 19));
        $items->add($this->createProductItem(25.0, 19));

        $packageItems = $this->packager->buildGroupPackage(100, $items$this->context);

        // verify we have 4 items, then we have reached 100.0         static::assertCount(4, $packageItems->getItems());
    }

    
static::assertEquals('COUNT', $this->packager->getKey());
    }

    /** * This test verifies that our packaging does correctly * return 2 items if we request that, and if more than 2 items exist. * * @group lineitemgroup */
    public function testPackageDoneWhenCountReached(): void
    {
        $p1 = $this->createProductItem(50.0, 0);
        $p2 = $this->createProductItem(23.5, 0);
        $p3 = $this->createProductItem(150.0, 0);

        $items = new LineItemFlatCollection();
        $items->add($p1);
        $items->add($p2);
        $items->add($p3);

        $packageItems = $this->packager->buildGroupPackage(2, $items$this->context);

        // verify we have only 2 items


    /** * This test verifies that our sorting works correctly. * We add 3 items with different item prices and test that * the sorted list comes in the correct order. * * @group lineitemgroup */
    public function testSortPriceDESC(): void
    {
        $p1 = $this->createProductItem(50.0, 0);
        $p2 = $this->createProductItem(23.5, 0);
        $p3 = $this->createProductItem(150.0, 0);

        $items = new LineItemFlatCollection();
        $items->add($p1);
        $items->add($p2);
        $items->add($p3);

        $sortedItems = $this->sorter->sort($items);

        static::assertEquals($p3->getId()$sortedItems->getElements()[0]->getId());
        
/** * This test verifies that we have finished building our * package, as soon as we have reached a gross item price of 100.0. * This is archived with 2 items - but most important - only if taxes are considered. * Otherwise it would be 3 items. * * @group lineitemgroup */
    public function testPackageDoneWhenSumReached(): void
    {
        $items = new LineItemFlatCollection();
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));

        $packageItems = $this->packager->buildGroupPackage(100, $items$this->context);

        // verify we have 2 items, then we have reached 100.0         static::assertCount(2, $packageItems->getItems());
    }
Home | Imprint | This part of the site doesn't use cookies.