5
5
*/
6
6
namespace Magento \Multishipping \Test \Unit \Model \Checkout \Type ;
7
7
8
+ use Magento \Quote \Model \ShippingAssignment ;
9
+
8
10
/**
9
11
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
10
12
*/
@@ -106,13 +108,10 @@ protected function setUp()
106
108
'' ,
107
109
false
108
110
);
109
- $ this ->addressRepositoryMock = $ this ->getMock (
110
- \Magento \Customer \Api \AddressRepositoryInterface::class,
111
- [],
112
- [],
113
- '' ,
114
- false
115
- );
111
+ $ this ->addressRepositoryMock = $ this ->getMockBuilder (\Magento \Customer \Api \AddressRepositoryInterface::class)
112
+ ->setMethods ([])
113
+ ->disableOriginalConstructor ()
114
+ ->getMock ();
116
115
/** This is used to get past _init() which is called in construct. */
117
116
$ data ['checkout_session ' ] = $ this ->checkoutSessionMock ;
118
117
$ this ->quoteMock = $ this ->getMock (\Magento \Quote \Model \Quote::class, [], [], '' , false );
@@ -121,13 +120,10 @@ protected function setUp()
121
120
$ this ->checkoutSessionMock ->expects ($ this ->atLeastOnce ())->method ('getQuote ' )->willReturn ($ this ->quoteMock );
122
121
$ this ->customerSessionMock ->expects ($ this ->atLeastOnce ())->method ('getCustomerDataObject ' )
123
122
->willReturn ($ this ->customerMock );
124
- $ this ->totalsCollectorMock = $ this ->getMock (
125
- \Magento \Quote \Model \Quote \TotalsCollector::class,
126
- [],
127
- [],
128
- '' ,
129
- false
130
- );
123
+ $ this ->totalsCollectorMock = $ this ->getMockBuilder (\Magento \Quote \Model \Quote \TotalsCollector::class)
124
+ ->setMethods ([])
125
+ ->disableOriginalConstructor ()
126
+ ->getMock ();
131
127
$ this ->model = new \Magento \Multishipping \Model \Checkout \Type \Multishipping (
132
128
$ this ->checkoutSessionMock ,
133
129
$ this ->customerSessionMock ,
@@ -221,6 +217,8 @@ public function testSetShippingItemsInformation()
221
217
$ resultMock ->expects ($ this ->atLeastOnce ())->method ('getItems ' )->willReturn ([$ addressItemMock ]);
222
218
$ addressItemMock ->expects ($ this ->atLeastOnce ())->method ('getId ' )->willReturn (null );
223
219
220
+ $ this ->mockShippingAssignment ();
221
+
224
222
$ this ->assertEquals ($ this ->model , $ this ->model ->setShippingItemsInformation ($ info ));
225
223
}
226
224
@@ -330,54 +328,80 @@ public function testSetShippingMethods()
330
328
331
329
$ methodsArray = [1 => 'flatrate_flatrate ' , 2 => 'tablerate_bestway ' ];
332
330
$ addressId = 1 ;
333
- $ addressMock = $ this ->getMock (
334
- \Magento \Quote \Model \Quote \Address::class,
335
- ['getId ' , 'setShippingMethod ' ],
336
- [],
337
- '' ,
338
- false
339
- );
340
- $ extensionAttributesMock = $ this ->getMock (
341
- \Magento \Quote \Api \Data \CartExtension::class,
342
- ['setShippingAssignments ' ],
343
- [],
344
- '' ,
345
- false
346
- );
347
- $ shippingAssignmentMock = $ this ->getMock (
348
- \Magento \Quote \Model \ShippingAssignment::class,
349
- ['getShipping ' , 'setShipping ' ],
350
- [],
351
- '' ,
352
- false
353
- );
354
- $ shippingMock = $ this ->getMock (\Magento \Quote \Model \Shipping::class, ['setMethod ' ], [], '' , false );
331
+ $ addressMock = $ this ->getMockBuilder (\Magento \Quote \Model \Quote \Address::class)
332
+ ->setMethods (['getId ' , 'setShippingMethod ' ])
333
+ ->disableOriginalConstructor ()
334
+ ->getMock ();
355
335
356
336
$ addressMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ addressId );
357
337
$ this ->quoteMock ->expects ($ this ->once ())->method ('getAllShippingAddresses ' )->willReturn ([$ addressMock ]);
358
338
$ addressMock ->expects ($ this ->once ())->method ('setShippingMethod ' )->with ($ methodsArray [$ addressId ]);
359
- //mock for prepareShippingAssignment
360
- $ this ->quoteMock
339
+
340
+ $ this ->mockShippingAssignment ();
341
+
342
+ //save mock
343
+ $ this ->quoteMock ->expects ($ this ->once ())->method ('collectTotals ' )->willReturnSelf ();
344
+ $ this ->quoteRepositoryMock ->expects ($ this ->once ())->method ('save ' )->with ($ this ->quoteMock );
345
+ $ this ->model ->setShippingMethods ($ methodsArray );
346
+ }
347
+
348
+ /**
349
+ * @param ShippingAssignment $shippingAssignmentMock
350
+ * @return \Magento\Quote\Api\Data\CartExtension|\PHPUnit_Framework_MockObject_MockObject
351
+ */
352
+ private function getExtensionAttributesMock (ShippingAssignment $ shippingAssignmentMock )
353
+ {
354
+ $ extensionAttributesMock = $ this ->getMockBuilder (\Magento \Quote \Api \Data \CartExtension::class)
355
+ ->setMethods (['setShippingAssignments ' ])
356
+ ->getMock ();
357
+
358
+ $ extensionAttributesMock
361
359
->expects ($ this ->once ())
362
- ->method ('getExtensionAttributes ' )
363
- ->willReturn ($ extensionAttributesMock );
360
+ ->method ('setShippingAssignments ' )
361
+ ->with ([$ shippingAssignmentMock ])
362
+ ->willReturnSelf ();
363
+
364
+ return $ extensionAttributesMock ;
365
+ }
366
+
367
+ /**
368
+ * @return ShippingAssignment | \PHPUnit_Framework_MockObject_MockObject
369
+ */
370
+ private function getShippingAssignmentMock ()
371
+ {
372
+ $ shippingAssignmentMock = $ this ->getMockBuilder (ShippingAssignment::class)
373
+ ->disableOriginalConstructor ()
374
+ ->setMethods (['getShipping ' , 'setShipping ' ])
375
+ ->getMock ();
376
+ $ shippingMock = $ this ->getMockBuilder (\Magento \Quote \Model \Shipping::class)
377
+ ->disableOriginalConstructor ()
378
+ ->setMethods (['setMethod ' ])
379
+ ->getMock ();
380
+
381
+ $ shippingAssignmentMock ->expects ($ this ->once ())->method ('getShipping ' )->willReturn ($ shippingMock );
382
+ $ shippingMock ->expects ($ this ->once ())->method ('setMethod ' )->with (null )->willReturnSelf ();
383
+ $ shippingAssignmentMock ->expects ($ this ->once ())->method ('setShipping ' )->with ($ shippingMock );
384
+
385
+ return $ shippingAssignmentMock ;
386
+ }
387
+
388
+ private function mockShippingAssignment ()
389
+ {
390
+ $ shippingAssignmentMock = $ this ->getShippingAssignmentMock ();
391
+
392
+ $ extensionAttributesMock = $ this ->getExtensionAttributesMock ($ shippingAssignmentMock );
393
+
364
394
$ this ->shippingAssignmentProcessorMock
365
395
->expects ($ this ->once ())
366
396
->method ('create ' )
367
397
->with ($ this ->quoteMock )
368
398
->willReturn ($ shippingAssignmentMock );
369
- $ shippingAssignmentMock ->expects ($ this ->once ())->method ('getShipping ' )->willReturn ($ shippingMock );
370
- $ shippingMock ->expects ($ this ->once ())->method ('setMethod ' )->with (null )->willReturnSelf ();
371
- $ shippingAssignmentMock ->expects ($ this ->once ())->method ('setShipping ' )->with ($ shippingMock );
372
- $ extensionAttributesMock
399
+
400
+ $ this ->quoteMock
373
401
->expects ($ this ->once ())
374
- ->method ('setShippingAssignments ' )
375
- ->with ([ $ shippingAssignmentMock ])
376
- -> willReturnSelf ();
402
+ ->method ('getExtensionAttributes ' )
403
+ ->willReturn ( $ extensionAttributesMock );
404
+
377
405
$ this ->quoteMock ->expects ($ this ->once ())->method ('setExtensionAttributes ' )->with ($ extensionAttributesMock );
378
- //save mock
379
- $ this ->quoteMock ->expects ($ this ->once ())->method ('collectTotals ' )->willReturnSelf ();
380
- $ this ->quoteRepositoryMock ->expects ($ this ->once ())->method ('save ' )->with ($ this ->quoteMock );
381
- $ this ->model ->setShippingMethods ($ methodsArray );
382
406
}
383
407
}
0 commit comments