ProductBuilder example


                    'switchedGroup' => $switched,
                    'options' => $options,
                ]
            ),
            $this->context
        );
    }

    private function createProduct(): void
    {
        (new ProductBuilder($this->ids, 'base', 10))->configuratorSetting(
            'Red',
            'Color'
        )->configuratorSetting(
            'Green',
            'Color'
        )->configuratorSetting(
            'XL',
            'Size'
        )->configuratorSetting(
            'L',
            'Size'
        )
/** * @param array<int, string[]> $productDownloads */
    private function addProducts(Cart $cart, array $productDownloads): Cart
    {
        $ids = new IdsCollection();
        $taxIds = $this->salesChannelContext->getTaxRules()->getIds();
        $ids->set('t1', (string) array_pop($taxIds));
        $products = [];

        foreach ($productDownloads as $key => $downloadFiles) {
            $products[] = (new ProductBuilder($ids, 'product-' . $key))
                ->price(1.0)
                ->tax('t1')
                ->visibility()
                ->add('downloads', array_map(function Dstring $file): array {
                    [$fileName$fileExtension] = explode('.', $file);

                    return [
                        'media' => [
                            'id' => Uuid::randomHex(),
                            'fileName' => $fileName,
                            'fileExtension' => $fileExtension,
                            
/** * @dataProvider rangeAggregationDataProvider * * @param array<int, array<string, string|float>> $rangesDefinition * @param array<string, int> $rangesExpectedResult */
    public function testRangeAggregation(array $rangesDefinition, array $rangesExpectedResult): void
    {
        $ids = new TestDataCollection();

        $data = [
            (new ProductBuilder($ids, 'a'))->price(5, 5)->build(),
            (new ProductBuilder($ids, 'b'))->price(10, 10)->build(),
            (new ProductBuilder($ids, 'c'))->price(15, 15)->build(),
            (new ProductBuilder($ids, 'd'))->price(15, 15)->build(),
            (new ProductBuilder($ids, 'e'))->price(25, 25)->build(),
            (new ProductBuilder($ids, 'f'))->price(26, 26)->build(),
            (new ProductBuilder($ids, 'g'))->price(30, 30)->build(),
            (new ProductBuilder($ids, 'h'))->price(100, 100)->build(),
        ];

        $this->repository->create($data$this->context);

        
#[Package('core')] class Migration1642757286FixProductMediaForeignKeyTest extends TestCase
{
    use IntegrationTestBehaviour;

    public function testProductMediaConstraint(): void
    {
        $ids = new IdsCollection();

        $productRepository = $this->getContainer()->get('product.repository');

        $product = (new ProductBuilder($ids, 'p1'))
            ->price(100)
            ->media('m1')
            ->cover('m1');

        $productRepository->create([$product->build()], Context::createDefaultContext());

        $productMediaRepository = $this->getContainer()->get('product_media.repository');
        $productMediaRepository->delete([['id' => $ids->get('m1')]], Context::createDefaultContext());

        $product = $productRepository->search(new Criteria([$ids->get('p1')]), Context::createDefaultContext())->first();
        static::assertNull($product->getCoverId());
    }

class Migration1688556247FixCoverMediaVersionIDTest extends TestCase
{
    use DatabaseTransactionBehaviour;
    use KernelTestBehaviour;

    public function testMigration(): void
    {
        $product = (new ProductBuilder(new IdsCollection(), 'test'))
            ->cover('cover')
            ->price(100)
            ->build();

        $connection = $this->getContainer()->get(Connection::class);
        $connection->executeStatement('DELETE FROM product');

        $this->getContainer()->get('product.repository')->create([$product], Context::createDefaultContext());

        $connection->executeStatement('UPDATE product SET product_media_version_id = NULL');

        
continue;
            }

            static::assertEquals($value$actual[$key]sprintf('Value for key %s not matching', $current));
        }
    }

    private function createData(): void
    {
        $products = [
            (new ProductBuilder($this->ids, 'product'))
                ->price(15)
                ->manufacturer('m1')
                ->visibility($this->ids->get('sales-channel'))
                ->build(),

            // regression test for: NEXT-17603             (new ProductBuilder($this->ids, 'with-layout'))
                ->price(100)
                ->media('m1', 1)
                ->media('m2', 2)
                ->media('m3', 3)
                

        parent::setUp();
        $this->handler = $this->getContainer()->get(CleanupProductKeywordDictionaryTaskHandler::class);
    }

    public function testCleanup(): void
    {
        $now = new \DateTimeImmutable();
        $ids = new IdsCollection();
        $context = Context::createDefaultContext();

        $product = (new ProductBuilder($ids, 'test'))->price(100)->build();

        $this->getContainer()->get('product.repository')->create([$product]$context);

        $this->getContainer()->get(Connection::class)->executeStatement('DELETE FROM product_keyword_dictionary');

        $productId = Uuid::fromHexToBytes($product['id']);
        $this->createProductSearchKeyword('test 1', $productId$now$context);
        $this->createProductSearchKeyword('test 2', $productId$now$context);

        $this->createProductKeywordDictionary('test 1');
        $this->createProductKeywordDictionary('test 2');
        
foreach ($fields as $field => $value) {
            $this->connection->executeStatement(
                'UPDATE product_search_config_field SET ranking = ? WHERE product_search_config_id = (SELECT id FROM product_search_config WHERE language_id = ?) and field = ?',
                [$value, Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM)$field]
            );
        }
    }

    private function createData(TestDataCollection $ids): void
    {
        $products = [
            (new ProductBuilder($ids, 'product-1'))
                ->name('Aerodynamic Leather DotCondom')
                ->tax('t1')
                ->price(50, 50)
                ->category('Shoes')
                ->build(),
            (new ProductBuilder($ids, 'product-2'))
                ->name('Aerodynamic Leather Portaline')
                ->price(50, 50)
                ->build(),
            (new ProductBuilder($ids, 'product-3'))
                ->name('Aerodynamic Leather Wordlobster')
                
protected function setUp(): void
    {
        parent::setUp();
        $this->service = $this->getContainer()->get(SyncService::class);
        $this->connection = $this->getContainer()->get(Connection::class);
    }

    public function testDeleteProductMediaAndUpdateProduct(): void
    {
        $ids = new IdsCollection();
        $product = (new ProductBuilder($ids, 'p1'))
            ->price(100)
            ->media('media-1')
            ->media('media-2')
            ->media('media-3')
            ->build();

        $this->getContainer()->get('product.repository')
            ->create([$product], Context::createDefaultContext());

        $operations = [
            new SyncOperation('delete-media', 'product_media', 'delete', [['id' => $ids->get('media-2')]]),
            
static::assertFalse($customer->getGuest());
    }

    /** * @param array<mixed> $customer */
    private function createOrderForCustomer(array $customer): string
    {
        $productRepository = $this->getContainer()->get('product.repository');
        $orderRepository = $this->getContainer()->get('order.repository');

        $product = (new ProductBuilder($this->ids, 'Product-1'))
            ->price(10)
            ->build();

        $productRepository->create([$product], Context::createDefaultContext());

        $orderId = Uuid::randomHex();

        $order = [
            'id' => $orderId,
            'orderDateTime' => (new \DateTimeImmutable())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
            'itemRounding' => json_decode(json_encode(new CashRoundingConfig(2, 0.01, true), \JSON_THROW_ON_ERROR), true, 512, \JSON_THROW_ON_ERROR),
            

class ProductPricingHookTest extends TestCase
{
    use IntegrationTestBehaviour;

    public function testScripts(): void
    {
        $ids = new IdsCollection();

        $products = [
            (new ProductBuilder($ids, 'p1'))
                ->visibility()
                ->price(100)
                ->build(),
            (new ProductBuilder($ids, 'p2'))
                ->price(100)
                ->visibility()
                ->prices('rule-A', 50)
                ->prices('rule-A', 30, 'default', null, 10)
                ->prices('rule-A', 15, 'default', null, 20)
                ->build(),
            (new ProductBuilder($ids, 'p3'))
                
$parameters = array_merge($defaults$parameters);

        $this->manufacturerRepository->create([$parameters], Context::createDefaultContext());

        return $id;
    }

    private function createProduct(): void
    {
        $products = [
            (new ProductBuilder($this->ids, 'product1-with-category', 10))
                ->categories(['category1', 'category2'])
                ->visibility()
                ->price(10)
                ->build(),
            (new ProductBuilder($this->ids, 'product2-with-category', 12))
                ->category('category2')
                ->visibility()
                ->price(20)
                ->build(),
            (new ProductBuilder($this->ids, 'product1-without-category', 14))
                ->visibility()
                
static::assertCount(3, $content['elements']);
        $names = array_column($content['elements'], 'name');
        static::assertContains('v.1.1', $names);
        static::assertContains('v.1.2', $names);
        static::assertNotContains('v.2.1', $names);
        static::assertNotContains('v.2.2', $names);
    }

    private function prepareTestData(): void
    {
        $products = [
            (new ProductBuilder($this->ids, 'p.1'))
                ->price(900)
                ->property('rot', 'Farbe')
                ->property('grĂ¼n', 'Farbe')
                ->visibility(TestDefaults::SALES_CHANNEL)
                ->prices('rule-a', 220, 'default', null, 3, true)
                ->variant(
                    (new ProductBuilder($this->ids, 'v.1.1'))
                        ->build()
                )
                ->variant(
                    (new ProductBuilder($this->ids, 'v.1.2'))
                        


        static::expectException(ApiProtectionException::class);
        $facade->search('product', $criteria);
    }

    private function createProducts(): void
    {
        $taxId = $this->getExistingTaxId();
        $this->ids->set('t1', $taxId);

        $product1 = (new ProductBuilder($this->ids, 'p1'))
            ->price(100)
            ->visibility()
            ->manufacturer('m1')
            ->variant(
                (new ProductBuilder($this->ids, 'v1.1'))
                    ->build()
            );

        $product2 = (new ProductBuilder($this->ids, 'p2'))
            ->price(200)
            ->visibility()
            
/** * @internal */
class BasicOrderProductTest extends TestCase
{
    use IntegrationTestBehaviour;
    use TestShortHands;

    public function testBasicOrderFlow(): void
    {
        $product = (new ProductBuilder(new IdsCollection(), 'p1'))
            ->stock(100)
            ->price(100)
            ->visibility();

        // the product builder has a helper function to write the product values to the database, including all dependencies (rules, currencies, properties, etc)         $product->write($this->getContainer());

        $context = $this->getContext();
        $context = $this->login($context);

        // now we test that the product can be added to a customers cart
Home | Imprint | This part of the site doesn't use cookies.