UpdaterTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Test\Unit\Model\Quote\Item;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. /**
  9. * Tests for Magento\Quote\Model\Service\Quote\Updater
  10. *
  11. */
  12. class UpdaterTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Quote\Model\Quote\Item\Updater |\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $object;
  18. /**
  19. * @var \Magento\Quote\Model\Quote\Item |\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $itemMock;
  22. /**
  23. * @var \Magento\CatalogInventory\Model\Stock\Item |\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $stockItemMock;
  26. /**
  27. * @var \Magento\Framework\Locale\Format |\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $localeFormat;
  30. /**
  31. * @var \Magento\Catalog\Model\Product |\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $productMock;
  34. /**
  35. * @var \Magento\Framework\Serialize\Serializer\Json
  36. */
  37. private $serializer;
  38. protected function setUp()
  39. {
  40. $this->productMock = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [
  41. 'getStockItem',
  42. '__wakeup',
  43. 'setIsSuperMode',
  44. 'unsSkipCheckRequiredOption'
  45. ]);
  46. $this->localeFormat = $this->createPartialMock(\Magento\Framework\Locale\Format::class, [
  47. 'getNumber', 'getPriceFormat'
  48. ]);
  49. $this->itemMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, [
  50. 'updateItem',
  51. 'getProduct',
  52. 'setQty',
  53. 'setNoDiscount',
  54. 'checkData',
  55. '__wakeup',
  56. 'getBuyRequest',
  57. 'addOption',
  58. 'setCustomPrice',
  59. 'setOriginalCustomPrice',
  60. 'setData',
  61. 'hasData',
  62. 'setIsQtyDecimal'
  63. ]);
  64. $this->stockItemMock = $this->createPartialMock(\Magento\CatalogInventory\Model\Stock\Item::class, [
  65. 'getIsQtyDecimal',
  66. '__wakeup'
  67. ]);
  68. $this->serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
  69. ->setMethods(['serialize'])
  70. ->getMockForAbstractClass();
  71. $this->object = (new ObjectManager($this))
  72. ->getObject(
  73. \Magento\Quote\Model\Quote\Item\Updater::class,
  74. [
  75. 'localeFormat' => $this->localeFormat,
  76. 'serializer' => $this->serializer
  77. ]
  78. );
  79. }
  80. /**
  81. * @expectedException \InvalidArgumentException
  82. * @expectedExceptionMessage The qty value is required to update quote item.
  83. */
  84. public function testUpdateNoQty()
  85. {
  86. $this->object->update($this->itemMock, []);
  87. }
  88. /**
  89. * @dataProvider qtyProvider
  90. */
  91. public function testUpdateNotQtyDecimal($qty, $expectedQty)
  92. {
  93. $this->itemMock->expects($this->any())
  94. ->method('setNoDiscount')
  95. ->will($this->returnValue(true));
  96. $this->itemMock->expects($this->any())
  97. ->method('setQty')
  98. ->with($this->equalTo($expectedQty));
  99. $this->productMock->expects($this->any())
  100. ->method('getStockItem')
  101. ->will($this->returnValue($this->stockItemMock));
  102. $this->productMock->expects($this->any())
  103. ->method('setIsSuperMode')
  104. ->with($this->equalTo(true));
  105. $this->productMock->expects($this->any())
  106. ->method('unsSkipCheckRequiredOption');
  107. $this->itemMock->expects($this->any())
  108. ->method('getProduct')
  109. ->will($this->returnValue($this->productMock));
  110. $result = $this->object->update($this->itemMock, ['qty' => $qty]);
  111. $this->assertEquals($result, $this->object);
  112. }
  113. /**
  114. * @return array
  115. */
  116. public function qtyProvider()
  117. {
  118. return [
  119. [1, 1],
  120. [5.66, 5],
  121. ['test', 1],
  122. [-3, 1],
  123. [0, 1],
  124. [-2.99, 1]
  125. ];
  126. }
  127. /**
  128. * @return array
  129. */
  130. public function qtyProviderDecimal()
  131. {
  132. return [
  133. [1, 1],
  134. [5.66, 5.66],
  135. ['test', 1],
  136. [-3, 1],
  137. [0, 1],
  138. [-2.99, 1]
  139. ];
  140. }
  141. /**
  142. * @dataProvider qtyProviderDecimal
  143. */
  144. public function testUpdateQtyDecimal($qty, $expectedQty)
  145. {
  146. $this->itemMock->expects($this->any())
  147. ->method('setNoDiscount')
  148. ->will($this->returnValue(true));
  149. $this->itemMock->expects($this->any())
  150. ->method('setQty')
  151. ->with($this->equalTo($expectedQty));
  152. $this->itemMock->expects($this->any())
  153. ->method('setIsQtyDecimal')
  154. ->will($this->returnValue(true));
  155. $this->stockItemMock->expects($this->any())
  156. ->method('getIsQtyDecimal')
  157. ->will($this->returnValue(true));
  158. $this->productMock->expects($this->any())
  159. ->method('getStockItem')
  160. ->will($this->returnValue($this->stockItemMock));
  161. $this->productMock->expects($this->any())
  162. ->method('setIsSuperMode')
  163. ->with($this->equalTo(true));
  164. $this->productMock->expects($this->any())
  165. ->method('unsSkipCheckRequiredOption');
  166. $this->itemMock->expects($this->any())
  167. ->method('getProduct')
  168. ->will($this->returnValue($this->productMock));
  169. $object = $this->object->update($this->itemMock, ['qty' => $qty]);
  170. $this->assertEquals($this->object, $object);
  171. }
  172. public function testUpdateQtyDecimalWithConfiguredOption()
  173. {
  174. $this->itemMock->expects($this->any())
  175. ->method('setIsQtyDecimal')
  176. ->with($this->equalTo(1));
  177. $this->stockItemMock->expects($this->any())
  178. ->method('getIsQtyDecimal')
  179. ->will($this->returnValue(true));
  180. $this->productMock->expects($this->any())
  181. ->method('getStockItem')
  182. ->will($this->returnValue($this->stockItemMock));
  183. $this->itemMock->expects($this->any())
  184. ->method('getProduct')
  185. ->will($this->returnValue($this->productMock));
  186. $object = $this->object->update($this->itemMock, ['qty' => 3, 'use_discount' => true]);
  187. $this->assertEquals($this->object, $object);
  188. }
  189. /**
  190. * @covers \Magento\Quote\Model\Quote\Item\Updater::setCustomPrice()
  191. */
  192. public function testUpdateCustomPrice()
  193. {
  194. $customPrice = 9.99;
  195. $qty = 3;
  196. $buyRequestMock = $this->createPartialMock(\Magento\Framework\DataObject::class, [
  197. 'setCustomPrice',
  198. 'setValue',
  199. 'setCode',
  200. 'setProduct',
  201. 'getData'
  202. ]);
  203. $buyRequestMock->expects($this->any())
  204. ->method('setCustomPrice')
  205. ->with($this->equalTo($customPrice));
  206. $buyRequestMock->expects($this->any())
  207. ->method('getData')
  208. ->will($this->returnValue(['custom_price' => $customPrice]));
  209. $this->serializer->expects($this->any())
  210. ->method('serialize')
  211. ->willReturn(json_encode($buyRequestMock->getData()));
  212. $buyRequestMock->expects($this->any())
  213. ->method('setValue')
  214. ->with($this->equalTo('{"custom_price":' . $customPrice . '}'));
  215. $buyRequestMock->expects($this->any())
  216. ->method('setCode')
  217. ->with($this->equalTo('info_buyRequest'));
  218. $buyRequestMock->expects($this->any())
  219. ->method('setProduct')
  220. ->with($this->equalTo($this->productMock));
  221. $this->itemMock->expects($this->any())
  222. ->method('setIsQtyDecimal')
  223. ->with($this->equalTo(1));
  224. $this->itemMock->expects($this->any())
  225. ->method('getBuyRequest')
  226. ->will($this->returnValue($buyRequestMock));
  227. $this->stockItemMock->expects($this->any())
  228. ->method('getIsQtyDecimal')
  229. ->will($this->returnValue(true));
  230. $this->productMock->expects($this->any())
  231. ->method('getStockItem')
  232. ->will($this->returnValue($this->stockItemMock));
  233. $this->itemMock->expects($this->any())
  234. ->method('getProduct')
  235. ->will($this->returnValue($this->productMock));
  236. $this->itemMock->expects($this->any())
  237. ->method('addOption')
  238. ->will($this->returnValue($buyRequestMock));
  239. $this->itemMock->expects($this->any())
  240. ->method('setQty')
  241. ->with($this->equalTo($qty));
  242. $this->localeFormat->expects($this->any())
  243. ->method('getNumber')
  244. ->will($this->returnArgument(0));
  245. $object = $this->object->update($this->itemMock, ['qty' => $qty, 'custom_price' => $customPrice]);
  246. $this->assertEquals($this->object, $object);
  247. }
  248. /**
  249. * @covers \Magento\Quote\Model\Quote\Item\Updater::unsetCustomPrice()
  250. */
  251. public function testUpdateUnsetCustomPrice()
  252. {
  253. $qty = 3;
  254. $buyRequestMock = $this->createPartialMock(\Magento\Framework\DataObject::class, [
  255. 'setCustomPrice',
  256. 'setValue',
  257. 'setCode',
  258. 'setProduct',
  259. 'getData',
  260. 'unsetData',
  261. 'hasData',
  262. ]);
  263. $buyRequestMock->expects($this->never())->method('setCustomPrice');
  264. $buyRequestMock->expects($this->once())->method('getData')->will($this->returnValue([]));
  265. $serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
  266. ->setMethods(['serialize'])
  267. ->getMockForAbstractClass();
  268. $serializer->expects($this->any())
  269. ->method('serialize')
  270. ->willReturn('{}');
  271. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  272. $objectManagerHelper->setBackwardCompatibleProperty($this->object, 'serializer', $serializer);
  273. $buyRequestMock->expects($this->once())->method('unsetData')->with($this->equalTo('custom_price'));
  274. $buyRequestMock->expects($this->once())
  275. ->method('hasData')
  276. ->with($this->equalTo('custom_price'))
  277. ->will($this->returnValue(true));
  278. $buyRequestMock->expects($this->any())
  279. ->method('setValue')
  280. ->with($this->equalTo('{}'));
  281. $buyRequestMock->expects($this->any())
  282. ->method('setCode')
  283. ->with($this->equalTo('info_buyRequest'));
  284. $buyRequestMock->expects($this->any())
  285. ->method('setProduct')
  286. ->with($this->equalTo($this->productMock));
  287. $this->itemMock->expects($this->any())
  288. ->method('setIsQtyDecimal')
  289. ->with($this->equalTo(1));
  290. $this->itemMock->expects($this->any())
  291. ->method('getBuyRequest')
  292. ->will($this->returnValue($buyRequestMock));
  293. $this->stockItemMock->expects($this->any())
  294. ->method('getIsQtyDecimal')
  295. ->will($this->returnValue(true));
  296. $this->productMock->expects($this->any())
  297. ->method('getStockItem')
  298. ->will($this->returnValue($this->stockItemMock));
  299. $this->itemMock->expects($this->any())
  300. ->method('getProduct')
  301. ->will($this->returnValue($this->productMock));
  302. $this->itemMock->expects($this->any())
  303. ->method('addOption')
  304. ->will($this->returnValue($buyRequestMock));
  305. $this->itemMock->expects($this->exactly(2))
  306. ->method('setData')
  307. ->withConsecutive(
  308. ['custom_price', null],
  309. ['original_custom_price', null]
  310. );
  311. $this->itemMock->expects($this->once())
  312. ->method('hasData')
  313. ->with($this->equalTo('custom_price'))
  314. ->will($this->returnValue(true));
  315. $this->itemMock->expects($this->never())->method('setCustomPrice');
  316. $this->itemMock->expects($this->never())->method('setOriginalCustomPrice');
  317. $this->localeFormat->expects($this->any())
  318. ->method('getNumber')
  319. ->will($this->returnArgument(0));
  320. $object = $this->object->update($this->itemMock, ['qty' => $qty]);
  321. $this->assertEquals($this->object, $object);
  322. }
  323. }