Skip to content

Commit a0cfee5

Browse files
committed
MAGETWO-65402: [Backport] - [Performance] Optimize validation of swatches attributes - for 2.1.6
1 parent 43d4820 commit a0cfee5

File tree

9 files changed

+150
-13
lines changed

9 files changed

+150
-13
lines changed

app/code/Magento/Swatches/Model/SwatchAttributeCodes.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
namespace Magento\Swatches\Model;
77

8-
use Magento\Eav\Model\Entity\Attribute;
98
use Magento\Framework\App\CacheInterface;
109
use Magento\Framework\App\ResourceConnection;
1110
use Magento\Framework\DB\Select;
@@ -51,8 +50,8 @@ class SwatchAttributeCodes
5150
public function __construct(
5251
CacheInterface $cache,
5352
ResourceConnection $resourceConnection,
54-
$cacheKey = 'swatch-attribute-list',
55-
array $cacheTags = [Attribute::CACHE_TAG]
53+
$cacheKey,
54+
array $cacheTags
5655
) {
5756
$this->cache = $cache;
5857
$this->resourceConnection = $resourceConnection;

app/code/Magento/Swatches/Model/SwatchAttributesProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class SwatchAttributesProvider
2525
private $swatchAttributeCodes;
2626

2727
/**
28-
* @var Attribute[]
28+
* @var [productId => Attribute[]]
2929
*/
3030
private $attributesPerProduct;
3131

app/code/Magento/Swatches/Test/Unit/Helper/DataTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class DataTest extends \PHPUnit_Framework_TestCase
4747
protected $productRepoMock;
4848

4949
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Swatches\Model\SwatchAttributesProvider */
50-
protected $swatchAttributesProviderMock;
50+
private $swatchAttributesProviderMock;
5151

5252
protected function setUp()
5353
{

app/code/Magento/Swatches/Test/Unit/Model/SwatchAttributeCodesTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ class SwatchAttributeCodesTest extends \PHPUnit_Framework_TestCase
2525
/**
2626
* @var SwatchAttributeCodes
2727
*/
28-
protected $swatchAttributeCodesModel;
28+
private $swatchAttributeCodesModel;
2929

3030
/**
3131
* @var CacheInterface
3232
*/
33-
protected $cache;
33+
private $cache;
3434

3535
/**
3636
* @var ResourceConnection;
3737
*/
38-
protected $resourceConnection;
38+
private $resourceConnection;
3939

40-
protected $swatchAttributesCodes = [
40+
private $swatchAttributesCodes = [
4141
10 => 'text_swatch',
4242
11 => 'image_swatch'
4343
];

app/code/Magento/Swatches/Test/Unit/Model/SwatchAttributesProviderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ class SwatchAttributesProviderTest extends \PHPUnit_Framework_TestCase
1616
/**
1717
* @var SwatchAttributesProvider
1818
*/
19-
protected $swatchAttributesProvider;
19+
private $swatchAttributesProvider;
2020

2121
/**
2222
* @var Configurable
2323
*/
24-
protected $typeConfigurableMock;
24+
private $typeConfigurableMock;
2525

2626
/**
2727
* @var SwatchAttributeCodes
2828
*/
29-
protected $swatchAttributeCodesMock;
29+
private $swatchAttributeCodesMock;
3030

3131
/**
3232
* @var \Magento\Catalog\Model\Product
3333
*/
34-
protected $productMock;
34+
private $productMock;
3535

3636
protected function setUp()
3737
{

app/code/Magento/Swatches/etc/di.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,12 @@
7373
</argument>
7474
</arguments>
7575
</type>
76+
<type name="Magento\Swatches\Model\SwatchAttributeCodes">
77+
<arguments>
78+
<argument name="cacheKey" xsi:type="string">swatch-attribute-list</argument>
79+
<argument name="cacheTags" xsi:type="array">
80+
<item name="0" xsi:type="const">Magento\Eav\Model\Entity\Attribute::CACHE_TAG</item>
81+
</argument>
82+
</arguments>
83+
</type>
7684
</config>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Swatches\Model;
7+
8+
class SwatchAttributeCodesTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/** @var \Magento\Swatches\Model\SwatchAttributeCodes */
11+
private $swatchAttributeCodes;
12+
13+
/**
14+
* @var \Magento\Framework\ObjectManagerInterface
15+
*/
16+
private $_objectManager;
17+
18+
protected function setUp()
19+
{
20+
$this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
21+
$this->swatchAttributeCodes = $this->_objectManager->create(
22+
\Magento\Swatches\Model\SwatchAttributeCodes::class
23+
);
24+
}
25+
26+
/**
27+
* @magentoDbIsolation enabled
28+
* @magentoDataFixture Magento/Swatches/_files/swatch_attribute.php
29+
*/
30+
public function testGetCodes()
31+
{
32+
$attribute = $this->_objectManager
33+
->create(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
34+
->load('color_swatch', 'attribute_code');
35+
$expected = [
36+
$attribute->getAttributeId() => $attribute->getAttributeCode()
37+
];
38+
$swatchAttributeCodes = $this->swatchAttributeCodes->getCodes();
39+
40+
$this->assertEquals($expected, $swatchAttributeCodes);
41+
}
42+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
use Magento\Eav\Api\Data\AttributeOptionInterface;
3+
4+
/** @var \Magento\Framework\ObjectManagerInterface $objectManager */
5+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
6+
7+
$data = [
8+
'is_required' => 1,
9+
'is_visible_on_front' => 1,
10+
'is_visible_in_advanced_search' => 0,
11+
'attribute_code' => 'color_swatch',
12+
'backend_type' => '',
13+
'is_searchable' => 0,
14+
'is_filterable' => 0,
15+
'is_filterable_in_search' => 0,
16+
'frontend_label' => 'Attribute ',
17+
'entity_type_id' => 4
18+
];
19+
$optionsPerAttribute = 3;
20+
21+
$data['frontend_input'] = 'swatch_visual';
22+
$data['swatch_input_type'] = 'visual';
23+
$data['swatchvisual']['value'] = array_reduce(
24+
range(1, $optionsPerAttribute),
25+
function ($values, $index) use ($optionsPerAttribute) {
26+
$values['option_' . $index] = '#'
27+
. str_repeat(
28+
dechex(255 * $index / $optionsPerAttribute),
29+
3
30+
);
31+
return $values;
32+
},
33+
[]
34+
);
35+
$data['optionvisual']['value'] = array_reduce(
36+
range(1, $optionsPerAttribute),
37+
function ($values, $index) use ($optionsPerAttribute) {
38+
$values['option_' . $index] = ['option ' . $index];
39+
return $values;
40+
},
41+
[]
42+
);
43+
44+
$data['options']['option'] = array_reduce(
45+
range(1, $optionsPerAttribute),
46+
function ($values, $index) use ($optionsPerAttribute) {
47+
$values[] = [
48+
'label' => 'option ' . $index,
49+
'value' => 'option_' . $index
50+
];
51+
return $values;
52+
},
53+
[]
54+
);
55+
56+
$options = [];
57+
foreach ($data['options']['option'] as $optionData) {
58+
$options[] = $objectManager->get(AttributeOptionInterface::class)
59+
->setLabel($optionData['label'])
60+
->setValue($optionData['value'])
61+
;
62+
}
63+
64+
$attribute = $objectManager->create(
65+
\Magento\Catalog\Api\Data\ProductAttributeInterface::class,
66+
['data' => $data])
67+
;
68+
$attribute->setOptions($options);
69+
$attribute->save();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/** @var \Magento\Framework\Registry $registry */
3+
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
4+
5+
$registry->unregister('isSecureArea');
6+
$registry->register('isSecureArea', true);
7+
8+
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
9+
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
10+
->create(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
11+
12+
$attribute->loadByCode(4, 'color_swatch');
13+
14+
if ($attribute->getId()) {
15+
$attribute->delete();
16+
}
17+
18+
$registry->unregister('isSecureArea');
19+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)