Skip to content

Commit 8b2c486

Browse files
ACPT-490
updating unit tests
1 parent 00ae524 commit 8b2c486

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
namespace Magento\CatalogImportExport\Model\Import\Product\Type;
77

8-
use Magento\Catalog\Api\Data\ProductAttributeInterface;
8+
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
99
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;
1010
use Magento\Eav\Model\Entity\Attribute\Source\Table;
1111
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory as AttributeOptionCollectionFactory;
@@ -369,10 +369,7 @@ protected function attachAttributesById($attributeSetName, $attributeIds)
369369
{
370370
foreach ($this->_prodAttrColFac->create()->addFieldToFilter(
371371
['main_table.attribute_id', 'main_table.attribute_code'],
372-
[
373-
['in' => $attributeIds],
374-
['in' => $this->_forcedAttributesCodes]
375-
]
372+
[['in' => $attributeIds], ['in' => $this->_forcedAttributesCodes]]
376373
) as $attribute) {
377374
$this->attachAttribute($attribute);
378375
}
@@ -388,7 +385,7 @@ private function attachAttributesByOnlyId(array $attributeIds) : array
388385
{
389386
$addedAttributes = [];
390387
foreach ($this->_prodAttrColFac->create()->addFieldToFilter(
391-
['main_table.attribute_id', 'main_table.attribute_code'],
388+
['main_table.attribute_id'],
392389
[['in' => $attributeIds]]
393390
) as $attribute) {
394391
$cachedAttribute = $this->attachAttribute($attribute);
@@ -422,10 +419,10 @@ private function attachAttributesByForcedCodes() : array
422419
/**
423420
* Attach Attribute to self::$commonAttributesCache or self::$invAttributesCache
424421
*
425-
* @param ProductAttributeInterface $attribute
422+
* @param Attribute $attribute
426423
* @return array|null
427424
*/
428-
private function attachAttribute(ProductAttributeInterface $attribute)
425+
private function attachAttribute(Attribute $attribute)
429426
{
430427
$cachedAttribute = null;
431428
$attributeCode = $attribute->getAttributeCode();
@@ -629,7 +626,6 @@ public function isSuitable()
629626
public function prepareAttributesWithDefaultValueForSave(array $rowData, $withDefaultValue = true)
630627
{
631628
$resultAttrs = [];
632-
633629
foreach ($this->_getProductAttributes($rowData) as $attrCode => $attrParams) {
634630
if ($attrParams['is_static']) {
635631
continue;
@@ -653,7 +649,6 @@ public function prepareAttributesWithDefaultValueForSave(array $rowData, $withDe
653649
$resultAttrs[$attrCode] = $attrParams['default_value'];
654650
}
655651
}
656-
657652
return $resultAttrs;
658653
}
659654

app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/Type/AbstractTypeTest.php

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
*/
77
namespace Magento\CatalogImportExport\Test\Unit\Model\Import\Product\Type;
88

9+
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
910
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;
1011
use Magento\CatalogImportExport\Model\Import\Product;
1112
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;
1213
use Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType as AbstractType;
1314
use Magento\CatalogImportExport\Model\Import\Product\Type\Simple;
14-
use Magento\Eav\Model\Entity\Attribute;
1515
use Magento\Eav\Model\Entity\Attribute\Set;
1616
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection;
1717
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory as AttributeSetCollectionFactory;
@@ -87,7 +87,7 @@ protected function setUp(): void
8787
]
8888
);
8989
$attribute = $this->getMockBuilder(Attribute::class)
90-
->addMethods(['getIsVisible', 'getIsGlobal', 'getFrontendLabel', 'getApplyTo'])
90+
->addMethods(['getFrontendLabel'])
9191
->onlyMethods(
9292
[
9393
'getAttributeCode',
@@ -97,7 +97,10 @@ protected function setUp(): void
9797
'isStatic',
9898
'getDefaultValue',
9999
'usesSource',
100-
'getFrontendInput'
100+
'getFrontendInput',
101+
'getIsVisible',
102+
'getApplyTo',
103+
'getIsGlobal',
101104
]
102105
)
103106
->disableOriginalConstructor()
@@ -118,21 +121,23 @@ protected function setUp(): void
118121
->willReturn('default_value');
119122
$attribute->method('usesSource')
120123
->willReturn(true);
121-
122124
$entityAttributes = [
123125
[
124-
'attribute_id' => 'attribute_id',
126+
'attribute_id' => '1',
125127
'attribute_set_name' => 'attributeSetName',
126128
],
127129
[
128-
'attribute_id' => 'boolean_attribute',
130+
'attribute_id' => '2',
129131
'attribute_set_name' => 'attributeSetName'
130-
]
132+
],
133+
[
134+
'attribute_id' => '3',
135+
'attribute_set_name' => 'attributeSetName'
136+
],
131137
];
132138
$attribute1 = clone $attribute;
133139
$attribute2 = clone $attribute;
134140
$attribute3 = clone $attribute;
135-
136141
$attribute1->method('getId')
137142
->willReturn('1');
138143
$attribute1->method('getAttributeCode')
@@ -141,7 +146,6 @@ protected function setUp(): void
141146
->willReturn('multiselect');
142147
$attribute1->method('isStatic')
143148
->willReturn(true);
144-
145149
$attribute2->method('getId')
146150
->willReturn('2');
147151
$attribute2->method('getAttributeCode')
@@ -150,7 +154,6 @@ protected function setUp(): void
150154
->willReturn('boolean');
151155
$attribute2->method('isStatic')
152156
->willReturn(false);
153-
154157
$attribute3->method('getId')
155158
->willReturn('3');
156159
$attribute3->method('getAttributeCode')
@@ -159,7 +162,6 @@ protected function setUp(): void
159162
->willReturn('text');
160163
$attribute3->method('isStatic')
161164
->willReturn(false);
162-
163165
$this->entityModel->method('getEntityTypeId')
164166
->willReturn(3);
165167
$this->entityModel->method('getAttributeOptions')
@@ -179,32 +181,34 @@ protected function setUp(): void
179181
->willReturn(1);
180182
$attributeSet->method('getAttributeSetName')
181183
->willReturn('attribute_set_name');
182-
183184
$attrCollection->method('addFieldToFilter')
184-
->with(
185-
['main_table.attribute_id', 'main_table.attribute_code'],
185+
->withConsecutive(
186186
[
187+
['main_table.attribute_id'],
187188
[
188-
'in' => [
189-
'attribute_id',
190-
'boolean_attribute',
189+
[
190+
'in' => ['1', '2', '3'],
191191
],
192-
],
192+
]
193+
],
194+
[
195+
['main_table.attribute_code'],
193196
[
194-
'in' => [
195-
'related_tgtr_position_behavior',
196-
'related_tgtr_position_limit',
197-
'upsell_tgtr_position_behavior',
198-
'upsell_tgtr_position_limit',
199-
'thumbnail_label',
200-
'small_image_label',
201-
'image_label',
197+
[
198+
'in' => [
199+
'related_tgtr_position_behavior',
200+
'related_tgtr_position_limit',
201+
'upsell_tgtr_position_behavior',
202+
'upsell_tgtr_position_limit',
203+
'thumbnail_label',
204+
'small_image_label',
205+
'image_label',
206+
],
202207
],
203-
],
204-
]
208+
]
209+
],
205210
)
206-
->willReturn([$attribute1, $attribute2, $attribute3]);
207-
211+
->willReturnOnConsecutiveCalls([$attribute1, $attribute2, $attribute3], []);
208212
$this->connection = $this->getMockBuilder(Mysql::class)
209213
->addMethods(['joinLeft'])
210214
->onlyMethods(['select', 'fetchAll', 'fetchPairs', 'insertOnDuplicate', 'delete', 'quoteInto'])
@@ -240,7 +244,6 @@ protected function setUp(): void
240244
->willReturn('');
241245
$this->connection->method('fetchAll')
242246
->willReturn($entityAttributes);
243-
244247
$this->resource = $this->createPartialMock(
245248
ResourceConnection::class,
246249
[
@@ -252,7 +255,6 @@ protected function setUp(): void
252255
->willReturn($this->connection);
253256
$this->resource->method('getTableName')
254257
->willReturn('tableName');
255-
256258
$this->objectManagerHelper = new ObjectManagerHelper($this);
257259
$this->simpleType = $this->objectManagerHelper->getObject(
258260
Simple::class,
@@ -263,12 +265,24 @@ protected function setUp(): void
263265
'resource' => $this->resource,
264266
]
265267
);
266-
267268
$this->abstractType = $this->getMockBuilder(AbstractType::class)
268269
->disableOriginalConstructor()
269270
->getMockForAbstractClass();
270271
}
271272

273+
/**
274+
* Because AbstractType has static member variables, we must clean them in between tests.
275+
* Luckily they are publicly accessible.
276+
*
277+
* @return void
278+
*/
279+
protected function tearDown(): void
280+
{
281+
AbstractType::$commonAttributesCache = [];
282+
AbstractType::$invAttributesCache = [];
283+
AbstractType::$attributeCodeToId = [];
284+
}
285+
272286
/**
273287
* @dataProvider addAttributeOptionDataProvider
274288
*/
@@ -428,7 +442,6 @@ public function testPrepareAttributesWithDefaultValueForSave()
428442
'_attribute_set' => 'attributeSetName',
429443
'boolean_attribute' => 'Yes',
430444
];
431-
432445
$expected = [
433446
'boolean_attribute' => 1,
434447
'text_attribute' => 'default_value'

0 commit comments

Comments
 (0)