6
6
7
7
namespace Magento \ConfigurableProduct \Test \Unit \Model ;
8
8
9
+ use Magento \ConfigurableProduct \Model \LinkManagement ;
9
10
use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
10
11
11
12
class LinkManagementTest extends \PHPUnit_Framework_TestCase
@@ -142,15 +143,59 @@ public function testAddChild()
142
143
143
144
$ configurable = $ this ->getMockBuilder ('Magento\Catalog\Model\Product ' )
144
145
->disableOriginalConstructor ()
146
+ ->setMethods (['getId ' , 'getExtensionAttributes ' , 'save ' ])
147
+ ->getMock ();
148
+ $ simple = $ this ->getMockBuilder ('Magento\Catalog\Model\Product ' )
149
+ ->disableOriginalConstructor ()
150
+ ->setMethods (['getId ' , 'getData ' ])
145
151
->getMock ();
146
152
147
- $ configurable ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue (666 ));
153
+ $ extensionAttributesMock = $ this ->getMockBuilder ('Magento\Catalog\Api\Data\ProductExtension ' )
154
+ ->disableOriginalConstructor ()
155
+ ->setMethods ([
156
+ 'getConfigurableProductOptions ' , 'setConfigurableProductOptions ' , 'setConfigurableProductLinks '
157
+ ])
158
+ ->getMock ();
159
+ $ optionMock = $ this ->getMockBuilder ('Magento\ConfigurableProduct\Api\Data\Option ' )
160
+ ->disableOriginalConstructor ()
161
+ ->setMethods (['getProductAttribute ' , 'getAttributeId ' ])
162
+ ->getMock ();
163
+ $ productAttributeMock = $ this ->getMockBuilder ('Magento\Eav\Model\Entity\Attribute\AbstractAttribute ' )
164
+ ->disableOriginalConstructor ()
165
+ ->setMethods (['getAttributeCode ' ])
166
+ ->getMock ();
167
+ $ optionsFactoryMock = $ this ->getMockBuilder ('Magento\ConfigurableProduct\Helper\Product\Options\Factory ' )
168
+ ->disableOriginalConstructor ()
169
+ ->setMethods (['create ' ])
170
+ ->getMock ();
171
+ $ reflectionClass = new \ReflectionClass ('Magento\ConfigurableProduct\Model\LinkManagement ' );
172
+ $ optionsFactoryReflectionProperty = $ reflectionClass ->getProperty ('optionsFactory ' );
173
+ $ optionsFactoryReflectionProperty ->setAccessible (true );
174
+ $ optionsFactoryReflectionProperty ->setValue ($ this ->object , $ optionsFactoryMock );
148
175
149
- $ simple = $ this ->getMockBuilder ('Magento\Catalog\Model\Product ' )
176
+ $ attributeFactoryMock = $ this ->getMockBuilder ('Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory ' )
150
177
->disableOriginalConstructor ()
178
+ ->setMethods (['create ' ])
151
179
->getMock ();
180
+ $ attributeFactoryReflectionProperty = $ reflectionClass ->getProperty ('attributeFactory ' );
181
+ $ attributeFactoryReflectionProperty ->setAccessible (true );
182
+ $ attributeFactoryReflectionProperty ->setValue ($ this ->object , $ attributeFactoryMock );
152
183
153
- $ simple ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue (999 ));
184
+ $ attributeMock = $ this ->getMockBuilder ('Magento\Catalog\Model\ResourceModel\Eav\Attribute ' )
185
+ ->disableOriginalConstructor ()
186
+ ->setMethods (['getCollection ' , 'getOptions ' , 'getId ' , 'getAttributeCode ' , 'getStoreLabel ' ])
187
+ ->getMock ();
188
+ $ attributeOptionMock = $ this ->getMockBuilder ('Magento\Eav\Model\Entity\Attribute\Option ' )
189
+ ->disableOriginalConstructor ()
190
+ ->setMethods (['getValue ' , 'getLabel ' ])
191
+ ->getMock ();
192
+
193
+ $ attributeCollectionMock = $ this ->getMockBuilder (
194
+ 'Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\Collection '
195
+ )
196
+ ->disableOriginalConstructor ()
197
+ ->setMethods (['addFieldToFilter ' , 'getItems ' ])
198
+ ->getMock ();
154
199
155
200
$ this ->productRepository ->expects ($ this ->at (0 ))->method ('get ' )->with ($ productSku )->willReturn ($ configurable );
156
201
$ this ->productRepository ->expects ($ this ->at (1 ))->method ('get ' )->with ($ childSku )->willReturn ($ simple );
@@ -159,7 +204,31 @@ public function testAddChild()
159
204
->will (
160
205
$ this ->returnValue ([0 => [1 , 2 , 3 ]])
161
206
);
162
- $ configurable ->expects ($ this ->once ())->method ('save ' );
207
+
208
+ $ configurable ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue (666 ));
209
+ $ simple ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue (999 ));
210
+
211
+ $ configurable ->expects ($ this ->any ())->method ('getExtensionAttributes ' )->willReturn ($ extensionAttributesMock );
212
+ $ extensionAttributesMock ->expects ($ this ->any ())
213
+ ->method ('getConfigurableProductOptions ' )
214
+ ->willReturn ([$ optionMock ]);
215
+ $ optionMock ->expects ($ this ->any ())->method ('getProductAttribute ' )->willReturn ($ productAttributeMock );
216
+ $ productAttributeMock ->expects ($ this ->any ())->method ('getAttributeCode ' )->willReturn ('color ' );
217
+ $ simple ->expects ($ this ->any ())->method ('getData ' )->willReturn ('color ' );
218
+ $ optionMock ->expects ($ this ->any ())->method ('getAttributeId ' )->willReturn ('1 ' );
219
+
220
+ $ optionsFactoryMock ->expects ($ this ->any ())->method ('create ' )->willReturn ([$ optionMock ]);
221
+ $ attributeFactoryMock ->expects ($ this ->any ())->method ('create ' )->willReturn ($ attributeMock );
222
+ $ attributeMock ->expects ($ this ->any ())->method ('getCollection ' )->willReturn ($ attributeCollectionMock );
223
+ $ attributeCollectionMock ->expects ($ this ->any ())->method ('addFieldToFilter ' )->willReturnSelf ();
224
+ $ attributeCollectionMock ->expects ($ this ->any ())->method ('getItems ' )->willReturn ([$ attributeMock ]);
225
+
226
+ $ attributeMock ->expects ($ this ->any ())->method ('getOptions ' )->willReturn ([$ attributeOptionMock ]);
227
+
228
+ $ extensionAttributesMock ->expects ($ this ->any ())->method ('setConfigurableProductOptions ' );
229
+ $ extensionAttributesMock ->expects ($ this ->any ())->method ('setConfigurableProductLinks ' );
230
+
231
+ $ this ->productRepository ->expects ($ this ->once ())->method ('save ' );
163
232
164
233
$ this ->assertTrue (true , $ this ->object ->addChild ($ productSku , $ childSku ));
165
234
}
@@ -236,7 +305,7 @@ public function testRemoveChild()
236
305
->getMock ();
237
306
238
307
$ product ->expects ($ this ->once ())->method ('getExtensionAttributes ' )->willReturn ($ extensionAttributesMock );
239
- $ product ->expects ($ this ->once ())->method ('save ' );
308
+ $ this -> productRepository ->expects ($ this ->once ())->method ('save ' );
240
309
$ this ->assertTrue ($ this ->object ->removeChild ($ productSku , $ childSku ));
241
310
}
242
311
0 commit comments