Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/DependencyInjection/shopware.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
<argument type="service" id="SwagMigrationAssistant\Migration\Media\MediaFileService"/>
<argument type="service" id="SwagMigrationAssistant\Migration\Mapping\Lookup\MediaDefaultFolderLookup"/>
<argument type="service" id="SwagMigrationAssistant\Migration\Mapping\Lookup\DocumentTypeLookup"/>
<argument type="service" id="SwagMigrationAssistant\Migration\Mapping\Lookup\GlobalDocumentBaseConfigLookup"/>
</service>

<service id="SwagMigrationAssistant\Profile\Shopware\Converter\CustomerGroupAttributeConverter"
Expand Down
25 changes: 25 additions & 0 deletions src/Migration/Mapping/Lookup/GlobalDocumentBaseConfigLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class GlobalDocumentBaseConfigLookup implements ResetInterface
*/
private array $cache = [];

/**
* @var array<string, array<string, mixed>|null>
*/
private array $configCache = [];

/**
* @param EntityRepository<DocumentBaseConfigCollection> $documentBaseConfigRepository
*
Expand Down Expand Up @@ -51,8 +56,28 @@ public function get(string $documentTypeId, Context $context): ?string
return $baseConfigId;
}

/**
* @return array<string, mixed>|null
*/
public function getBaseConfig(string $baseConfigId, Context $context): ?array
{
if (\array_key_exists($baseConfigId, $this->configCache)) {
return $this->configCache[$baseConfigId];
}

$criteria = new Criteria([$baseConfigId]);

$baseConfig = $this->documentBaseConfigRepository->search($criteria, $context)->first();

$config = $baseConfig?->getConfig();
$this->configCache[$baseConfigId] = $config;

return $config;
}

