Skip to content

Commit 6d5c701

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

File tree

6 files changed

+57
-79
lines changed

6 files changed

+57
-79
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,14 @@ public function expectedDataProvider(): array
153153
* @param array $expectedData
154154
* @return void
155155
*/
156-
protected function assertConfig(array $data, array $expectedData): void
156+
private function assertConfig(array $data, array $expectedData): void
157157
{
158158
$this->assertEquals($expectedData['label'], $data['label']);
159159
foreach ($expectedData['options'] as $expectedOption) {
160160
$found = false;
161161
foreach ($data['options'] as $option) {
162-
$expectedProductId = $this->productRepository->get($expectedOption['sku'])->getId();
163162
if ($option['label'] === $expectedOption['label']) {
163+
$expectedProductId = $this->productRepository->get($expectedOption['sku'])->getId();
164164
$this->assertEquals(
165165
[$expectedProductId],
166166
$option['products'],

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,8 @@ protected function setUp()
4141
public function testCategoryPageVisualSwatchAttributeView(array $expectedConfig, array $expectedSwatchConfig): void
4242
{
4343
$this->setAttributeUsedInProductListing('visual_swatch_attribute');
44-
$product = $this->productRepository->get('configurable');
45-
$this->block->setProduct($product);
4644
$result = $this->generateBlockJsonConfigData();
47-
$this->checkResultIsNotEmpty($result);
48-
$this->assertConfig($result['json_config'], $expectedConfig);
49-
$this->assertSwatchConfig($result['json_swatch_config'], $expectedSwatchConfig);
45+
$this->processAssert($result, $expectedConfig, $expectedSwatchConfig);
5046
}
5147

5248
/**
@@ -61,12 +57,8 @@ public function testCategoryPageVisualSwatchAttributeView(array $expectedConfig,
6157
public function testCategoryPageTextSwatchAttributeView(array $expectedConfig, array $expectedSwatchConfig): void
6258
{
6359
$this->setAttributeUsedInProductListing('text_swatch_attribute');
64-
$product = $this->productRepository->get('configurable');
65-
$this->block->setProduct($product);
6660
$result = $this->generateBlockJsonConfigData();
67-
$this->checkResultIsNotEmpty($result);
68-
$this->assertConfig($result['json_config'], $expectedConfig);
69-
$this->assertSwatchConfig($result['json_swatch_config'], $expectedSwatchConfig);
61+
$this->processAssert($result, $expectedConfig, $expectedSwatchConfig);
7062
}
7163

7264
/**
@@ -82,12 +74,8 @@ public function testCategoryPageTwoAttributesView(array $expectedConfig, array $
8274
{
8375
$this->setAttributeUsedInProductListing('visual_swatch_attribute');
8476
$this->setAttributeUsedInProductListing('text_swatch_attribute');
85-
$product = $this->productRepository->get('configurable');
86-
$this->block->setProduct($product);
8777
$result = $this->generateBlockJsonConfigData();
88-
$this->checkResultIsNotEmpty($result);
89-
$this->assertConfig($result['json_config'], $expectedConfig);
90-
$this->assertSwatchConfig($result['json_swatch_config'], $expectedSwatchConfig);
78+
$this->processAssert($result, $expectedConfig, $expectedSwatchConfig);
9179
}
9280

9381
/**

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

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77

88
namespace Magento\Swatches\Block\Product\Renderer\Configurable;
99

10-
use Magento\Catalog\Api\Data\ProductInterface;
1110
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
1211
use Magento\Catalog\Api\ProductRepositoryInterface;
13-
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
1413
use Magento\Framework\ObjectManagerInterface;
1514
use Magento\Framework\Registry;
1615
use Magento\Framework\Serialize\SerializerInterface;
@@ -40,18 +39,21 @@ class ProductPageViewTest extends TestCase
4039
/** @var ProductAttributeRepositoryInterface */
4140
protected $productAttributeRepository;
4241

43-
/** @var ProductRepositoryInterface */
44-
protected $productRepository;
45-
4642
/** @var LayoutInterface */
4743
protected $layout;
4844

45+
/** @var ProductRepositoryInterface */
46+
private $productRepository;
47+
4948
/** @var Registry */
5049
private $registry;
5150

5251
/** @var SerializerInterface */
5352
private $json;
5453

54+
/** @var ProductResource */
55+
private $productResource;
56+
5557
/**
5658
* @inheritdoc
5759
*/
@@ -66,21 +68,11 @@ protected function setUp()
6668
$this->registry = $this->objectManager->get(Registry::class);
6769
$this->json = $this->objectManager->get(SerializerInterface::class);
6870
$this->productAttributeRepository = $this->objectManager->create(ProductAttributeRepositoryInterface::class);
71+
$this->productResource = $this->objectManager->create(ProductResource::class);
6972
$this->template = Configurable::SWATCH_RENDERER_TEMPLATE;
7073
}
7174

7275
/**
73-
* @inheritdoc
74-
*/
75-
protected function tearDown()
76-
{
77-
$this->registry->unregister('product');
78-
79-
parent::tearDown();
80-
}
81-
82-
/**
83-
* @magentoDbIsolation disabled
8476
* @magentoDataFixture Magento/Swatches/_files/configurable_product_text_swatch_attribute.php
8577
*
8678
* @dataProvider expectedTextSwatchDataProvider
@@ -91,12 +83,8 @@ protected function tearDown()
9183
*/
9284
public function testProductPageTextSwatchAttributeView(array $expectedConfig, array $expectedSwatchConfig): void
9385
{
94-
$product = $this->productRepository->get('configurable');
95-
$this->registerProduct($product);
9686
$result = $this->generateBlockJsonConfigData();
97-
$this->checkResultIsNotEmpty($result);
98-
$this->assertConfig($result['json_config'], $expectedConfig);
99-
$this->assertSwatchConfig($result['json_swatch_config'], $expectedSwatchConfig);
87+
$this->processAssert($result, $expectedConfig, $expectedSwatchConfig);
10088
}
10189

10290
/**
@@ -152,12 +140,8 @@ public function expectedTextSwatchDataProvider(): array
152140
*/
153141
public function testProductPageVisualSwatchAttributeView(array $expectedConfig, array $expectedSwatchConfig): void
154142
{
155-
$product = $this->productRepository->get('configurable');
156-
$this->registerProduct($product);
157143
$result = $this->generateBlockJsonConfigData();
158-
$this->checkResultIsNotEmpty($result);
159-
$this->assertConfig($result['json_config'], $expectedConfig);
160-
$this->assertSwatchConfig($result['json_swatch_config'], $expectedSwatchConfig);
144+
$this->processAssert($result, $expectedConfig, $expectedSwatchConfig);
161145
}
162146

163147
/**
@@ -212,12 +196,8 @@ public function expectedVisualSwatchDataProvider(): array
212196
*/
213197
public function testProductPageTwoAttributesView(array $expectedConfig, array $expectedSwatchConfig): void
214198
{
215-
$product = $this->productRepository->get('configurable');
216-
$this->registerProduct($product);
217199
$result = $this->generateBlockJsonConfigData();
218-
$this->checkResultIsNotEmpty($result);
219-
$this->assertConfig($result['json_config'], $expectedConfig);
220-
$this->assertSwatchConfig($result['json_swatch_config'], $expectedSwatchConfig);
200+
$this->processAssert($result, $expectedConfig, $expectedSwatchConfig);
221201
}
222202

223203
/**
@@ -338,21 +318,38 @@ public function expectedTwoAttributesProvider(): array
338318
*/
339319
protected function generateBlockJsonConfigData(): array
340320
{
321+
$product = $this->productRepository->get('configurable');
322+
$this->block->setProduct($product);
341323
$this->block->setTemplate($this->template);
342324
$jsonConfig = $this->json->unserialize($this->block->getJsonConfig())['attributes'] ?? null;
343325
$jsonSwatchConfig = $this->json->unserialize($this->block->getJsonSwatchConfig());
344326

345327
return ['json_config' => $jsonConfig, 'json_swatch_config' => $jsonSwatchConfig];
346328
}
347329

330+
/**
331+
* Process test asserts
332+
*
333+
* @param $actualConfig
334+
* @param $expectedConfig
335+
* @param $expectedSwatchConfig
336+
* @return void
337+
*/
338+
protected function processAssert($actualConfig, $expectedConfig, $expectedSwatchConfig): void
339+
{
340+
$this->checkResultIsNotEmpty($actualConfig);
341+
$this->assertConfig($actualConfig['json_config'], $expectedConfig);
342+
$this->assertSwatchConfig($actualConfig['json_swatch_config'], $expectedSwatchConfig);
343+
}
344+
348345
/**
349346
* Assert that correct data was generated
350347
*
351348
* @param array $actualData
352349
* @param array $expectedData
353350
* @return void
354351
*/
355-
protected function assertSwatchConfig(array $actualData, array $expectedData): void
352+
private function assertSwatchConfig(array $actualData, array $expectedData): void
356353
{
357354
foreach ($actualData as $actualDataItem) {
358355
$currentType = $this->json->unserialize($actualDataItem['additional_data'])['swatch_input_type'] ?? null;
@@ -372,11 +369,11 @@ protected function assertSwatchConfig(array $actualData, array $expectedData): v
372369
* @param array $expectedData
373370
* @return void
374371
*/
375-
protected function assertConfig(array $actualData, array $expectedData): void
372+
private function assertConfig(array $actualData, array $expectedData): void
376373
{
377374
foreach ($actualData as $actualDataItem) {
378375
$expectedItem = $expectedData[$actualDataItem['code']];
379-
$this->assertEquals($expectedData[$actualDataItem['code']]['label'], $actualDataItem['label']);
376+
$this->assertEquals($expectedItem['label'], $actualDataItem['label']);
380377
$this->checkOptions($actualDataItem, $expectedItem);
381378
}
382379
}
@@ -386,7 +383,7 @@ protected function assertConfig(array $actualData, array $expectedData): void
386383
*
387384
* @param array $result
388385
*/
389-
protected function checkResultIsNotEmpty(array $result): void
386+
private function checkResultIsNotEmpty(array $result): void
390387
{
391388
foreach ($result as $item) {
392389
$this->assertNotNull($item);
@@ -404,28 +401,12 @@ private function getProductIdsBySkus(array $skus): array
404401
{
405402
$productIds = [];
406403
foreach ($skus as $sku) {
407-
try {
408-
$productIds[] = $this->productRepository->get($sku)->getId();
409-
} catch (NoSuchEntityException $e) {
410-
$this->fail(sprintf('The product with sku %s was not created', $sku));
411-
}
404+
$productIds[] = $this->productResource->getIdBySku($sku);
412405
}
413406

414407
return $productIds;
415408
}
416409

417-
/**
418-
* Register product
419-
*
420-
* @param ProductInterface $product
421-
* @return void
422-
*/
423-
private function registerProduct(ProductInterface $product): void
424-
{
425-
$this->registry->unregister('product');
426-
$this->registry->register('product', $product);
427-
}
428-
429410
/**
430411
* Check attribute options
431412
*

dev/tests/integration/testsuite/Magento/Swatches/_files/configurable_product_text_swatch_attribute.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
declare(strict_types=1);
77

8+
use Magento\Catalog\Api\Data\ProductExtensionFactory;
89
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
910
use Magento\Catalog\Api\ProductRepositoryInterface;
1011
use Magento\Catalog\Model\Product\Attribute\Source\Status;
@@ -26,7 +27,7 @@
2627
$attribute = $productAttributeRepository->get('text_swatch_attribute');
2728
$options = $attribute->getOptions();
2829
/** @var WebsiteRepositoryInterface $websiteRepository */
29-
$websiteRepository = $objectManager->create(WebsiteRepositoryInterface::class);
30+
$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
3031
$baseWebsite = $websiteRepository->get('base');
3132
/** @var ProductRepositoryInterface $productRepository */
3233
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
@@ -60,7 +61,7 @@
6061
$associatedProductIds[] = $product->getId();
6162
}
6263
/** @var Factory $optionsFactory */
63-
$optionsFactory = $objectManager->create(Factory::class);
64+
$optionsFactory = $objectManager->get(Factory::class);
6465
$configurableAttributesData = [
6566
[
6667
'attribute_id' => $attribute->getId(),
@@ -73,7 +74,9 @@
7374
$configurableOptions = $optionsFactory->create($configurableAttributesData);
7475

7576
$product = $productFactory->create();
76-
$extensionConfigurableAttributes = $product->getExtensionAttributes();
77+
/** @var ProductExtensionFactory $extensionAttributesFactory */
78+
$extensionAttributesFactory = $objectManager->get(ProductExtensionFactory::class);
79+
$extensionConfigurableAttributes = $product->getExtensionAttributes() ?: $extensionAttributesFactory->create();
7780
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
7881
$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
7982
$product->setExtensionAttributes($extensionConfigurableAttributes);

dev/tests/integration/testsuite/Magento/Swatches/_files/configurable_product_two_attributes.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
declare(strict_types=1);
77

8+
use Magento\Catalog\Api\Data\ProductExtensionFactory;
89
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
910
use Magento\Catalog\Api\ProductRepositoryInterface;
1011
use Magento\Catalog\Model\Product;
@@ -30,7 +31,7 @@
3031
$options = $attribute->getOptions();
3132
$secondAttributeOptions = $secondAttribute->getOptions();
3233
/** @var WebsiteRepositoryInterface $websiteRepository */
33-
$websiteRepository = $objectManager->create(WebsiteRepositoryInterface::class);
34+
$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
3435
$baseWebsite = $websiteRepository->get('base');
3536
/** @var ProductAttributeRepositoryInterface $productRepository */
3637
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
@@ -82,7 +83,7 @@
8283
}
8384

8485
$allAttributes = [$attribute, $secondAttribute];
85-
$optionsFactory = $objectManager->create(Factory::class);
86+
$optionsFactory = $objectManager->get(Factory::class);
8687

8788
foreach ($allAttributes as $attribute) {
8889
$configurableAttributesData[] =
@@ -99,8 +100,10 @@
99100
}
100101

101102
$configurableOptions = $optionsFactory->create($configurableAttributesData);
102-
$product = $objectManager->create(Product::class);
103-
$extensionConfigurableAttributes = $product->getExtensionAttributes();
103+
$product = $productFactory->create();
104+
/** @var ProductExtensionFactory $extensionAttributesFactory */
105+
$extensionAttributesFactory = $objectManager->get(ProductExtensionFactory::class);
106+
$extensionConfigurableAttributes = $product->getExtensionAttributes() ?: $extensionAttributesFactory->create();
104107
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
105108
$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
106109
$product->setExtensionAttributes($extensionConfigurableAttributes);

dev/tests/integration/testsuite/Magento/Swatches/_files/configurable_product_visual_swatch_attribute.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
declare(strict_types=1);
77

8+
use Magento\Catalog\Api\Data\ProductExtensionFactory;
89
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
910
use Magento\Catalog\Api\ProductRepositoryInterface;
1011
use Magento\Catalog\Model\Product\Attribute\Source\Status;
@@ -26,7 +27,7 @@
2627
$attribute = $productAttributeRepository->get('visual_swatch_attribute');
2728
$options = $attribute->getOptions();
2829
/** @var WebsiteRepositoryInterface $websiteRepository */
29-
$websiteRepository = $objectManager->create(WebsiteRepositoryInterface::class);
30+
$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
3031
$baseWebsite = $websiteRepository->get('base');
3132
/** @var ProductRepositoryInterface $productRepository */
3233
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
@@ -60,7 +61,7 @@
6061
$associatedProductIds[] = $product->getId();
6162
}
6263
/** @var Factory $optionsFactory */
63-
$optionsFactory = $objectManager->create(Factory::class);
64+
$optionsFactory = $objectManager->get(Factory::class);
6465
$configurableAttributesData = [
6566
[
6667
'attribute_id' => $attribute->getId(),
@@ -73,7 +74,9 @@
7374
$configurableOptions = $optionsFactory->create($configurableAttributesData);
7475

7576
$product = $productFactory->create();
76-
$extensionConfigurableAttributes = $product->getExtensionAttributes();
77+
/** @var ProductExtensionFactory $extensionAttributesFactory */
78+
$extensionAttributesFactory = $objectManager->get(ProductExtensionFactory::class);
79+
$extensionConfigurableAttributes = $product->getExtensionAttributes() ?: $extensionAttributesFactory->create();
7780
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
7881
$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
7982
$product->setExtensionAttributes($extensionConfigurableAttributes);

0 commit comments

Comments
 (0)