4
4
* See COPYING.txt for license details.
5
5
*/
6
6
7
- // @codingStandardsIgnoreFile
8
-
9
7
namespace Magento \ConfigurableProduct \Test \Unit \Model \Product ;
10
8
9
+ use Magento \Catalog \Model \Product \Type ;
11
10
use Magento \ConfigurableProduct \Model \Product \VariationHandler ;
12
11
13
12
/**
14
- * @SuppressWarnings(PHPMD.LongVariable)
13
+ * Tests \Magento\ConfigurableProduct\Model\Product\VariationHandler.
14
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15
15
*/
16
16
class VariationHandlerTest extends \PHPUnit_Framework_TestCase
17
17
{
@@ -62,45 +62,45 @@ protected function setUp()
62
62
{
63
63
$ this ->objectHelper = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
64
64
$ this ->productFactoryMock = $ this ->getMock (
65
- ' Magento\Catalog\Model\ProductFactory ' ,
65
+ \ Magento \Catalog \Model \ProductFactory::class ,
66
66
['create ' ],
67
67
[],
68
68
'' ,
69
69
false
70
70
);
71
71
$ this ->entityFactoryMock = $ this ->getMock (
72
- ' Magento\Eav\Model\EntityFactory ' ,
72
+ \ Magento \Eav \Model \EntityFactory::class ,
73
73
['create ' ],
74
74
[],
75
75
'' ,
76
76
false
77
77
);
78
78
$ this ->attributeSetFactory = $ this ->getMock (
79
- ' Magento\Eav\Model\Entity\Attribute\SetFactory ' ,
79
+ \ Magento \Eav \Model \Entity \Attribute \SetFactory::class ,
80
80
['create ' ],
81
81
[],
82
82
'' ,
83
83
false
84
84
);
85
85
$ this ->stockConfiguration = $ this ->getMock (
86
- ' Magento\CatalogInventory\Api\StockConfigurationInterface ' ,
86
+ \ Magento \CatalogInventory \Api \StockConfigurationInterface::class ,
87
87
[],
88
88
[],
89
89
'' ,
90
90
false
91
91
);
92
92
$ this ->configurableProduct = $ this ->getMock (
93
- ' Magento\ConfigurableProduct\Model\Product\Type\Configurable ' ,
93
+ \ Magento \ConfigurableProduct \Model \Product \Type \Configurable::class ,
94
94
[],
95
95
[],
96
96
'' ,
97
97
false
98
98
);
99
99
100
- $ this ->product = $ this ->getMock (' Magento\Catalog\Model\Product ' , ['getMediaGallery ' ], [], '' , false );
100
+ $ this ->product = $ this ->getMock (\ Magento \Catalog \Model \Product::class , ['getMediaGallery ' ], [], '' , false );
101
101
102
102
$ this ->model = $ this ->objectHelper ->getObject (
103
- ' Magento\ConfigurableProduct\Model\Product\ VariationHandler' ,
103
+ VariationHandler::class ,
104
104
[
105
105
'productFactory ' => $ this ->productFactoryMock ,
106
106
'entityFactory ' => $ this ->entityFactoryMock ,
@@ -114,19 +114,19 @@ protected function setUp()
114
114
public function testPrepareAttributeSet ()
115
115
{
116
116
117
- $ productMock = $ this ->getMockBuilder (' \Magento\Catalog\Model\Product ' )
117
+ $ productMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class )
118
118
->setMethods (['getNewVariationsAttributeSetId ' ])
119
119
->disableOriginalConstructor ()
120
120
->getMock ();
121
- $ attributeMock = $ this ->getMockBuilder (' \Magento\Eav\Model\Entity\Attribute ' )
121
+ $ attributeMock = $ this ->getMockBuilder (\Magento \Eav \Model \Entity \Attribute::class )
122
122
->setMethods (['isInSet ' , 'setAttributeSetId ' , 'setAttributeGroupId ' , 'save ' ])
123
123
->disableOriginalConstructor ()
124
124
->getMock ();
125
- $ attributeSetMock = $ this ->getMockBuilder (' Magento\Eav\Model\Entity\Attribute\Set ' )
125
+ $ attributeSetMock = $ this ->getMockBuilder (\ Magento \Eav \Model \Entity \Attribute \Set::class )
126
126
->setMethods (['load ' , 'addSetInfo ' , 'getDefaultGroupId ' ])
127
127
->disableOriginalConstructor ()
128
128
->getMock ();
129
- $ eavEntityMock = $ this ->getMockBuilder (' \Magento\Eav\Model\Entity ' )
129
+ $ eavEntityMock = $ this ->getMockBuilder (\Magento \Eav \Model \Entity::class )
130
130
->setMethods (['setType ' , 'getTypeId ' ])
131
131
->disableOriginalConstructor ()
132
132
->getMock ();
@@ -161,23 +161,30 @@ public function testPrepareAttributeSet()
161
161
162
162
/**
163
163
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
164
+ * @dataProvider dataProviderTestGenerateSimpleProducts
165
+ * @param int|string|null $weight
166
+ * @param string $typeId
164
167
*/
165
- public function testGenerateSimpleProducts ()
168
+ public function testGenerateSimpleProducts ($ weight , $ typeId )
166
169
{
167
170
$ 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
+ ]
180
181
];
182
+
183
+ // Do not add 'weight' attribute if it's value is null!
184
+ if ($ weight !== null ) {
185
+ $ productsData [0 ]['weight ' ] = $ weight ;
186
+ }
187
+
181
188
$ stockData = [
182
189
'manage_stock ' => '0 ' ,
183
190
'use_config_enable_qty_increments ' => '1 ' ,
@@ -186,7 +193,7 @@ public function testGenerateSimpleProducts()
186
193
'is_decimal_divided ' => 0
187
194
];
188
195
189
- $ parentProductMock = $ this ->getMockBuilder (' \Magento\Catalog\Model\Product ' )
196
+ $ parentProductMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class )
190
197
->setMethods (
191
198
[
192
199
'__wakeup ' ,
@@ -198,7 +205,7 @@ public function testGenerateSimpleProducts()
198
205
)
199
206
->disableOriginalConstructor ()
200
207
->getMock ();
201
- $ newSimpleProductMock = $ this ->getMockBuilder (' \Magento\Catalog\Model\Product ' )
208
+ $ newSimpleProductMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class )
202
209
->setMethods (
203
210
[
204
211
'__wakeup ' ,
@@ -217,15 +224,15 @@ public function testGenerateSimpleProducts()
217
224
)
218
225
->disableOriginalConstructor ()
219
226
->getMock ();
220
- $ productTypeMock = $ this ->getMockBuilder (' Magento\Catalog\Model\Product\ Type' )
227
+ $ productTypeMock = $ this ->getMockBuilder (Type::class )
221
228
->setMethods (['getSetAttributes ' ])
222
229
->disableOriginalConstructor ()
223
230
->getMock ();
224
- $ editableAttributeMock = $ this ->getMockBuilder (' Magento\Eav\Model\Entity\Attribute ' )
231
+ $ editableAttributeMock = $ this ->getMockBuilder (\ Magento \Eav \Model \Entity \Attribute::class )
225
232
->setMethods (['getIsUnique ' , 'getAttributeCode ' , 'getFrontend ' , 'getIsVisible ' ])
226
233
->disableOriginalConstructor ()
227
234
->getMock ();
228
- $ frontendAttributeMock = $ this ->getMockBuilder (' Magento\Eav\Model\Entity\Attribute\Frontend ' )
235
+ $ frontendAttributeMock = $ this ->getMockBuilder (\ Magento \Eav \Model \Entity \Attribute \Frontend::class )
229
236
->setMethods (['getInputType ' ])
230
237
->disableOriginalConstructor ()
231
238
->getMock ();
@@ -235,7 +242,7 @@ public function testGenerateSimpleProducts()
235
242
->willReturn ('new_attr_set_id ' );
236
243
$ this ->productFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ newSimpleProductMock );
237
244
$ 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 ();
239
246
$ newSimpleProductMock ->expects ($ this ->once ())
240
247
->method ('setAttributeSetId ' )
241
248
->with ('new_attr_set_id ' )
@@ -264,6 +271,27 @@ public function testGenerateSimpleProducts()
264
271
$ this ->assertEquals (['product_id ' ], $ this ->model ->generateSimpleProducts ($ parentProductMock , $ productsData ));
265
272
}
266
273
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
+
267
295
public function testProcessMediaGalleryWithImagesAndGallery ()
268
296
{
269
297
$ this ->product ->expects ($ this ->atLeastOnce ())->method ('getMediaGallery ' )->with ('images ' )->willReturn ([]);
0 commit comments