|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * (c) shopware AG <info@shopware.com> |
| 4 | + * For the full copyright and license information, please view the LICENSE |
| 5 | + * file that was distributed with this source code. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace SwagMigrationAssistant\Test\Profile\Shopware6\Converter; |
| 9 | + |
| 10 | +use Doctrine\DBAL\Connection; |
| 11 | +use Doctrine\DBAL\Schema\AbstractSchemaManager; |
| 12 | +use Doctrine\DBAL\Schema\Column; |
| 13 | +use Doctrine\DBAL\Types\StringType; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Shopware\Core\Content\Product\ProductDefinition; |
| 16 | +use Shopware\Core\Framework\Context; |
| 17 | +use Shopware\Core\Framework\Log\Package; |
| 18 | +use Shopware\Core\Framework\Uuid\Uuid; |
| 19 | +use SwagMigrationAssistant\Migration\Connection\SwagMigrationConnectionEntity; |
| 20 | +use SwagMigrationAssistant\Migration\MigrationContext; |
| 21 | +use SwagMigrationAssistant\Profile\Shopware6\Converter\ProductConverter; |
| 22 | +use SwagMigrationAssistant\Profile\Shopware6\DataSelection\DataSet\ProductDataSet; |
| 23 | +use SwagMigrationAssistant\Profile\Shopware6\Shopware6MajorProfile; |
| 24 | +use SwagMigrationAssistant\Test\Mock\Migration\Logging\DummyLoggingService; |
| 25 | +use SwagMigrationAssistant\Test\Mock\Migration\Mapping\Dummy6MappingService; |
| 26 | +use SwagMigrationAssistant\Test\Mock\Migration\Media\DummyMediaFileService; |
| 27 | + |
| 28 | +#[Package('fundamentals@after-sales')] |
| 29 | +class ProductConverterUnitTest extends TestCase |
| 30 | +{ |
| 31 | + public function testConvertStatesToTypeWithDownloadState(): void |
| 32 | + { |
| 33 | + $converter = $this->createConverterWithTypeColumn(true); |
| 34 | + $context = Context::createDefaultContext(); |
| 35 | + $migrationContext = $this->createMigrationContext(); |
| 36 | + |
| 37 | + $input = [ |
| 38 | + 'id' => Uuid::randomHex(), |
| 39 | + 'productNumber' => 'TEST-001', |
| 40 | + 'stock' => 10, |
| 41 | + 'states' => ['is-download'], |
| 42 | + 'name' => 'Digital Product', |
| 43 | + ]; |
| 44 | + |
| 45 | + $result = $converter->convert($input, $context, $migrationContext); |
| 46 | + $converted = $result->getConverted(); |
| 47 | + |
| 48 | + static::assertNotNull($converted); |
| 49 | + static::assertArrayHasKey('type', $converted); |
| 50 | + static::assertSame(ProductDefinition::TYPE_DIGITAL, $converted['type']); |
| 51 | + } |
| 52 | + |
| 53 | + public function testConvertStatesToTypeWithPhysicalState(): void |
| 54 | + { |
| 55 | + $converter = $this->createConverterWithTypeColumn(true); |
| 56 | + $context = Context::createDefaultContext(); |
| 57 | + $migrationContext = $this->createMigrationContext(); |
| 58 | + |
| 59 | + $input = [ |
| 60 | + 'id' => Uuid::randomHex(), |
| 61 | + 'productNumber' => 'TEST-002', |
| 62 | + 'stock' => 10, |
| 63 | + 'states' => ['is-physical'], |
| 64 | + 'name' => 'Physical Product', |
| 65 | + ]; |
| 66 | + |
| 67 | + $result = $converter->convert($input, $context, $migrationContext); |
| 68 | + $converted = $result->getConverted(); |
| 69 | + |
| 70 | + static::assertNotNull($converted); |
| 71 | + static::assertArrayHasKey('type', $converted); |
| 72 | + static::assertSame(ProductDefinition::TYPE_PHYSICAL, $converted['type']); |
| 73 | + } |
| 74 | + |
| 75 | + public function testConvertPreservesExistingType(): void |
| 76 | + { |
| 77 | + $converter = $this->createConverterWithTypeColumn(true); |
| 78 | + $context = Context::createDefaultContext(); |
| 79 | + $migrationContext = $this->createMigrationContext(); |
| 80 | + |
| 81 | + $input = [ |
| 82 | + 'id' => Uuid::randomHex(), |
| 83 | + 'productNumber' => 'TEST-003', |
| 84 | + 'stock' => 10, |
| 85 | + 'type' => 'digital', |
| 86 | + 'name' => 'Product With Type', |
| 87 | + ]; |
| 88 | + |
| 89 | + $result = $converter->convert($input, $context, $migrationContext); |
| 90 | + $converted = $result->getConverted(); |
| 91 | + |
| 92 | + static::assertNotNull($converted); |
| 93 | + static::assertArrayHasKey('type', $converted); |
| 94 | + static::assertSame('digital', $converted['type']); |
| 95 | + } |
| 96 | + |
| 97 | + public function testConvertDefaultsToPhysicalType(): void |
| 98 | + { |
| 99 | + $converter = $this->createConverterWithTypeColumn(true); |
| 100 | + $context = Context::createDefaultContext(); |
| 101 | + $migrationContext = $this->createMigrationContext(); |
| 102 | + |
| 103 | + $input = [ |
| 104 | + 'id' => Uuid::randomHex(), |
| 105 | + 'productNumber' => 'TEST-004', |
| 106 | + 'stock' => 10, |
| 107 | + 'name' => 'Product Without Type Or States', |
| 108 | + ]; |
| 109 | + |
| 110 | + $result = $converter->convert($input, $context, $migrationContext); |
| 111 | + $converted = $result->getConverted(); |
| 112 | + |
| 113 | + static::assertNotNull($converted); |
| 114 | + static::assertArrayHasKey('type', $converted); |
| 115 | + static::assertSame(ProductDefinition::TYPE_PHYSICAL, $converted['type']); |
| 116 | + } |
| 117 | + |
| 118 | + public function testConvertDoesNotSetTypeWhenColumnDoesNotExist(): void |
| 119 | + { |
| 120 | + $converter = $this->createConverterWithTypeColumn(false); |
| 121 | + $context = Context::createDefaultContext(); |
| 122 | + $migrationContext = $this->createMigrationContext(); |
| 123 | + |
| 124 | + $input = [ |
| 125 | + 'id' => Uuid::randomHex(), |
| 126 | + 'productNumber' => 'TEST-005', |
| 127 | + 'stock' => 10, |
| 128 | + 'states' => ['is-download'], |
| 129 | + 'name' => 'Product On Old Database', |
| 130 | + ]; |
| 131 | + |
| 132 | + $result = $converter->convert($input, $context, $migrationContext); |
| 133 | + $converted = $result->getConverted(); |
| 134 | + |
| 135 | + static::assertNotNull($converted); |
| 136 | + static::assertArrayNotHasKey('type', $converted); |
| 137 | + } |
| 138 | + |
| 139 | + private function createConverterWithTypeColumn(bool $hasTypeColumn): ProductConverter |
| 140 | + { |
| 141 | + $schemaManager = $this->createMock(AbstractSchemaManager::class); |
| 142 | + |
| 143 | + if ($hasTypeColumn) { |
| 144 | + $schemaManager->method('listTableColumns')->willReturn([ |
| 145 | + 'type' => new Column('type', new StringType()), |
| 146 | + ]); |
| 147 | + } else { |
| 148 | + $schemaManager->method('listTableColumns')->willReturn([]); |
| 149 | + } |
| 150 | + |
| 151 | + $connection = $this->createMock(Connection::class); |
| 152 | + $connection->method('createSchemaManager')->willReturn($schemaManager); |
| 153 | + |
| 154 | + return new ProductConverter( |
| 155 | + new Dummy6MappingService(), |
| 156 | + new DummyLoggingService(), |
| 157 | + new DummyMediaFileService(), |
| 158 | + $connection |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + private function createMigrationContext(): MigrationContext |
| 163 | + { |
| 164 | + $connection = new SwagMigrationConnectionEntity(); |
| 165 | + $connection->setId(Uuid::randomHex()); |
| 166 | + $connection->setProfileName(Shopware6MajorProfile::PROFILE_NAME); |
| 167 | + |
| 168 | + return new MigrationContext( |
| 169 | + new Shopware6MajorProfile('6.5'), |
| 170 | + $connection, |
| 171 | + Uuid::randomHex(), |
| 172 | + new ProductDataSet(), |
| 173 | + 0, |
| 174 | + 250 |
| 175 | + ); |
| 176 | + } |
| 177 | +} |
0 commit comments