55 */
66namespace Magento \Shipping \Test \Unit \Model ;
77
8- use \Magento \Shipping \Model \Shipping ;
9-
8+ use Magento \Catalog \Model \Product ;
9+ use Magento \Catalog \Model \Product \Type as ProductType ;
10+ use Magento \CatalogInventory \Model \Stock \Item as StockItem ;
11+ use Magento \CatalogInventory \Model \StockRegistry ;
12+ use Magento \Quote \Model \Quote \Item as QuoteItem ;
13+ use Magento \Shipping \Model \Carrier \AbstractCarrierInterface ;
14+ use Magento \Shipping \Model \CarrierFactory ;
15+ use Magento \Shipping \Model \Shipping ;
1016use Magento \Quote \Model \Quote \Address \RateRequest ;
1117use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
18+ use Magento \Store \Model \Store ;
19+ use PHPUnit_Framework_MockObject_MockObject as MockObject ;
1220
1321class ShippingTest extends \PHPUnit \Framework \TestCase
1422{
@@ -25,71 +33,69 @@ class ShippingTest extends \PHPUnit\Framework\TestCase
2533 protected $ shipping ;
2634
2735 /**
28- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Shipping\Model\Carrier\AbstractCarrier
36+ * @var MockObject|StockRegistry
2937 */
30- protected $ carrier ;
38+ protected $ stockRegistry ;
3139
3240 /**
33- * @var \PHPUnit_Framework_MockObject_MockObject
41+ * @var MockObject|StockItem
3442 */
35- protected $ stockRegistry ;
43+ protected $ stockItemData ;
3644
3745 /**
38- * @var \PHPUnit_Framework_MockObject_MockObject
46+ * @var MockObject|AbstractCarrierInterface
3947 */
40- protected $ stockItemData ;
48+ private $ carrier ;
4149
4250 protected function setUp ()
4351 {
44- $ this ->carrier = $ this ->createMock (\Magento \Shipping \Model \Carrier \AbstractCarrier::class);
45- $ this ->carrier ->expects ($ this ->any ())->method ('getConfigData ' )->will ($ this ->returnCallback (function ($ key ) {
46- $ configData = [
47- 'max_package_weight ' => 10 ,
48- ];
49- return isset ($ configData [$ key ]) ? $ configData [$ key ] : 0 ;
50- }));
51- $ this ->stockRegistry = $ this ->createMock (\Magento \CatalogInventory \Model \StockRegistry::class);
52- $ this ->stockItemData = $ this ->createMock (\Magento \CatalogInventory \Model \Stock \Item::class);
53-
54- $ objectManagerHelper = new ObjectManagerHelper ($ this );
55- $ this ->shipping = $ objectManagerHelper ->getObject (
56- \Magento \Shipping \Model \Shipping::class,
57- ['stockRegistry ' => $ this ->stockRegistry ]
52+ $ this ->stockRegistry = $ this ->createMock (StockRegistry::class);
53+ $ this ->stockItemData = $ this ->createMock (StockItem::class);
54+
55+ $ this ->shipping = (new ObjectManagerHelper ($ this ))->getObject (
56+ Shipping::class,
57+ [
58+ 'stockRegistry ' => $ this ->stockRegistry ,
59+ 'carrierFactory ' => $ this ->getCarrierFactory (),
60+ ]
5861 );
5962 }
6063
6164 /**
62- * @covers \Magento\Shipping\Model\Shipping::composePackagesForCarrier
65+ *
6366 */
6467 public function testComposePackages ()
6568 {
6669 $ request = new RateRequest ();
67- /** \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface */
68- $ item = $ this ->getMockBuilder (\Magento \Quote \Model \Quote \Item::class)
70+ $ item = $ this ->getMockBuilder (QuoteItem::class)
6971 ->disableOriginalConstructor ()
70- ->setMethods ([
71- 'getQty ' , 'getIsQtyDecimal ' , 'getProductType ' , 'getProduct ' , 'getWeight ' , '__wakeup ' , 'getStore ' ,
72- ])
73- ->getMock ();
74- $ product = $ this ->createMock (\Magento \Catalog \Model \Product::class);
75-
76- $ item ->expects ($ this ->any ())->method ('getQty ' )->will ($ this ->returnValue (1 ));
77- $ item ->expects ($ this ->any ())->method ('getWeight ' )->will ($ this ->returnValue (10 ));
78- $ item ->expects ($ this ->any ())->method ('getIsQtyDecimal ' )->will ($ this ->returnValue (true ));
79- $ item ->expects ($ this ->any ())->method ('getProductType ' )
80- ->will ($ this ->returnValue (\Magento \Catalog \Model \Product \Type::TYPE_SIMPLE ));
81- $ item ->expects ($ this ->any ())->method ('getProduct ' )->will ($ this ->returnValue ($ product ));
82-
83- $ store = $ this ->createPartialMock (\Magento \Store \Model \Store::class, ['getWebsiteId ' ]);
84- $ store ->expects ($ this ->any ())
85- ->method ('getWebsiteId ' )
86- ->will ($ this ->returnValue (10 ));
87- $ item ->expects ($ this ->any ())->method ('getStore ' )->will ($ this ->returnValue ($ store ));
88-
89- $ product ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue ($ this ->productId ));
72+ ->setMethods (
73+ [
74+ 'getQty ' ,
75+ 'getIsQtyDecimal ' ,
76+ 'getProductType ' ,
77+ 'getProduct ' ,
78+ 'getWeight ' ,
79+ '__wakeup ' ,
80+ 'getStore ' ,
81+ ]
82+ )->getMock ();
83+ $ product = $ this ->createMock (Product::class);
84+
85+ $ item ->method ('getQty ' )->will ($ this ->returnValue (1 ));
86+ $ item ->method ('getWeight ' )->will ($ this ->returnValue (10 ));
87+ $ item ->method ('getIsQtyDecimal ' )->will ($ this ->returnValue (true ));
88+ $ item ->method ('getProductType ' )->will ($ this ->returnValue (ProductType::TYPE_SIMPLE ));
89+ $ item ->method ('getProduct ' )->will ($ this ->returnValue ($ product ));
90+
91+ $ store = $ this ->createPartialMock (Store::class, ['getWebsiteId ' ]);
92+ $ store ->method ('getWebsiteId ' )->will ($ this ->returnValue (10 ));
93+ $ item ->method ('getStore ' )->will ($ this ->returnValue ($ store ));
94+
95+ $ product ->method ('getId ' )->will ($ this ->returnValue ($ this ->productId ));
9096 $ request ->setData ('all_items ' , [$ item ]);
9197
92- $ this ->stockItemData ->expects ( $ this -> any ())-> method ('getIsDecimalDivided ' )->will ($ this ->returnValue (true ));
98+ $ this ->stockItemData ->method ('getIsDecimalDivided ' )->will ($ this ->returnValue (true ));
9399
94100 /** Testable service calls to CatalogInventory module */
95101 $ this ->stockRegistry ->expects ($ this ->atLeastOnce ())->method ('getStockItem ' )
@@ -101,7 +107,51 @@ public function testComposePackages()
101107 ->will ($ this ->returnValue (true ));
102108 $ this ->stockItemData ->expects ($ this ->atLeastOnce ())->method ('getQtyIncrements ' )
103109 ->will ($ this ->returnValue (0.5 ));
110+ $ this ->carrier ->method ('getConfigData ' )
111+ ->willReturnCallback (function ($ key ) {
112+ $ configData = [
113+ 'max_package_weight ' => 10 ,
114+ ];
115+ return isset ($ configData [$ key ]) ? $ configData [$ key ] : 0 ;
116+ });
104117
105118 $ this ->shipping ->composePackagesForCarrier ($ this ->carrier , $ request );
106119 }
120+
121+ /**
122+ * Active flag should be set before collecting carrier rates.
123+ */
124+ public function testCollectCarrierRatesSetActiveFlag ()
125+ {
126+ $ this ->carrier ->expects ($ this ->atLeastOnce ())
127+ ->method ('setActiveFlag ' )
128+ ->with ('active ' );
129+
130+ $ this ->shipping ->collectCarrierRates ('carrier ' , new RateRequest ());
131+ }
132+
133+ /**
134+ * @return CarrierFactory|MockObject
135+ */
136+ private function getCarrierFactory ()
137+ {
138+ $ carrierFactory = $ this ->getMockBuilder (CarrierFactory::class)
139+ ->disableOriginalConstructor ()
140+ ->getMock ();
141+
142+ $ this ->carrier = $ this ->getMockBuilder (AbstractCarrierInterface::class)
143+ ->setMethods (
144+ [
145+ 'setActiveFlag ' ,
146+ 'checkAvailableShipCountries ' ,
147+ 'processAdditionalValidation ' ,
148+ 'getConfigData ' ,
149+ 'collectRates ' ,
150+ ]
151+ )
152+ ->getMockForAbstractClass ();
153+ $ carrierFactory ->method ('create ' )->willReturn ($ this ->carrier );
154+
155+ return $ carrierFactory ;
156+ }
107157}
0 commit comments