|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Oro\Bundle\ProductBundle\Tests\Functional\ImportExport\Strategy; |
| 4 | + |
| 5 | +use Oro\Bundle\EntityConfigBundle\Attribute\Entity\AttributeFamily; |
| 6 | +use Oro\Bundle\EntityExtendBundle\Entity\AbstractEnumValue; |
| 7 | +use Oro\Bundle\EntityExtendBundle\Tools\ExtendHelper; |
| 8 | +use Oro\Bundle\ImportExportBundle\Context\Context; |
| 9 | +use Oro\Bundle\LocaleBundle\Entity\LocalizedFallbackValue; |
| 10 | +use Oro\Bundle\ProductBundle\Entity\Product; |
| 11 | +use Oro\Bundle\ProductBundle\Entity\ProductUnit; |
| 12 | +use Oro\Bundle\ProductBundle\Entity\ProductUnitPrecision; |
| 13 | +use Oro\Bundle\ProductBundle\Entity\ProductVariantLink; |
| 14 | +use Oro\Bundle\ProductBundle\ImportExport\Strategy\ProductStrategy; |
| 15 | +use Oro\Bundle\ProductBundle\Tests\Functional\DataFixtures\LoadProductData; |
| 16 | +use Oro\Bundle\ProductBundle\Tests\Functional\DataFixtures\LoadProductUnits; |
| 17 | +use Oro\Bundle\TestFrameworkBundle\Test\WebTestCase; |
| 18 | +use Oro\Component\Testing\Unit\EntityTrait; |
| 19 | + |
| 20 | +/** |
| 21 | + * @dbIsolation |
| 22 | + */ |
| 23 | +class ProductStrategyTest extends WebTestCase |
| 24 | +{ |
| 25 | + use EntityTrait; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var ProductStrategy |
| 29 | + * */ |
| 30 | + protected $strategy; |
| 31 | + |
| 32 | + protected function setUp() |
| 33 | + { |
| 34 | + $this->initClient(); |
| 35 | + $this->loadFixtures([LoadProductData::class]); |
| 36 | + $container = $this->getContainer(); |
| 37 | + |
| 38 | + $container->get('oro_importexport.field.database_helper')->onClear(); |
| 39 | + $this->strategy = new ProductStrategy( |
| 40 | + $container->get('event_dispatcher'), |
| 41 | + $container->get('oro_importexport.strategy.import.helper'), |
| 42 | + $container->get('oro_entity.helper.field_helper'), |
| 43 | + $container->get('oro_importexport.field.database_helper'), |
| 44 | + $container->get('oro_entity.entity_class_name_provider'), |
| 45 | + $container->get('translator'), |
| 46 | + $container->get('oro_importexport.strategy.new_entities_helper'), |
| 47 | + $container->get('oro_entity.doctrine_helper') |
| 48 | + ); |
| 49 | + $this->strategy->setEntityName(Product::class); |
| 50 | + $this->strategy->setVariantLinkClass(ProductVariantLink::class); |
| 51 | + $this->strategy->setLocalizedFallbackValueClass(LocalizedFallbackValue::class); |
| 52 | + $this->strategy->setSecurityFacade($container->get('oro_security.security_facade')); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Impossible to import product when it's variant is in the same batch. |
| 57 | + * Caused because variant is not in DB it could not be loaded by SKU and variant link contains invalid relation. |
| 58 | + * |
| 59 | + * @link https://magecore.atlassian.net/browse/BB-7908 |
| 60 | + */ |
| 61 | + public function testProcessWithVariantLinks() |
| 62 | + { |
| 63 | + $context = new Context([]); |
| 64 | + $context->setValue('itemData', []); |
| 65 | + $this->strategy->setImportExportContext($context); |
| 66 | + |
| 67 | + $inventoryStatusClassName = ExtendHelper::buildEnumValueClassName('prod_inventory_status'); |
| 68 | + /** @var AbstractEnumValue $inventoryStatus */ |
| 69 | + $inventoryStatus = $this->getContainer() |
| 70 | + ->get('doctrine') |
| 71 | + ->getRepository($inventoryStatusClassName) |
| 72 | + ->find('in_stock'); |
| 73 | + |
| 74 | + /** @var ProductUnit $unit */ |
| 75 | + $unit = $this->getReference(LoadProductUnits::BOX); |
| 76 | + /** @var AttributeFamily $attributeFamily */ |
| 77 | + $attributeFamily = $this->getEntity(AttributeFamily::class, ['code' => 'default_family']); |
| 78 | + $newProductSku = 'PR-V1'; |
| 79 | + |
| 80 | + // Prepare new product that is imported in same batch and will be used later as variant link |
| 81 | + $newProduct = $this->createProduct($newProductSku, $attributeFamily, $unit, $inventoryStatus); |
| 82 | + /** @var Product $processedNewProduct */ |
| 83 | + $processedNewProduct = $this->strategy->process($newProduct); |
| 84 | + $this->assertEquals([], $context->getErrors()); |
| 85 | + $this->assertInstanceOf(Product::class, $processedNewProduct); |
| 86 | + $this->assertSame($newProductSku, $processedNewProduct->getSku()); |
| 87 | + |
| 88 | + // Get existing product that should be found by SKU as variant link relation |
| 89 | + /** @var Product $existingProduct */ |
| 90 | + $existingProduct = $this->getReference(LoadProductData::PRODUCT_1); |
| 91 | + |
| 92 | + $linkToNewProduct = new ProductVariantLink(); |
| 93 | + $linkToNewProduct->setProduct((new Product())->setSku($newProductSku)); |
| 94 | + $linkToExistingProduct = new ProductVariantLink(); |
| 95 | + $linkToExistingProduct->setProduct((new Product())->setSku($existingProduct->getSku())); |
| 96 | + |
| 97 | + // Add prepared variant links to newly imported product |
| 98 | + $productWithVariants = $this->createProduct('PR-VV', $attributeFamily, $unit, $inventoryStatus); |
| 99 | + $productWithVariants->addVariantLink($linkToNewProduct); |
| 100 | + $productWithVariants->addVariantLink($linkToExistingProduct); |
| 101 | + |
| 102 | + // Check that all variant links present and were attached correctly |
| 103 | + /** @var Product $processedProductWithVariants */ |
| 104 | + $processedProductWithVariants = $this->strategy->process($productWithVariants); |
| 105 | + $this->assertEquals([], $context->getErrors()); |
| 106 | + $this->assertInstanceOf(Product::class, $processedProductWithVariants); |
| 107 | + $this->assertSame($productWithVariants->getSku(), $processedProductWithVariants->getSku()); |
| 108 | + $this->assertCount(2, $processedProductWithVariants->getVariantLinks()); |
| 109 | + $usedVariantLinksProductSkus = array_map( |
| 110 | + function (ProductVariantLink $variantLink) { |
| 111 | + return $variantLink->getProduct()->getSku(); |
| 112 | + }, |
| 113 | + $processedProductWithVariants->getVariantLinks()->toArray() |
| 114 | + ); |
| 115 | + $this->assertContains($newProductSku, $usedVariantLinksProductSkus); |
| 116 | + $this->assertContains($existingProduct->getSku(), $usedVariantLinksProductSkus); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @param string $sku |
| 121 | + * @param AttributeFamily $attributeFamily |
| 122 | + * @param ProductUnit $unit |
| 123 | + * @param AbstractEnumValue $inventoryStatus |
| 124 | + * @return Product |
| 125 | + */ |
| 126 | + protected function createProduct( |
| 127 | + $sku, |
| 128 | + AttributeFamily $attributeFamily, |
| 129 | + ProductUnit $unit, |
| 130 | + AbstractEnumValue $inventoryStatus |
| 131 | + ) { |
| 132 | + $newProduct = new Product(); |
| 133 | + $newProduct->setSku($sku); |
| 134 | + $newProduct->setAttributeFamily($attributeFamily); |
| 135 | + $newProduct->setInventoryStatus($inventoryStatus); |
| 136 | + $newProduct->setPrimaryUnitPrecision( |
| 137 | + (new ProductUnitPrecision())->setUnit($unit) |
| 138 | + ); |
| 139 | + |
| 140 | + return $newProduct; |
| 141 | + } |
| 142 | +} |
0 commit comments