Skip to content

Commit e44f88c

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-58395' into 2.1-develop-pr30
2 parents e56ab24 + 75754be commit e44f88c

File tree

3 files changed

+88
-37
lines changed

3 files changed

+88
-37
lines changed

app/code/Magento/ConfigurableProduct/Model/Product/VariationHandler.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,14 @@ protected function fillSimpleProductData(
152152
\Magento\Catalog\Model\Product $parentProduct,
153153
$postData
154154
) {
155+
$typeId = !empty($postData['weight'])
156+
? ProductType::TYPE_SIMPLE
157+
: ProductType::TYPE_VIRTUAL;
158+
155159
$product->setStoreId(
156160
\Magento\Store\Model\Store::DEFAULT_STORE_ID
157161
)->setTypeId(
158-
$postData['weight'] ? ProductType::TYPE_SIMPLE : ProductType::TYPE_VIRTUAL
162+
$typeId
159163
)->setAttributeSetId(
160164
$parentProduct->getNewVariationsAttributeSetId()
161165
);

app/code/Magento/ConfigurableProduct/Test/Unit/Model/Product/VariationHandlerTest.php

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
// @codingStandardsIgnoreFile
8-
97
namespace Magento\ConfigurableProduct\Test\Unit\Model\Product;
108

9+
use Magento\Catalog\Model\Product\Type;
1110
use Magento\ConfigurableProduct\Model\Product\VariationHandler;
1211

1312
/**
14-
* @SuppressWarnings(PHPMD.LongVariable)
13+
* Tests \Magento\ConfigurableProduct\Model\Product\VariationHandler.
14+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1515
*/
1616
class VariationHandlerTest extends \PHPUnit_Framework_TestCase
1717
{
@@ -62,45 +62,45 @@ protected function setUp()
6262
{
6363
$this->objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
6464
$this->productFactoryMock = $this->getMock(
65-
'Magento\Catalog\Model\ProductFactory',
65+
\Magento\Catalog\Model\ProductFactory::class,
6666
['create'],
6767
[],
6868
'',
6969
false
7070
);
7171
$this->entityFactoryMock = $this->getMock(
72-
'Magento\Eav\Model\EntityFactory',
72+
\Magento\Eav\Model\EntityFactory::class,
7373
['create'],
7474
[],
7575
'',
7676
false
7777
);
7878
$this->attributeSetFactory = $this->getMock(
79-
'Magento\Eav\Model\Entity\Attribute\SetFactory',
79+
\Magento\Eav\Model\Entity\Attribute\SetFactory::class,
8080
['create'],
8181
[],
8282
'',
8383
false
8484
);
8585
$this->stockConfiguration = $this->getMock(
86-
'Magento\CatalogInventory\Api\StockConfigurationInterface',
86+
\Magento\CatalogInventory\Api\StockConfigurationInterface::class,
8787
[],
8888
[],
8989
'',
9090
false
9191
);
9292
$this->configurableProduct = $this->getMock(
93-
'Magento\ConfigurableProduct\Model\Product\Type\Configurable',
93+
\Magento\ConfigurableProduct\Model\Product\Type\Configurable::class,
9494
[],
9595
[],
9696
'',
9797
false
9898
);
9999

100-
$this->product = $this->getMock('Magento\Catalog\Model\Product', ['getMediaGallery'], [], '', false);
100+
$this->product = $this->getMock(\Magento\Catalog\Model\Product::class, ['getMediaGallery'], [], '', false);
101101

102102
$this->model = $this->objectHelper->getObject(
103-
'Magento\ConfigurableProduct\Model\Product\VariationHandler',
103+
VariationHandler::class,
104104
[
105105
'productFactory' => $this->productFactoryMock,
106106
'entityFactory' => $this->entityFactoryMock,
@@ -114,19 +114,19 @@ protected function setUp()
114114
public function testPrepareAttributeSet()
115115
{
116116

117-
$productMock = $this->getMockBuilder('\Magento\Catalog\Model\Product')
117+
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
118118
->setMethods(['getNewVariationsAttributeSetId'])
119119
->disableOriginalConstructor()
120120
->getMock();
121-
$attributeMock = $this->getMockBuilder('\Magento\Eav\Model\Entity\Attribute')
121+
$attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute::class)
122122
->setMethods(['isInSet', 'setAttributeSetId', 'setAttributeGroupId', 'save'])
123123
->disableOriginalConstructor()
124124
->getMock();
125-
$attributeSetMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\Set')
125+
$attributeSetMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Set::class)
126126
->setMethods(['load', 'addSetInfo', 'getDefaultGroupId'])
127127
->disableOriginalConstructor()
128128
->getMock();
129-
$eavEntityMock = $this->getMockBuilder('\Magento\Eav\Model\Entity')
129+
$eavEntityMock = $this->getMockBuilder(\Magento\Eav\Model\Entity::class)
130130
->setMethods(['setType', 'getTypeId'])
131131
->disableOriginalConstructor()
132132
->getMock();
@@ -161,23 +161,30 @@ public function testPrepareAttributeSet()
161161

162162
/**
163163
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
164+
* @dataProvider dataProviderTestGenerateSimpleProducts
165+
* @param int|string|null $weight
166+
* @param string $typeId
164167
*/
165-
public function testGenerateSimpleProducts()
168+
public function testGenerateSimpleProducts($weight, $typeId)
166169
{
167170
$productsData = [
168-
6 =>
169-
[
170-
'image' => 'image.jpg',
171-
'name' => 'config-red',
172-
'configurable_attribute' => '{"new_attr":"6"}',
173-
'sku' => 'config-red',
174-
'quantity_and_stock_status' =>
175-
[
176-
'qty' => '',
177-
],
178-
'weight' => '333',
179-
]
171+
[
172+
'image' => 'image.jpg',
173+
'name' => 'config-red',
174+
'configurable_attribute' => '{"new_attr":"6"}',
175+
'sku' => 'config-red',
176+
'quantity_and_stock_status' =>
177+
[
178+
'qty' => '',
179+
],
180+
]
180181
];
182+
183+
// Do not add 'weight' attribute if it's value is null!
184+
if ($weight !== null) {
185+
$productsData[0]['weight'] = $weight;
186+
}
187+
181188
$stockData = [
182189
'manage_stock' => '0',
183190
'use_config_enable_qty_increments' => '1',
@@ -186,7 +193,7 @@ public function testGenerateSimpleProducts()
186193
'is_decimal_divided' => 0
187194
];
188195

189-
$parentProductMock = $this->getMockBuilder('\Magento\Catalog\Model\Product')
196+
$parentProductMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
190197
->setMethods(
191198
[
192199
'__wakeup',
@@ -198,7 +205,7 @@ public function testGenerateSimpleProducts()
198205
)
199206
->disableOriginalConstructor()
200207
->getMock();
201-
$newSimpleProductMock = $this->getMockBuilder('\Magento\Catalog\Model\Product')
208+
$newSimpleProductMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
202209
->setMethods(
203210
[
204211
'__wakeup',
@@ -217,15 +224,15 @@ public function testGenerateSimpleProducts()
217224
)
218225
->disableOriginalConstructor()
219226
->getMock();
220-
$productTypeMock = $this->getMockBuilder('Magento\Catalog\Model\Product\Type')
227+
$productTypeMock = $this->getMockBuilder(Type::class)
221228
->setMethods(['getSetAttributes'])
222229
->disableOriginalConstructor()
223230
->getMock();
224-
$editableAttributeMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute')
231+
$editableAttributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute::class)
225232
->setMethods(['getIsUnique', 'getAttributeCode', 'getFrontend', 'getIsVisible'])
226233
->disableOriginalConstructor()
227234
->getMock();
228-
$frontendAttributeMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\Frontend')
235+
$frontendAttributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Frontend::class)
229236
->setMethods(['getInputType'])
230237
->disableOriginalConstructor()
231238
->getMock();
@@ -235,7 +242,7 @@ public function testGenerateSimpleProducts()
235242
->willReturn('new_attr_set_id');
236243
$this->productFactoryMock->expects($this->once())->method('create')->willReturn($newSimpleProductMock);
237244
$newSimpleProductMock->expects($this->once())->method('setStoreId')->with(0)->willReturnSelf();
238-
$newSimpleProductMock->expects($this->once())->method('setTypeId')->with('simple')->willReturnSelf();
245+
$newSimpleProductMock->expects($this->once())->method('setTypeId')->with($typeId)->willReturnSelf();
239246
$newSimpleProductMock->expects($this->once())
240247
->method('setAttributeSetId')
241248
->with('new_attr_set_id')
@@ -264,6 +271,27 @@ public function testGenerateSimpleProducts()
264271
$this->assertEquals(['product_id'], $this->model->generateSimpleProducts($parentProductMock, $productsData));
265272
}
266273

274+
/**
275+
* @return array
276+
*/
277+
public function dataProviderTestGenerateSimpleProducts()
278+
{
279+
return [
280+
[
281+
'weight' => 333,
282+
'type_id' => Type::TYPE_SIMPLE,
283+
],
284+
[
285+
'weight' => '',
286+
'type_id' => Type::TYPE_VIRTUAL,
287+
],
288+
[
289+
'weight' => null,
290+
'type_id' => Type::TYPE_VIRTUAL,
291+
],
292+
];
293+
}
294+
267295
public function testProcessMediaGalleryWithImagesAndGallery()
268296
{
269297
$this->product->expects($this->atLeastOnce())->method('getMediaGallery')->with('images')->willReturn([]);

dev/tests/integration/testsuite/Magento/ConfigurableProduct/Model/Product/VariationHandlerTest.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,29 @@ public function testGenerateSimpleProducts($productsData)
4848
$this->_product->setNewVariationsAttributeSetId(4);
4949
// Default attribute set id
5050
$generatedProducts = $this->_model->generateSimpleProducts($this->_product, $productsData);
51-
$this->assertEquals(3, count($generatedProducts));
51+
$this->assertEquals(4, count($generatedProducts));
5252
foreach ($generatedProducts as $productId) {
5353
$stockItem = $this->stockRegistry->getStockItem($productId);
5454
/** @var $product \Magento\Catalog\Model\Product */
5555
$product = Bootstrap::getObjectManager()->create(
56-
'Magento\Catalog\Model\Product'
56+
\Magento\Catalog\Model\Product::class
5757
);
5858
$product->load($productId);
59+
60+
$productDataWeight = null;
61+
foreach ($productsData as $productItemData) {
62+
if ($productItemData['name'] == $product->getName()
63+
&& isset($productItemData['weight'])
64+
) {
65+
$productDataWeight = (int)$productItemData['weight'];
66+
break;
67+
}
68+
}
69+
5970
$this->assertNotNull($product->getName());
6071
$this->assertNotNull($product->getSku());
6172
$this->assertNotNull($product->getPrice());
62-
$this->assertNotNull($product->getWeight());
73+
$this->assertEquals($productDataWeight, (int)$product->getWeight());
6374
$this->assertEquals('1', $stockItem->getIsInStock());
6475
}
6576
}
@@ -113,6 +124,14 @@ public static function generateSimpleProductsDataProvider()
113124
'quantity_and_stock_status' => ['qty' => '5'],
114125
'weight' => '6'
115126
],
127+
[
128+
'name' => '1-ddd',
129+
'configurable_attribute' => '{"configurable_attribute":"22"}',
130+
'price' => '3',
131+
'sku' => '1-ddd',
132+
'quantity_and_stock_status' => ['qty' => '5'],
133+
'weight' => ''
134+
],
116135
],
117136
]
118137
];

0 commit comments

Comments
 (0)