@@ -25,6 +25,11 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase
25
25
*/
26
26
protected $ productOptionRepositoryMock ;
27
27
28
+ /**
29
+ * @var \PHPUnit_Framework_MockObject_MockObject
30
+ */
31
+ protected $ productFactoryMock ;
32
+
28
33
/**
29
34
* @var \PHPUnit_Framework_MockObject_MockObject
30
35
*/
@@ -70,19 +75,29 @@ protected function setUp()
70
75
$ this ->productInterfaceMock = $ this ->getMock ('\Magento\Catalog\Api\Data\ProductInterface ' );
71
76
$ this ->productMock = $ this ->getMock (
72
77
'Magento\Catalog\Model\Product ' ,
73
- ['getExtensionAttributes ' , 'getTypeId ' , 'getSku ' , 'getStoreId ' , 'getId ' ],
78
+ ['getExtensionAttributes ' , 'getTypeId ' , 'getSku ' , 'getStoreId ' , 'getId ' , ' getTypeInstance ' ],
74
79
[],
75
80
'' ,
76
81
false
77
82
);
78
83
$ this ->closureMock = function () {
79
84
return $ this ->productMock ;
80
85
};
81
- $ this ->plugin = new AroundProductRepositorySave (
82
- $ this ->productOptionRepositoryMock ,
83
- $ this ->priceDataMock ,
84
- $ this ->configurableTypeFactoryMock
86
+
87
+ $ this ->productFactoryMock = $ this ->getMockBuilder ('\Magento\Catalog\Model\ProductFactory ' )
88
+ ->disableOriginalConstructor ()
89
+ ->getMock ();
90
+ $ objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
91
+ $ this ->plugin = $ objectManager ->getObject (
92
+ 'Magento\ConfigurableProduct\Model\Plugin\AroundProductRepositorySave ' ,
93
+ [
94
+ 'optionRepository ' => $ this ->productOptionRepositoryMock ,
95
+ 'productFactory ' => $ this ->productFactoryMock ,
96
+ 'priceData ' => $ this ->priceDataMock ,
97
+ 'typeConfigurableFactory ' => $ this ->configurableTypeFactoryMock
98
+ ]
85
99
);
100
+
86
101
$ this ->productExtensionMock = $ this ->getMock (
87
102
'Magento\Catalog\Api\Data\ProductExtension ' ,
88
103
[
@@ -108,7 +123,7 @@ public function testAroundSaveWhenProductIsSimple()
108
123
);
109
124
}
110
125
111
- public function testAroundSaveWhenProductIsConfigurableWithoutOptions ()
126
+ public function testAroundSaveWithoutOptions ()
112
127
{
113
128
$ this ->productInterfaceMock ->expects ($ this ->once ())->method ('getTypeId ' )
114
129
->willReturn (\Magento \ConfigurableProduct \Model \Product \Type \Configurable::TYPE_CODE );
@@ -131,9 +146,89 @@ public function testAroundSaveWhenProductIsConfigurableWithoutOptions()
131
146
);
132
147
}
133
148
134
- public function testAroundSaveWhenProductIsConfigurableWithLinks ()
149
+ protected function setupProducts ($ productIds , $ attributeCode , $ additionalProductId = null )
150
+ {
151
+ $ count = 0 ;
152
+ $ products = [];
153
+ foreach ($ productIds as $ productId ) {
154
+ $ productMock = $ this ->getMockBuilder ('\Magento\Catalog\Model\Product ' )
155
+ ->disableOriginalConstructor ()
156
+ ->getMock ();
157
+ $ productMock ->expects ($ this ->once ())
158
+ ->method ('load ' )
159
+ ->with ($ productId )
160
+ ->willReturnSelf ();
161
+ $ productMock ->expects ($ this ->once ())
162
+ ->method ('getId ' )
163
+ ->willReturn ($ productId );
164
+ $ productMock ->expects ($ this ->once ())
165
+ ->method ('getData ' )
166
+ ->with ($ attributeCode )
167
+ ->willReturn ('value ' );
168
+ $ this ->productFactoryMock ->expects ($ this ->at ($ count ))
169
+ ->method ('create ' )
170
+ ->willReturn ($ productMock );
171
+ $ products [] = $ productMock ;
172
+ $ count ++;
173
+ }
174
+
175
+ if ($ additionalProductId ) {
176
+ $ nonExistingProductMock = $ this ->getMockBuilder ('\Magento\Catalog\Model\Product ' )
177
+ ->disableOriginalConstructor ()
178
+ ->getMock ();
179
+ $ nonExistingProductMock ->expects ($ this ->once ())
180
+ ->method ('load ' )
181
+ ->with ($ additionalProductId )
182
+ ->willReturnSelf ();
183
+ $ this ->productFactoryMock ->expects ($ this ->at ($ count ))
184
+ ->method ('create ' )
185
+ ->willReturn ($ nonExistingProductMock );
186
+ $ products [] = $ nonExistingProductMock ;
187
+ }
188
+ return $ products ;
189
+ }
190
+
191
+ protected function setupConfigurableProductAttributes ($ attributeCodes )
192
+ {
193
+ $ configurableProductTypeMock = $ this ->getMockBuilder (
194
+ '\Magento\ConfigurableProduct\Model\Product\Type\Configurable '
195
+ )->disableOriginalConstructor ()->getMock ();
196
+
197
+ $ this ->productMock ->expects ($ this ->once ())
198
+ ->method ('getTypeInstance ' )
199
+ ->willReturn ($ configurableProductTypeMock );
200
+
201
+ $ configurableAttributes = [];
202
+ foreach ($ attributeCodes as $ attributeCode ) {
203
+ $ configurableAttribute = $ this ->getMockBuilder (
204
+ '\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute '
205
+ )->setMethods (['getProductAttribute ' ])
206
+ ->disableOriginalConstructor ()
207
+ ->getMock ();
208
+ $ productAttributeMock = $ this ->getMockBuilder ('\Magento\Catalog\Model\Resource\Eav\Attribute ' )
209
+ ->disableOriginalConstructor ()
210
+ ->getMock ();
211
+ $ productAttributeMock ->expects ($ this ->once ())
212
+ ->method ('getAttributeCode ' )
213
+ ->willReturn ($ attributeCode );
214
+ $ configurableAttribute ->expects ($ this ->once ())
215
+ ->method ('getProductAttribute ' )
216
+ ->willReturn ($ productAttributeMock );
217
+ $ configurableAttributes [] = $ configurableAttribute ;
218
+ }
219
+
220
+ $ configurableProductTypeMock ->expects ($ this ->once ())
221
+ ->method ('getConfigurableAttributes ' )
222
+ ->with ($ this ->productMock )
223
+ ->willReturn ($ configurableAttributes );
224
+
225
+ return $ this ;
226
+ }
227
+
228
+ public function testAroundSaveWithLinks ()
135
229
{
136
230
$ links = [4 , 5 ];
231
+ $ configurableAttributeCode = 'color ' ;
137
232
$ this ->productMock ->expects ($ this ->once ())->method ('getTypeId ' )
138
233
->willReturn (\Magento \ConfigurableProduct \Model \Product \Type \Configurable::TYPE_CODE );
139
234
$ this ->productMock ->expects ($ this ->once ())
@@ -146,6 +241,9 @@ public function testAroundSaveWhenProductIsConfigurableWithLinks()
146
241
->method ('getConfigurableProductLinks ' )
147
242
->willReturn ($ links );
148
243
244
+ $ this ->setupConfigurableProductAttributes ([$ configurableAttributeCode ]);
245
+ $ this ->setupProducts ($ links , $ configurableAttributeCode );
246
+
149
247
$ configurableTypeMock = $ this ->getMockBuilder (
150
248
'\Magento\ConfigurableProduct\Model\Resource\Product\Type\Configurable '
151
249
)->disableOriginalConstructor ()->getMock ();
@@ -157,7 +255,7 @@ public function testAroundSaveWhenProductIsConfigurableWithLinks()
157
255
->with ($ this ->productMock , $ links );
158
256
159
257
$ productId = 3 ;
160
- $ this ->productMock ->expects ($ this ->once ())
258
+ $ this ->productMock ->expects ($ this ->any ())
161
259
->method ('getId ' )
162
260
->willReturn ($ productId );
163
261
$ this ->priceDataMock ->expects ($ this ->once ())
@@ -176,7 +274,98 @@ public function testAroundSaveWhenProductIsConfigurableWithLinks()
176
274
);
177
275
}
178
276
179
- public function testAroundSaveWhenProductIsConfigurableWithOptions ()
277
+ /**
278
+ * @expectedException \Magento\Framework\Exception\InputException
279
+ * @expectedExceptionMessage Product with id "6" does not exist.
280
+ */
281
+ public function testAroundSaveWithNonExistingLinks ()
282
+ {
283
+ $ links = [4 , 5 ];
284
+ $ nonExistingId = 6 ;
285
+ $ configurableAttributeCode = 'color ' ;
286
+
287
+ $ this ->setupConfigurableProductAttributes ([$ configurableAttributeCode ]);
288
+ $ productMocks = $ this ->setupProducts ($ links , $ configurableAttributeCode , $ nonExistingId );
289
+ $ nonExistingProductMock = $ productMocks [2 ];
290
+ $ nonExistingProductMock ->expects ($ this ->once ())
291
+ ->method ('getId ' )
292
+ ->willReturn (null );
293
+ $ links [] = $ nonExistingId ;
294
+
295
+ $ this ->productMock ->expects ($ this ->once ())->method ('getTypeId ' )
296
+ ->willReturn (\Magento \ConfigurableProduct \Model \Product \Type \Configurable::TYPE_CODE );
297
+ $ this ->productMock ->expects ($ this ->once ())
298
+ ->method ('getExtensionAttributes ' )
299
+ ->willReturn ($ this ->productExtensionMock );
300
+ $ this ->productExtensionMock ->expects ($ this ->once ())
301
+ ->method ('getConfigurableProductOptions ' )
302
+ ->willReturn (null );
303
+ $ this ->productExtensionMock ->expects ($ this ->once ())
304
+ ->method ('getConfigurableProductLinks ' )
305
+ ->willReturn ($ links );
306
+
307
+ $ configurableTypeMock = $ this ->getMockBuilder (
308
+ '\Magento\ConfigurableProduct\Model\Resource\Product\Type\Configurable '
309
+ )->disableOriginalConstructor ()->getMock ();
310
+ $ this ->configurableTypeFactoryMock ->expects ($ this ->once ())
311
+ ->method ('create ' )
312
+ ->willReturn ($ configurableTypeMock );
313
+ $ configurableTypeMock ->expects ($ this ->never ())
314
+ ->method ('saveProducts ' )
315
+ ->with ($ this ->productMock , $ links );
316
+
317
+ $ this ->plugin ->aroundSave ($ this ->productRepositoryMock , $ this ->closureMock , $ this ->productMock );
318
+ }
319
+
320
+ /**
321
+ * @expectedException \Magento\Framework\Exception\InputException
322
+ * @expectedExceptionMessage Product with id "6" does not contain required attribute "color".
323
+ */
324
+ public function testAroundSaveWithLinksWithMissingAttribute ()
325
+ {
326
+ $ links = [4 , 5 ];
327
+ $ simpleProductId = 6 ;
328
+ $ configurableAttributeCode = 'color ' ;
329
+
330
+ $ this ->setupConfigurableProductAttributes ([$ configurableAttributeCode ]);
331
+ $ productMocks = $ this ->setupProducts ($ links , $ configurableAttributeCode , $ simpleProductId );
332
+ $ simpleProductMock = $ productMocks [2 ];
333
+ $ simpleProductMock ->expects ($ this ->once ())
334
+ ->method ('getId ' )
335
+ ->willReturn ($ simpleProductId );
336
+ $ simpleProductMock ->expects ($ this ->once ())
337
+ ->method ('getData ' )
338
+ ->with ($ configurableAttributeCode )
339
+ ->willReturn (null );
340
+
341
+ $ links [] = $ simpleProductId ;
342
+
343
+ $ this ->productMock ->expects ($ this ->once ())->method ('getTypeId ' )
344
+ ->willReturn (\Magento \ConfigurableProduct \Model \Product \Type \Configurable::TYPE_CODE );
345
+ $ this ->productMock ->expects ($ this ->once ())
346
+ ->method ('getExtensionAttributes ' )
347
+ ->willReturn ($ this ->productExtensionMock );
348
+ $ this ->productExtensionMock ->expects ($ this ->once ())
349
+ ->method ('getConfigurableProductOptions ' )
350
+ ->willReturn (null );
351
+ $ this ->productExtensionMock ->expects ($ this ->once ())
352
+ ->method ('getConfigurableProductLinks ' )
353
+ ->willReturn ($ links );
354
+
355
+ $ configurableTypeMock = $ this ->getMockBuilder (
356
+ '\Magento\ConfigurableProduct\Model\Resource\Product\Type\Configurable '
357
+ )->disableOriginalConstructor ()->getMock ();
358
+ $ this ->configurableTypeFactoryMock ->expects ($ this ->once ())
359
+ ->method ('create ' )
360
+ ->willReturn ($ configurableTypeMock );
361
+ $ configurableTypeMock ->expects ($ this ->never ())
362
+ ->method ('saveProducts ' )
363
+ ->with ($ this ->productMock , $ links );
364
+
365
+ $ this ->plugin ->aroundSave ($ this ->productRepositoryMock , $ this ->closureMock , $ this ->productMock );
366
+ }
367
+
368
+ public function testAroundSaveWithOptions ()
180
369
{
181
370
$ productSku = "configurable_sku " ;
182
371
$ this ->productInterfaceMock ->expects ($ this ->once ())->method ('getTypeId ' )
0 commit comments