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 \AbstractCarrier ;
14
+ use Magento \Shipping \Model \Carrier \AbstractCarrierInterface ;
15
+ use Magento \Shipping \Model \Carrier \CarrierInterface ;
16
+ use Magento \Shipping \Model \CarrierFactory ;
17
+ use Magento \Shipping \Model \Shipping ;
10
18
use Magento \Quote \Model \Quote \Address \RateRequest ;
11
19
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
20
+ use Magento \Store \Model \Store ;
21
+ use PHPUnit_Framework_MockObject_MockObject as MockObject ;
12
22
13
23
class ShippingTest extends \PHPUnit \Framework \TestCase
14
24
{
@@ -25,71 +35,69 @@ class ShippingTest extends \PHPUnit\Framework\TestCase
25
35
protected $ shipping ;
26
36
27
37
/**
28
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Shipping\Model\Carrier\AbstractCarrier
38
+ * @var MockObject|StockRegistry
29
39
*/
30
- protected $ carrier ;
40
+ protected $ stockRegistry ;
31
41
32
42
/**
33
- * @var \PHPUnit_Framework_MockObject_MockObject
43
+ * @var MockObject|StockItem
34
44
*/
35
- protected $ stockRegistry ;
45
+ protected $ stockItemData ;
36
46
37
47
/**
38
- * @var \PHPUnit_Framework_MockObject_MockObject
48
+ * @var MockObject|AbstractCarrierInterface
39
49
*/
40
- protected $ stockItemData ;
50
+ private $ carrier ;
41
51
42
52
protected function setUp ()
43
53
{
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 ]
54
+ $ this ->stockRegistry = $ this ->createMock (StockRegistry::class);
55
+ $ this ->stockItemData = $ this ->createMock (StockItem::class);
56
+
57
+ $ this ->shipping = (new ObjectManagerHelper ($ this ))->getObject (
58
+ Shipping::class,
59
+ [
60
+ 'stockRegistry ' => $ this ->stockRegistry ,
61
+ 'carrierFactory ' => $ this ->getCarrierFactory (),
62
+ ]
58
63
);
59
64
}
60
65
61
66
/**
62
- * @covers \Magento\Shipping\Model\Shipping::composePackagesForCarrier
67
+ *
63
68
*/
64
69
public function testComposePackages ()
65
70
{
66
71
$ request = new RateRequest ();
67
- /** \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface */
68
- $ item = $ this ->getMockBuilder (\Magento \Quote \Model \Quote \Item::class)
72
+ $ item = $ this ->getMockBuilder (QuoteItem::class)
69
73
->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 ));
74
+ ->setMethods (
75
+ [
76
+ 'getQty ' ,
77
+ 'getIsQtyDecimal ' ,
78
+ 'getProductType ' ,
79
+ 'getProduct ' ,
80
+ 'getWeight ' ,
81
+ '__wakeup ' ,
82
+ 'getStore ' ,
83
+ ]
84
+ )->getMock ();
85
+ $ product = $ this ->createMock (Product::class);
86
+
87
+ $ item ->method ('getQty ' )->will ($ this ->returnValue (1 ));
88
+ $ item ->method ('getWeight ' )->will ($ this ->returnValue (10 ));
89
+ $ item ->method ('getIsQtyDecimal ' )->will ($ this ->returnValue (true ));
90
+ $ item ->method ('getProductType ' )->will ($ this ->returnValue (ProductType::TYPE_SIMPLE ));
91
+ $ item ->method ('getProduct ' )->will ($ this ->returnValue ($ product ));
92
+
93
+ $ store = $ this ->createPartialMock (Store::class, ['getWebsiteId ' ]);
94
+ $ store ->method ('getWebsiteId ' )->will ($ this ->returnValue (10 ));
95
+ $ item ->method ('getStore ' )->will ($ this ->returnValue ($ store ));
96
+
97
+ $ product ->method ('getId ' )->will ($ this ->returnValue ($ this ->productId ));
90
98
$ request ->setData ('all_items ' , [$ item ]);
91
99
92
- $ this ->stockItemData ->expects ( $ this -> any ())-> method ('getIsDecimalDivided ' )->will ($ this ->returnValue (true ));
100
+ $ this ->stockItemData ->method ('getIsDecimalDivided ' )->will ($ this ->returnValue (true ));
93
101
94
102
/** Testable service calls to CatalogInventory module */
95
103
$ this ->stockRegistry ->expects ($ this ->atLeastOnce ())->method ('getStockItem ' )
@@ -101,7 +109,51 @@ public function testComposePackages()
101
109
->will ($ this ->returnValue (true ));
102
110
$ this ->stockItemData ->expects ($ this ->atLeastOnce ())->method ('getQtyIncrements ' )
103
111
->will ($ this ->returnValue (0.5 ));
112
+ $ this ->carrier ->method ('getConfigData ' )
113
+ ->willReturnCallback (function ($ key ) {
114
+ $ configData = [
115
+ 'max_package_weight ' => 10 ,
116
+ ];
117
+ return isset ($ configData [$ key ]) ? $ configData [$ key ] : 0 ;
118
+ });
104
119
105
120
$ this ->shipping ->composePackagesForCarrier ($ this ->carrier , $ request );
106
121
}
122
+
123
+ /**
124
+ * Active flag should be set before collecting carrier rates.
125
+ */
126
+ public function testCollectCarrierRatesSetActiveFlag ()
127
+ {
128
+ $ this ->carrier ->expects ($ this ->atLeastOnce ())
129
+ ->method ('setActiveFlag ' )
130
+ ->with ('active ' );
131
+
132
+ $ this ->shipping ->collectCarrierRates ('carrier ' , new RateRequest ());
133
+ }
134
+
135
+ /**
136
+ * @return CarrierFactory|MockObject
137
+ */
138
+ private function getCarrierFactory ()
139
+ {
140
+ $ carrierFactory = $ this ->getMockBuilder (CarrierFactory::class)
141
+ ->disableOriginalConstructor ()
142
+ ->getMock ();
143
+
144
+ $ this ->carrier = $ this ->getMockBuilder (AbstractCarrierInterface::class)
145
+ ->setMethods (
146
+ [
147
+ 'setActiveFlag ' ,
148
+ 'checkAvailableShipCountries ' ,
149
+ 'processAdditionalValidation ' ,
150
+ 'getConfigData ' ,
151
+ 'collectRates ' ,
152
+ ]
153
+ )
154
+ ->getMockForAbstractClass ();
155
+ $ carrierFactory ->method ('create ' )->willReturn ($ this ->carrier );
156
+
157
+ return $ carrierFactory ;
158
+ }
107
159
}
0 commit comments