CustomerOrdersTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Test\Unit\Model;
  7. use Magento\Directory\Model\Currency;
  8. use Magento\Framework\Api\FilterBuilder;
  9. use Magento\Framework\Api\SearchCriteriaBuilder;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. use Magento\Sales\Api\Data\OrderSearchResultInterface;
  12. use Magento\Sales\Model\Order;
  13. use Magento\Signifyd\Model\CustomerOrders;
  14. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  15. use Magento\Sales\Api\OrderRepositoryInterface;
  16. use Psr\Log\LoggerInterface;
  17. /**
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class CustomerOrdersTest extends \PHPUnit\Framework\TestCase
  21. {
  22. /**
  23. * @var int
  24. */
  25. private static $customerId = 1;
  26. /**
  27. * Order amount in EUR
  28. * @var int
  29. */
  30. private static $eurAmount = 100;
  31. /**
  32. * Order amount in UAH
  33. * @var int
  34. */
  35. private static $uahAmount = 270;
  36. /**
  37. * Order amount in USD
  38. * @var int
  39. */
  40. private static $usdAmount = 50;
  41. /**
  42. * @var ObjectManager
  43. */
  44. private $objectManager;
  45. /**
  46. * @var FilterBuilder|MockObject
  47. */
  48. private $filterBuilder;
  49. /**
  50. * @var Currency|MockObject
  51. */
  52. private $eurCurrency;
  53. /**
  54. * @var Currency|MockObject
  55. */
  56. private $uahCurrency;
  57. /**
  58. * @var CustomerOrders
  59. */
  60. private $model;
  61. /**
  62. * @var OrderRepositoryInterface|MockObject
  63. */
  64. private $orderRepository;
  65. /**
  66. * @var SearchCriteriaBuilder|MockObject
  67. */
  68. private $searchCriteriaBuilder;
  69. /**
  70. * @var LoggerInterface|MockObject
  71. */
  72. private $logger;
  73. /**
  74. * @inheritdoc
  75. */
  76. protected function setUp()
  77. {
  78. $this->objectManager = new ObjectManager($this);
  79. $this->orderRepository = $this->getMockBuilder(OrderRepositoryInterface::class)
  80. ->getMockForAbstractClass();
  81. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  82. ->getMockForAbstractClass();
  83. $this->filterBuilder = $this->getMockBuilder(FilterBuilder::class)
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $this->searchCriteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class)
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $this->model = $this->objectManager->getObject(CustomerOrders::class, [
  90. 'filterBuilder' => $this->filterBuilder,
  91. 'orderRepository' => $this->orderRepository,
  92. 'searchCriteriaBuilder' => $this->searchCriteriaBuilder,
  93. 'logger' => $this->logger
  94. ]);
  95. $this->initCurrencies();
  96. $this->initOrderRepository();
  97. $this->objectManager->setBackwardCompatibleProperty(
  98. $this->model,
  99. 'currencies',
  100. ['EUR' => $this->eurCurrency, 'UAH' => $this->uahCurrency]
  101. );
  102. }
  103. /**
  104. * @covers \Magento\Signifyd\Model\CustomerOrders::getAggregatedOrdersInfo()
  105. */
  106. public function testGetCountAndTotalAmount()
  107. {
  108. $this->eurCurrency->expects($this->once())
  109. ->method('convert')
  110. ->with(self::$eurAmount, 'USD')
  111. ->willReturn(109);
  112. $this->uahCurrency->expects($this->once())
  113. ->method('convert')
  114. ->with(self::$uahAmount, 'USD')
  115. ->willReturn(10.35);
  116. $actual = $this->model->getAggregatedOrdersInfo(self::$customerId);
  117. static::assertEquals(3, $actual['aggregateOrderCount']);
  118. static::assertEquals(169.35, $actual['aggregateOrderDollars']);
  119. }
  120. /**
  121. * Test case when required currency rate is absent and exception is thrown
  122. * @covers \Magento\Signifyd\Model\CustomerOrders::getAggregatedOrdersInfo()
  123. */
  124. public function testGetCountAndTotalAmountNegative()
  125. {
  126. $this->eurCurrency->expects($this->once())
  127. ->method('convert')
  128. ->with(self::$eurAmount, 'USD')
  129. ->willReturn(109);
  130. $this->uahCurrency->expects($this->once())
  131. ->method('convert')
  132. ->with(self::$uahAmount, 'USD')
  133. ->willThrowException(new \Exception());
  134. $this->logger->expects($this->once())
  135. ->method('error');
  136. $actual = $this->model->getAggregatedOrdersInfo(self::$customerId);
  137. $this->assertNull($actual['aggregateOrderCount']);
  138. $this->assertNull($actual['aggregateOrderDollars']);
  139. }
  140. /**
  141. * Populate order repository with mocked orders
  142. */
  143. private function initOrderRepository()
  144. {
  145. $this->filterBuilder->expects($this->once())
  146. ->method('setField')
  147. ->willReturnSelf();
  148. $this->filterBuilder->expects($this->once())
  149. ->method('setValue')
  150. ->willReturnSelf();
  151. $filter = $this->getMockBuilder(\Magento\Framework\Api\Filter::class)
  152. ->disableOriginalConstructor()
  153. ->getMock();
  154. $this->filterBuilder->expects($this->once())
  155. ->method('create')
  156. ->willReturn($filter);
  157. $searchCriteria = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteria::class)
  158. ->disableOriginalConstructor()
  159. ->getMock();
  160. $this->searchCriteriaBuilder->expects($this->once())
  161. ->method('create')
  162. ->willReturn($searchCriteria);
  163. $orderSearchResult = $this->getMockBuilder(OrderSearchResultInterface::class)
  164. ->getMockForAbstractClass();
  165. $orderSearchResult->expects($this->once())
  166. ->method('getItems')
  167. ->willReturn($this->getOrders());
  168. $this->orderRepository->expects($this->once())
  169. ->method('getList')
  170. ->willReturn($orderSearchResult);
  171. }
  172. /**
  173. * Creates mocks for currencies
  174. * @return void
  175. */
  176. private function initCurrencies()
  177. {
  178. $this->eurCurrency = $this->getMockBuilder(Currency::class)
  179. ->disableOriginalConstructor()
  180. ->setMethods(['convert'])
  181. ->getMock();
  182. $this->uahCurrency = $this->getMockBuilder(Currency::class)
  183. ->disableOriginalConstructor()
  184. ->setMethods(['convert'])
  185. ->getMock();
  186. }
  187. /**
  188. * Get list of mocked orders with different currencies
  189. * @return array
  190. */
  191. private function getOrders()
  192. {
  193. $eurOrder = $this->getMockBuilder(Order::class)
  194. ->disableOriginalConstructor()
  195. ->setMethods(['getBaseGrandTotal', 'getBaseCurrencyCode'])
  196. ->getMock();
  197. $eurOrder->expects($this->once())
  198. ->method('getBaseGrandTotal')
  199. ->willReturn(self::$eurAmount);
  200. $eurOrder->expects($this->once())
  201. ->method('getBaseCurrencyCode')
  202. ->willReturn('EUR');
  203. $uahOrder = $this->getMockBuilder(Order::class)
  204. ->disableOriginalConstructor()
  205. ->setMethods(['getBaseGrandTotal', 'getBaseCurrencyCode'])
  206. ->getMock();
  207. $uahOrder->expects($this->once())
  208. ->method('getBaseGrandTotal')
  209. ->willReturn(self::$uahAmount);
  210. $uahOrder->expects($this->once())
  211. ->method('getBaseCurrencyCode')
  212. ->willReturn('UAH');
  213. $usdOrder = $this->getMockBuilder(Order::class)
  214. ->disableOriginalConstructor()
  215. ->setMethods(['getBaseGrandTotal', 'getBaseCurrencyCode'])
  216. ->getMock();
  217. $usdOrder->expects($this->once())
  218. ->method('getBaseGrandTotal')
  219. ->willReturn(self::$usdAmount);
  220. $usdOrder->expects($this->once())
  221. ->method('getBaseCurrencyCode')
  222. ->willReturn('USD');
  223. return [$usdOrder, $eurOrder, $uahOrder];
  224. }
  225. }