createVersion example

if ($versionName !== null && !ctype_alnum($versionName)) {
            throw ApiException::invalidVersionName();
        }

        try {
            $entityDefinition = $this->definitionRegistry->getByEntityName($entity);
        } catch (DefinitionNotFoundException $e) {
            throw ApiException::definitionNotFound($e);
        }

        $versionId = $context->scope(Context::CRUD_API_SCOPE, fn (Context $context): string => $this->definitionRegistry->getRepository($entityDefinition->getEntityName())->createVersion($id$context$versionName$versionId));

        return new JsonResponse([
            'versionId' => $versionId,
            'versionName' => $versionName,
            'id' => $id,
            'entity' => $entity,
        ]);
    }

    #[Route(path: '/api/_action/version/merge/{entity}/{versionId}', name: 'api.mergeVersion', methods: ['POST'], requirements: ['version' => '\d+', 'entity' => '[a-zA-Z-]+', 'versionId' => '[0-9a-f]{32}'])]     public function mergeVersion(Context $context, string $entity, string $versionId): JsonResponse
    {
$listener->replaceArgument(0, $config['formats']);
        }
    }

    private function registerAssetsConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader): void
    {
        $loader->load('assets.php');

        if ($config['version_strategy']) {
            $defaultVersion = new Reference($config['version_strategy']);
        } else {
            $defaultVersion = $this->createVersion($container$config['version']$config['version_format']$config['json_manifest_path'], '_default', $config['strict_mode']);
        }

        $defaultPackage = $this->createPackageDefinition($config['base_path']$config['base_urls']$defaultVersion);
        $container->setDefinition('assets._default_package', $defaultPackage);

        foreach ($config['packages'] as $name => $package) {
            if (null !== $package['version_strategy']) {
                $version = new Reference($package['version_strategy']);
            } elseif (!\array_key_exists('version', $package) && null === $package['json_manifest_path']) {
                // if neither version nor json_manifest_path are specified, use the default                 $version = $defaultVersion;
            }
'en-GB', 'de-DE',
    ];

    public function testTranslationsAreAllSelectable(): void
    {
        $enContext = Context::createDefaultContext();
        $manufacturerId = Uuid::randomHex();
        $manufacturerRepository = $this->getContainer()->get('product_manufacturer.repository');

        $this->createManufacturer($manufacturerRepository$manufacturerId$enContext);

        $versionId = $manufacturerRepository->createVersion($manufacturerId$enContext);
        $enVersionContext = $enContext->createWithVersionId($versionId);
        $deContext = $this->createDeContext($enContext);
        $deVersionContext = $deContext->createWithVersionId($versionId);

        $manufacturerRepository->update([[
            'id' => $manufacturerId,
            'name' => 'version-en-GB',
        ]]$enVersionContext);

        $manufacturerRepository->update([[
            'id' => $manufacturerId,
            


        /** @var EntityRepository $priceRepository */
        $priceRepository = $this->getContainer()->get('product_price.repository');

        $event = $priceRepository->create([$price]$context);
        $productEvent = $event->getEventByEntityName('product');

        static::assertInstanceOf(EntityWrittenEvent::class$productEvent);
        static::assertEquals([$id]$productEvent->getIds());

        $versionId = $this->productRepository->createVersion($id$context);
        $version = $context->createWithVersionId($versionId);

        $priceRepository->delete([['id' => $id]]$version);

        $commits = $this->getCommits('product', $id$versionId);
        static::assertCount(1, $commits);

        /** @var EntityRepository $mappingRepository */
        $mappingRepository = $this->getContainer()->get('product_category.repository');

        $event = $mappingRepository->delete([['productId' => $id, 'categoryId' => $categoryId]]$version);

        
$context = Context::createDefaultContext();

        $product = $this->productRepository->search(new Criteria([$id])$context)->get($id);

        static::assertInstanceOf(ProductEntity::class$product);
        $this->assertStock(5, $product);
        static::assertSame(0, $product->getSales());

        $orderId = $this->orderProduct($id, 1);

        $this->getContainer()->get('order.repository')
            ->createVersion($orderId$context);

        $this->getContainer()->get('order.repository')
            ->createVersion($orderId$context);

        $count = $this->getContainer()
            ->get(Connection::class)
            ->fetchOne('SELECT COUNT(id) FROM `order` WHERE id = :id', ['id' => Uuid::fromHexToBytes($orderId)]);

        static::assertEquals(3, $count);

        $product = $this->productRepository->search(new Criteria([$id])$context)->get($id);

        


    public function testCustomFieldOrderVersioning(): void
    {
        $id = Uuid::randomHex();
        $versionId = $this->context->getVersionId();

        $order = $this->getOrderFixture($id$versionId);

        // create order + order version and belonging context         $this->orderRepository->create([$order]$this->context);
        $versionedOrderId = $this->orderRepository->createVersion($id$this->context);
        $versionedContext = $this->context->createWithVersionId($versionedOrderId);

        /** @var OrderEntity $order */
        $order = $this->orderRepository->search(new Criteria([$id])$this->context)->first();

        /** @var OrderEntity $versionedOrder */
        $versionedOrder = $this->orderRepository->search(new Criteria([$id])$versionedContext)->first();

        // custom fields should be correctly copied from original order to versioned order         static::assertEquals($order->getCustomFields()$versionedOrder->getCustomFields());
    }

    
$context = Context::createDefaultContext();

        $product = $this->productRepository->search(new Criteria([$id])$context)->get($id);

        static::assertInstanceOf(ProductEntity::class$product);
        static::assertSame(5, $product->getStock());
        static::assertSame(0, $product->getSales());

        $orderId = $this->orderProduct($id, 1);

        $this->getContainer()->get('order.repository')
            ->createVersion($orderId$context);

        $this->getContainer()->get('order.repository')
            ->createVersion($orderId$context);

        $count = $this->getContainer()
            ->get(Connection::class)
            ->fetchOne('SELECT COUNT(id) FROM `order` WHERE id = :id', ['id' => Uuid::fromHexToBytes($orderId)]);

        static::assertEquals(3, $count);

        $product = $this->productRepository->search(new Criteria([$id])$context)->get($id);

        
$this->createMock(EntityReaderInterface::class),
            $this->createMock(VersionManager::class),
            $this->createMock(EntitySearcherInterface::class),
            $this->createMock(EntityAggregatorInterface::class),
            new EventDispatcher(),
            $this->createMock(EntityLoadedEventFactory::class),
        );

        static::expectException(\RuntimeException::class);
        static::expectExceptionMessage('Entity is not version aware');

        $repo->createVersion('test', Context::createDefaultContext());
    }

    public function testCreateVersionVersionAware(): void
    {
        $versionManager = $this->createMock(VersionManager::class);
        $versionManager->expects(static::once())->method('createVersion');
        $definition = $this->createMock(EntityDefinition::class);
        $definition->method('isVersionAware')->willReturn(true);

        $repo = new EntityRepository(
            $definition,
            
public function testContextScopeAvailableDuringMerge(): void
    {
        $ids = new IdsCollection();

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

        $context = Context::createDefaultContext();

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

        $versionId = $this->getContainer()->get('product.repository')
            ->createVersion($ids->get('p1')$context);

        $versionContext = $context->createWithVersionId($versionId);

        $this->getContainer()->get('product.repository')
            ->update([['id' => $ids->get('p1'), 'name' => 'test']]$versionContext);

        // now ensure that we get a validate event for the merge request         $called = false;

        $this->addEventListener(
            $this->getContainer()->get('event_dispatcher'),
            


    public function testCleanup(): void
    {
        $this->getContainer()->get(Connection::class)->executeStatement('DELETE FROM version');
        $this->getContainer()->get(Connection::class)->executeStatement('DELETE FROM version_commit');

        $ids = new IdsCollection();

        $date = new \DateTime();

        $this->createVersion($ids->create('version-1')$date);

        $date->modify(sprintf('-%d day', 31));
        $this->createVersion($ids->create('version-2')$date);

        $this->handler->run();

        $versions = $this->getContainer()->get(Connection::class)->fetchFirstColumn('SELECT LOWER(HEX(id)) FROM version');
        static::assertCount(1, $versions);
        static::assertContains($ids->get('version-1')$versions);

        $commits = $this->getContainer()->get(Connection::class)->fetchFirstColumn('SELECT LOWER(HEX(id)) FROM version_commit');
        
return $event;
    }

    public function createVersion(string $id, Context $context, ?string $name = null, ?string $versionId = null): string
    {
        ReplicaConnection::ensurePrimary();

        if (!$this->definition->isVersionAware()) {
            throw new \RuntimeException(sprintf('Entity %s is not version aware', $this->definition->getEntityName()));
        }

        return $this->versionManager->createVersion($this->definition, $id, WriteContext::createFromContext($context)$name$versionId);
    }

    public function merge(string $versionId, Context $context): void
    {
        ReplicaConnection::ensurePrimary();

        if (!$this->definition->isVersionAware()) {
            throw new \RuntimeException(sprintf('Entity %s is not version aware', $this->definition->getEntityName()));
        }
        $this->versionManager->merge($versionId, WriteContext::createFromContext($context));
    }

    

        $repository = $this->getContainer()->get('cms_page.repository');
        $context = Context::createDefaultContext();
        $fixture = $this->getCmsPageFixture();
        $initialCount = $repository->search(new Criteria()$context)->count();

        $result = $repository->create($fixture$context);
        static::assertSame($initialCount + 1, $repository->search(new Criteria()$context)->count());

        static::assertEmpty($result->getErrors());

        $versionId = $repository->createVersion($fixture[0]['id']$context, 'DRAFT');
        static::assertIsString($versionId);
        static::assertSame($initialCount + 1, $repository->search(new Criteria()$context)->count());
    }

    public static function assertContainsInstanceOf(string $className, iterable $collection): void
    {
        foreach ($collection as $item) {
            if ($item instanceof $className) {
                return;
            }
        }

        
'id' => $this->ids->get('p3')],
                ],
            ],
        ];

        $this->getContainer()->get('tag.repository')->create($tags$context);

        $order = $this->getOrderFixture($this->ids->get('o1')$context->getVersionId());

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

        $versionId = $this->getContainer()->get('order.repository')->createVersion(
            $this->ids->get('o1'),
            $context,
            Uuid::randomHex(),
            Uuid::randomHex()
        );

        $versionContext = Context::createDefaultContext()->createWithVersionId($versionId);

        $orders = [
            [
                'id' => $this->ids->get('o1'),
                
static::assertNull($categories->get($categoryA)->getPath());
        static::assertEquals("|{$categoryA}|", $categories->get($categoryB)->getPath());
        static::assertEquals("|{$categoryA}|", $categories->get($categoryC)->getPath());
        static::assertEquals("|{$categoryA}|{$categoryC}|", $categories->get($categoryD)->getPath());

        static::assertEquals(1, $categories->get($categoryA)->getLevel());
        static::assertEquals(2, $categories->get($categoryB)->getLevel());
        static::assertEquals(2, $categories->get($categoryC)->getLevel());
        static::assertEquals(3, $categories->get($categoryD)->getLevel());

        $versionId = $this->categoryRepository->createVersion($categoryD$this->context);
        $versionContext = $this->context->createWithVersionId($versionId);

        /** @var CategoryEntity $category */
        $category = $this->categoryRepository->search(new Criteria([$categoryD])$versionContext)->first();
        static::assertInstanceOf(CategoryEntity::class$category);
        static::assertEquals('|' . $categoryA . '|' . $categoryC . '|', $category->getPath());

        // update parent of last category in version scope         $updated = ['id' => $categoryD, 'parentId' => $categoryA];

        $this->categoryRepository->update([$updated]$versionContext);

        
'name' => 'root 1',
            'subCascade' => [
                'id' => $id,
                'name' => 'sub 1',
            ],
        ];

        $context = Context::createDefaultContext();

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

        $versionId = $this->repository->createVersion($id$context);

        $versionContext = $context->createWithVersionId($versionId);

        $this->repository->update([
            [
                'id' => $id,
                'name' => 'updated root',
                'subCascade' => [
                    'id' => $id,
                    'name' => 'updated sub',
                ],
            ],
Home | Imprint | This part of the site doesn't use cookies.