SidebarTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Block\Reorder;
  7. use Magento\Customer\Model\Context;
  8. /**
  9. * Class SidebarTest
  10. *
  11. * @package Magento\Sales\Block\Reorder
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class SidebarTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\Sales\Block\Reorder\Sidebar|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $block;
  20. /**
  21. * @var \Magento\Framework\View\Element\Template\Context|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $context;
  24. /**
  25. * @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $orderCollectionFactory;
  28. /**
  29. * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $customerSession;
  32. /**
  33. * @var \Magento\Sales\Model\Order\Config|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $orderConfig;
  36. /**
  37. * @var \Magento\Framework\App\Http\Context|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $httpContext;
  40. /**
  41. * @var \Magento\Sales\Model\ResourceModel\Order\Collection|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $orderCollection;
  44. /**
  45. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  46. */
  47. protected $objectManagerHelper;
  48. /** @var \PHPUnit_Framework_MockObject_MockObject */
  49. protected $stockItemMock;
  50. /**
  51. * @var \PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $stockRegistry;
  54. protected function setUp()
  55. {
  56. $this->markTestIncomplete('MAGETWO-36789');
  57. $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  58. $this->context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
  59. $this->httpContext = $this->createPartialMock(\Magento\Framework\App\Http\Context::class, ['getValue']);
  60. $this->orderCollectionFactory = $this->createPartialMock(
  61. \Magento\Sales\Model\ResourceModel\Order\CollectionFactory::class,
  62. ['create']
  63. );
  64. $this->customerSession = $this->createPartialMock(\Magento\Customer\Model\Session::class, ['getCustomerId']);
  65. $this->orderConfig = $this->createPartialMock(
  66. \Magento\Sales\Model\Order\Config::class,
  67. ['getVisibleOnFrontStatuses']
  68. );
  69. $this->orderCollection = $this->createPartialMock(
  70. \Magento\Sales\Model\ResourceModel\Order\Collection::class,
  71. [
  72. 'addAttributeToFilter',
  73. 'addAttributeToSort',
  74. 'setPage',
  75. 'setOrders',
  76. ]
  77. );
  78. $this->stockRegistry = $this->getMockBuilder(\Magento\CatalogInventory\Model\StockRegistry::class)
  79. ->disableOriginalConstructor()
  80. ->setMethods(['getStockItem', '__wakeup'])
  81. ->getMock();
  82. $this->stockItemMock = $this->createPartialMock(
  83. \Magento\CatalogInventory\Model\Stock\Item::class,
  84. ['getIsInStock', '__wakeup']
  85. );
  86. $this->stockRegistry->expects($this->any())
  87. ->method('getStockItem')
  88. ->will($this->returnValue($this->stockItemMock));
  89. }
  90. protected function tearDown()
  91. {
  92. $this->block = null;
  93. }
  94. protected function createBlockObject()
  95. {
  96. $this->block = $this->objectManagerHelper->getObject(
  97. \Magento\Sales\Block\Reorder\Sidebar::class,
  98. [
  99. 'context' => $this->context,
  100. 'orderCollectionFactory' => $this->orderCollectionFactory,
  101. 'orderConfig' => $this->orderConfig,
  102. 'customerSession' => $this->customerSession,
  103. 'httpContext' => $this->httpContext,
  104. 'stockRegistry' => $this->stockRegistry,
  105. ]
  106. );
  107. }
  108. public function testGetIdentities()
  109. {
  110. $websiteId = 1;
  111. $storeId = null;
  112. $productTags = ['catalog_product_1'];
  113. $limit = 5;
  114. $storeManager = $this->createPartialMock(\Magento\Store\Model\StoreManager::class, ['getStore']);
  115. $this->context->expects($this->once())
  116. ->method('getStoreManager')
  117. ->will($this->returnValue($storeManager));
  118. $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getWebsiteId']);
  119. $store->expects($this->once())
  120. ->method('getWebsiteId')
  121. ->will($this->returnValue($websiteId));
  122. $storeManager->expects($this->once())
  123. ->method('getStore')
  124. ->with($this->equalTo($storeId))
  125. ->will($this->returnValue($store));
  126. $product = $this->createPartialMock(
  127. \Magento\Catalog\Model\Product::class,
  128. ['__wakeUp', 'getIdentities', 'getWebsiteIds']
  129. );
  130. $product->expects($this->once())
  131. ->method('getIdentities')
  132. ->will($this->returnValue($productTags));
  133. $product->expects($this->atLeastOnce())
  134. ->method('getWebsiteIds')
  135. ->will($this->returnValue([$websiteId]));
  136. $item = $this->createPartialMock(
  137. \Magento\Sales\Model\ResourceModel\Order\Item::class,
  138. ['__wakeup', 'getProduct']
  139. );
  140. $item->expects($this->atLeastOnce())
  141. ->method('getProduct')
  142. ->will($this->returnValue($product));
  143. $order = $this->createPartialMock(
  144. \Magento\Sales\Model\Order::class,
  145. ['__wakeup', 'getParentItemsRandomCollection']
  146. );
  147. $order->expects($this->atLeastOnce())
  148. ->method('getParentItemsRandomCollection')
  149. ->with($this->equalTo($limit))
  150. ->will($this->returnValue([$item]));
  151. $this->createBlockObject();
  152. $this->assertSame($this->block, $this->block->setOrders([$order]));
  153. $this->assertEquals($productTags, $this->block->getIdentities());
  154. }
  155. public function testInitOrders()
  156. {
  157. $customerId = 25;
  158. $attribute = ['customer_id', 'status'];
  159. $this->httpContext->expects($this->once())
  160. ->method('getValue')
  161. ->with($this->equalTo(Context::CONTEXT_AUTH))
  162. ->will($this->returnValue(true));
  163. $this->customerSession->expects($this->once())
  164. ->method('getCustomerId')
  165. ->will($this->returnValue($customerId));
  166. $statuses = ['pending', 'processing', 'complete'];
  167. $this->orderConfig->expects($this->once())
  168. ->method('getVisibleOnFrontStatuses')
  169. ->will($this->returnValue($statuses));
  170. $this->orderCollection->expects($this->at(0))
  171. ->method('addAttributeToFilter')
  172. ->with(
  173. $attribute[0],
  174. $this->equalTo($customerId)
  175. )
  176. ->will($this->returnSelf());
  177. $this->orderCollection->expects($this->at(1))
  178. ->method('addAttributeToFilter')
  179. ->with($attribute[1], $this->equalTo(['in' => $statuses]))
  180. ->will($this->returnSelf());
  181. $this->orderCollection->expects($this->at(2))
  182. ->method('addAttributeToSort')
  183. ->with('created_at', 'desc')
  184. ->will($this->returnSelf());
  185. $this->orderCollection->expects($this->at(3))
  186. ->method('setPage')
  187. ->with($this->equalTo(1), $this->equalTo(1))
  188. ->will($this->returnSelf());
  189. $this->orderCollectionFactory->expects($this->atLeastOnce())
  190. ->method('create')
  191. ->will($this->returnValue($this->orderCollection));
  192. $this->createBlockObject();
  193. $this->assertEquals($this->orderCollection, $this->block->getOrders());
  194. }
  195. public function testIsItemAvailableForReorder()
  196. {
  197. $productId = 1;
  198. $result = true;
  199. $product = $this->createPartialMock(\Magento\Catalog\Model\Product::class, ['getId', '__wakeup']);
  200. $product->expects($this->once())
  201. ->method('getId')
  202. ->will($this->returnValue($productId));
  203. $this->stockItemMock->expects($this->once())
  204. ->method('getIsInStock')
  205. ->will($this->returnValue($result));
  206. $this->stockRegistry->expects($this->any())
  207. ->method('getStockItem')
  208. ->will($this->returnValue($this->stockItemMock));
  209. $orderItem = $this->createPartialMock(\Magento\Sales\Model\Order\Item::class, ['getStore', 'getProduct']);
  210. $orderItem->expects($this->any())
  211. ->method('getProduct')
  212. ->will($this->returnValue($product));
  213. $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getWebsiteId']);
  214. $store->expects($this->any())
  215. ->method('getWebsiteId')
  216. ->will($this->returnValue(10));
  217. $orderItem->expects($this->any())
  218. ->method('getStore')
  219. ->will($this->returnValue($store));
  220. $this->createBlockObject();
  221. $this->assertSame($result, $this->block->isItemAvailableForReorder($orderItem));
  222. }
  223. public function testItemNotAvailableForReorderWhenProductNotExist()
  224. {
  225. $this->stockItemMock->expects($this->never())->method('getIsInStock');
  226. $this->stockRegistry->expects($this->any())
  227. ->method('getStockItem')
  228. ->will($this->returnValue($this->stockItemMock));
  229. $orderItem = $this->createMock(\Magento\Sales\Model\Order\Item::class);
  230. $orderItem->expects($this->any())
  231. ->method('getProduct')
  232. ->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException());
  233. $this->createBlockObject();
  234. $this->assertSame(false, $this->block->isItemAvailableForReorder($orderItem));
  235. }
  236. }