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
13
21
class ShippingTest extends \PHPUnit \Framework \TestCase
14
22
{
@@ -25,71 +33,69 @@ class ShippingTest extends \PHPUnit\Framework\TestCase
25
33
protected $ shipping ;
26
34
27
35
/**
28
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Shipping\Model\Carrier\AbstractCarrier
36
+ * @var MockObject|StockRegistry
29
37
*/
30
- protected $ carrier ;
38
+ protected $ stockRegistry ;
31
39
32
40
/**
33
- * @var \PHPUnit_Framework_MockObject_MockObject
41
+ * @var MockObject|StockItem
34
42
*/
35
- protected $ stockRegistry ;
43
+ protected $ stockItemData ;
36
44
37
45
/**
38
- * @var \PHPUnit_Framework_MockObject_MockObject
46
+ * @var MockObject|AbstractCarrierInterface
39
47
*/
40
- protected $ stockItemData ;
48
+ private $ carrier ;
41
49
42
50
protected function setUp ()
43
51
{
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
+ ]
58
61
);
59
62
}
60
63
61
64
/**
62
- * @covers \Magento\Shipping\Model\Shipping::composePackagesForCarrier
65
+ *
63
66
*/
64
67
public function testComposePackages ()
65
68
{
66
69
$ 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)
69
71
->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 ));
90
96
$ request ->setData ('all_items ' , [$ item ]);
91
97
92
- $ this ->stockItemData ->expects ( $ this -> any ())-> method ('getIsDecimalDivided ' )->will ($ this ->returnValue (true ));
98
+ $ this ->stockItemData ->method ('getIsDecimalDivided ' )->will ($ this ->returnValue (true ));
93
99
94
100
/** Testable service calls to CatalogInventory module */
95
101
$ this ->stockRegistry ->expects ($ this ->atLeastOnce ())->method ('getStockItem ' )
@@ -101,7 +107,51 @@ public function testComposePackages()
101
107
->will ($ this ->returnValue (true ));
102
108
$ this ->stockItemData ->expects ($ this ->atLeastOnce ())->method ('getQtyIncrements ' )
103
109
->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
+ });
104
117
105
118
$ this ->shipping ->composePackagesForCarrier ($ this ->carrier , $ request );
106
119
}
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
+ }
107
157
}
0 commit comments