public function reset(): void
{
$this->cache = [];
$this->configCache = [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public function process(
if ($currentDataSet === null) {
try {
$currentDataSet = $this->dataSetRegistry->getDataSet($migrationContext, $mediaFile['entity']);
$migrationContext->setDataSet($currentDataSet);
} catch (DataSetNotFoundException $e) {
$this->logDataSetNotFoundException($migrationContext, $mediaFile);

Expand Down
2 changes: 2 additions & 0 deletions src/Migration/MigrationContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ public function getLimit(): int;
public function getGateway(): GatewayInterface;

public function setGateway(GatewayInterface $gateway): void;

public function setDataSet(DataSet $dataSet): void;
}
35 changes: 30 additions & 5 deletions src/Profile/Shopware/Converter/OrderDocumentConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use SwagMigrationAssistant\Migration\Logging\Log\EmptyNecessaryFieldRunLog;
use SwagMigrationAssistant\Migration\Logging\LoggingServiceInterface;
use SwagMigrationAssistant\Migration\Mapping\Lookup\DocumentTypeLookup;
use SwagMigrationAssistant\Migration\Mapping\Lookup\GlobalDocumentBaseConfigLookup;
use SwagMigrationAssistant\Migration\Mapping\Lookup\MediaDefaultFolderLookup;
use SwagMigrationAssistant\Migration\Mapping\MappingServiceInterface;
use SwagMigrationAssistant\Migration\Media\MediaFileServiceInterface;
Expand All @@ -45,6 +46,7 @@ public function __construct(
protected MediaFileServiceInterface $mediaFileService,
protected readonly MediaDefaultFolderLookup $mediaFolderLookup,
protected readonly DocumentTypeLookup $documentTypeLookup,
protected readonly GlobalDocumentBaseConfigLookup $globalDocumentBaseConfigLookup,
) {
parent::__construct($mappingService, $loggingService);
}
Expand Down Expand Up @@ -143,7 +145,16 @@ public function convert(array $data, Context $context, MigrationContextInterface
$converted['fileType'] = FileTypes::PDF;
$converted['static'] = true;
$converted['deepLinkCode'] = Random::getAlphanumericString(32);
$converted['config'] = [];
if (\array_key_exists('sent', $data)) {
$converted['sent'] = $data['sent'];
} else {
// In Shopware 5 "sent" not exists, so we force it to true, because we assume that if there is a document, the customer received it.
$converted['sent'] = true;
}

$documentType = $this->getDocumentType($data['documenttype']);
$converted['documentType'] = $documentType;
$converted['config'] = $this->getBaseDocumentTypeConfig($documentType['id'], $context);
if (isset($data['docID'])) {
$converted['config']['documentNumber'] = $data['docID'];

Expand All @@ -153,10 +164,6 @@ public function convert(array $data, Context $context, MigrationContextInterface

unset($data['docID']);
}

$documentType = $this->getDocumentType($data['documenttype']);

$converted['documentType'] = $documentType;
unset($data['documenttype']);

if (isset($data['attributes'])) {
Expand Down Expand Up @@ -223,6 +230,24 @@ protected function getDocumentType(array $data): array
return $documentType;
}

/**
* @return array<string, mixed>
*/
protected function getBaseDocumentTypeConfig(string $documentTypeId, Context $context): array
{
$documentConfigId = $this->globalDocumentBaseConfigLookup->get($documentTypeId, $context);
if ($documentConfigId === null) {
return [];
}

$documentConfig = $this->globalDocumentBaseConfigLookup->getBaseConfig($documentConfigId, $context);
if ($documentConfig === null) {
return [];
}

return $documentConfig;
}

/**
* @param array<mixed> $data
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,47 @@ public function testReset(): void
$cacheProperty = new \ReflectionProperty(GlobalDocumentBaseConfigLookup::class, 'cache');
$cacheProperty->setAccessible(true);

$configCacheProperty = new \ReflectionProperty(GlobalDocumentBaseConfigLookup::class, 'configCache');
$configCacheProperty->setAccessible(true);

static::assertNotEmpty($cacheProperty->getValue($globalDocumentBaseConfigLookup));
static::assertNotEmpty($configCacheProperty->getValue($globalDocumentBaseConfigLookup));

$globalDocumentBaseConfigLookup->reset();

static::assertEmpty($cacheProperty->getValue($globalDocumentBaseConfigLookup));
static::assertEmpty($configCacheProperty->getValue($globalDocumentBaseConfigLookup));
}

public function testGetBaseConfig(): void
{
$context = Context::createDefaultContext();

$data = self::getDatabaseData();
$documentTypeID = $data[0]['documentTypeId'];
static::assertIsString($documentTypeID);

$globalDocumentBaseConfigLookup = $this->getGlobalDocumentBaseConfigLookup();
$configId = $globalDocumentBaseConfigLookup->get($documentTypeID, $context);
static::assertIsString($configId);

$baseConfig = $globalDocumentBaseConfigLookup->getBaseConfig($configId, $context);

static::assertIsArray($baseConfig);
static::assertArrayHasKey('fileTypes', $baseConfig);
static::assertArrayHasKey('referencedDocumentType', $baseConfig);
static::assertSame('invoice', $baseConfig['referencedDocumentType']);
}

public function testGetBaseConfigFromCache(): void
{
$context = Context::createDefaultContext();

$globalDocumentBaseConfigLookup = $this->getMockedGlobalDocumentBaseConfigLookup();
$baseConfigResult = $globalDocumentBaseConfigLookup->getBaseConfig('anyConfigId', $context);

static::assertIsArray($baseConfigResult);
static::assertSame($this->getCachedConfig(), $baseConfigResult);
}

/**
Expand Down Expand Up @@ -107,6 +143,48 @@ private function getMockedGlobalDocumentBaseConfigLookup(): GlobalDocumentBaseCo

$reflectionProperty->setValue($globalDocumentBaseConfigLookup, $cache);

$configCache = ['anyConfigId' => $this->getCachedConfig()];
$reflectionProperty = new \ReflectionProperty(GlobalDocumentBaseConfigLookup::class, 'configCache');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($globalDocumentBaseConfigLookup, $configCache);

return $globalDocumentBaseConfigLookup;
}

/**
* @return array<string, mixed>
*/
private function getCachedConfig(): array
{
return [
'vatId' => 'anyVatId',
'bankBic' => 'anyBankBic',
'bankIban' => 'anyBankIban',
'bankName' => 'anyBankName',
'pageSize' => 'a4',
'fileTypes' => [
0 => 'html',
1 => 'pdf',
],
'taxNumber' => 'anyTaxNumber',
'taxOffice' => 'anyTaxOffice',
'companyName' => 'Example Company',
'itemsPerPage' => 10,
'displayFooter' => true,
'displayHeader' => true,
'displayPrices' => true,
'companyAddress' => 'anyAddress',
'pageOrientation' => 'portrait',
'displayLineItems' => true,
'displayPageCount' => true,
'executiveDirector' => 'anyExecutiveDirector',
'placeOfFulfillment' => 'anyPlaceOfFulfillment',
'placeOfJurisdiction' => 'anyPlaceOfJurisdiction',
'displayReturnAddress' => true,
'displayCompanyAddress' => true,
'diplayLineItemPosition' => true,
'referencedDocumentType' => 'anyReferencedDocumentType',
'displayAdditionalNoteDelivery' => false,
];
}
}
65 changes: 61 additions & 4 deletions tests/Profile/Shopware54/Converter/OrderDocumentConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use SwagMigrationAssistant\Migration\Logging\Log\DocumentTypeNotSupported;
use SwagMigrationAssistant\Migration\Logging\LoggingServiceInterface;
use SwagMigrationAssistant\Migration\Mapping\Lookup\DocumentTypeLookup;
use SwagMigrationAssistant\Migration\Mapping\Lookup\GlobalDocumentBaseConfigLookup;
use SwagMigrationAssistant\Migration\Mapping\Lookup\MediaDefaultFolderLookup;
use SwagMigrationAssistant\Migration\Mapping\MappingServiceInterface;
use SwagMigrationAssistant\Migration\MigrationContext;
Expand Down Expand Up @@ -63,7 +64,8 @@ protected function setUp(): void
$this->loggingService,
$mediaFileService,
$this->createMock(MediaDefaultFolderLookup::class),
$this->createMock(DocumentTypeLookup::class)
$this->createMock(DocumentTypeLookup::class),
$this->createDocumentBaseConfigLookupMock(),
);
$connectionId = Uuid::randomHex();
$this->runId = Uuid::randomHex();
Expand Down Expand Up @@ -145,10 +147,15 @@ public function testConvert(): void
static::assertArrayHasKey('documentType', $converted);
static::assertSame('pdf', $converted['fileType']);
static::assertTrue($converted['static']);
static::assertTrue($converted['sent']);
static::assertSame('Rechnung', $converted['documentType']['name']);
static::assertSame('invoice', $converted['documentType']['technicalName']);
static::assertSame($orderDocumentData[0]['docID'], $converted['config']['documentNumber']);
static::assertSame($orderDocumentData[0]['docID'], $converted['config']['custom']['invoiceNumber']);

$expectedConfig = $this->getDefaultConfig();
$expectedConfig['documentNumber'] = $orderDocumentData[0]['docID'];
$expectedConfig['custom']['invoiceNumber'] = $orderDocumentData[0]['docID'];

static::assertSame($expectedConfig, $converted['config']);
}

public function testConvertShouldLogUnknownType(): void
Expand Down Expand Up @@ -270,15 +277,65 @@ private function createDocumentConverter(string $converterClass, MappingServiceI
$loggingService = new DummyLoggingService();
}

$documentBaseConfigLookupMock = $this->createMock(GlobalDocumentBaseConfigLookup::class);
$documentBaseConfigLookupMock->method('getBaseConfig')->willReturn([]);

$instance = new $converterClass(
$mappingService,
$loggingService,
new DummyMediaFileService(),
$this->createMock(MediaDefaultFolderLookup::class),
$this->createMock(DocumentTypeLookup::class)
$this->createMock(DocumentTypeLookup::class),
$documentBaseConfigLookupMock,
);
static::assertInstanceOf(ShopwareConverter::class, $instance);

return $instance;
}

private function createDocumentBaseConfigLookupMock(): GlobalDocumentBaseConfigLookup
{
$documentBaseConfigLookupMock = $this->createMock(GlobalDocumentBaseConfigLookup::class);
$documentBaseConfigLookupMock->method('get')->willReturn(Uuid::randomHex());
$documentBaseConfigLookupMock->method('getBaseConfig')->willReturn($this->getDefaultConfig());

return $documentBaseConfigLookupMock;
}

/**
* @return array<string, mixed>
*/
private function getDefaultConfig(): array
{
return [
'vatId' => '',
'bankBic' => '',
'bankIban' => '',
'bankName' => '',
'pageSize' => 'a4',
'fileTypes' => [
'html',
'pdf',
],
'taxNumber' => '',
'taxOffice' => '',
'companyName' => 'Example Company',
'itemsPerPage' => 10,
'displayFooter' => true,
'displayHeader' => true,
'displayPrices' => true,
'companyAddress' => '',
'pageOrientation' => 'portrait',
'displayLineItems' => true,
'displayPageCount' => true,
'executiveDirector' => '',
'placeOfFulfillment' => '',
'placeOfJurisdiction' => '',
'displayReturnAddress' => true,
'displayCompanyAddress' => true,
'displayLineItemPosition' => true,
'referencedDocumentType' => 'invoice',
'displayAdditionalNoteDelivery' => false,
];
}
}
Loading