ShippingTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Shipping\Test\Unit\Model;
  7. use \Magento\Shipping\Model\Shipping;
  8. use Magento\Quote\Model\Quote\Address\RateRequest;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  10. class ShippingTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * Test identification number of product
  14. *
  15. * @var int
  16. */
  17. protected $productId = 1;
  18. /**
  19. * @var Shipping
  20. */
  21. protected $shipping;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Shipping\Model\Carrier\AbstractCarrier
  24. */
  25. protected $carrier;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $stockRegistry;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $stockItemData;
  34. protected function setUp()
  35. {
  36. $this->carrier = $this->createMock(\Magento\Shipping\Model\Carrier\AbstractCarrier::class);
  37. $this->carrier->expects($this->any())->method('getConfigData')->will($this->returnCallback(function ($key) {
  38. $configData = [
  39. 'max_package_weight' => 10,
  40. ];
  41. return isset($configData[$key]) ? $configData[$key] : 0;
  42. }));
  43. $this->stockRegistry = $this->createMock(\Magento\CatalogInventory\Model\StockRegistry::class);
  44. $this->stockItemData = $this->createMock(\Magento\CatalogInventory\Model\Stock\Item::class);
  45. $objectManagerHelper = new ObjectManagerHelper($this);
  46. $this->shipping = $objectManagerHelper->getObject(
  47. \Magento\Shipping\Model\Shipping::class,
  48. ['stockRegistry' => $this->stockRegistry]
  49. );
  50. }
  51. /**
  52. * @covers \Magento\Shipping\Model\Shipping::composePackagesForCarrier
  53. */
  54. public function testComposePackages()
  55. {
  56. $request = new RateRequest();
  57. /** \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface */
  58. $item = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
  59. ->disableOriginalConstructor()
  60. ->setMethods([
  61. 'getQty', 'getIsQtyDecimal', 'getProductType', 'getProduct', 'getWeight', '__wakeup', 'getStore',
  62. ])
  63. ->getMock();
  64. $product = $this->createMock(\Magento\Catalog\Model\Product::class);
  65. $item->expects($this->any())->method('getQty')->will($this->returnValue(1));
  66. $item->expects($this->any())->method('getWeight')->will($this->returnValue(10));
  67. $item->expects($this->any())->method('getIsQtyDecimal')->will($this->returnValue(true));
  68. $item->expects($this->any())->method('getProductType')
  69. ->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE));
  70. $item->expects($this->any())->method('getProduct')->will($this->returnValue($product));
  71. $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getWebsiteId']);
  72. $store->expects($this->any())
  73. ->method('getWebsiteId')
  74. ->will($this->returnValue(10));
  75. $item->expects($this->any())->method('getStore')->will($this->returnValue($store));
  76. $product->expects($this->any())->method('getId')->will($this->returnValue($this->productId));
  77. $request->setData('all_items', [$item]);
  78. $this->stockItemData->expects($this->any())->method('getIsDecimalDivided')->will($this->returnValue(true));
  79. /** Testable service calls to CatalogInventory module */
  80. $this->stockRegistry->expects($this->atLeastOnce())->method('getStockItem')
  81. ->with($this->productId, 10)
  82. ->will($this->returnValue($this->stockItemData));
  83. $this->stockItemData->expects($this->atLeastOnce())
  84. ->method('getEnableQtyIncrements')
  85. ->will($this->returnValue(true));
  86. $this->stockItemData->expects($this->atLeastOnce())->method('getQtyIncrements')
  87. ->will($this->returnValue(0.5));
  88. $this->shipping->composePackagesForCarrier($this->carrier, $request);
  89. }
  90. }