Skip to content

Commit 5d2a167

Browse files
committed
SFAPP-522: fix tests on PR for commerce-data-export and saas-export repos
1 parent 3328cd5 commit 5d2a167

File tree

2 files changed

+185
-3
lines changed

2 files changed

+185
-3
lines changed

ProductVariantDataExporter/Test/Integration/AbstractProductVariantsTest.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Magento\ProductVariantDataExporter\Test\Integration;
1010

1111
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\ConfigurableProductDataExporter\Model\Provider\Product\ProductVariants\ConfigurableId;
1213
use Magento\DataExporter\Model\FeedInterface;
1314
use Magento\DataExporter\Model\FeedPool;
1415
use Magento\Eav\Model\AttributeRepository;
@@ -21,6 +22,8 @@
2122
use PHPUnit\Framework\TestCase;
2223
use Magento\Framework\Stdlib\ArrayUtils;
2324
use Magento\Framework\Registry;
25+
use RuntimeException;
26+
use Throwable;
2427

2528
/**
2629
* Abstract class for product variant feed tests
@@ -83,6 +86,11 @@ abstract class AbstractProductVariantsTest extends TestCase
8386
*/
8487
protected $registry;
8588

89+
/**
90+
* @var ConfigurableId|mixed
91+
*/
92+
protected $idResolver;
93+
8694
/**
8795
* @inheritDoc
8896
*/
@@ -99,6 +107,7 @@ protected function setUp() : void
99107
$this->attributeRepository = Bootstrap::getObjectManager()->create(AttributeRepository::class);
100108
$this->arrayUtils = $objectManager->create(ArrayUtils::class);
101109
$this->registry = Bootstrap::getObjectManager()->get(Registry::class);
110+
$this->idResolver = Bootstrap::getObjectManager()->get(ConfigurableId::class);
102111
}
103112

