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,52 @@ 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 (['load ' , 'getOptions ' , 'getId ' , 'getAttributeCode ' , 'getStoreLabel ' ])
187
+ ->getMock ();
188
+ $ attributeOptionMock = $ this ->getMockBuilder ('Magento\Eav\Model\Entity\Attribute\Option ' )
189
+ ->disableOriginalConstructor ()
190
+ ->setMethods (['getValue ' , 'getLabel ' ])
191
+ ->getMock ();
154
192
155
193
$ this ->productRepository ->expects ($ this ->at (0 ))->method ('get ' )->with ($ productSku )->willReturn ($ configurable );
156
194
$ this ->productRepository ->expects ($ this ->at (1 ))->method ('get ' )->with ($ childSku )->willReturn ($ simple );
@@ -159,7 +197,28 @@ public function testAddChild()
159
197
->will (
160
198
$ this ->returnValue ([0 => [1 , 2 , 3 ]])
161
199
);
162
- $ configurable ->expects ($ this ->once ())->method ('save ' );
200
+
201
+ $ configurable ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue (666 ));
202
+ $ simple ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue (999 ));
203
+
204
+ $ configurable ->expects ($ this ->any ())->method ('getExtensionAttributes ' )->willReturn ($ extensionAttributesMock );
205
+ $ extensionAttributesMock ->expects ($ this ->any ())
206
+ ->method ('getConfigurableProductOptions ' )
207
+ ->willReturn ([$ optionMock ]);
208
+ $ optionMock ->expects ($ this ->any ())->method ('getProductAttribute ' )->willReturn ($ productAttributeMock );
209
+ $ productAttributeMock ->expects ($ this ->any ())->method ('getAttributeCode ' )->willReturn ('color ' );
210
+ $ simple ->expects ($ this ->any ())->method ('getData ' )->willReturn ('color ' );
211
+ $ optionMock ->expects ($ this ->any ())->method ('getAttributeId ' )->willReturn ('1 ' );
212
+
213
+ $ optionsFactoryMock ->expects ($ this ->any ())->method ('create ' )->willReturn ([$ optionMock ]);
214
+ $ attributeFactoryMock ->expects ($ this ->any ())->method ('create ' )->willReturn ($ attributeMock );
215
+ $ attributeMock ->expects ($ this ->any ())->method ('load ' );
216
+ $ attributeMock ->expects ($ this ->any ())->method ('getOptions ' )->willReturn ([$ attributeOptionMock ]);
217
+
218
+ $ extensionAttributesMock ->expects ($ this ->any ())->method ('setConfigurableProductOptions ' );
219
+ $ extensionAttributesMock ->expects ($ this ->any ())->method ('setConfigurableProductLinks ' );
220
+
221
+ $ this ->productRepository ->expects ($ this ->once ())->method ('save ' );
163
222
164
223
$ this ->assertTrue (true , $ this ->object ->addChild ($ productSku , $ childSku ));
165
224
}
@@ -236,7 +295,7 @@ public function testRemoveChild()
236
295
->getMock ();
237
296
238
297
$ product ->expects ($ this ->once ())->method ('getExtensionAttributes ' )->willReturn ($ extensionAttributesMock );
239
- $ product ->expects ($ this ->once ())->method ('save ' );
298
+ $ this -> productRepository ->expects ($ this ->once ())->method ('save ' );
240
299
$ this ->assertTrue ($ this ->object ->removeChild ($ productSku , $ childSku ));
241
300
}
242
301
0 commit comments