Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShippingTest.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Shipping\Model\Shipping;
9 
12 
13 class ShippingTest extends \PHPUnit\Framework\TestCase
14 {
20  protected $productId = 1;
21 
25  protected $shipping;
26 
30  protected $carrier;
31 
35  protected $stockRegistry;
36 
40  protected $stockItemData;
41 
42  protected function setUp()
43  {
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]
58  );
59  }
60 
64  public function testComposePackages()
65  {
66  $request = new RateRequest();
68  $item = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
69  ->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));
90  $request->setData('all_items', [$item]);
91 
92  $this->stockItemData->expects($this->any())->method('getIsDecimalDivided')->will($this->returnValue(true));
93 
95  $this->stockRegistry->expects($this->atLeastOnce())->method('getStockItem')
96  ->with($this->productId, 10)
97  ->will($this->returnValue($this->stockItemData));
98 
99  $this->stockItemData->expects($this->atLeastOnce())
100  ->method('getEnableQtyIncrements')
101  ->will($this->returnValue(true));
102  $this->stockItemData->expects($this->atLeastOnce())->method('getQtyIncrements')
103  ->will($this->returnValue(0.5));
104 
105  $this->shipping->composePackagesForCarrier($this->carrier, $request);
106  }
107 }