Skip to content

Commit a8ef49d

Browse files
committed
MC-29130: Storefront: View configurable product on storefront
1 parent 6d5c701 commit a8ef49d

File tree

3 files changed

+57
-65
lines changed

3 files changed

+57
-65
lines changed

dev/tests/integration/testsuite/Magento/ConfigurableProduct/Block/Product/View/Type/ConfigurableTest.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Catalog\Api\ProductRepositoryInterface;
1111
use Magento\Catalog\Model\Product;
12+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
1213
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\Collection;
1314
use Magento\Framework\ObjectManagerInterface;
1415
use Magento\Framework\Serialize\SerializerInterface;
@@ -44,6 +45,9 @@ class ConfigurableTest extends TestCase
4445
/** @var SerializerInterface */
4546
private $json;
4647

48+
/** @var ProductResource */
49+
private $productResource;
50+
4751
/**
4852
* @inheritdoc
4953
*/
@@ -56,6 +60,7 @@ protected function setUp()
5660
$this->block = $this->layout->createBlock(Configurable::class);
5761
$this->json = $this->objectManager->get(SerializerInterface::class);
5862
$this->block->setProduct($this->product);
63+
$this->productResource = $this->objectManager->create(ProductResource::class);
5964
}
6065

6166
/**
@@ -153,24 +158,21 @@ public function expectedDataProvider(): array
153158
* @param array $expectedData
154159
* @return void
155160
*/
156-
private function assertConfig(array $data, array $expectedData): void
161+
private function assertConfig($data, $expectedData): void
157162
{
158163
$this->assertEquals($expectedData['label'], $data['label']);
159-
foreach ($expectedData['options'] as $expectedOption) {
160-
$found = false;
161-
foreach ($data['options'] as $option) {
162-
if ($option['label'] === $expectedOption['label']) {
163-
$expectedProductId = $this->productRepository->get($expectedOption['sku'])->getId();
164-
$this->assertEquals(
165-
[$expectedProductId],
166-
$option['products'],
167-
'Wrong product linked as option'
168-
);
169-
$found = true;
170-
break;
164+
$skus = array_column($expectedData['options'], 'sku');
165+
$idBySkuMap = $this->productResource->getProductsIdsBySkus($skus);
166+
foreach ($expectedData['options'] as &$option) {
167+
$sku = $option['sku'];
168+
unset($option['sku']);
169+
$option['products'] = [$idBySkuMap[$sku]];
170+
foreach ($data['options'] as $actualOption) {
171+
if ($option['label'] === $actualOption['label']) {
172+
unset($actualOption['id']);
173+
$this->assertEquals($option, $actualOption);
171174
}
172175
}
173-
$this->assertTrue($found);
174176
}
175177
}
176178
}

dev/tests/integration/testsuite/Magento/Swatches/Block/Product/Renderer/Configurable/Listing/CategoryPageViewTest.php

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ protected function setUp()
4040
*/
4141
public function testCategoryPageVisualSwatchAttributeView(array $expectedConfig, array $expectedSwatchConfig): void
4242
{
43-
$this->setAttributeUsedInProductListing('visual_swatch_attribute');
44-
$result = $this->generateBlockJsonConfigData();
45-
$this->processAssert($result, $expectedConfig, $expectedSwatchConfig);
43+
$this->checkProductViewCategoryPage($expectedConfig, $expectedSwatchConfig, ['visual_swatch_attribute']);
4644
}
4745

4846
/**
@@ -56,9 +54,7 @@ public function testCategoryPageVisualSwatchAttributeView(array $expectedConfig,
5654
*/
5755
public function testCategoryPageTextSwatchAttributeView(array $expectedConfig, array $expectedSwatchConfig): void
5856
{
59-
$this->setAttributeUsedInProductListing('text_swatch_attribute');
60-
$result = $this->generateBlockJsonConfigData();
61-
$this->processAssert($result, $expectedConfig, $expectedSwatchConfig);
57+
$this->checkProductViewCategoryPage($expectedConfig, $expectedSwatchConfig, ['text_swatch_attribute']);
6258
}
6359

6460
/**
@@ -72,22 +68,42 @@ public function testCategoryPageTextSwatchAttributeView(array $expectedConfig, a
7268
*/
7369
public function testCategoryPageTwoAttributesView(array $expectedConfig, array $expectedSwatchConfig): void
7470
{
75-
$this->setAttributeUsedInProductListing('visual_swatch_attribute');
76-
$this->setAttributeUsedInProductListing('text_swatch_attribute');
77-
$result = $this->generateBlockJsonConfigData();
78-
$this->processAssert($result, $expectedConfig, $expectedSwatchConfig);
71+
$this->checkProductViewCategoryPage(
72+
$expectedConfig,
73+
$expectedSwatchConfig,
74+
['visual_swatch_attribute', 'text_swatch_attribute']
75+
);
7976
}
8077

8178
/**
82-
* Set used in product listing attribute value to true
79+
* Check configurable product view on category view page
8380
*
84-
* @param string $attributeCode
81+
* @param array $expectedConfig
82+
* @param array $expectedSwatchConfig
83+
* @param array $attributes
84+
* @return void
85+
*/
86+
private function checkProductViewCategoryPage(
87+
array $expectedConfig,
88+
array $expectedSwatchConfig,
89+
array $attributes
90+
): void {
91+
$this->setAttributeUsedInProductListing($attributes);
92+
$this->checkProductView($expectedConfig, $expectedSwatchConfig);
93+
}
94+
95+
/**
96+
* Set used in product listing attributes value to true
97+
*
98+
* @param array $attributeCodes
8599
* @return void
86100
*/
87-
private function setAttributeUsedInProductListing(string $attributeCode): void
101+
private function setAttributeUsedInProductListing(array $attributeCodes): void
88102
{
89-
$attribute = $this->productAttributeRepository->get($attributeCode);
90-
$attribute->setUsedInProductListing('1');
91-
$this->productAttributeRepository->save($attribute);
103+
foreach ($attributeCodes as $attributeCode) {
104+
$attribute = $this->productAttributeRepository->get($attributeCode);
105+
$attribute->setUsedInProductListing('1');
106+
$this->productAttributeRepository->save($attribute);
107+
}
92108
}
93109
}

dev/tests/integration/testsuite/Magento/Swatches/Block/Product/Renderer/Configurable/ProductPageViewTest.php

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ protected function setUp()
8383
*/
8484
public function testProductPageTextSwatchAttributeView(array $expectedConfig, array $expectedSwatchConfig): void
8585
{
86-
$result = $this->generateBlockJsonConfigData();
87-
$this->processAssert($result, $expectedConfig, $expectedSwatchConfig);
86+
$this->checkProductView($expectedConfig, $expectedSwatchConfig);
8887
}
8988

9089
/**
@@ -140,8 +139,7 @@ public function expectedTextSwatchDataProvider(): array
140139
*/
141140
public function testProductPageVisualSwatchAttributeView(array $expectedConfig, array $expectedSwatchConfig): void
142141
{
143-
$result = $this->generateBlockJsonConfigData();
144-
$this->processAssert($result, $expectedConfig, $expectedSwatchConfig);
142+
$this->checkProductView($expectedConfig, $expectedSwatchConfig);
145143
}
146144

147145
/**
@@ -196,8 +194,7 @@ public function expectedVisualSwatchDataProvider(): array
196194
*/
197195
public function testProductPageTwoAttributesView(array $expectedConfig, array $expectedSwatchConfig): void
198196
{
199-
$result = $this->generateBlockJsonConfigData();
200-
$this->processAssert($result, $expectedConfig, $expectedSwatchConfig);
197+
$this->checkProductView($expectedConfig, $expectedSwatchConfig);
201198
}
202199

203200
/**
@@ -328,15 +325,15 @@ protected function generateBlockJsonConfigData(): array
328325
}
329326

330327
/**
331-
* Process test asserts
328+
* Check configurable product view
332329
*
333-
* @param $actualConfig
334330
* @param $expectedConfig
335331
* @param $expectedSwatchConfig
336332
* @return void
337333
*/
338-
protected function processAssert($actualConfig, $expectedConfig, $expectedSwatchConfig): void
334+
protected function checkProductView($expectedConfig, $expectedSwatchConfig): void
339335
{
336+
$actualConfig = $this->generateBlockJsonConfigData();
340337
$this->checkResultIsNotEmpty($actualConfig);
341338
$this->assertConfig($actualConfig['json_config'], $expectedConfig);
342339
$this->assertSwatchConfig($actualConfig['json_swatch_config'], $expectedSwatchConfig);
@@ -391,22 +388,6 @@ private function checkResultIsNotEmpty(array $result): void
391388
}
392389
}
393390

