PageRenderTime 48ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/magento/module-catalog-inventory/Test/Unit/Model/Quote/Item/QuantityValidator/Initializer/StockItemTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 257 lines | 223 code | 15 blank | 19 comment | 0 complexity | ab85e663c098205a5ea00412028aabbb MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogInventory\Test\Unit\Model\Quote\Item\QuantityValidator\Initializer;
  7. use Magento\CatalogInventory\Model\Quote\Item\QuantityValidator\QuoteItemQtyList;
  8. class StockItemTest extends \PHPUnit_Framework_TestCase
  9. {
  10. /**
  11. * @var \Magento\CatalogInventory\Model\Quote\Item\QuantityValidator\Initializer\StockItem
  12. */
  13. protected $model;
  14. /**
  15. * @var QuoteItemQtyList| \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $quoteItemQtyList;
  18. /**
  19. * @var \Magento\Catalog\Model\ProductTypes\ConfigInterface| \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $typeConfig;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $stockStateMock;
  26. protected function setUp()
  27. {
  28. $this->quoteItemQtyList = $this
  29. ->getMockBuilder('Magento\CatalogInventory\Model\Quote\Item\QuantityValidator\QuoteItemQtyList')
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. $this->typeConfig = $this
  33. ->getMockBuilder('Magento\Catalog\Model\ProductTypes\ConfigInterface')
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  37. $this->stockStateMock = $this->getMockBuilder('Magento\CatalogInventory\Api\StockStateInterface')
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->model = $objectManagerHelper->getObject(
  41. 'Magento\CatalogInventory\Model\Quote\Item\QuantityValidator\Initializer\StockItem',
  42. [
  43. 'quoteItemQtyList' => $this->quoteItemQtyList,
  44. 'typeConfig' => $this->typeConfig,
  45. 'stockState' => $this->stockStateMock
  46. ]
  47. );
  48. }
  49. /**
  50. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  51. */
  52. public function testInitializeWithSubitem()
  53. {
  54. $qty = 2;
  55. $parentItemQty = 3;
  56. $websiteId = 1;
  57. $stockItem = $this->getMock(
  58. 'Magento\CatalogInventory\Model\Stock\Item',
  59. ['checkQuoteItemQty', 'setProductName', 'setIsChildItem', 'hasIsChildItem', 'unsIsChildItem', '__wakeup'],
  60. [],
  61. '',
  62. false
  63. );
  64. $quoteItem = $this->getMockBuilder('Magento\Quote\Model\Quote\Item')
  65. ->setMethods(
  66. [
  67. 'getParentItem',
  68. 'getProduct',
  69. 'getId',
  70. 'getQuoteId',
  71. 'setIsQtyDecimal',
  72. 'setData',
  73. 'setUseOldQty',
  74. 'setMessage',
  75. 'setBackorders',
  76. '__wakeup',
  77. ]
  78. )
  79. ->disableOriginalConstructor()
  80. ->getMock();
  81. $parentItem = $this->getMockBuilder('Magento\Quote\Model\Quote\Item')
  82. ->setMethods(['getQty', 'setIsQtyDecimal', 'getProduct', '__wakeup'])
  83. ->disableOriginalConstructor()
  84. ->getMock();
  85. $product = $this->getMockBuilder('Magento\Catalog\Model\Product')
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. $parentProduct = $this->getMockBuilder('Magento\Catalog\Model\Product')
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. $productTypeInstance = $this->getMockBuilder('Magento\Catalog\Model\Product\Type\AbstractType')
  92. ->disableOriginalConstructor()
  93. ->getMock();
  94. $storeMock = $this->getMockBuilder('Magento\Store\Model\Store')
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. $storeMock->expects($this->any())
  98. ->method('getWebsiteId')
  99. ->willReturn($websiteId);
  100. $productTypeCustomOption = $this->getMockBuilder('Magento\Catalog\Model\Product\Configuration\Item\Option')
  101. ->disableOriginalConstructor()
  102. ->getMock();
  103. $result = $this->getMockBuilder('Magento\Framework\DataObject')
  104. ->setMethods(
  105. [
  106. 'getItemIsQtyDecimal',
  107. 'getHasQtyOptionUpdate',
  108. 'getOrigQty',
  109. 'getItemUseOldQty',
  110. 'getMessage',
  111. 'getItemBackorders',
  112. ]
  113. )
  114. ->disableOriginalConstructor()
  115. ->getMock();
  116. $quoteItem->expects($this->any())->method('getParentItem')->will($this->returnValue($parentItem));
  117. $parentItem->expects($this->once())->method('getQty')->will($this->returnValue($parentItemQty));
  118. $quoteItem->expects($this->any())->method('getProduct')->will($this->returnValue($product));
  119. $product->expects($this->any())->method('getId')->will($this->returnValue('product_id'));
  120. $quoteItem->expects($this->once())->method('getId')->will($this->returnValue('quote_item_id'));
  121. $quoteItem->expects($this->once())->method('getQuoteId')->will($this->returnValue('quote_id'));
  122. $this->quoteItemQtyList->expects($this->any())
  123. ->method('getQty')
  124. ->with('product_id', 'quote_item_id', 'quote_id', 0)
  125. ->will($this->returnValue('summary_qty'));
  126. $this->stockStateMock->expects($this->once())
  127. ->method('checkQuoteItemQty')
  128. ->withAnyParameters()
  129. ->will($this->returnValue($result));
  130. $product->expects($this->once())
  131. ->method('getCustomOption')
  132. ->with('product_type')
  133. ->will($this->returnValue($productTypeCustomOption));
  134. $productTypeCustomOption->expects($this->once())
  135. ->method('getValue')
  136. ->will(($this->returnValue('option_value')));
  137. $this->typeConfig->expects($this->once())
  138. ->method('isProductSet')
  139. ->with('option_value')
  140. ->will($this->returnValue(true));
  141. $product->expects($this->once())->method('getName')->will($this->returnValue('product_name'));
  142. $product->expects($this->any())
  143. ->method('getStore')
  144. ->willReturn($storeMock);
  145. $stockItem->expects($this->once())->method('setProductName')->with('product_name')->will($this->returnSelf());
  146. $stockItem->expects($this->once())->method('setIsChildItem')->with(true)->will($this->returnSelf());
  147. $stockItem->expects($this->once())->method('hasIsChildItem')->will($this->returnValue(true));
  148. $stockItem->expects($this->once())->method('unsIsChildItem');
  149. $result->expects($this->exactly(3))->method('getItemIsQtyDecimal')->will($this->returnValue(true));
  150. $quoteItem->expects($this->once())->method('setIsQtyDecimal')->with(true)->will($this->returnSelf());
  151. $parentItem->expects($this->once())->method('setIsQtyDecimal')->with(true)->will($this->returnSelf());
  152. $parentItem->expects($this->any())->method('getProduct')->will($this->returnValue($parentProduct));
  153. $result->expects($this->once())->method('getHasQtyOptionUpdate')->will($this->returnValue(true));
  154. $parentProduct->expects($this->once())
  155. ->method('getTypeInstance')
  156. ->will($this->returnValue($productTypeInstance));
  157. $productTypeInstance->expects($this->once())
  158. ->method('getForceChildItemQtyChanges')
  159. ->with($product)->will($this->returnValue(true));
  160. $result->expects($this->once())->method('getOrigQty')->will($this->returnValue('orig_qty'));
  161. $quoteItem->expects($this->once())->method('setData')->with('qty', 'orig_qty')->will($this->returnSelf());
  162. $result->expects($this->exactly(2))->method('getItemUseOldQty')->will($this->returnValue('item'));
  163. $quoteItem->expects($this->once())->method('setUseOldQty')->with('item')->will($this->returnSelf());
  164. $result->expects($this->exactly(2))->method('getMessage')->will($this->returnValue('message'));
  165. $quoteItem->expects($this->once())->method('setMessage')->with('message')->will($this->returnSelf());
  166. $result->expects($this->exactly(2))->method('getItemBackorders')->will($this->returnValue('backorders'));
  167. $quoteItem->expects($this->once())->method('setBackorders')->with('backorders')->will($this->returnSelf());
  168. $this->model->initialize($stockItem, $quoteItem, $qty);
  169. }
  170. public function testInitializeWithoutSubitem()
  171. {
  172. $qty = 3;
  173. $websiteId = 1;
  174. $productId = 1;
  175. $stockItem = $this->getMockBuilder('Magento\CatalogInventory\Model\Stock\Item')
  176. ->setMethods(['checkQuoteItemQty', 'setProductName', 'setIsChildItem', 'hasIsChildItem', '__wakeup'])
  177. ->disableOriginalConstructor()
  178. ->getMock();
  179. $storeMock = $this->getMockBuilder('Magento\Store\Model\Store')
  180. ->disableOriginalConstructor()
  181. ->getMock();
  182. $storeMock->expects($this->any())
  183. ->method('getWebsiteId')
  184. ->willReturn($websiteId);
  185. $quoteItem = $this->getMockBuilder('Magento\Quote\Model\Quote\Item')
  186. ->setMethods(['getProduct', 'getParentItem', 'getQtyToAdd', 'getId', 'getQuoteId', '__wakeup'])
  187. ->disableOriginalConstructor()
  188. ->getMock();
  189. $product = $this->getMockBuilder('Magento\Catalog\Model\Product')
  190. ->disableOriginalConstructor()
  191. ->getMock();
  192. $productTypeCustomOption = $this->getMockBuilder('Magento\Catalog\Model\Product\Configuration\Item\Option')
  193. ->disableOriginalConstructor()
  194. ->getMock();
  195. $result = $this->getMockBuilder('Magento\Framework\DataObject')
  196. ->setMethods(
  197. ['getItemIsQtyDecimal', 'getHasQtyOptionUpdate', 'getItemUseOldQty', 'getMessage', 'getItemBackorders']
  198. )
  199. ->disableOriginalConstructor()
  200. ->getMock();
  201. $product->expects($this->any())
  202. ->method('getStore')
  203. ->willReturn($storeMock);
  204. $product->expects($this->any())
  205. ->method('getId')
  206. ->willReturn($productId);
  207. $quoteItem->expects($this->once())->method('getParentItem')->will($this->returnValue(false));
  208. $quoteItem->expects($this->once())->method('getQtyToAdd')->will($this->returnValue(false));
  209. $quoteItem->expects($this->any())->method('getProduct')->will($this->returnValue($product));
  210. $quoteItem->expects($this->once())->method('getId')->will($this->returnValue('quote_item_id'));
  211. $quoteItem->expects($this->once())->method('getQuoteId')->will($this->returnValue('quote_id'));
  212. $this->quoteItemQtyList->expects($this->any())
  213. ->method('getQty')
  214. ->with($productId, 'quote_item_id', 'quote_id', $qty)
  215. ->will($this->returnValue('summary_qty'));
  216. $this->stockStateMock->expects($this->once())
  217. ->method('checkQuoteItemQty')
  218. ->withAnyParameters()
  219. ->will($this->returnValue($result));
  220. $product->expects($this->once())
  221. ->method('getCustomOption')
  222. ->with('product_type')
  223. ->will($this->returnValue($productTypeCustomOption));
  224. $productTypeCustomOption->expects($this->once())
  225. ->method('getValue')
  226. ->will($this->returnValue('option_value'));
  227. $this->typeConfig->expects($this->once())
  228. ->method('isProductSet')
  229. ->with('option_value')
  230. ->will($this->returnValue(true));
  231. $product->expects($this->once())->method('getName')->will($this->returnValue('product_name'));
  232. $stockItem->expects($this->once())->method('setProductName')->with('product_name')->will($this->returnSelf());
  233. $stockItem->expects($this->once())->method('setIsChildItem')->with(true)->will($this->returnSelf());
  234. $stockItem->expects($this->once())->method('hasIsChildItem')->will($this->returnValue(false));
  235. $result->expects($this->once())->method('getItemIsQtyDecimal')->will($this->returnValue(null));
  236. $result->expects($this->once())->method('getHasQtyOptionUpdate')->will($this->returnValue(false));
  237. $result->expects($this->once())->method('getItemUseOldQty')->will($this->returnValue(null));
  238. $result->expects($this->once())->method('getMessage')->will($this->returnValue(null));
  239. $result->expects($this->once())->method('getItemBackorders')->will($this->returnValue(null));
  240. $this->model->initialize($stockItem, $quoteItem, $qty);
  241. }
  242. }