getBytes example

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

        
public static function getBytes(int $length): string
    {
        if ($length <= 0) {
            throw new \DomainException('Length should be >= 1');
        }

        return random_bytes($length);
    }

    public static function getBoolean(): bool
    {
        $byte = static::getBytes(1);

        return (bool) (\ord($byte) % 2);
    }

    public static function getInteger(int $min, int $max): int
    {
        if ($min > $max) {
            throw new \DomainException(
                'The min parameter must be lower than max parameter'
            );
        }

        
->get(Connection::class)
            ->executeStatement('DROP TABLE IF EXISTS `test_tree`, `test_tree_without_version`');

        $this->startTransactionBefore();
    }

    public function testTreeUpdate(): void
    {
        $connection = $this->getContainer()->get(Connection::class);
        $ids = new TestDataCollection();
        $data = [
            ['id' => $ids->getBytes('r')],
            ['id' => $ids->getBytes('a'), 'parent_id' => $ids->getBytes('r')],
            ['id' => $ids->getBytes('aa'), 'parent_id' => $ids->getBytes('a')],
            ['id' => $ids->getBytes('ab'), 'parent_id' => $ids->getBytes('a')],
            ['id' => $ids->getBytes('b'), 'parent_id' => $ids->getBytes('r')],
        ];
        foreach ($data as $row) {
            $row['created_at'] = (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT);
            $row['version_id'] = Uuid::fromHexToBytes(Defaults::LIVE_VERSION);

            if (isset($row['parent_id'])) {
                $row['parent_version_id'] = Uuid::fromHexToBytes(Defaults::LIVE_VERSION);
            }
$products = [
            (new ProductBuilder($ids, 'parent'))
                ->price(100)
                ->variant((new ProductBuilder($ids, 'variant-1'))->price(200)->build())
                ->variant((new ProductBuilder($ids, 'variant-2'))->price(200)->build())
                ->build(),
        ];

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

        $count = $this->connection->fetchOne('SELECT child_count FROM product WHERE id = :id', ['id' => $ids->getBytes('parent')]);
        static::assertEquals(2, $count);

        $this->getContainer()->get('product.repository')->delete([['id' => $ids->get('variant-1')]], Context::createDefaultContext());
        $count = $this->connection->fetchOne('SELECT child_count FROM product WHERE id = :id', ['id' => $ids->getBytes('parent')]);
        static::assertEquals(1, $count);

        $this->getContainer()->get('product.repository')->delete([['id' => $ids->get('variant-2')]], Context::createDefaultContext());
        $count = $this->connection->fetchOne('SELECT child_count FROM product WHERE id = :id', ['id' => $ids->getBytes('parent')]);
        static::assertEquals(0, $count);
    }

    
public function testBeforeWriteOnlyReactsToLiveVersions(): void
    {
        $context = Context::createDefaultContext()->createWithVersionId($this->ids->create('version'));

        $subscriber = new AvailableStockMirrorSubscriber();

        $definition = $this->getDefinition();

        $command = new UpdateCommand(
            $definition,
            ['stock' => 10],
            ['id' => $this->ids->getBytes('product-1')],
            new EntityExistence(
                ProductDefinition::ENTITY_NAME,
                ['id' => $this->ids->get('product-1')],
                true,
                false,
                false,
                []
            ),
            '/0'
        );

        
 false);

        $this->getContainer()->get('product.repository')->clone(
            $ids->get('child'),
            Context::createDefaultContext(),
            $ids->get('new-child'),
            $behavior
        );

        $translations = $this->getContainer()->get(Connection::class)->fetchAssociative(
            'SELECT name, description FROM product_translation WHERE language_id = :language AND product_id = :id',
            ['language' => $ids->getBytes('language'), 'id' => $ids->getBytes('new-child')]
        );
        static::assertIsArray($translations);

        static::assertIsArray($translations);
        static::assertNull($translations['name']);
        static::assertEquals('update', $translations['description']);
    }

    protected function createWriteContext(): WriteContext
    {
        return WriteContext::createFromContext(Context::createDefaultContext());
    }
