WishlistTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Test\Unit\CustomerData;
  7. use Magento\Catalog\Helper\Image;
  8. use Magento\Catalog\Model\Product;
  9. use Magento\Catalog\Model\Product\Type\AbstractType;
  10. use Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface;
  11. use Magento\Framework\App\ViewInterface;
  12. use Magento\Framework\Pricing\Render;
  13. use Magento\Wishlist\Block\Customer\Sidebar;
  14. use Magento\Wishlist\CustomerData\Wishlist;
  15. use Magento\Wishlist\CustomerData\Wishlist as WishlistModel;
  16. use Magento\Wishlist\Helper\Data;
  17. use Magento\Wishlist\Model\Item;
  18. use Magento\Wishlist\Model\ResourceModel\Item\Collection;
  19. /**
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. */
  22. class WishlistTest extends \PHPUnit\Framework\TestCase
  23. {
  24. /** @var Wishlist */
  25. private $model;
  26. /** @var Data|\PHPUnit_Framework_MockObject_MockObject */
  27. private $wishlistHelperMock;
  28. /** @var Sidebar|\PHPUnit_Framework_MockObject_MockObject */
  29. private $sidebarMock;
  30. /** @var Image|\PHPUnit_Framework_MockObject_MockObject */
  31. private $catalogImageHelperMock;
  32. /** @var ViewInterface|\PHPUnit_Framework_MockObject_MockObject */
  33. private $viewMock;
  34. /** @var \Magento\Catalog\Block\Product\ImageBuilder|\PHPUnit_Framework_MockObject_MockObject */
  35. private $itemResolver;
  36. protected function setUp()
  37. {
  38. $this->wishlistHelperMock = $this->getMockBuilder(\Magento\Wishlist\Helper\Data::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->sidebarMock = $this->getMockBuilder(\Magento\Wishlist\Block\Customer\Sidebar::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->viewMock = $this->getMockBuilder(\Magento\Framework\App\ViewInterface::class)
  45. ->getMockForAbstractClass();
  46. $this->catalogImageHelperMock = $this->getMockBuilder(\Magento\Catalog\Helper\Image::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $imageHelperFactory = $this->getMockBuilder(\Magento\Catalog\Helper\ImageFactory::class)
  50. ->disableOriginalConstructor()
  51. ->setMethods(['create'])
  52. ->getMock();
  53. $imageHelperFactory->expects($this->any())
  54. ->method('create')
  55. ->willReturn($this->catalogImageHelperMock);
  56. $this->itemResolver = $this->createMock(
  57. ItemResolverInterface::class
  58. );
  59. $this->model = new Wishlist(
  60. $this->wishlistHelperMock,
  61. $this->sidebarMock,
  62. $imageHelperFactory,
  63. $this->viewMock,
  64. $this->itemResolver
  65. );
  66. }
  67. /**
  68. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  69. */
  70. public function testGetSectionData()
  71. {
  72. $imageUrl = 'image_url';
  73. $imageLabel = 'image_label';
  74. $imageWidth = 'image_width';
  75. $imageHeight = 'image_height';
  76. $productSku = 'product_sku';
  77. $productId = 'product_id';
  78. $productUrl = 'product_url';
  79. $productName = 'product_name';
  80. $productPrice = 'product_price';
  81. $productIsSalable = true;
  82. $productIsVisible = true;
  83. $productHasOptions = false;
  84. $itemAddParams = ['add_params'];
  85. $itemRemoveParams = ['remove_params'];
  86. $result = [
  87. 'counter' => __('1 item'),
  88. 'items' => [
  89. [
  90. 'image' => [
  91. 'template' => 'Magento_Catalog/product/image_with_borders',
  92. 'src' => $imageUrl,
  93. 'alt' => $imageLabel,
  94. 'width' => $imageWidth,
  95. 'height' => $imageHeight,
  96. ],
  97. 'product_sku' => $productSku,
  98. 'product_id' => $productId,
  99. 'product_url' => $productUrl,
  100. 'product_name' => $productName,
  101. 'product_price' => $productPrice,
  102. 'product_is_saleable_and_visible' => $productIsSalable && $productIsVisible,
  103. 'product_has_required_options' => $productHasOptions,
  104. 'add_to_cart_params' => $itemAddParams,
  105. 'delete_item_params' => $itemRemoveParams,
  106. ],
  107. ],
  108. ];
  109. /** @var Item|\PHPUnit_Framework_MockObject_MockObject $itemMock */
  110. $itemMock = $this->getMockBuilder(\Magento\Wishlist\Model\Item::class)
  111. ->disableOriginalConstructor()
  112. ->getMock();
  113. $items = [$itemMock];
  114. $this->wishlistHelperMock->expects($this->once())
  115. ->method('getItemCount')
  116. ->willReturn(count($items));
  117. $this->viewMock->expects($this->once())
  118. ->method('loadLayout');
  119. /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $itemCollectionMock */
  120. $itemCollectionMock = $this->getMockBuilder(\Magento\Wishlist\Model\ResourceModel\Item\Collection::class)
  121. ->disableOriginalConstructor()
  122. ->getMock();
  123. $this->wishlistHelperMock->expects($this->once())
  124. ->method('getWishlistItemCollection')
  125. ->willReturn($itemCollectionMock);
  126. $itemCollectionMock->expects($this->once())
  127. ->method('clear')
  128. ->willReturnSelf();
  129. $itemCollectionMock->expects($this->once())
  130. ->method('setPageSize')
  131. ->with(WishlistModel::SIDEBAR_ITEMS_NUMBER)
  132. ->willReturnSelf();
  133. $itemCollectionMock->expects($this->once())
  134. ->method('setInStockFilter')
  135. ->with(true)
  136. ->willReturnSelf();
  137. $itemCollectionMock->expects($this->once())
  138. ->method('setOrder')
  139. ->with('added_at')
  140. ->willReturnSelf();
  141. $itemCollectionMock->expects($this->once())
  142. ->method('getIterator')
  143. ->willReturn(new \ArrayIterator($items));
  144. /** @var Product|\PHPUnit_Framework_MockObject_MockObject $productMock */
  145. $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  146. ->disableOriginalConstructor()
  147. ->getMock();
  148. $itemMock->expects($this->once())
  149. ->method('getProduct')
  150. ->willReturn($productMock);
  151. $this->itemResolver->expects($this->once())
  152. ->method('getFinalProduct')
  153. ->willReturn($productMock);
  154. $this->catalogImageHelperMock->expects($this->once())
  155. ->method('init')
  156. ->with($productMock, 'wishlist_sidebar_block', [])
  157. ->willReturnSelf();
  158. $this->catalogImageHelperMock->expects($this->once())
  159. ->method('getUrl')
  160. ->willReturn($imageUrl);
  161. $this->catalogImageHelperMock->expects($this->once())
  162. ->method('getLabel')
  163. ->willReturn($imageLabel);
  164. $this->catalogImageHelperMock->expects($this->once())
  165. ->method('getWidth')
  166. ->willReturn($imageWidth);
  167. $this->catalogImageHelperMock->expects($this->once())
  168. ->method('getHeight')
  169. ->willReturn($imageHeight);
  170. $this->catalogImageHelperMock->expects($this->any())
  171. ->method('getFrame')
  172. ->willReturn(true);
  173. $this->catalogImageHelperMock->expects($this->once())
  174. ->method('getResizedImageInfo')
  175. ->willReturn([]);
  176. $this->wishlistHelperMock->expects($this->once())
  177. ->method('getProductUrl')
  178. ->with($itemMock, [])
  179. ->willReturn($productUrl);
  180. $productMock->expects($this->once())
  181. ->method('getSku')
  182. ->willReturn($productSku);
  183. $productMock->expects($this->once())
  184. ->method('getId')
  185. ->willReturn($productId);
  186. $productMock->expects($this->once())
  187. ->method('getName')
  188. ->willReturn($productName);
  189. $this->sidebarMock->expects($this->once())
  190. ->method('getProductPriceHtml')
  191. ->with(
  192. $productMock,
  193. 'wishlist_configured_price',
  194. Render::ZONE_ITEM_LIST,
  195. ['item' => $itemMock]
  196. )
  197. ->willReturn($productPrice);
  198. $productMock->expects($this->once())
  199. ->method('getName')
  200. ->willReturn($productName);
  201. $productMock->expects($this->once())
  202. ->method('isSaleable')
  203. ->willReturn($productIsSalable);
  204. $productMock->expects($this->once())
  205. ->method('isVisibleInSiteVisibility')
  206. ->willReturn($productIsVisible);
  207. /** @var AbstractType|\PHPUnit_Framework_MockObject_MockObject $productTypeMock */
  208. $productTypeMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Type\AbstractType::class)
  209. ->disableOriginalConstructor()
  210. ->setMethods(['hasRequiredOptions'])
  211. ->getMockForAbstractClass();
  212. $productMock->expects($this->once())
  213. ->method('getTypeInstance')
  214. ->willReturn($productTypeMock);
  215. $productTypeMock->expects($this->once())
  216. ->method('hasRequiredOptions')
  217. ->with($productMock)
  218. ->willReturn($productHasOptions);
  219. $this->wishlistHelperMock->expects($this->once())
  220. ->method('getAddToCartParams')
  221. ->with($itemMock)
  222. ->willReturn($itemAddParams);
  223. $this->wishlistHelperMock->expects($this->once())
  224. ->method('getRemoveParams')
  225. ->with($itemMock)
  226. ->willReturn($itemRemoveParams);
  227. $this->assertEquals($result, $this->model->getSectionData());
  228. }
  229. /**
  230. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  231. */
  232. public function testGetSectionDataWithTwoItems()
  233. {
  234. $imageUrl = 'image_url';
  235. $imageLabel = 'image_label';
  236. $imageWidth = 'image_width';
  237. $imageHeight = 'image_height';
  238. $productSku = 'product_sku';
  239. $productId = 'product_id';
  240. $productUrl = 'product_url';
  241. $productName = 'product_name';
  242. $productPrice = 'product_price';
  243. $productIsSalable = false;
  244. $productIsVisible = true;
  245. $productHasOptions = true;
  246. $itemAddParams = ['add_params'];
  247. $itemRemoveParams = ['remove_params'];
  248. /** @var Item|\PHPUnit_Framework_MockObject_MockObject $itemMock */
  249. $itemMock = $this->getMockBuilder(\Magento\Wishlist\Model\Item::class)
  250. ->disableOriginalConstructor()
  251. ->getMock();
  252. $items = [$itemMock, $itemMock];
  253. $result = [
  254. 'counter' => __('%1 items', count($items)),
  255. 'items' => [
  256. [
  257. 'image' => [
  258. 'template' => 'Magento_Catalog/product/image_with_borders',
  259. 'src' => $imageUrl,
  260. 'alt' => $imageLabel,
  261. 'width' => $imageWidth,
  262. 'height' => $imageHeight,
  263. ],
  264. 'product_sku' => $productSku,
  265. 'product_id' => $productId,
  266. 'product_url' => $productUrl,
  267. 'product_name' => $productName,
  268. 'product_price' => $productPrice,
  269. 'product_is_saleable_and_visible' => $productIsSalable && $productIsVisible,
  270. 'product_has_required_options' => $productHasOptions,
  271. 'add_to_cart_params' => $itemAddParams,
  272. 'delete_item_params' => $itemRemoveParams,
  273. ],
  274. [
  275. 'image' => [
  276. 'template' => 'Magento_Catalog/product/image_with_borders',
  277. 'src' => $imageUrl,
  278. 'alt' => $imageLabel,
  279. 'width' => $imageWidth,
  280. 'height' => $imageHeight,
  281. ],
  282. 'product_sku' => $productSku,
  283. 'product_id' => $productId,
  284. 'product_url' => $productUrl,
  285. 'product_name' => $productName,
  286. 'product_price' => $productPrice,
  287. 'product_is_saleable_and_visible' => $productIsSalable && $productIsVisible,
  288. 'product_has_required_options' => $productHasOptions,
  289. 'add_to_cart_params' => $itemAddParams,
  290. 'delete_item_params' => $itemRemoveParams,
  291. ],
  292. ],
  293. ];
  294. $this->wishlistHelperMock->expects($this->once())
  295. ->method('getItemCount')
  296. ->willReturn(count($items));
  297. $this->viewMock->expects($this->once())
  298. ->method('loadLayout');
  299. /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $itemCollectionMock */
  300. $itemCollectionMock = $this->getMockBuilder(\Magento\Wishlist\Model\ResourceModel\Item\Collection::class)
  301. ->disableOriginalConstructor()
  302. ->getMock();
  303. $this->wishlistHelperMock->expects($this->once())
  304. ->method('getWishlistItemCollection')
  305. ->willReturn($itemCollectionMock);
  306. $itemCollectionMock->expects($this->once())
  307. ->method('clear')
  308. ->willReturnSelf();
  309. $itemCollectionMock->expects($this->once())
  310. ->method('setPageSize')
  311. ->with(WishlistModel::SIDEBAR_ITEMS_NUMBER)
  312. ->willReturnSelf();
  313. $itemCollectionMock->expects($this->once())
  314. ->method('setInStockFilter')
  315. ->with(true)
  316. ->willReturnSelf();
  317. $itemCollectionMock->expects($this->once())
  318. ->method('setOrder')
  319. ->with('added_at')
  320. ->willReturnSelf();
  321. $itemCollectionMock->expects($this->once())
  322. ->method('getIterator')
  323. ->willReturn(new \ArrayIterator($items));
  324. /** @var Product|\PHPUnit_Framework_MockObject_MockObject $productMock */
  325. $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  326. ->disableOriginalConstructor()
  327. ->getMock();
  328. $itemMock->expects($this->exactly(2))
  329. ->method('getProduct')
  330. ->willReturn($productMock);
  331. $this->itemResolver->expects($this->exactly(2))
  332. ->method('getFinalProduct')
  333. ->willReturn($productMock);
  334. $this->catalogImageHelperMock->expects($this->exactly(2))
  335. ->method('init')
  336. ->with($productMock, 'wishlist_sidebar_block', [])
  337. ->willReturnSelf();
  338. $this->catalogImageHelperMock->expects($this->exactly(2))
  339. ->method('getUrl')
  340. ->willReturn($imageUrl);
  341. $this->catalogImageHelperMock->expects($this->exactly(2))
  342. ->method('getLabel')
  343. ->willReturn($imageLabel);
  344. $this->catalogImageHelperMock->expects($this->exactly(2))
  345. ->method('getWidth')
  346. ->willReturn($imageWidth);
  347. $this->catalogImageHelperMock->expects($this->exactly(2))
  348. ->method('getHeight')
  349. ->willReturn($imageHeight);
  350. $this->catalogImageHelperMock->expects($this->any())
  351. ->method('getFrame')
  352. ->willReturn(true);
  353. $this->catalogImageHelperMock->expects($this->exactly(2))
  354. ->method('getResizedImageInfo')
  355. ->willReturn([]);
  356. $this->wishlistHelperMock->expects($this->exactly(2))
  357. ->method('getProductUrl')
  358. ->with($itemMock, [])
  359. ->willReturn($productUrl);
  360. $productMock->expects($this->exactly(2))
  361. ->method('getName')
  362. ->willReturn($productName);
  363. $productMock->expects($this->exactly(2))
  364. ->method('getId')
  365. ->willReturn($productId);
  366. $productMock->expects($this->exactly(2))
  367. ->method('getSku')
  368. ->willReturn($productSku);
  369. $this->sidebarMock->expects($this->exactly(2))
  370. ->method('getProductPriceHtml')
  371. ->with(
  372. $productMock,
  373. 'wishlist_configured_price',
  374. Render::ZONE_ITEM_LIST,
  375. ['item' => $itemMock]
  376. )
  377. ->willReturn($productPrice);
  378. $productMock->expects($this->exactly(2))
  379. ->method('getName')
  380. ->willReturn($productName);
  381. $productMock->expects($this->exactly(2))
  382. ->method('isSaleable')
  383. ->willReturn($productIsSalable);
  384. $productMock->expects($this->never())
  385. ->method('isVisibleInSiteVisibility');
  386. /** @var AbstractType|\PHPUnit_Framework_MockObject_MockObject $productTypeMock */
  387. $productTypeMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Type\AbstractType::class)
  388. ->disableOriginalConstructor()
  389. ->setMethods(['hasRequiredOptions'])
  390. ->getMockForAbstractClass();
  391. $productMock->expects($this->exactly(2))
  392. ->method('getTypeInstance')
  393. ->willReturn($productTypeMock);
  394. $productTypeMock->expects($this->exactly(2))
  395. ->method('hasRequiredOptions')
  396. ->with($productMock)
  397. ->willReturn($productHasOptions);
  398. $this->wishlistHelperMock->expects($this->exactly(2))
  399. ->method('getAddToCartParams')
  400. ->with($itemMock)
  401. ->willReturn($itemAddParams);
  402. $this->wishlistHelperMock->expects($this->exactly(2))
  403. ->method('getRemoveParams')
  404. ->with($itemMock)
  405. ->willReturn($itemRemoveParams);
  406. $this->assertEquals($result, $this->model->getSectionData());
  407. }
  408. public function testGetSectionDataWithoutItems()
  409. {
  410. $items = [];
  411. $result = [
  412. 'counter' => null,
  413. 'items' => [],
  414. ];
  415. $this->wishlistHelperMock->expects($this->once())
  416. ->method('getItemCount')
  417. ->willReturn(count($items));
  418. $this->viewMock->expects($this->never())
  419. ->method('loadLayout');
  420. $this->wishlistHelperMock->expects($this->never())
  421. ->method('getWishlistItemCollection');
  422. $this->catalogImageHelperMock->expects($this->never())
  423. ->method('init');
  424. $this->catalogImageHelperMock->expects($this->never())
  425. ->method('getUrl');
  426. $this->catalogImageHelperMock->expects($this->never())
  427. ->method('getLabel');
  428. $this->catalogImageHelperMock->expects($this->never())
  429. ->method('getWidth');
  430. $this->catalogImageHelperMock->expects($this->never())
  431. ->method('getHeight');
  432. $this->wishlistHelperMock->expects($this->never())
  433. ->method('getProductUrl');
  434. $this->sidebarMock->expects($this->never())
  435. ->method('getProductPriceHtml');
  436. $this->wishlistHelperMock->expects($this->never())
  437. ->method('getAddToCartParams');
  438. $this->wishlistHelperMock->expects($this->never())
  439. ->method('getRemoveParams');
  440. $this->assertEquals($result, $this->model->getSectionData());
  441. }
  442. }