LastOrderedItemsTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\CustomerData;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class LastOrderedItemsTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. private $orderCollectionFactoryMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $orderConfigMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $customerSessionMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $stockRegistryMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $storeManagerMock;
  33. /**
  34. * @var ObjectManagerHelper
  35. */
  36. private $objectManagerHelper;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $orderMock;
  41. /**
  42. * @var \PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $productRepositoryMock;
  45. /**
  46. * @var \Magento\Sales\CustomerData\LastOrderedItems
  47. */
  48. private $section;
  49. /**
  50. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. private $loggerMock;
  53. protected function setUp()
  54. {
  55. $this->objectManagerHelper = new ObjectManagerHelper($this);
  56. $this->orderCollectionFactoryMock =
  57. $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\CollectionFactory::class)
  58. ->disableOriginalConstructor()
  59. ->setMethods(['create'])
  60. ->getMock();
  61. $this->orderConfigMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Config::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $this->customerSessionMock = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $this->stockRegistryMock = $this->getMockBuilder(\Magento\CatalogInventory\Api\StockRegistryInterface::class)
  68. ->getMockForAbstractClass();
  69. $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  70. ->getMockForAbstractClass();
  71. $this->orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $this->productRepositoryMock = $this->getMockBuilder(\Magento\Catalog\Api\ProductRepositoryInterface::class)
  75. ->getMockForAbstractClass();
  76. $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
  77. ->getMockForAbstractClass();
  78. $this->section = new \Magento\Sales\CustomerData\LastOrderedItems(
  79. $this->orderCollectionFactoryMock,
  80. $this->orderConfigMock,
  81. $this->customerSessionMock,
  82. $this->stockRegistryMock,
  83. $this->storeManagerMock,
  84. $this->productRepositoryMock,
  85. $this->loggerMock
  86. );
  87. }
  88. public function testGetSectionData()
  89. {
  90. $storeId = 1;
  91. $websiteId = 4;
  92. $expectedItem1 = [
  93. 'id' => 1,
  94. 'name' => 'Product Name 1',
  95. 'url' => 'http://example.com',
  96. 'is_saleable' => true,
  97. ];
  98. $expectedItem2 = [
  99. 'id' => 2,
  100. 'name' => 'Product Name 2',
  101. 'url' => null,
  102. 'is_saleable' => true,
  103. ];
  104. $productIdVisible = 1;
  105. $productIdNotVisible = 2;
  106. $stockItemMock = $this->getMockBuilder(\Magento\CatalogInventory\Api\Data\StockItemInterface::class)
  107. ->getMockForAbstractClass();
  108. $itemWithVisibleProduct = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  109. ->disableOriginalConstructor()
  110. ->getMock();
  111. $itemWithNotVisibleProduct = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  112. ->disableOriginalConstructor()
  113. ->getMock();
  114. $productVisible = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  115. ->disableOriginalConstructor()
  116. ->getMock();
  117. $productNotVisible = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  118. ->disableOriginalConstructor()
  119. ->getMock();
  120. $items = [$itemWithVisibleProduct, $itemWithNotVisibleProduct];
  121. $this->getLastOrderMock();
  122. $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)->getMockForAbstractClass();
  123. $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
  124. $storeMock->expects($this->any())->method('getWebsiteId')->willReturn($websiteId);
  125. $storeMock->expects($this->any())->method('getId')->willReturn($storeId);
  126. $this->orderMock->expects($this->once())
  127. ->method('getParentItemsRandomCollection')
  128. ->with(\Magento\Sales\CustomerData\LastOrderedItems::SIDEBAR_ORDER_LIMIT)
  129. ->willReturn($items);
  130. $productVisible->expects($this->once())->method('isVisibleInSiteVisibility')->willReturn(true);
  131. $productVisible->expects($this->once())->method('getProductUrl')->willReturn($expectedItem1['url']);
  132. $productVisible->expects($this->once())->method('getWebsiteIds')->willReturn([1, 4]);
  133. $productVisible->expects($this->once())->method('getId')->willReturn($productIdVisible);
  134. $productNotVisible->expects($this->once())->method('isVisibleInSiteVisibility')->willReturn(false);
  135. $productNotVisible->expects($this->never())->method('getProductUrl');
  136. $productNotVisible->expects($this->once())->method('getWebsiteIds')->willReturn([1, 4]);
  137. $productNotVisible->expects($this->once())->method('getId')->willReturn($productIdNotVisible);
  138. $itemWithVisibleProduct->expects($this->once())->method('getProductId')->willReturn($productIdVisible);
  139. $itemWithVisibleProduct->expects($this->once())->method('getProduct')->willReturn($productVisible);
  140. $itemWithVisibleProduct->expects($this->once())->method('getId')->willReturn($expectedItem1['id']);
  141. $itemWithVisibleProduct->expects($this->once())->method('getName')->willReturn($expectedItem1['name']);
  142. $itemWithVisibleProduct->expects($this->once())->method('getStore')->willReturn($storeMock);
  143. $itemWithNotVisibleProduct->expects($this->once())->method('getProductId')->willReturn($productIdNotVisible);
  144. $itemWithNotVisibleProduct->expects($this->once())->method('getProduct')->willReturn($productNotVisible);
  145. $itemWithNotVisibleProduct->expects($this->once())->method('getId')->willReturn($expectedItem2['id']);
  146. $itemWithNotVisibleProduct->expects($this->once())->method('getName')->willReturn($expectedItem2['name']);
  147. $itemWithNotVisibleProduct->expects($this->once())->method('getStore')->willReturn($storeMock);
  148. $this->productRepositoryMock->expects($this->any())
  149. ->method('getById')
  150. ->willReturnMap([
  151. [$productIdVisible, false, $storeId, false, $productVisible],
  152. [$productIdNotVisible, false, $storeId, false, $productNotVisible],
  153. ]);
  154. $this->stockRegistryMock
  155. ->expects($this->any())
  156. ->method('getStockItem')
  157. ->willReturnMap([
  158. [$productIdVisible, $websiteId, $stockItemMock],
  159. [$productIdNotVisible, $websiteId, $stockItemMock],
  160. ]);
  161. $stockItemMock->expects($this->exactly(2))->method('getIsInStock')->willReturn($expectedItem1['is_saleable']);
  162. $this->assertEquals(['items' => [$expectedItem1, $expectedItem2]], $this->section->getSectionData());
  163. }
  164. /**
  165. * @return \PHPUnit_Framework_MockObject_MockObject
  166. */
  167. private function getLastOrderMock()
  168. {
  169. $customerId = 1;
  170. $visibleOnFrontStatuses = ['complete'];
  171. $orderCollectionMock = $this->objectManagerHelper
  172. ->getCollectionMock(\Magento\Sales\Model\ResourceModel\Order\Collection::class, [$this->orderMock]);
  173. $this->customerSessionMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
  174. $this->orderConfigMock
  175. ->expects($this->once())
  176. ->method('getVisibleOnFrontStatuses')
  177. ->willReturn($visibleOnFrontStatuses);
  178. $this->orderCollectionFactoryMock->expects($this->once())->method('create')->willReturn($orderCollectionMock);
  179. $orderCollectionMock->expects($this->at(0))
  180. ->method('addAttributeToFilter')
  181. ->with('customer_id', $customerId)
  182. ->willReturnSelf();
  183. $orderCollectionMock->expects($this->at(1))
  184. ->method('addAttributeToFilter')
  185. ->with('status', ['in' => $visibleOnFrontStatuses])
  186. ->willReturnSelf();
  187. $orderCollectionMock->expects($this->once())
  188. ->method('addAttributeToSort')
  189. ->willReturnSelf();
  190. $orderCollectionMock->expects($this->once())
  191. ->method('setPage')
  192. ->willReturnSelf();
  193. return $this->orderMock;
  194. }
  195. public function testGetSectionDataWithNotExistingProduct()
  196. {
  197. $storeId = 1;
  198. $websiteId = 4;
  199. $productId = 1;
  200. $exception = new \Magento\Framework\Exception\NoSuchEntityException(__("Product doesn't exist"));
  201. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  202. ->disableOriginalConstructor()
  203. ->setMethods(['getProductId'])
  204. ->getMock();
  205. $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)->getMockForAbstractClass();
  206. $this->getLastOrderMock();
  207. $this->storeManagerMock->expects($this->exactly(2))->method('getStore')->willReturn($storeMock);
  208. $storeMock->expects($this->once())->method('getWebsiteId')->willReturn($websiteId);
  209. $storeMock->expects($this->once())->method('getId')->willReturn($storeId);
  210. $this->orderMock->expects($this->once())
  211. ->method('getParentItemsRandomCollection')
  212. ->with(\Magento\Sales\CustomerData\LastOrderedItems::SIDEBAR_ORDER_LIMIT)
  213. ->willReturn([$orderItemMock]);
  214. $orderItemMock->expects($this->once())->method('getProductId')->willReturn($productId);
  215. $this->productRepositoryMock->expects($this->once())
  216. ->method('getById')
  217. ->with($productId, false, $storeId)
  218. ->willThrowException($exception);
  219. $this->loggerMock->expects($this->once())->method('critical')->with($exception);
  220. $this->assertEquals(['items' => []], $this->section->getSectionData());
  221. }
  222. }