5
5
*/
6
6
namespace Magento \Shipping \Test \Unit \Model ;
7
7
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 ;
10
16
use Magento \Quote \Model \Quote \Address \RateRequest ;
11
17
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
18
+ use Magento \Store \Model \Store ;
19
+ use PHPUnit_Framework_MockObject_MockObject as MockObject ;
12
20
21
+ /**
22
+ * @see Shipping
23
+ */
13
24
class ShippingTest extends \PHPUnit \Framework \TestCase
14
25
{
15
26
/**
@@ -25,71 +36,69 @@ class ShippingTest extends \PHPUnit\Framework\TestCase
25
36
protected $ shipping ;
26
37
27
38
/**
28
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Shipping\Model\Carrier\AbstractCarrier
39
+ * @var MockObject|StockRegistry
29
40
*/
30
- protected $ carrier ;
41
+ protected $ stockRegistry ;
31
42
32
43
/**
33
- * @var \PHPUnit_Framework_MockObject_MockObject
44
+ * @var MockObject|StockItem
34
45
*/
35
- protected $ stockRegistry ;
46
+ protected $ stockItemData ;
36
47
37
48
/**
38
- * @var \PHPUnit_Framework_MockObject_MockObject
49
+ * @var MockObject|AbstractCarrierInterface
39
50
*/
40
- protected $ stockItemData ;
51
+ private $ carrier ;
41
52
42
53
protected function setUp ()
43
54
{
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
+ ]
58
64
);
59
65
}
60
66
61
67
/**
62
- * @covers \Magento\Shipping\Model\Shipping::composePackagesForCarrier
68
+ *
63
69
*/
64
70
public function testComposePackages ()
65
71
{
66
72
$ 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)
69
74
->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 ));
90
99
$ request ->setData ('all_items ' , [$ item ]);
91
100
92
- $ this ->stockItemData ->expects ( $ this -> any ())-> method ('getIsDecimalDivided ' )->will ($ this ->returnValue (true ));
101
+ $ this ->stockItemData ->method ('getIsDecimalDivided ' )->will ($ this ->returnValue (true ));
93
102
94
103
/** Testable service calls to CatalogInventory module */
95
104
$ this ->stockRegistry ->expects ($ this ->atLeastOnce ())->method ('getStockItem ' )
@@ -101,7 +110,53 @@ public function testComposePackages()
101
110
->will ($ this ->returnValue (true ));
102
111
$ this ->stockItemData ->expects ($ this ->atLeastOnce ())->method ('getQtyIncrements ' )
103
112
->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
+ );
104
122
105
123
$ this ->shipping ->composePackagesForCarrier ($ this ->carrier , $ request );
106
124
}
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
+ }
107
162
}
0 commit comments