PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-quote/Test/Unit/Model/Quote/Item/ProcessorTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 435 lines | 338 code | 66 blank | 31 comment | 0 complexity | ca17435823ccb9c5aaaeee3f19a7714e MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Test\Unit\Model\Quote\Item;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Quote\Api\Data\CartItemInterface;
  9. use Magento\Quote\Model\Quote\Item\Processor;
  10. use Magento\Quote\Model\Quote\ItemFactory;
  11. use Magento\Quote\Model\Quote\Item;
  12. use Magento\Store\Model\StoreManagerInterface;
  13. use Magento\Store\Model\Store;
  14. use Magento\Framework\App\State;
  15. use Magento\Framework\DataObject;
  16. /**
  17. * Tests for Magento\Quote\Model\Service\Quote\Processor
  18. */
  19. class ProcessorTest extends \PHPUnit_Framework_TestCase
  20. {
  21. /**
  22. * @var Processor
  23. */
  24. protected $processor;
  25. /**
  26. * @var ItemFactory |\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $quoteItemFactoryMock;
  29. /**
  30. * @var StoreManagerInterface |\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $storeManagerMock;
  33. /**
  34. * @var State |\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $stateMock;
  37. /**
  38. * @var Product |\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $productMock;
  41. /**
  42. * @var Object |\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $objectMock;
  45. /**
  46. * @var Item |\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. protected $itemMock;
  49. /**
  50. * @var Store |\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. protected $storeMock;
  53. protected function setUp()
  54. {
  55. $this->quoteItemFactoryMock = $this->getMock(
  56. 'Magento\Quote\Model\Quote\ItemFactory',
  57. ['create'],
  58. [],
  59. '',
  60. false
  61. );
  62. $this->itemMock = $this->getMock(
  63. 'Magento\Quote\Model\Quote\Item',
  64. [
  65. 'getId',
  66. 'setOptions',
  67. '__wakeup',
  68. 'setProduct',
  69. 'addQty',
  70. 'setCustomPrice',
  71. 'setOriginalCustomPrice',
  72. 'setData'
  73. ],
  74. [],
  75. '',
  76. false
  77. );
  78. $this->quoteItemFactoryMock->expects($this->any())
  79. ->method('create')
  80. ->will($this->returnValue($this->itemMock));
  81. $this->storeManagerMock = $this->getMock(
  82. 'Magento\Store\Model\StoreManager',
  83. ['getStore'],
  84. [],
  85. '',
  86. false
  87. );
  88. $this->storeMock = $this->getMock('Magento\Store\Model\Store', ['getId', '__wakeup'], [], '', false);
  89. $this->storeManagerMock->expects($this->any())
  90. ->method('getStore')
  91. ->will($this->returnValue($this->storeMock));
  92. $this->stateMock = $this->getMock(
  93. 'Magento\Framework\App\State',
  94. [],
  95. [],
  96. '',
  97. false
  98. );
  99. $this->processor = new Processor(
  100. $this->quoteItemFactoryMock,
  101. $this->storeManagerMock,
  102. $this->stateMock
  103. );
  104. $this->productMock = $this->getMock(
  105. 'Magento\Catalog\Model\Product',
  106. ['getCustomOptions', '__wakeup', 'getParentProductId', 'getCartQty', 'getStickWithinParent'],
  107. [],
  108. '',
  109. false
  110. );
  111. $this->objectMock = $this->getMock(
  112. 'Magento\Framework\DataObject',
  113. ['getResetCount', 'getId', 'getCustomPrice'],
  114. [],
  115. '',
  116. false
  117. );
  118. }
  119. public function testInitWithQtyModification()
  120. {
  121. $storeId = 1000000000;
  122. $productCustomOptions = 'test_custom_options';
  123. $requestId = 20000000;
  124. $itemId = $requestId;
  125. $this->productMock->expects($this->any())
  126. ->method('getCustomOptions')
  127. ->will($this->returnValue($productCustomOptions));
  128. $this->productMock->expects($this->any())
  129. ->method('getStickWithinParent')
  130. ->will($this->returnValue(false));
  131. $this->itemMock->expects($this->any())
  132. ->method('setOptions')
  133. ->will($this->returnValue($productCustomOptions));
  134. $this->itemMock->expects($this->any())
  135. ->method('setProduct')
  136. ->will($this->returnValue($this->productMock));
  137. $this->itemMock->expects($this->any())
  138. ->method('getId')
  139. ->will($this->returnValue($itemId));
  140. $this->itemMock->expects($this->any())
  141. ->method('setData')
  142. ->willReturnMap(
  143. [
  144. ['store_id', $storeId],
  145. ['qty', 0],
  146. ]
  147. );
  148. $this->storeMock->expects($this->any())
  149. ->method('getId')
  150. ->will($this->returnValue($storeId));
  151. $this->objectMock->expects($this->any())
  152. ->method('getResetCount')
  153. ->will($this->returnValue(true));
  154. $this->objectMock->expects($this->any())
  155. ->method('getId')
  156. ->will($this->returnValue($requestId));
  157. $this->processor->init($this->productMock, $this->objectMock);
  158. }
  159. public function testInitWithoutModification()
  160. {
  161. $storeId = 1000000000;
  162. $itemId = 2000000000;
  163. $this->productMock->expects($this->never())->method('getCustomOptions');
  164. $this->productMock->expects($this->never())->method('getStickWithinParent');
  165. $this->productMock->expects($this->any())
  166. ->method('getParentProductId')
  167. ->will($this->returnValue(true));
  168. $this->itemMock->expects($this->never())->method('setOptions');
  169. $this->itemMock->expects($this->never())->method('setProduct');
  170. $this->itemMock->expects($this->any())
  171. ->method('getId')
  172. ->will($this->returnValue($itemId));
  173. $this->itemMock->expects($this->any())
  174. ->method('setData')
  175. ->willReturnMap(
  176. [
  177. ['store_id', $storeId],
  178. ]
  179. );
  180. $this->storeMock->expects($this->any())
  181. ->method('getId')
  182. ->will($this->returnValue($storeId));
  183. $this->objectMock->expects($this->never())->method('getResetCount');
  184. $this->objectMock->expects($this->never())->method('getId');
  185. $this->processor->init($this->productMock, $this->objectMock);
  186. }
  187. public function testInitWithoutModificationAdminhtmlAreaCode()
  188. {
  189. $areaCode = \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE;
  190. $storeId = 1000000000;
  191. $requestId = 20000000;
  192. $itemId = $requestId;
  193. $this->stateMock->expects($this->any())
  194. ->method('getAreaCode')
  195. ->will($this->returnValue($areaCode));
  196. $this->productMock->expects($this->never())->method('getCustomOptions');
  197. $this->productMock->expects($this->never())->method('getStickWithinParent');
  198. $this->productMock->expects($this->any())
  199. ->method('getParentProductId')
  200. ->will($this->returnValue(true));
  201. $this->itemMock->expects($this->never())->method('setOptions');
  202. $this->itemMock->expects($this->never())->method('setProduct');
  203. $this->itemMock->expects($this->any())
  204. ->method('getId')
  205. ->will($this->returnValue($itemId));
  206. $this->itemMock->expects($this->any())
  207. ->method('setData')
  208. ->willReturnMap(
  209. [
  210. ['store_id', $storeId],
  211. ]
  212. );
  213. $this->storeMock->expects($this->any())
  214. ->method('getId')
  215. ->will($this->returnValue($storeId));
  216. $this->objectMock->expects($this->never())->method('getResetCount');
  217. $this->objectMock->expects($this->never())->method('getId');
  218. $this->processor->init($this->productMock, $this->objectMock);
  219. }
  220. public function testPrepare()
  221. {
  222. $qty = 3000000000;
  223. $customPrice = 400000000;
  224. $itemId = 1;
  225. $requestItemId = 1;
  226. $this->productMock->expects($this->any())
  227. ->method('getCartQty')
  228. ->will($this->returnValue($qty));
  229. $this->productMock->expects($this->any())
  230. ->method('getStickWithinParent')
  231. ->will($this->returnValue(false));
  232. $this->itemMock->expects($this->once())
  233. ->method('addQty')
  234. ->with($qty);
  235. $this->itemMock->expects($this->any())
  236. ->method('getId')
  237. ->will($this->returnValue($itemId));
  238. $this->itemMock->expects($this->never())
  239. ->method('setData');
  240. $this->objectMock->expects($this->any())
  241. ->method('getCustomPrice')
  242. ->will($this->returnValue($customPrice));
  243. $this->objectMock->expects($this->any())
  244. ->method('getResetCount')
  245. ->will($this->returnValue(false));
  246. $this->objectMock->expects($this->any())
  247. ->method('getId')
  248. ->will($this->returnValue($requestItemId));
  249. $this->itemMock->expects($this->once())
  250. ->method('setCustomPrice')
  251. ->will($this->returnValue($customPrice));
  252. $this->itemMock->expects($this->once())
  253. ->method('setOriginalCustomPrice')
  254. ->will($this->returnValue($customPrice));
  255. $this->processor->prepare($this->itemMock, $this->objectMock, $this->productMock);
  256. }
  257. public function testPrepareWithResetCountAndStick()
  258. {
  259. $qty = 3000000000;
  260. $customPrice = 400000000;
  261. $itemId = 1;
  262. $requestItemId = 1;
  263. $this->productMock->expects($this->any())
  264. ->method('getCartQty')
  265. ->will($this->returnValue($qty));
  266. $this->productMock->expects($this->any())
  267. ->method('getStickWithinParent')
  268. ->will($this->returnValue(true));
  269. $this->itemMock->expects($this->once())
  270. ->method('addQty')
  271. ->with($qty);
  272. $this->itemMock->expects($this->any())
  273. ->method('getId')
  274. ->will($this->returnValue($itemId));
  275. $this->itemMock->expects($this->never())
  276. ->method('setData');
  277. $this->objectMock->expects($this->any())
  278. ->method('getCustomPrice')
  279. ->will($this->returnValue($customPrice));
  280. $this->objectMock->expects($this->any())
  281. ->method('getResetCount')
  282. ->will($this->returnValue(true));
  283. $this->objectMock->expects($this->any())
  284. ->method('getId')
  285. ->will($this->returnValue($requestItemId));
  286. $this->itemMock->expects($this->once())
  287. ->method('setCustomPrice')
  288. ->will($this->returnValue($customPrice));
  289. $this->itemMock->expects($this->once())
  290. ->method('setOriginalCustomPrice')
  291. ->will($this->returnValue($customPrice));
  292. $this->processor->prepare($this->itemMock, $this->objectMock, $this->productMock);
  293. }
  294. public function testPrepareWithResetCountAndNotStickAndOtherItemId()
  295. {
  296. $qty = 3000000000;
  297. $customPrice = 400000000;
  298. $itemId = 1;
  299. $requestItemId = 2;
  300. $this->productMock->expects($this->any())
  301. ->method('getCartQty')
  302. ->will($this->returnValue($qty));
  303. $this->productMock->expects($this->any())
  304. ->method('getStickWithinParent')
  305. ->will($this->returnValue(false));
  306. $this->itemMock->expects($this->once())
  307. ->method('addQty')
  308. ->with($qty);
  309. $this->itemMock->expects($this->any())
  310. ->method('getId')
  311. ->will($this->returnValue($itemId));
  312. $this->itemMock->expects($this->never())
  313. ->method('setData');
  314. $this->objectMock->expects($this->any())
  315. ->method('getCustomPrice')
  316. ->will($this->returnValue($customPrice));
  317. $this->objectMock->expects($this->any())
  318. ->method('getResetCount')
  319. ->will($this->returnValue(true));
  320. $this->objectMock->expects($this->any())
  321. ->method('getId')
  322. ->will($this->returnValue($requestItemId));
  323. $this->itemMock->expects($this->once())
  324. ->method('setCustomPrice')
  325. ->will($this->returnValue($customPrice));
  326. $this->itemMock->expects($this->once())
  327. ->method('setOriginalCustomPrice')
  328. ->will($this->returnValue($customPrice));
  329. $this->processor->prepare($this->itemMock, $this->objectMock, $this->productMock);
  330. }
  331. public function testPrepareWithResetCountAndNotStickAndSameItemId()
  332. {
  333. $qty = 3000000000;
  334. $customPrice = 400000000;
  335. $itemId = 1;
  336. $requestItemId = 1;
  337. $this->objectMock->expects($this->any())
  338. ->method('getResetCount')
  339. ->will($this->returnValue(true));
  340. $this->itemMock->expects($this->any())
  341. ->method('getId')
  342. ->will($this->returnValue($itemId));
  343. $this->itemMock->expects($this->once())
  344. ->method('setData')
  345. ->with(CartItemInterface::KEY_QTY, 0);
  346. $this->productMock->expects($this->any())
  347. ->method('getCartQty')
  348. ->will($this->returnValue($qty));
  349. $this->productMock->expects($this->any())
  350. ->method('getStickWithinParent')
  351. ->will($this->returnValue(false));
  352. $this->itemMock->expects($this->once())
  353. ->method('addQty')
  354. ->with($qty);
  355. $this->objectMock->expects($this->any())
  356. ->method('getCustomPrice')
  357. ->will($this->returnValue($customPrice));
  358. $this->objectMock->expects($this->any())
  359. ->method('getId')
  360. ->will($this->returnValue($requestItemId));
  361. $this->itemMock->expects($this->once())
  362. ->method('setCustomPrice')
  363. ->will($this->returnValue($customPrice));
  364. $this->itemMock->expects($this->once())
  365. ->method('setOriginalCustomPrice')
  366. ->will($this->returnValue($customPrice));
  367. $this->processor->prepare($this->itemMock, $this->objectMock, $this->productMock);
  368. }
  369. }