$context = Context::createDefaultContext();
        $writeContext = WriteContext::createFromContext($context);

        $registry = new StaticDefinitionInstanceRegistry(
            [new ProductDefinition()new MediaDefinition()],
            $this->createMock(ValidatorInterface::class),
            $this->createMock(EntityWriteGatewayInterface::class)
        );

        $productDelete = new DeleteCommand(
            $registry->get(ProductDefinition::class),
            ['id' => $ids->getBytes('p1')],
            new EntityExistence('product', ['id' => $ids->getBytes('p1')], true, true, true, [])
        );

        $mediaDelete = new DeleteCommand(
            $registry->get(MediaDefinition::class),
            ['id' => $ids->getBytes('m1')],
            new EntityExistence('media', ['id' => $ids->getBytes('m1')], true, true, true, [])
        );

        $event = EntityWriteEvent::create($writeContext[
            $productDelete,
            
$cheapestPriceQuery = $connection->prepare('UPDATE product SET cheapest_price = :price WHERE id = :id AND version_id = :version');

        /** @var string $prices */
        $prices = file_get_contents(__DIR__ . '/_fixtures/serialized_prices.json');
        foreach ($ids->all() as $key => $id) {
            $prices = str_replace(sprintf('__id_placeholder_%s__', $key)$id$prices);
        }
        foreach (\json_decode($prices, true, 512, \JSON_THROW_ON_ERROR) as $productName => $serializedPrice) {
            $cheapestPriceQuery->executeStatement([
                'price' => $serializedPrice,
                'id' => $ids->getBytes($productName),
                'version' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION),
            ]);
        }

        try {
            $cases = $this->calculationProvider($ids);

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

            $currency = $this->getContainer()->get(SalesChannelContextFactory::class)
                
'stateId' => $this->getStateId(),
            'currencyFactor' => 1,
            'orderDateTime' => new \DateTime(),
            'price' => json_decode(json_encode(new CartPrice(1, 1, 1, new CalculatedTaxCollection()new TaxRuleCollection(), CartPrice::TAX_STATE_FREE), \JSON_THROW_ON_ERROR), true, 512, \JSON_THROW_ON_ERROR),
            'shippingCosts' => json_decode(json_encode(new CalculatedPrice(1, 1, new CalculatedTaxCollection()new TaxRuleCollection()), \JSON_THROW_ON_ERROR), true, 512, \JSON_THROW_ON_ERROR),
        ];

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

        $this->getContainer()->get(Connection::class)
            ->executeStatement('UPDATE `order` SET total_rounding = NULL WHERE id = :id', ['id' => $ids->getBytes('order')]);

        $rounding = $this->getContainer()->get(Connection::class)
            ->fetchOne('SELECT total_rounding FROM `order` WHERE id = :id', ['id' => $ids->getBytes('order')]);

        static::assertNull($rounding);

        $migration = new Migration1618900427FixTotalRounding();
        $migration->update($this->getContainer()->get(Connection::class));

        $rounding = $this->getContainer()->get(Connection::class)
            ->fetchOne('SELECT total_rounding FROM `order` WHERE id = :id', ['id' => $ids->getBytes('order')]);

        
throw $e;
        }
    }

    /** * @depends testIndexing */
    public function testLanguageFieldsWorkSimilarToDAL(IdsCollection $ids): void
    {
        $context = $this->createIndexingContext();

        $dal1 = $ids->getBytes('dal-1');

        // Fetch: Default language         $esProducts = $this->definition->fetch([$dal1]$context);

        $esProduct = $esProducts[$ids->get('dal-1')];

        $criteria = new Criteria([$ids->get('dal-1')]);
        $dalProduct = $this->productRepository->search($criteria$context)->first();

        static::assertInstanceOf(ProductEntity::class$dalProduct);
        if (Feature::isActive('ES_MULTILINGUAL_INDEX')) {
            
$product = (new ProductBuilder($ids, 'p1'))
            ->price(100)
            ->manufacturer('m1')
            ->build();

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

        $connection = $this->getContainer()->get(Connection::class);

        $value = $connection->fetchOne('SELECT LOWER(HEX(product_manufacturer_version_id)) FROM product WHERE id = :id', ['id' => $ids->getBytes('p1')]);
        static::assertEquals(Defaults::LIVE_VERSION, $value);

        $connection->executeStatement('UPDATE product SET product_manufacturer_version_id = NULL WHERE id = :id', ['id' => $ids->getBytes('p1')]);

        $value = $connection->fetchOne('SELECT LOWER(HEX(product_manufacturer_version_id)) FROM product WHERE id = :id', ['id' => $ids->getBytes('p1')]);
        static::assertNull($value);

        $update = [
            'id' => $ids->get('p1'),
            'manufacturerId' => $ids->get('m1'),
        ];

        
$stockStorage,
            true
        );

        $definition = $this->getDefinition(ProductDefinition::class);

        $event = EntityWriteEvent::create(
            WriteContext::createFromContext($context),
            [
                new DeleteCommand(
                    $definition,
                    ['id' => $this->ids->getBytes('item-1')],
                    new EntityExistence(
                        OrderLineItemDefinition::ENTITY_NAME,
                        ['id' => $this->ids->get('item-1')],
                        true,
                        false,
                        false,
                        []
                    ),
                ),
            ],
        );

        
$context = Context::createDefaultContext();
        $writeContext = WriteContext::createFromContext($context);

        $registry = new StaticDefinitionInstanceRegistry(
            [new ProductDefinition()new MediaDefinition()],
            $this->createMock(ValidatorInterface::class),
            $this->createMock(EntityWriteGatewayInterface::class)
        );

        $productDelete = new DeleteCommand(
            $registry->get(ProductDefinition::class),
            ['id' => $ids->getBytes('p1')],
            new EntityExistence('product', ['id' => $ids->getBytes('p1')], true, true, true, [])
        );

        $mediaDelete = new DeleteCommand(
            $registry->get(MediaDefinition::class),
            ['id' => $ids->getBytes('m1')],
            new EntityExistence('media', ['id' => $ids->getBytes('m1')], true, true, true, [])
        );

        $event = EntityDeleteEvent::create($writeContext[
            $productDelete,
            

            'parent-and-child' => [
                'invalidate' => ['p1', 'p2'],
                'expected' => ['p1', 'p2'],
            ],
            'multiple-child-same-parent' => [
                'invalidate' => ['p1', 'p2', 'p3'],
                'expected' => ['p1', 'p2', 'p3'],
            ],
        ];

        $this->createProduct($this->ids->getBytes('p1'));
        $this->createProduct($this->ids->getBytes('p2')$this->ids->getBytes('p1'));
        $this->createProduct($this->ids->getBytes('p3')$this->ids->getBytes('p1'));
        $this->createProduct($this->ids->getBytes('p4'));

        /** @var EventDispatcherInterface $eventDispatcher */
        $eventDispatcher = static::getContainer()->get('event_dispatcher');

        $listener = new class() {
            /** * @param array<string> $tags */
            
return random_bytes($length);
    }

    /** * Generate random boolean * * @return bool */
    public static function getBoolean()
    {
        $byte = static::getBytes(1);

        return (bool) (\ord($byte) % 2);
    }

    /** * Generate a random integer between $min and $max inclusive * * @param int $min * @param int $max * * @throws DomainException * * @return int */
Home | Imprint | This part of the site doesn't use cookies.