createProduct example

protected function setUp(): void
    {
        parent::setUp();

        $this->ids = new IdsCollection();
        $this->cartService = $this->getContainer()->get(CartService::class);
        $this->calculator = $this->getContainer()->get(QuantityPriceCalculator::class);
    }

    public function testDeliveryInformation(): void
    {
        $this->createProduct();

        $cart = $this->getProductCart();
        $lineItem = $cart->get($this->ids->get('product'));

        static::assertInstanceOf(LineItem::class$lineItem);
        static::assertInstanceOf(DeliveryInformation::class$lineItem->getDeliveryInformation());

        $info = $lineItem->getDeliveryInformation();
        static::assertEquals(100, $info->getWeight());
        static::assertEquals(101, $info->getHeight());
        static::assertEquals(102, $info->getWidth());
        
$listener = $this->getMockBuilder(CallableClass::class)->getMock();
        $listener->expects(static::once())->method('__invoke');
        $this->getContainer()->get('event_dispatcher')->addListener(ProductListingResolvePreviewEvent::class$listener);
        $context = $this->getContainer()->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), TestDefaults::SALES_CHANNEL);

        $criteria = new Criteria($ids->getList(['p1']));
        $this->productListingLoader->load($criteria$context);
    }

    public function testMainVariant(): void
    {
        $this->createProduct([], true);

        $listing = $this->fetchListing();

        static::assertEquals(1, $listing->getTotal());

        $mainVariant = $listing->getEntities()->first();
        static::assertNotNull($mainVariant);

        static::assertEquals($this->mainVariantId, $mainVariant->getId());
        static::assertContains($this->optionIds['red']$mainVariant->getOptionIds() ?: []);
        static::assertContains($this->optionIds['l']$mainVariant->getOptionIds() ?: []);
        
protected function setUp(): void
    {
        $this->repository = $this->getContainer()->get('product.repository');

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

        $this->findProductVariantRoute = $this->getContainer()->get(FindProductVariantRoute::class);

        $this->ids = new IdsCollection();

        $this->createProduct();

        parent::setUp();
    }

    public function testFindVariant(): void
    {
        $options = [
            $this->ids->get('Color') => $this->ids->get('Red'),
            $this->ids->get('Size') => $this->ids->get('XL'),
        ];

        
static::assertCount(\count($ids)$actual->getIds());
        }
    }

    public function testImportExportAdvancedPrices(): void
    {
        $context = Context::createDefaultContext();
        $context->addState(EntityIndexerRegistry::DISABLE_INDEXING);

        $productId = 'e12a77e8ed6642698b987250d8ec705d';
        $ruleId = 'cb34dc6f20b6479aa975e1290f442e65';
        $this->createProduct($productId);
        $this->createRule($ruleId);

        $progress = $this->import($context, ProductPriceDefinition::ENTITY_NAME, '/fixtures/advanced_prices.csv', 'advanced_prices.csv');

        static::assertImportExportSucceeded($progress$this->getInvalidLogContent($progress->getInvalidRecordsLogId()));

        /** @var ProductEntity $product */
        $product = $this->productRepository->search((new Criteria([$productId]))->addAssociation('prices')$context)->first();

        static::assertInstanceOf(ProductPriceCollection::class$product->getPrices());
        static::assertEquals(2, $product->getPrices()->count());
        
$this->connection->executeStatement('DROP TABLE `extended_product`');
        $this->connection->beginTransaction();

        $this->removeExtension(ProductExtension::class);

        parent::tearDown();
    }

    public function testWriteExtensionWithExtensionKey(): void
    {
        $productId = Uuid::randomHex();
        $this->createProduct($productId);

        $this->productRepository->update([
            [
                'id' => $productId,
                'extensions' => [
                    'oneToMany' => [
                        ['name' => 'test 1', 'languageId' => Defaults::LANGUAGE_SYSTEM],
                        ['name' => 'test 2', 'languageId' => Defaults::LANGUAGE_SYSTEM],
                    ],
                ],
            ],
        ],
$traces = $this->getContainer()->get(ScriptTraces::class)->getTraces();
        static::assertArrayHasKey(CheckoutFinishPageLoadedHook::HOOK_NAME, $traces);
    }

    public function testCheckoutInfoWidget(): void
    {
        $contextToken = Uuid::randomHex();

        $cartService = $this->getContainer()->get(CartService::class);
        $cart = $cartService->createNew($contextToken);

        $productId = $this->createProduct();
        $salesChannelContext = $this->createSalesChannelContext($contextToken);

        $cart = $cartService->add(
            $cart,
            [new LineItem('lineItem1', LineItem::PRODUCT_LINE_ITEM_TYPE, $productId)],
            $salesChannelContext
        );

        $request = $this->createRequest($salesChannelContext);

        $response = $this->getContainer()->get(CheckoutController::class)->info($request$salesChannelContext);
        
public function testEqualsAnyFilter(): void
    {
        $id1 = Uuid::randomHex();
        $id2 = Uuid::randomHex();

        $red = Uuid::randomHex();
        $green = Uuid::randomHex();
        $blue = Uuid::randomHex();
        $notAssigned = Uuid::randomHex();

        $this->createProduct($id1[
            ['id' => $red, 'name' => 'red'],
            ['id' => $blue, 'name' => 'blue'],
        ]);

        $this->createProduct($id2[
            ['id' => $green, 'name' => 'green'],
            ['id' => $red, 'name' => 'red'],
        ]);

        $context = Context::createDefaultContext();

        
$response = $this->browser->getResponse();

        // After login successfully, the context token will be set in the header         $contextToken = $response->headers->get(PlatformRequest::HEADER_CONTEXT_TOKEN) ?? '';
        static::assertNotEmpty($contextToken);

        $this->browser->setServerParameter('HTTP_SW_CONTEXT_TOKEN', $contextToken);
    }

    public function testMergeProductShouldReturnSuccessNoWishlistExisted(): void
    {
        $productData = $this->createProduct($this->context);

        $this->browser
            ->request(
                'POST',
                '/store-api/customer/wishlist/merge',
                [
                    'productIds' => [
                        'id' => $productData,
                    ],
                ]
            );
        
$criteriaCollection = $this->buyBoxResolver->collect($slot$resolverContext);

        static::assertNull($criteriaCollection);
    }

    public function testReviewCountLoaded(): void
    {
        $productId = Uuid::randomHex();
        $saleChannelContext = $this->createSalesChannelContext();

        $this->createProduct($productId);
        $this->createReviews($productId);

        $fieldConfig = new FieldConfigCollection();
        $fieldConfig->add(new FieldConfig('product', FieldConfig::SOURCE_STATIC, $productId));

        $slot = new CmsSlotEntity();
        $slot->setUniqueIdentifier('id');
        $slot->setType('buy-box');
        $slot->setFieldConfig($fieldConfig);

        $product = new SalesChannelProductEntity();
        
$response = $this->browser->getResponse();

        // After login successfully, the context token will be set in the header         $contextToken = $response->headers->get(PlatformRequest::HEADER_CONTEXT_TOKEN) ?? '';
        static::assertNotEmpty($contextToken);

        $this->browser->setServerParameter('HTTP_SW_CONTEXT_TOKEN', $contextToken);
    }

    public function testDeleteProductShouldReturnSuccess(): void
    {
        $productId = $this->createProduct($this->context);
        $dispatcher = $this->getContainer()->get('event_dispatcher');
        $eventWasThrown = false;

        $this->createCustomerWishlist($this->context, $this->customerId, $productId);

        $listener = static function DWishlistProductRemovedEvent $event) use ($productId, &$eventWasThrown): void {
            static::assertSame($productId$event->getProductId());
            $eventWasThrown = true;
        };
        $dispatcher->addListener(WishlistProductRemovedEvent::class$listener);

        
'available_stock' => '10',
            ],
            [
                'id' => $this->ids->get('p5'),
                'stock' => '-6',
                'available_stock' => '-6',
            ],
        ];

        $migration = new Migration1691662140MigrateAvailableStock();

        $this->createProduct($this->ids->getBytes('p1'), 5, 7, true);
        $this->createProduct($this->ids->getBytes('p2'), 6, 6, true);
        $this->createProduct($this->ids->getBytes('p3'), 8, 8, false);
        $this->createProduct($this->ids->getBytes('p4'), 8, 10, false);
        $this->createProduct($this->ids->getBytes('p5'), 8, -6, true);

        $migration->update($this->connection);

        static::assertSame(
            $expected,
            $this->connection->fetchAllAssociative('SELECT LOWER(HEX(id)) as id, stock, available_stock FROM product ORDER BY created_at ASC')
        );

        
