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
21+ /**
22+ * @see Shipping
23+ */
1324class ShippingTest extends \PHPUnit \Framework \TestCase
1425{
1526 /**
@@ -25,71 +36,69 @@ class ShippingTest extends \PHPUnit\Framework\TestCase
2536 protected $ shipping ;
2637
2738 /**
28- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Shipping\Model\Carrier\AbstractCarrier
39+ * @var MockObject|StockRegistry
2940 */
30- protected $ carrier ;
41+ protected $ stockRegistry ;
3142
3243 /**
33- * @var \PHPUnit_Framework_MockObject_MockObject
44+ * @var MockObject|StockItem
3445 */
35- protected $ stockRegistry ;
46+ protected $ stockItemData ;
3647
3748 /**
38- * @var \PHPUnit_Framework_MockObject_MockObject
49+ * @var MockObject|AbstractCarrierInterface
3950 */
40- protected $ stockItemData ;
51+ private $ carrier ;
4152
4253 protected function setUp ()
4354 {
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 ]
55+ $ this ->stockRegistry = $ this ->createMock (StockRegistry::class);
56+ $ this ->stockItemData = $ this ->createMock (StockItem::class);
57+
58+ $ this ->shipping = (new ObjectManagerHelper ($ this ))->getObject (
59+ Shipping::class,
60+ [
61+ 'stockRegistry ' => $ this ->stockRegistry ,
62+ 'carrierFactory ' => $ this ->getCarrierFactory (),
63+ ]
5864 );
5965 }
6066
6167 /**
62- * @covers \Magento\Shipping\Model\Shipping::composePackagesForCarrier
68+ *
6369 */
6470 public function testComposePackages ()
6571 {
6672 $ request = new RateRequest ();
67- /** \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface */
68- $ item = $ this ->getMockBuilder (\Magento \Quote \Model \Quote \Item::class)
73+ $ item = $ this ->getMockBuilder (QuoteItem::class)
6974 ->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 ));
75+ ->setMethods (
76+ [
77+ 'getQty ' ,
78+ 'getIsQtyDecimal ' ,
79+ 'getProductType ' ,
80+ 'getProduct ' ,
81+ 'getWeight ' ,
82+ '__wakeup ' ,
83+ 'getStore ' ,
84+ ]
85+ )->getMock ();
86+ $ product = $ this ->createMock (Product::class);
87+
88+ $ item ->method ('getQty ' )->will ($ this ->returnValue (1 ));
89+ $ item ->method ('getWeight ' )->will ($ this ->returnValue (10 ));
90+ $ item ->method ('getIsQtyDecimal ' )->will ($ this ->returnValue (true ));
91+ $ item ->method ('getProductType ' )->will ($ this ->returnValue (ProductType::TYPE_SIMPLE ));
92+ $ item ->method ('getProduct ' )->will ($ this ->returnValue ($ product ));
93+
94+ $ store = $ this ->createPartialMock (Store::class, ['getWebsiteId ' ]);
95+ $ store ->method ('getWebsiteId ' )->will ($ this ->returnValue (10 ));
96+ $ item ->method ('getStore ' )->will ($ this ->returnValue ($ store ));
97+
98+ $ product ->method ('getId ' )->will ($ this ->returnValue ($ this ->productId ));
9099 $ request ->setData ('all_items ' , [$ item ]);
91100
92- $ this ->stockItemData ->expects ( $ this -> any ())-> method ('getIsDecimalDivided ' )->will ($ this ->returnValue (true ));
101+ $ this ->stockItemData ->method ('getIsDecimalDivided ' )->will ($ this ->returnValue (true ));
93102
94103 /** Testable service calls to CatalogInventory module */
95104 $ this ->stockRegistry ->expects ($ this ->atLeastOnce ())->method ('getStockItem ' )
@@ -101,7 +110,53 @@ public function testComposePackages()
101110 ->will ($ this ->returnValue (true ));
102111 $ this ->stockItemData ->expects ($ this ->atLeastOnce ())->method ('getQtyIncrements ' )
103112 ->will ($ this ->returnValue (0.5 ));
113+ $ this ->carrier ->method ('getConfigData ' )
114+ ->willReturnCallback (
115+ function ($ key ) {
116+ $ configData = [
117+ 'max_package_weight ' => 10 ,
118+ ];
119+ return isset ($ configData [$ key ]) ? $ configData [$ key ] : 0 ;
120+ }
121+ );
104122
105123 $ this ->shipping ->composePackagesForCarrier ($ this ->carrier , $ request );
106124 }
125+
126+ /**
127+ * Active flag should be set before collecting carrier rates.
128+ */
129+ public function testCollectCarrierRatesSetActiveFlag ()
130+ {
131+ $ this ->carrier ->expects ($ this ->atLeastOnce ())
132+ ->method ('setActiveFlag ' )
133+ ->with ('active ' );
134+
135+ $ this ->shipping ->collectCarrierRates ('carrier ' , new RateRequest ());
136+ }
137+
138+ /**
139+ * @return CarrierFactory|MockObject
140+ */
141+ private function getCarrierFactory ()
142+ {
143+ $ carrierFactory = $ this ->getMockBuilder (CarrierFactory::class)
144+ ->disableOriginalConstructor ()
145+ ->getMock ();
146+
147+ $ this ->carrier = $ this ->getMockBuilder (AbstractCarrierInterface::class)
148+ ->setMethods (
149+ [
150+ 'setActiveFlag ' ,
151+ 'checkAvailableShipCountries ' ,
152+ 'processAdditionalValidation ' ,
153+ 'getConfigData ' ,
154+ 'collectRates ' ,
155+ ]
156+ )
157+ ->getMockForAbstractClass ();
158+ $ carrierFactory ->method ('create ' )->willReturn ($ this ->carrier );
159+
160+ return $ carrierFactory ;
161+ }
107162}
0 commit comments