8
8
9
9
namespace Magento \ConfigurableProduct \Test \Unit \Model \Product ;
10
10
11
+ use Magento \Catalog \Model \Product \Type ;
11
12
use Magento \ConfigurableProduct \Model \Product \VariationHandler ;
12
13
13
14
/**
@@ -62,45 +63,45 @@ protected function setUp()
62
63
{
63
64
$ this ->objectHelper = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
64
65
$ this ->productFactoryMock = $ this ->getMock (
65
- ' Magento\Catalog\Model\ProductFactory ' ,
66
+ \ Magento \Catalog \Model \ProductFactory::class ,
66
67
['create ' ],
67
68
[],
68
69
'' ,
69
70
false
70
71
);
71
72
$ this ->entityFactoryMock = $ this ->getMock (
72
- ' Magento\Eav\Model\EntityFactory ' ,
73
+ \ Magento \Eav \Model \EntityFactory::class ,
73
74
['create ' ],
74
75
[],
75
76
'' ,
76
77
false
77
78
);
78
79
$ this ->attributeSetFactory = $ this ->getMock (
79
- ' Magento\Eav\Model\Entity\Attribute\SetFactory ' ,
80
+ \ Magento \Eav \Model \Entity \Attribute \SetFactory::class ,
80
81
['create ' ],
81
82
[],
82
83
'' ,
83
84
false
84
85
);
85
86
$ this ->stockConfiguration = $ this ->getMock (
86
- ' Magento\CatalogInventory\Api\StockConfigurationInterface ' ,
87
+ \ Magento \CatalogInventory \Api \StockConfigurationInterface::class ,
87
88
[],
88
89
[],
89
90
'' ,
90
91
false
91
92
);
92
93
$ this ->configurableProduct = $ this ->getMock (
93
- ' Magento\ConfigurableProduct\Model\Product\Type\Configurable ' ,
94
+ \ Magento \ConfigurableProduct \Model \Product \Type \Configurable::class ,
94
95
[],
95
96
[],
96
97
'' ,
97
98
false
98
99
);
99
100
100
- $ this ->product = $ this ->getMock (' Magento\Catalog\Model\Product ' , ['getMediaGallery ' ], [], '' , false );
101
+ $ this ->product = $ this ->getMock (\ Magento \Catalog \Model \Product::class , ['getMediaGallery ' ], [], '' , false );
101
102
102
103
$ this ->model = $ this ->objectHelper ->getObject (
103
- ' Magento\ConfigurableProduct\Model\Product\VariationHandler ' ,
104
+ \ Magento \ConfigurableProduct \Model \Product \VariationHandler::class ,
104
105
[
105
106
'productFactory ' => $ this ->productFactoryMock ,
106
107
'entityFactory ' => $ this ->entityFactoryMock ,
@@ -114,19 +115,19 @@ protected function setUp()
114
115
public function testPrepareAttributeSet ()
115
116
{
116
117
117
- $ productMock = $ this ->getMockBuilder (' \Magento\Catalog\Model\Product ' )
118
+ $ productMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class )
118
119
->setMethods (['getNewVariationsAttributeSetId ' ])
119
120
->disableOriginalConstructor ()
120
121
->getMock ();
121
- $ attributeMock = $ this ->getMockBuilder (' \Magento\Eav\Model\Entity\Attribute ' )
122
+ $ attributeMock = $ this ->getMockBuilder (\Magento \Eav \Model \Entity \Attribute::class )
122
123
->setMethods (['isInSet ' , 'setAttributeSetId ' , 'setAttributeGroupId ' , 'save ' ])
123
124
->disableOriginalConstructor ()
124
125
->getMock ();
125
- $ attributeSetMock = $ this ->getMockBuilder (' Magento\Eav\Model\Entity\Attribute\Set ' )
126
+ $ attributeSetMock = $ this ->getMockBuilder (\ Magento \Eav \Model \Entity \Attribute \Set::class )
126
127
->setMethods (['load ' , 'addSetInfo ' , 'getDefaultGroupId ' ])
127
128
->disableOriginalConstructor ()
128
129
->getMock ();
129
- $ eavEntityMock = $ this ->getMockBuilder (' \Magento\Eav\Model\Entity ' )
130
+ $ eavEntityMock = $ this ->getMockBuilder (\Magento \Eav \Model \Entity::class )
130
131
->setMethods (['setType ' , 'getTypeId ' ])
131
132
->disableOriginalConstructor ()
132
133
->getMock ();
@@ -161,23 +162,30 @@ public function testPrepareAttributeSet()
161
162
162
163
/**
163
164
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
165
+ * @dataProvider dataProviderTestGenerateSimpleProducts
166
+ * @param int|string|null $weight
167
+ * @param string $typeId
164
168
*/
165
- public function testGenerateSimpleProducts ()
169
+ public function testGenerateSimpleProducts ($ weight , $ typeId )
166
170
{
167
171
$ 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
- ]
172
+ [
173
+ 'image ' => 'image.jpg ' ,
174
+ 'name ' => 'config-red ' ,
175
+ 'configurable_attribute ' => '{"new_attr":"6"} ' ,
176
+ 'sku ' => 'config-red ' ,
177
+ 'quantity_and_stock_status ' =>
178
+ [
179
+ 'qty ' => '' ,
180
+ ],
181
+ ]
180
182
];
183
+
184
+ // Do not add 'weight' attribute if it's value is null!
185
+ if ($ weight !== null ) {
186
+ $ productsData [0 ]['weight ' ] = $ weight ;
187
+ }
188
+
181
189
$ stockData = [
182
190
'manage_stock ' => '0 ' ,
183
191
'use_config_enable_qty_increments ' => '1 ' ,
@@ -186,7 +194,7 @@ public function testGenerateSimpleProducts()
186
194
'is_decimal_divided ' => 0
187
195
];
188
196
189
- $ parentProductMock = $ this ->getMockBuilder (' \Magento\Catalog\Model\Product ' )
197
+ $ parentProductMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class )
190
198
->setMethods (
191
199
[
192
200
'__wakeup ' ,
@@ -198,7 +206,7 @@ public function testGenerateSimpleProducts()
198
206
)
199
207
->disableOriginalConstructor ()
200
208
->getMock ();
201
- $ newSimpleProductMock = $ this ->getMockBuilder (' \Magento\Catalog\Model\Product ' )
209
+ $ newSimpleProductMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class )
202
210
->setMethods (
203
211
[
204
212
'__wakeup ' ,
@@ -217,15 +225,15 @@ public function testGenerateSimpleProducts()
217
225
)
218
226
->disableOriginalConstructor ()
219
227
->getMock ();
220
- $ productTypeMock = $ this ->getMockBuilder (' Magento\Catalog\Model\Product\ Type' )
228
+ $ productTypeMock = $ this ->getMockBuilder (Type::class )
221
229
->setMethods (['getSetAttributes ' ])
222
230
->disableOriginalConstructor ()
223
231
->getMock ();
224
- $ editableAttributeMock = $ this ->getMockBuilder (' Magento\Eav\Model\Entity\Attribute ' )
232
+ $ editableAttributeMock = $ this ->getMockBuilder (\ Magento \Eav \Model \Entity \Attribute::class )
225
233
->setMethods (['getIsUnique ' , 'getAttributeCode ' , 'getFrontend ' , 'getIsVisible ' ])
226
234
->disableOriginalConstructor ()
227
235
->getMock ();
228
- $ frontendAttributeMock = $ this ->getMockBuilder (' Magento\Eav\Model\Entity\Attribute\Frontend ' )
236
+ $ frontendAttributeMock = $ this ->getMockBuilder (\ Magento \Eav \Model \Entity \Attribute \Frontend::class )
229
237
->setMethods (['getInputType ' ])
230
238
->disableOriginalConstructor ()
231
239
->getMock ();
@@ -235,7 +243,7 @@ public function testGenerateSimpleProducts()
235
243
->willReturn ('new_attr_set_id ' );
236
244
$ this ->productFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ newSimpleProductMock );
237
245
$ newSimpleProductMock ->expects ($ this ->once ())->method ('setStoreId ' )->with (0 )->willReturnSelf ();
238
- $ newSimpleProductMock ->expects ($ this ->once ())->method ('setTypeId ' )->with (' simple ' )->willReturnSelf ();
246
+ $ newSimpleProductMock ->expects ($ this ->once ())->method ('setTypeId ' )->with ($ typeId )->willReturnSelf ();
239
247
$ newSimpleProductMock ->expects ($ this ->once ())
240
248
->method ('setAttributeSetId ' )
241
249
->with ('new_attr_set_id ' )
@@ -264,6 +272,27 @@ public function testGenerateSimpleProducts()
264
272
$ this ->assertEquals (['product_id ' ], $ this ->model ->generateSimpleProducts ($ parentProductMock , $ productsData ));
265
273
}
266
274
275
+ /**
276
+ * @return array
277
+ */
278
+ public function dataProviderTestGenerateSimpleProducts ()
279
+ {
280
+ return [
281
+ [
282
+ 'weight ' => 333 ,
283
+ 'type_id ' => Type::TYPE_SIMPLE ,
284
+ ],
285
+ [
286
+ 'weight ' => '' ,
287
+ 'type_id ' => Type::TYPE_VIRTUAL ,
288
+ ],
289
+ [
290
+ 'weight ' => null ,
291
+ 'type_id ' => Type::TYPE_VIRTUAL ,
292
+ ],
293
+ ];
294
+ }
295
+
267
296
public function testProcessMediaGalleryWithImagesAndGallery ()
268
297
{
269
298
$ this ->product ->expects ($ this ->atLeastOnce ())->method ('getMediaGallery ' )->with ('images ' )->willReturn ([]);
0 commit comments