$options = $result->getEntities();

        $ids = array_keys($options->getOptionIdMap());

        static::assertContains($this->testData->getId('green')$ids);
        static::assertContains($this->testData->getId('red')$ids);
        static::assertNotContains($this->testData->getId('blue')$ids);
    }

    private function insertProducts(): void
    {
        $this->createProduct(
            'product1',
            [
                [
                    'combination' => [$this->testData->getId('red')],
                    'stock' => 1,
                ],
                [
                    'combination' => [$this->testData->getId('blue')],
                    'stock' => 0,
                ],
                [
                    
$orderId = $order['orderId'];
        $oldTotal = $order['total'];
        $oldOrderStateId = $order['stateId'];

        // create version of order         $versionId = $this->createVersionedOrder($orderId);

        $productName = 'Test';
        $productPrice = 10.0;
        $productTaxRate = 19.0;

        $productId = $this->createProduct($productName$productPrice$productTaxRate);

        // add product to order         $this->getBrowser()->request(
            'POST',
            sprintf(
                '/api/_action/order/%s/product/%s',
                $orderId,
                $productId
            ),
            [],
            [],
            [
private function insertData(): void
    {
        $this->salesChannelId1 = $this->createSalesChannel('sales-1');
        $this->salesChannelId2 = $this->createSalesChannel('sales-2');
        $this->categoryId = $this->ids->get('category');
        $this->productId1 = $this->ids->get('product-1');
        $this->productId2 = $this->ids->get('product-2');
        $this->productId3 = $this->ids->get('product-3');
        $this->productId4 = $this->ids->get('product-4');

        $products = [
            $this->createProduct($this->productId1, [
                $this->salesChannelId2 => ProductVisibilityDefinition::VISIBILITY_ALL,
            ]),
            $this->createProduct($this->productId2, [
                $this->salesChannelId1 => ProductVisibilityDefinition::VISIBILITY_SEARCH,
                $this->salesChannelId2 => ProductVisibilityDefinition::VISIBILITY_SEARCH,
            ]),
            $this->createProduct($this->productId3, [
                $this->salesChannelId1 => ProductVisibilityDefinition::VISIBILITY_ALL,
                $this->salesChannelId2 => ProductVisibilityDefinition::VISIBILITY_LINK,
            ]),
            $this->createProduct($this->productId4, [
                
'ALTER TABLE `customer` DROP COLUMN `review_count`;'
            );
        } catch (\Throwable) {
        }
    }

    public function testUpdate(): void
    {
        $migration = new Migration1679584289AddCustomerReviewCount();

        $this->createCustomer();
        $this->createProduct();
        $this->createReview($this->ids->create('review1'));
        $this->createReview($this->ids->create('review2'));
        $this->createReview($this->ids->create('review3'));

        $migration->update($this->connection);
        $migration->update($this->connection);

        $reviewCount = $this->connection->fetchOne(
            'SELECT `review_count` FROM `customer` WHERE `id` = :customerId;',
            ['customerId' => Uuid::fromHexToBytes($this->ids->get('customer'))],
        );
        
Home | Imprint | This part of the site doesn't use cookies.