ExportBehavior example

$this->context = Context::createDefaultContext();
    }

    public function testExport(): void
    {
        $productExportId = $this->createTestEntity();

        $criteria = $this->createProductExportCriteria($productExportId);

        $productExport = $this->repository->search($criteria$this->context)->first();

        $exportResult = $this->service->generate($productExportnew ExportBehavior());

        static::assertInstanceOf(ProductExportResult::class$exportResult);
        static::assertStringEqualsFile(__DIR__ . '/fixtures/test-export.csv', $exportResult->getContent());
    }

    public function testProductExportGenerationEvents(): void
    {
        $productExportId = $this->createTestEntity();

        $criteria = $this->createProductExportCriteria($productExportId);

        
ProductExportEntity $productExport,
        int $offset,
        Context $context
    ): ?ProductExportResult {
        $this->productExportRepository->update([[
            'id' => $productExport->getId(),
            'isRunning' => true,
        ]]$context);

        return $this->productExportGenerator->generate(
            $productExport,
            new ExportBehavior(
                false,
                false,
                true,
                false,
                false,
                $offset
            )
        );
    }

    private function finalizeExport(ProductExportEntity $productExport, string $filePath): void
    {
$this->context = Context::createDefaultContext();
        $this->fileSystem = $this->getContainer()->get('shopware.filesystem.private');

        $salesChannelContextFactory = $this->getContainer()->get(SalesChannelContextFactory::class);
        $this->salesChannelContext = $salesChannelContextFactory->create(Uuid::randomHex()$this->getSalesChannelDomain()->getSalesChannelId());
    }

    public function testExport(): void
    {
        $this->createTestEntity();

        $this->service->export($this->salesChannelContext, new ExportBehavior());

        $filePath = sprintf('%s/Testexport.csv', $this->getContainer()->getParameter('product_export.directory'));
        $fileContent = $this->fileSystem->read($filePath);

        static::assertIsString($fileContent);
        $csvRows = explode(\PHP_EOL, $fileContent);

        static::assertTrue($this->fileSystem->directoryExists($this->getContainer()->getParameter('product_export.directory')));
        static::assertTrue($this->fileSystem->fileExists($filePath));
        static::assertCount(4, $csvRows);
    }

    
public function testValidXmlExport(): void
    {
        $productExportId = $this->createTestEntity();

        $criteria = new Criteria([$productExportId]);
        $criteria->addAssociation('salesChannelDomain.language');
        $criteria->addAssociation('salesChannel');

        /** @var ProductExportEntity $productExport */
        $productExport = $this->repository->search($criteria$this->context)->first();

        $exportResult = $this->service->generate($productExportnew ExportBehavior());

        static::assertFalse($exportResult->hasErrors());
    }

    public function testInvalidXmlExport(): void
    {
        $productExportId = $this->createTestEntity();

        $criteria = new Criteria([$productExportId]);
        $criteria->addAssociation('salesChannelDomain.language');
        $criteria->addAssociation('salesChannel');

        
$forceGeneration = $input->getOption('force');
        $includeInactive = $input->getOption('include-inactive');

        $salesChannelContext = $this->salesChannelContextFactory->create(Uuid::randomHex()$salesChannelId);

        if ($salesChannelContext->getSalesChannel()->getTypeId() !== Defaults::SALES_CHANNEL_TYPE_STOREFRONT) {
            throw new SalesChannelNotFoundException();
        }

        $this->productExportService->export(
            $salesChannelContext,
            new ExportBehavior($forceGeneration$includeInactive),
            $productExportId
        );

        return self::SUCCESS;
    }
}
$this->logException(Context::createDefaultContext()$exportNotFoundException, Level::Warning);

            throw $exportNotFoundException;
        }

        $context = $this->contextFactory->create('', $productExport->getSalesChannelDomain()->getSalesChannelId());

        $filePath = $this->productExportFileHandler->getFilePath($productExport);

        // if file not present or interval = live         if (!$this->fileSystem->fileExists($filePath) || $productExport->getInterval() === 0) {
            $this->productExportService->export($contextnew ExportBehavior()$productExport->getId());
        }

        if (!$this->fileSystem->fileExists($filePath)) {
            $exportNotGeneratedException = new ExportNotGeneratedException();
            $this->logException($context->getContext()$exportNotGeneratedException);

            throw $exportNotGeneratedException;
        }

        $content = $this->fileSystem->read($filePath);
        $contentType = $this->getContentType($productExport->getFileFormat());
        
private function generateExportPreview(RequestDataBag $dataBag, Context $context): ?ProductExportResult
    {
        $salesChannelDomain = $this->getSalesChannelDomain($dataBag->get('salesChannelDomainId')$context);
        $salesChannel = $this->getSalesChannel($dataBag->get('salesChannelId')$context);

        $productExportEntity = $this->createEntity($dataBag);
        $productExportEntity->setSalesChannelDomain($salesChannelDomain);
        $productExportEntity->setStorefrontSalesChannelId($salesChannelDomain->getSalesChannelId());
        $productExportEntity->setSalesChannel($salesChannel);

        $exportBehavior = new ExportBehavior(true, true, true);

        return $this->productExportGenerator->generate($productExportEntity$exportBehavior);
    }

    private function getSalesChannelDomain(string $salesChannelDomainId, Context $context): SalesChannelDomainEntity
    {
        $criteria = (new Criteria([$salesChannelDomainId]))
            ->addAssociation('language.locale')
            ->addAssociation('salesChannel');
        $salesChannelDomain = $this->salesChannelDomainRepository->search(
            $criteria,
            
Home | Imprint | This part of the site doesn't use cookies.