ItemCarrierTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Test\Unit\Model;
  7. use Magento\Wishlist\Model\ResourceModel\Item\Collection;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class ItemCarrierTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var \Magento\Wishlist\Model\ItemCarrier */
  14. protected $model;
  15. /** @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject */
  16. protected $sessionMock;
  17. /** @var \Magento\Wishlist\Model\LocaleQuantityProcessor|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $quantityProcessorMock;
  19. /** @var \Magento\Checkout\Model\Cart|\PHPUnit_Framework_MockObject_MockObject */
  20. protected $cartMock;
  21. /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $loggerMock;
  23. /** @var \Magento\Wishlist\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
  24. protected $wishlistHelperMock;
  25. /** @var \Magento\Checkout\Helper\Cart|\PHPUnit_Framework_MockObject_MockObject */
  26. protected $cartHelperMock;
  27. /** @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
  28. protected $urlBuilderMock;
  29. /** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  30. protected $managerMock;
  31. /** @var \Magento\Framework\App\Response\RedirectInterface|\PHPUnit_Framework_MockObject_MockObject */
  32. protected $redirectMock;
  33. protected function setUp()
  34. {
  35. $this->sessionMock = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->quantityProcessorMock = $this->getMockBuilder(\Magento\Wishlist\Model\LocaleQuantityProcessor::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->cartMock = $this->getMockBuilder(\Magento\Checkout\Model\Cart::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
  45. ->getMockForAbstractClass();
  46. $this->wishlistHelperMock = $this->getMockBuilder(\Magento\Wishlist\Helper\Data::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->cartHelperMock = $this->getMockBuilder(\Magento\Checkout\Helper\Cart::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->urlBuilderMock = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
  53. ->getMockForAbstractClass();
  54. $this->managerMock = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
  55. ->getMockForAbstractClass();
  56. $this->redirectMock = $this->getMockBuilder(\Magento\Framework\App\Response\RedirectInterface::class)
  57. ->getMockForAbstractClass();
  58. $this->model = new \Magento\Wishlist\Model\ItemCarrier(
  59. $this->sessionMock,
  60. $this->quantityProcessorMock,
  61. $this->cartMock,
  62. $this->loggerMock,
  63. $this->wishlistHelperMock,
  64. $this->cartHelperMock,
  65. $this->urlBuilderMock,
  66. $this->managerMock,
  67. $this->redirectMock
  68. );
  69. }
  70. /**
  71. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  72. */
  73. public function testMoveAllToCart()
  74. {
  75. $wishlistId = 7;
  76. $sessionCustomerId = 23;
  77. $itemOneId = 14;
  78. $itemTwoId = 17;
  79. $productOneName = 'product one';
  80. $productTwoName = 'product two';
  81. $qtys = [14 => 21];
  82. $isOwner = true;
  83. $indexUrl = 'index_url';
  84. $redirectUrl = 'redirect_url';
  85. /** @var \Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject $itemOneMock */
  86. $itemOneMock = $this->getMockBuilder(\Magento\Wishlist\Model\Item::class)
  87. ->setMethods(['getProduct', 'unsProduct', 'getId', 'setQty', 'addToCart', 'delete', 'getProductUrl'])
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. /** @var \Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject $itemTwoMock */
  91. $itemTwoMock = $this->getMockBuilder(\Magento\Wishlist\Model\Item::class)
  92. ->setMethods(['getProduct', 'unsProduct', 'getId', 'setQty', 'addToCart', 'delete', 'getProductUrl'])
  93. ->disableOriginalConstructor()
  94. ->getMock();
  95. /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $productOneMock */
  96. $productOneMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  97. ->setMethods(['getDisableAddToCart', 'setDisableAddToCart', 'getName'])
  98. ->disableOriginalConstructor()
  99. ->getMock();
  100. /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $productTwoMock */
  101. $productTwoMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  102. ->setMethods(['getDisableAddToCart', 'setDisableAddToCart', 'getName'])
  103. ->disableOriginalConstructor()
  104. ->getMock();
  105. $itemOneMock->expects($this->any())
  106. ->method('getProduct')
  107. ->willReturn($productOneMock);
  108. $itemTwoMock->expects($this->any())
  109. ->method('getProduct')
  110. ->willReturn($productTwoMock);
  111. $collection = [$itemOneMock, $itemTwoMock];
  112. /** @var \Magento\Wishlist\Model\Wishlist|\PHPUnit_Framework_MockObject_MockObject $wishlistMock */
  113. $wishlistMock = $this->getMockBuilder(\Magento\Wishlist\Model\Wishlist::class)
  114. ->disableOriginalConstructor()
  115. ->getMock();
  116. $this->sessionMock->expects($this->once())
  117. ->method('getCustomerId')
  118. ->willReturn($sessionCustomerId);
  119. $wishlistMock->expects($this->once())
  120. ->method('isOwner')
  121. ->with($sessionCustomerId)
  122. ->willReturn($isOwner);
  123. $wishlistMock->expects($this->once())
  124. ->method('getId')
  125. ->willReturn($wishlistId);
  126. /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
  127. $collectionMock = $this->getMockBuilder(\Magento\Wishlist\Model\ResourceModel\Item\Collection::class)
  128. ->disableOriginalConstructor()
  129. ->getMock();
  130. $wishlistMock->expects($this->once())
  131. ->method('getItemCollection')
  132. ->willReturn($collectionMock);
  133. $collectionMock->expects($this->once())
  134. ->method('setVisibilityFilter')
  135. ->with(true)
  136. ->willReturn($collection);
  137. $productOneMock->expects($this->once())
  138. ->method('getDisableAddToCart')
  139. ->willReturn(true);
  140. $productOneMock->expects($this->once())
  141. ->method('setDisableAddToCart')
  142. ->with(true);
  143. $productTwoMock->expects($this->once())
  144. ->method('getDisableAddToCart')
  145. ->willReturn(false);
  146. $productTwoMock->expects($this->once())
  147. ->method('setDisableAddToCart')
  148. ->with(false);
  149. $itemOneMock->expects($this->once())
  150. ->method('unsProduct');
  151. $itemTwoMock->expects($this->once())
  152. ->method('unsProduct');
  153. $itemOneMock->expects($this->exactly(2))
  154. ->method('getId')
  155. ->willReturn($itemOneId);
  156. $itemTwoMock->expects($this->once())
  157. ->method('getId')
  158. ->willReturn($itemTwoId);
  159. $this->quantityProcessorMock->expects($this->once())
  160. ->method('process')
  161. ->with($qtys[$itemOneId])
  162. ->willReturnArgument(0);
  163. $itemOneMock->expects($this->once())
  164. ->method('setQty')
  165. ->with($qtys[$itemOneId])
  166. ->willReturnSelf();
  167. $itemTwoMock->expects($this->never())
  168. ->method('setQty');
  169. $itemOneMock->expects($this->once())
  170. ->method('addToCart')
  171. ->with($this->cartMock, $isOwner)
  172. ->willReturn(false);
  173. $itemTwoMock->expects($this->once())
  174. ->method('addToCart')
  175. ->with($this->cartMock, $isOwner)
  176. ->willReturn(true);
  177. $this->wishlistHelperMock->expects($this->once())
  178. ->method('getListUrl')
  179. ->with($wishlistId)
  180. ->willReturn($indexUrl);
  181. $this->cartHelperMock->expects($this->once())
  182. ->method('getShouldRedirectToCart')
  183. ->with(null)
  184. ->willReturn(true);
  185. $this->cartHelperMock->expects($this->once())
  186. ->method('getCartUrl')
  187. ->willReturn($redirectUrl);
  188. $wishlistMock->expects($this->once())
  189. ->method('save')
  190. ->willReturnSelf();
  191. $productOneMock->expects($this->any())
  192. ->method('getName')
  193. ->willReturn($productOneName);
  194. $productTwoMock->expects($this->any())
  195. ->method('getName')
  196. ->willReturn($productTwoName);
  197. $this->managerMock->expects($this->once())
  198. ->method('addSuccess')
  199. ->with(__('%1 product(s) have been added to shopping cart: %2.', 1, '"' . $productTwoName . '"'), null)
  200. ->willReturnSelf();
  201. $this->cartMock->expects($this->once())
  202. ->method('save')
  203. ->willReturnSelf();
  204. /** @var \Magento\Quote\Model\Quote|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
  205. $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
  206. ->disableOriginalConstructor()
  207. ->getMock();
  208. $this->cartMock->expects($this->once())
  209. ->method('getQuote')
  210. ->willReturn($quoteMock);
  211. $quoteMock->expects($this->once())
  212. ->method('collectTotals')
  213. ->willReturnSelf();
  214. $this->wishlistHelperMock->expects($this->once())
  215. ->method('calculate')
  216. ->willReturnSelf();
  217. $this->assertEquals($redirectUrl, $this->model->moveAllToCart($wishlistMock, $qtys));
  218. }
  219. /**
  220. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  221. */
  222. public function testMoveAllToCartWithNotSalableAndOptions()
  223. {
  224. $sessionCustomerId = 23;
  225. $itemOneId = 14;
  226. $itemTwoId = 17;
  227. $productOneName = 'product one';
  228. $productTwoName = 'product two';
  229. $qtys = [14 => 21, 17 => 29];
  230. $isOwner = false;
  231. $indexUrl = 'index_url';
  232. $redirectUrl = 'redirect_url';
  233. $sharingCode = 'sharingcode';
  234. /** @var \Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject $itemOneMock */
  235. $itemOneMock = $this->getMockBuilder(\Magento\Wishlist\Model\Item::class)
  236. ->setMethods(['getProduct', 'unsProduct', 'getId', 'setQty', 'addToCart', 'delete', 'getProductUrl'])
  237. ->disableOriginalConstructor()
  238. ->getMock();
  239. /** @var \Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject $itemTwoMock */
  240. $itemTwoMock = $this->getMockBuilder(\Magento\Wishlist\Model\Item::class)
  241. ->setMethods(['getProduct', 'unsProduct', 'getId', 'setQty', 'addToCart', 'delete', 'getProductUrl'])
  242. ->disableOriginalConstructor()
  243. ->getMock();
  244. /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $productOneMock */
  245. $productOneMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  246. ->setMethods(['getDisableAddToCart', 'setDisableAddToCart', 'getName'])
  247. ->disableOriginalConstructor()
  248. ->getMock();
  249. /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $productTwoMock */
  250. $productTwoMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  251. ->setMethods(['getDisableAddToCart', 'setDisableAddToCart', 'getName'])
  252. ->disableOriginalConstructor()
  253. ->getMock();
  254. $itemOneMock->expects($this->any())
  255. ->method('getProduct')
  256. ->willReturn($productOneMock);
  257. $itemTwoMock->expects($this->any())
  258. ->method('getProduct')
  259. ->willReturn($productTwoMock);
  260. $collection = [$itemOneMock, $itemTwoMock];
  261. /** @var \Magento\Wishlist\Model\Wishlist|\PHPUnit_Framework_MockObject_MockObject $wishlistMock */
  262. $wishlistMock = $this->getMockBuilder(\Magento\Wishlist\Model\Wishlist::class)
  263. ->setMethods(['isOwner', 'getItemCollection', 'getId', 'getSharingCode', 'save'])
  264. ->disableOriginalConstructor()
  265. ->getMock();
  266. $this->sessionMock->expects($this->once())
  267. ->method('getCustomerId')
  268. ->willReturn($sessionCustomerId);
  269. $wishlistMock->expects($this->once())
  270. ->method('isOwner')
  271. ->with($sessionCustomerId)
  272. ->willReturn($isOwner);
  273. /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
  274. $collectionMock = $this->getMockBuilder(\Magento\Wishlist\Model\ResourceModel\Item\Collection::class)
  275. ->disableOriginalConstructor()
  276. ->getMock();
  277. $wishlistMock->expects($this->once())
  278. ->method('getItemCollection')
  279. ->willReturn($collectionMock);
  280. $collectionMock->expects($this->once())
  281. ->method('setVisibilityFilter')
  282. ->with(true)
  283. ->willReturn($collection);
  284. $productOneMock->expects($this->once())
  285. ->method('getDisableAddToCart')
  286. ->willReturn(false);
  287. $productOneMock->expects($this->once())
  288. ->method('setDisableAddToCart')
  289. ->with(false);
  290. $productTwoMock->expects($this->once())
  291. ->method('getDisableAddToCart')
  292. ->willReturn(true);
  293. $productTwoMock->expects($this->once())
  294. ->method('setDisableAddToCart')
  295. ->with(true);
  296. $itemOneMock->expects($this->once())
  297. ->method('unsProduct');
  298. $itemTwoMock->expects($this->once())
  299. ->method('unsProduct');
  300. $itemOneMock->expects($this->exactly(2))
  301. ->method('getId')
  302. ->willReturn($itemOneId);
  303. $itemTwoMock->expects($this->exactly(2))
  304. ->method('getId')
  305. ->willReturn($itemTwoId);
  306. $this->quantityProcessorMock->expects($this->exactly(2))
  307. ->method('process')
  308. ->willReturnMap(
  309. [
  310. [$qtys[$itemOneId], $qtys[$itemOneId]],
  311. [$qtys[$itemTwoId], $qtys[$itemTwoId]],
  312. ]
  313. );
  314. $itemOneMock->expects($this->once())
  315. ->method('setQty')
  316. ->with($qtys[$itemOneId])
  317. ->willReturnSelf();
  318. $itemTwoMock->expects($this->once())
  319. ->method('setQty')
  320. ->with($qtys[$itemTwoId])
  321. ->willReturnSelf();
  322. $itemOneMock->expects($this->once())
  323. ->method('addToCart')
  324. ->with($this->cartMock, $isOwner)
  325. ->willThrowException(new \Magento\Catalog\Model\Product\Exception(__('Product Exception.')));
  326. $itemTwoMock->expects($this->once())
  327. ->method('addToCart')
  328. ->with($this->cartMock, $isOwner)
  329. ->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('Localized Exception.')));
  330. /** @var \Magento\Quote\Model\Quote|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
  331. $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
  332. ->disableOriginalConstructor()
  333. ->getMock();
  334. $this->cartMock->expects($this->exactly(4))
  335. ->method('getQuote')
  336. ->willReturn($quoteMock);
  337. /** @var \Magento\Quote\Model\Quote\Item|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
  338. $itemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
  339. ->disableOriginalConstructor()
  340. ->getMock();
  341. $quoteMock->expects($this->exactly(2))
  342. ->method('getItemByProduct')
  343. ->willReturn($itemMock);
  344. $quoteMock->expects($this->exactly(2))
  345. ->method('deleteItem')
  346. ->with($itemMock)
  347. ->willReturnSelf();
  348. $wishlistMock->expects($this->once())
  349. ->method('getSharingCode')
  350. ->willReturn($sharingCode);
  351. $this->urlBuilderMock->expects($this->once())
  352. ->method('getUrl')
  353. ->with('wishlist/shared', ['code' => $sharingCode])
  354. ->willReturn($indexUrl);
  355. $this->cartHelperMock->expects($this->once())
  356. ->method('getShouldRedirectToCart')
  357. ->with(null)
  358. ->willReturn(false);
  359. $this->redirectMock->expects($this->exactly(2))
  360. ->method('getRefererUrl')
  361. ->willReturn($redirectUrl);
  362. $productOneMock->expects($this->any())
  363. ->method('getName')
  364. ->willReturn($productOneName);
  365. $productTwoMock->expects($this->any())
  366. ->method('getName')
  367. ->willReturn($productTwoName);
  368. $this->managerMock->expects($this->at(0))
  369. ->method('addError')
  370. ->with(__('%1 for "%2".', 'Localized Exception', $productTwoName), null)
  371. ->willReturnSelf();
  372. $this->managerMock->expects($this->at(1))
  373. ->method('addError')
  374. ->with(
  375. __(
  376. 'We couldn\'t add the following product(s) to the shopping cart: %1.',
  377. '"' . $productOneName . '"'
  378. ),
  379. null
  380. )->willReturnSelf();
  381. $this->wishlistHelperMock->expects($this->once())
  382. ->method('calculate')
  383. ->willReturnSelf();
  384. $this->assertEquals($indexUrl, $this->model->moveAllToCart($wishlistMock, $qtys));
  385. }
  386. /**
  387. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  388. */
  389. public function testMoveAllToCartWithException()
  390. {
  391. $wishlistId = 7;
  392. $sessionCustomerId = 23;
  393. $itemOneId = 14;
  394. $itemTwoId = 17;
  395. $productOneName = 'product one';
  396. $productTwoName = 'product two';
  397. $qtys = [14 => 21];
  398. $isOwner = true;
  399. $indexUrl = 'index_url';
  400. /** @var \Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject $itemOneMock */
  401. $itemOneMock = $this->getMockBuilder(\Magento\Wishlist\Model\Item::class)
  402. ->setMethods(['getProduct', 'unsProduct', 'getId', 'setQty', 'addToCart', 'delete', 'getProductUrl'])
  403. ->disableOriginalConstructor()
  404. ->getMock();
  405. /** @var \Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject $itemTwoMock */
  406. $itemTwoMock = $this->getMockBuilder(\Magento\Wishlist\Model\Item::class)
  407. ->setMethods(['getProduct', 'unsProduct', 'getId', 'setQty', 'addToCart', 'delete', 'getProductUrl'])
  408. ->disableOriginalConstructor()
  409. ->getMock();
  410. /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $productOneMock */
  411. $productOneMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  412. ->setMethods(['getDisableAddToCart', 'setDisableAddToCart', 'getName'])
  413. ->disableOriginalConstructor()
  414. ->getMock();
  415. /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $productTwoMock */
  416. $productTwoMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  417. ->setMethods(['getDisableAddToCart', 'setDisableAddToCart', 'getName'])
  418. ->disableOriginalConstructor()
  419. ->getMock();
  420. $itemOneMock->expects($this->any())
  421. ->method('getProduct')
  422. ->willReturn($productOneMock);
  423. $itemTwoMock->expects($this->any())
  424. ->method('getProduct')
  425. ->willReturn($productTwoMock);
  426. $collection = [$itemOneMock, $itemTwoMock];
  427. /** @var \Magento\Wishlist\Model\Wishlist|\PHPUnit_Framework_MockObject_MockObject $wishlistMock */
  428. $wishlistMock = $this->getMockBuilder(\Magento\Wishlist\Model\Wishlist::class)
  429. ->disableOriginalConstructor()
  430. ->getMock();
  431. $this->sessionMock->expects($this->once())
  432. ->method('getCustomerId')
  433. ->willReturn($sessionCustomerId);
  434. $wishlistMock->expects($this->once())
  435. ->method('isOwner')
  436. ->with($sessionCustomerId)
  437. ->willReturn($isOwner);
  438. $wishlistMock->expects($this->once())
  439. ->method('getId')
  440. ->willReturn($wishlistId);
  441. /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
  442. $collectionMock = $this->getMockBuilder(\Magento\Wishlist\Model\ResourceModel\Item\Collection::class)
  443. ->disableOriginalConstructor()
  444. ->getMock();
  445. $wishlistMock->expects($this->once())
  446. ->method('getItemCollection')
  447. ->willReturn($collectionMock);
  448. $collectionMock->expects($this->once())
  449. ->method('setVisibilityFilter')
  450. ->with(true)
  451. ->willReturn($collection);
  452. $productOneMock->expects($this->once())
  453. ->method('getDisableAddToCart')
  454. ->willReturn(true);
  455. $productOneMock->expects($this->once())
  456. ->method('setDisableAddToCart')
  457. ->with(true);
  458. $productTwoMock->expects($this->once())
  459. ->method('getDisableAddToCart')
  460. ->willReturn(false);
  461. $productTwoMock->expects($this->once())
  462. ->method('setDisableAddToCart')
  463. ->with(false);
  464. $itemOneMock->expects($this->once())
  465. ->method('unsProduct');
  466. $itemTwoMock->expects($this->once())
  467. ->method('unsProduct');
  468. $itemOneMock->expects($this->exactly(2))
  469. ->method('getId')
  470. ->willReturn($itemOneId);
  471. $itemTwoMock->expects($this->once())
  472. ->method('getId')
  473. ->willReturn($itemTwoId);
  474. $this->quantityProcessorMock->expects($this->once())
  475. ->method('process')
  476. ->with($qtys[$itemOneId])
  477. ->willReturnArgument(0);
  478. $itemOneMock->expects($this->once())
  479. ->method('setQty')
  480. ->with($qtys[$itemOneId])
  481. ->willReturnSelf();
  482. $itemTwoMock->expects($this->never())
  483. ->method('setQty');
  484. $itemOneMock->expects($this->once())
  485. ->method('addToCart')
  486. ->with($this->cartMock, $isOwner)
  487. ->willReturn(true);
  488. $exception = new \Exception('Exception.');
  489. $itemTwoMock->expects($this->once())
  490. ->method('addToCart')
  491. ->with($this->cartMock, $isOwner)
  492. ->willThrowException($exception);
  493. $this->loggerMock->expects($this->once())
  494. ->method('critical')
  495. ->with($exception, []);
  496. $this->managerMock->expects($this->at(0))
  497. ->method('addError')
  498. ->with(__('We can\'t add this item to your shopping cart right now.'), null)
  499. ->willReturnSelf();
  500. $this->wishlistHelperMock->expects($this->once())
  501. ->method('getListUrl')
  502. ->with($wishlistId)
  503. ->willReturn($indexUrl);
  504. $this->cartHelperMock->expects($this->once())
  505. ->method('getShouldRedirectToCart')
  506. ->with(null)
  507. ->willReturn(false);
  508. $this->redirectMock->expects($this->once())
  509. ->method('getRefererUrl')
  510. ->willReturn('');
  511. $wishlistMock->expects($this->once())
  512. ->method('save')
  513. ->willThrowException(new \Exception());
  514. $this->managerMock->expects($this->at(1))
  515. ->method('addError')
  516. ->with(__('We can\'t update the Wish List right now.'), null)
  517. ->willReturnSelf();
  518. $productOneMock->expects($this->any())
  519. ->method('getName')
  520. ->willReturn($productOneName);
  521. $productTwoMock->expects($this->any())
  522. ->method('getName')
  523. ->willReturn($productTwoName);
  524. $this->managerMock->expects($this->once())
  525. ->method('addSuccess')
  526. ->with(__('%1 product(s) have been added to shopping cart: %2.', 1, '"' . $productOneName . '"'), null)
  527. ->willReturnSelf();
  528. $this->cartMock->expects($this->once())
  529. ->method('save')
  530. ->willReturnSelf();
  531. /** @var \Magento\Quote\Model\Quote|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
  532. $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
  533. ->disableOriginalConstructor()
  534. ->getMock();
  535. $this->cartMock->expects($this->once())
  536. ->method('getQuote')
  537. ->willReturn($quoteMock);
  538. $quoteMock->expects($this->once())
  539. ->method('collectTotals')
  540. ->willReturnSelf();
  541. $this->wishlistHelperMock->expects($this->once())
  542. ->method('calculate')
  543. ->willReturnSelf();
  544. $this->assertEquals($indexUrl, $this->model->moveAllToCart($wishlistMock, $qtys));
  545. }
  546. }