104113
/**
@@ -107,15 +116,15 @@ protected function setUp() : void
107116
* @param array $parentIds
108117
* @return void
109118
*
110-
* @throws \RuntimeException
119+
* @throws RuntimeException
111120
*/
112121
protected function runIndexer(array $parentIds) : void
113122
{
114123
try {
115124
$this->indexer->load(self::PRODUCT_VARIANT_FEED_INDEXER);
116125
$this->indexer->reindexList($parentIds);
117-
} catch (\Throwable $e) {
118-
throw new \RuntimeException('Could not reindex product variant data');
126+
} catch (Throwable $e) {
127+
throw new RuntimeException('Could not reindex product variant data');
119128
}
120129
}
121130
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ProductVariantDataExporter\Test\Integration;
9+
10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Model\Product;
12+
use Magento\ConfigurableProductDataExporter\Model\Provider\Product\ProductVariants\ConfigurableId;
13+
use RuntimeException;
14+
use Throwable;
15+
16+
/**
17+
* Test class for configurable product variants export
18+
*/
19+
class ConfigurableProductVariantsTest extends AbstractProductVariantsTest
20+
{
21+
/**
22+
* Test configurable product variants.
23+
*
24+
* @magentoAppIsolation enabled
25+
* @magentoDbIsolation disabled
26+
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_products_with_two_attributes.php
27+
* @return void
28+
*/
29+
public function testConfigurableVariants(): void
30+
{
31+
try {
32+
$expected = $this->getExpectedProductVariants(['simple_10','simple_20']);
33+
34+
$variantSimple10 = $this->idResolver->resolve([
35+
ConfigurableId::PARENT_SKU_KEY => 'configurable',
36+
ConfigurableId::CHILD_SKU_KEY => 'simple_10'
37+
]);
38+
$variantSimple20 = $this->idResolver->resolve([
39+
ConfigurableId::PARENT_SKU_KEY => 'configurable',
40+
ConfigurableId::CHILD_SKU_KEY => 'simple_20'
41+
]);
42+
$actual = $this->productVariantsFeed->getFeedByIds(
43+
[$variantSimple10, $variantSimple20]
44+
)['feed'];
45+
46+
$diff = $this->arrayUtils->recursiveDiff($expected, $actual);
47+
self::assertEquals([], $diff, "Product variants response doesn't equal expected response");
48+
49+
} catch (Throwable $e) {
50+
$this->fail($e->getMessage());
51+
}
52+
}
53+
54+
/**
55+
* Test that variants are deleted as expected.
56+
*
57+
* @magentoDbIsolation disabled
58+
* @magentoAppIsolation enabled
59+
* @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
60+
*
61+
* @return void
62+
*/
63+
public function testDeleteConfigurableProductVariants(): void
64+
{
65+
try {
66+
$configurable = $this->productRepository->get('configurable');
67+
$configurableId = $configurable->getId();
68+
$variantSimple10 = $this->idResolver->resolve([
69+
ConfigurableId::PARENT_SKU_KEY => 'configurable',
70+
ConfigurableId::CHILD_SKU_KEY => 'simple_10'
71+
]);
72+
$variantSimple20 = $this->idResolver->resolve([
73+
ConfigurableId::PARENT_SKU_KEY => 'configurable',
74+
ConfigurableId::CHILD_SKU_KEY => 'simple_20'
75+
]);
76+
$realVariantsData = $this->productVariantsFeed->getFeedByIds(
77+
[$variantSimple10, $variantSimple20]
78+
)['feed'];
79+
$this->assertCount(2, $realVariantsData);
80+
81+
$simple = $this->productRepository->get('simple_10'); //id10 and id20
82+
$this->deleteProduct($simple->getSku());
83+
$this->runIndexer([$configurableId]);
84+
85+
$emptyVariantsData = $this->productVariantsFeed->getFeedByIds(
86+
[$variantSimple10, $variantSimple20]
87+
)['feed'];
88+
$this->assertCount(1, $emptyVariantsData); //id20
89+
$deletedVariantsData = $this->productVariantsFeed->getDeletedByIds(
90+
[$variantSimple10, $variantSimple20]
91+
);
92+
$this->assertCount(1, $deletedVariantsData); //id10
93+
} catch (Throwable $e) {
94+
$this->fail($e->getMessage());
95+
}
96+
}
97+
98+
/**
99+
* Delete product variant
100+
*
101+
* @param string $productSku
102+
* @throws RuntimeException
103+
*/
104+
private function deleteProduct(string $productSku): void
105+
{
106+
$this->registry->unregister('isSecureArea');
107+
$this->registry->register('isSecureArea', true);
108+
109+
try {
110+
$this->productRepository->deleteById($productSku);
111+
} catch (Throwable $e) {
112+
throw new RuntimeException('Could not delete product ' . $productSku);
113+
}
114+
115+
$this->registry->unregister('isSecureArea');
116+
$this->registry->register('isSecureArea', false);
117+
}
118+
119+
/**
120+
* Get the expected variants for the first combination of options being tested.
121+
*
122+
* @param array $simples
123+
* @return array
124+
*/
125+
private function getExpectedProductVariants(array $simples): array
126+
{
127+
$variants = [
128+
'simple_10' =>
129+
[
130+
'id' => '8a880c29baa2ec8a5068350ec04f5b7d',
131+
'optionValues' =>
132+
[
133+
[
134+
'attributeCode' => 'test_configurable_first',
135+
'label' => 'First Option 1',
136+
// 'valueIndex' => '107', //Skipped because they are unique
137+
// 'uid' => 'Y29uZmlndXJhYmxlLzIwOS8xMDc=', //Skipped because they are unique
138+
],
139+
[
140+
'attributeCode' => 'test_configurable_second',
141+
'label' => 'Second Option 1',
142+
],
143+
],
144+
'parentId' => '1',
145+
'productId' => '10',
146+
'parentSku' => 'configurable',
147+
'productSku' => 'simple_10',
148+
'deleted' => false,
149+
],
150+
'simple_20' =>
151+
[
152+
'id' => 'b91c35230afd24649f2ff60c79e7e7ba',
153+
'optionValues' =>
154+
[
155+
[
156+
'attributeCode' => 'test_configurable_first',
157+
'label' => 'First Option 2',
158+
],
159+
[
160+
'attributeCode' => 'test_configurable_second',
161+
'label' => 'Second Option 2',
162+
],
163+
],
164+
'parentId' => '1',
165+
'productId' => '20',
166+
'parentSku' => 'configurable',
167+
'productSku' => 'simple_20',
168+
'deleted' => false,
169+
],
170+
];
171+
return array_values(array_intersect_key($variants, array_flip($simples)));
172+
}
173+
}

0 commit comments

Comments
 (0)