394-
/**
395-
* Get product ids by skus
396-
*
397-
* @param array $skus
398-
* @return array
399-
*/
400-
private function getProductIdsBySkus(array $skus): array
401-
{
402-
$productIds = [];
403-
foreach ($skus as $sku) {
404-
$productIds[] = $this->productResource->getIdBySku($sku);
405-
}
406-
407-
return $productIds;
408-
}
409-
410391
/**
411392
* Check attribute options
412393
*
@@ -417,20 +398,13 @@ private function getProductIdsBySkus(array $skus): array
417398
private function checkOptions(array $actualDataItem, array $expectedItem): void
418399
{
419400
foreach ($expectedItem['options'] as $expectedItemKey => $expectedOption) {
420-
$found = false;
401+
$expectedSkus = array_values($expectedOption['skus']);
402+
$expectedIds = array_values($this->productResource->getProductsIdsBySkus($expectedSkus));
421403
foreach ($actualDataItem['options'] as $option) {
422404
if ($option['label'] === $expectedOption['label']) {
423-
$productIds = $this->getProductIdsBySkus($expectedItem['options'][$expectedItemKey]['skus']);
424-
$this->assertEquals(
425-
$productIds,
426-
$option['products'],
427-
'Wrong product linked as option'
428-
);
429-
$found = true;
430-
break;
405+
$this->assertEquals($expectedIds, $option['products'], 'Wrong product linked as option');
431406
}
432407
}
433-
$this->assertTrue($found);
434408
}
435409
}
436410
}

0 commit comments

Comments
 (0)