WishlistTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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\Wishlist;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. * @SuppressWarnings(PHPMD.TooManyFields)
  11. */
  12. class WishlistTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $registry;
  18. /**
  19. * @var \Magento\Catalog\Helper\Product|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $productHelper;
  22. /**
  23. * @var \Magento\Wishlist\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $helper;
  26. /**
  27. * @var \Magento\Wishlist\Model\ResourceModel\Wishlist|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $resource;
  30. /**
  31. * @var \Magento\Wishlist\Model\ResourceModel\Wishlist\Collection|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $collection;
  34. /**
  35. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $storeManager;
  38. /**
  39. * @var \Magento\Framework\Stdlib\DateTime\DateTime|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $date;
  42. /**
  43. * @var \Magento\Wishlist\Model\ItemFactory|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $itemFactory;
  46. /**
  47. * @var \Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $itemsFactory;
  50. /**
  51. * @var \Magento\Catalog\Model\ProductFactory|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $productFactory;
  54. /**
  55. * @var \Magento\Framework\Math\Random|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. protected $mathRandom;
  58. /**
  59. * @var \Magento\Framework\Stdlib\DateTime|\PHPUnit_Framework_MockObject_MockObject
  60. */
  61. protected $dateTime;
  62. /**
  63. * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  64. */
  65. protected $eventDispatcher;
  66. /**
  67. * @var Wishlist
  68. */
  69. protected $wishlist;
  70. /**
  71. * @var \Magento\Catalog\Api\ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  72. */
  73. protected $productRepository;
  74. /**
  75. * @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject
  76. */
  77. protected $serializer;
  78. protected function setUp()
  79. {
  80. $context = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $this->eventDispatcher = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
  84. ->getMock();
  85. $this->registry = $this->getMockBuilder(\Magento\Framework\Registry::class)
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. $this->productHelper = $this->getMockBuilder(\Magento\Catalog\Helper\Product::class)
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. $this->helper = $this->getMockBuilder(\Magento\Wishlist\Helper\Data::class)
  92. ->disableOriginalConstructor()
  93. ->getMock();
  94. $this->resource = $this->getMockBuilder(\Magento\Wishlist\Model\ResourceModel\Wishlist::class)
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. $this->collection = $this->getMockBuilder(\Magento\Wishlist\Model\ResourceModel\Wishlist\Collection::class)
  98. ->disableOriginalConstructor()
  99. ->getMock();
  100. $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  101. ->getMock();
  102. $this->date = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
  103. ->disableOriginalConstructor()
  104. ->getMock();
  105. $this->itemFactory = $this->getMockBuilder(\Magento\Wishlist\Model\ItemFactory::class)
  106. ->disableOriginalConstructor()
  107. ->setMethods(['create'])
  108. ->getMock();
  109. $this->itemsFactory = $this->getMockBuilder(\Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory::class)
  110. ->disableOriginalConstructor()
  111. ->setMethods(['create'])
  112. ->getMock();
  113. $this->productFactory = $this->getMockBuilder(\Magento\Catalog\Model\ProductFactory::class)
  114. ->disableOriginalConstructor()
  115. ->setMethods(['create'])
  116. ->getMock();
  117. $this->mathRandom = $this->getMockBuilder(\Magento\Framework\Math\Random::class)
  118. ->disableOriginalConstructor()
  119. ->getMock();
  120. $this->dateTime = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime::class)
  121. ->disableOriginalConstructor()
  122. ->getMock();
  123. $this->productRepository = $this->createMock(\Magento\Catalog\Api\ProductRepositoryInterface::class);
  124. $this->serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
  125. ->disableOriginalConstructor()
  126. ->getMock();
  127. $context->expects($this->once())
  128. ->method('getEventDispatcher')
  129. ->will($this->returnValue($this->eventDispatcher));
  130. $this->wishlist = new Wishlist(
  131. $context,
  132. $this->registry,
  133. $this->productHelper,
  134. $this->helper,
  135. $this->resource,
  136. $this->collection,
  137. $this->storeManager,
  138. $this->date,
  139. $this->itemFactory,
  140. $this->itemsFactory,
  141. $this->productFactory,
  142. $this->mathRandom,
  143. $this->dateTime,
  144. $this->productRepository,
  145. false,
  146. [],
  147. $this->serializer
  148. );
  149. }
  150. public function testLoadByCustomerId()
  151. {
  152. $customerId = 1;
  153. $customerIdFieldName = 'customer_id';
  154. $sharingCode = 'expected_sharing_code';
  155. $this->eventDispatcher->expects($this->any())
  156. ->method('dispatch');
  157. $this->resource->expects($this->any())
  158. ->method('getCustomerIdFieldName');
  159. $this->resource->expects($this->once())
  160. ->method('load')
  161. ->with($this->logicalOr($this->wishlist, $customerId, $customerIdFieldName));
  162. $this->mathRandom->expects($this->once())
  163. ->method('getUniqueHash')
  164. ->will($this->returnValue($sharingCode));
  165. $this->assertInstanceOf(
  166. \Magento\Wishlist\Model\Wishlist::class,
  167. $this->wishlist->loadByCustomerId($customerId, true)
  168. );
  169. $this->assertEquals($customerId, $this->wishlist->getCustomerId());
  170. $this->assertEquals($sharingCode, $this->wishlist->getSharingCode());
  171. }
  172. /**
  173. * @param int|\Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject $itemId
  174. * @param \Magento\Framework\DataObject $buyRequest
  175. * @param null|array|\Magento\Framework\DataObject $param
  176. * @throws \Magento\Framework\Exception\LocalizedException
  177. *
  178. * @dataProvider updateItemDataProvider
  179. */
  180. public function testUpdateItem($itemId, $buyRequest, $param)
  181. {
  182. $storeId = 1;
  183. $productId = 1;
  184. $stores = [(new \Magento\Framework\DataObject())->setId($storeId)];
  185. $newItem = $this->getMockBuilder(\Magento\Wishlist\Model\Item::class)
  186. ->setMethods(
  187. ['setProductId', 'setWishlistId', 'setStoreId', 'setOptions', 'setProduct', 'setQty', 'getItem', 'save']
  188. )
  189. ->disableOriginalConstructor()
  190. ->getMock();
  191. $newItem->expects($this->any())->method('setProductId')->will($this->returnSelf());
  192. $newItem->expects($this->any())->method('setWishlistId')->will($this->returnSelf());
  193. $newItem->expects($this->any())->method('setStoreId')->will($this->returnSelf());
  194. $newItem->expects($this->any())->method('setOptions')->will($this->returnSelf());
  195. $newItem->expects($this->any())->method('setProduct')->will($this->returnSelf());
  196. $newItem->expects($this->any())->method('setQty')->will($this->returnSelf());
  197. $newItem->expects($this->any())->method('getItem')->will($this->returnValue(2));
  198. $newItem->expects($this->any())->method('save')->will($this->returnSelf());
  199. $this->itemFactory->expects($this->once())->method('create')->will($this->returnValue($newItem));
  200. $this->storeManager->expects($this->any())->method('getStores')->will($this->returnValue($stores));
  201. $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($stores[0]));
  202. $product = $this->getMockBuilder(
  203. \Magento\Catalog\Model\Product::class
  204. )->disableOriginalConstructor()->getMock();
  205. $product->expects($this->any())->method('getId')->will($this->returnValue($productId));
  206. $product->expects($this->any())->method('getStoreId')->will($this->returnValue($storeId));
  207. $instanceType = $this->getMockBuilder(\Magento\Catalog\Model\Product\Type\AbstractType::class)
  208. ->disableOriginalConstructor()
  209. ->getMock();
  210. $instanceType->expects($this->once())
  211. ->method('processConfiguration')
  212. ->will(
  213. $this->returnValue(
  214. $this->getMockBuilder(
  215. \Magento\Catalog\Model\Product::class
  216. )->disableOriginalConstructor()->getMock()
  217. )
  218. );
  219. $newProduct = $this->getMockBuilder(
  220. \Magento\Catalog\Model\Product::class
  221. )->disableOriginalConstructor()->getMock();
  222. $newProduct->expects($this->any())
  223. ->method('setStoreId')
  224. ->with($storeId)
  225. ->will($this->returnSelf());
  226. $newProduct->expects($this->once())
  227. ->method('getTypeInstance')
  228. ->will($this->returnValue($instanceType));
  229. $item = $this->getMockBuilder(\Magento\Wishlist\Model\Item::class)->disableOriginalConstructor()->getMock();
  230. $item->expects($this->once())
  231. ->method('getProduct')
  232. ->will($this->returnValue($product));
  233. $items = $this->getMockBuilder(\Magento\Wishlist\Model\ResourceModel\Item\Collection::class)
  234. ->disableOriginalConstructor()
  235. ->getMock();
  236. $items->expects($this->once())
  237. ->method('addWishlistFilter')
  238. ->will($this->returnSelf());
  239. $items->expects($this->once())
  240. ->method('addStoreFilter')
  241. ->will($this->returnSelf());
  242. $items->expects($this->once())
  243. ->method('setVisibilityFilter')
  244. ->will($this->returnSelf());
  245. $items->expects($this->once())
  246. ->method('getItemById')
  247. ->will($this->returnValue($item));
  248. $items->expects($this->any())
  249. ->method('getIterator')
  250. ->will($this->returnValue(new \ArrayIterator([$item])));
  251. $this->itemsFactory->expects($this->any())
  252. ->method('create')
  253. ->will($this->returnValue($items));
  254. $this->productRepository->expects($this->once())
  255. ->method('getById')
  256. ->with($productId, false, $storeId)
  257. ->will($this->returnValue($newProduct));
  258. $this->assertInstanceOf(
  259. \Magento\Wishlist\Model\Wishlist::class,
  260. $this->wishlist->updateItem($itemId, $buyRequest, $param)
  261. );
  262. }
  263. /**
  264. * @return array
  265. */
  266. public function updateItemDataProvider()
  267. {
  268. return [
  269. '0' => [1, new \Magento\Framework\DataObject(), null]
  270. ];
  271. }
  272. public function testAddNewItem()
  273. {
  274. $productId = 1;
  275. $storeId = 1;
  276. $buyRequest = json_encode([
  277. 'number' => 42,
  278. 'string' => 'string_value',
  279. 'boolean' => true,
  280. 'collection' => [1, 2, 3],
  281. 'product' => 1,
  282. 'form_key' => 'abc'
  283. ]);
  284. $result = 'product';
  285. $instanceType = $this->getMockBuilder(\Magento\Catalog\Model\Product\Type\AbstractType::class)
  286. ->disableOriginalConstructor()
  287. ->getMock();
  288. $instanceType->expects($this->once())
  289. ->method('processConfiguration')
  290. ->willReturn('product');
  291. $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  292. ->disableOriginalConstructor()
  293. ->setMethods(['getId', 'hasWishlistStoreId', 'getStoreId', 'getTypeInstance'])
  294. ->getMock();
  295. $productMock->expects($this->once())
  296. ->method('getId')
  297. ->willReturn($productId);
  298. $productMock->expects($this->once())
  299. ->method('hasWishlistStoreId')
  300. ->willReturn(false);
  301. $productMock->expects($this->once())
  302. ->method('getStoreId')
  303. ->willReturn($storeId);
  304. $productMock->expects($this->once())
  305. ->method('getTypeInstance')
  306. ->willReturn($instanceType);
  307. $this->productRepository->expects($this->once())
  308. ->method('getById')
  309. ->with($productId, false, $storeId)
  310. ->willReturn($productMock);
  311. $this->serializer->expects($this->once())
  312. ->method('unserialize')
  313. ->willReturnCallback(
  314. function ($value) {
  315. return json_decode($value, true);
  316. }
  317. );
  318. $this->assertEquals($result, $this->wishlist->addNewItem($productMock, $buyRequest));
  319. }
  320. }