Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerOrdersTest.php
Go to the documentation of this file.
1 <?php
7 
15 use PHPUnit_Framework_MockObject_MockObject as MockObject;
17 use Psr\Log\LoggerInterface;
18 
22 class CustomerOrdersTest extends \PHPUnit\Framework\TestCase
23 {
27  private static $customerId = 1;
28 
33  private static $eurAmount = 100;
34 
39  private static $uahAmount = 270;
40 
45  private static $usdAmount = 50;
46 
50  private $objectManager;
51 
55  private $filterBuilder;
56 
60  private $eurCurrency;
61 
65  private $uahCurrency;
66 
70  private $model;
71 
75  private $orderRepository;
76 
80  private $searchCriteriaBuilder;
81 
85  private $logger;
86 
90  protected function setUp()
91  {
92  $this->objectManager = new ObjectManager($this);
93 
94  $this->orderRepository = $this->getMockBuilder(OrderRepositoryInterface::class)
95  ->getMockForAbstractClass();
96 
97  $this->logger = $this->getMockBuilder(LoggerInterface::class)
98  ->getMockForAbstractClass();
99 
100  $this->filterBuilder = $this->getMockBuilder(FilterBuilder::class)
101  ->disableOriginalConstructor()
102  ->getMock();
103 
104  $this->searchCriteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class)
105  ->disableOriginalConstructor()
106  ->getMock();
107 
108  $this->model = $this->objectManager->getObject(CustomerOrders::class, [
109  'filterBuilder' => $this->filterBuilder,
110  'orderRepository' => $this->orderRepository,
111  'searchCriteriaBuilder' => $this->searchCriteriaBuilder,
112  'logger' => $this->logger
113  ]);
114 
115  $this->initCurrencies();
116  $this->initOrderRepository();
117 
118  $this->objectManager->setBackwardCompatibleProperty(
119  $this->model,
120  'currencies',
121  ['EUR' => $this->eurCurrency, 'UAH' => $this->uahCurrency]
122  );
123  }
124 
128  public function testGetCountAndTotalAmount()
129  {
130  $this->eurCurrency->expects($this->once())
131  ->method('convert')
132  ->with(self::$eurAmount, 'USD')
133  ->willReturn(109);
134 
135  $this->uahCurrency->expects($this->once())
136  ->method('convert')
137  ->with(self::$uahAmount, 'USD')
138  ->willReturn(10.35);
139 
140  $actual = $this->model->getAggregatedOrdersInfo(self::$customerId);
141 
142  static::assertEquals(3, $actual['aggregateOrderCount']);
143  static::assertEquals(169.35, $actual['aggregateOrderDollars']);
144  }
145 
151  {
152  $this->eurCurrency->expects($this->once())
153  ->method('convert')
154  ->with(self::$eurAmount, 'USD')
155  ->willReturn(109);
156 
157  $this->uahCurrency->expects($this->once())
158  ->method('convert')
159  ->with(self::$uahAmount, 'USD')
160  ->willThrowException(new \Exception());
161 
162  $this->logger->expects($this->once())
163  ->method('error');
164 
165  $actual = $this->model->getAggregatedOrdersInfo(self::$customerId);
166 
167  $this->assertNull($actual['aggregateOrderCount']);
168  $this->assertNull($actual['aggregateOrderDollars']);
169  }
170 
174  private function initOrderRepository()
175  {
176  $this->filterBuilder->expects($this->once())
177  ->method('setField')
178  ->willReturnSelf();
179  $this->filterBuilder->expects($this->once())
180  ->method('setValue')
181  ->willReturnSelf();
182  $filter = $this->getMockBuilder(\Magento\Framework\Api\Filter::class)
183  ->disableOriginalConstructor()
184  ->getMock();
185  $this->filterBuilder->expects($this->once())
186  ->method('create')
187  ->willReturn($filter);
188 
189  $searchCriteria = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteria::class)
190  ->disableOriginalConstructor()
191  ->getMock();
192  $this->searchCriteriaBuilder->expects($this->once())
193  ->method('create')
194  ->willReturn($searchCriteria);
195 
196  $orderSearchResult = $this->getMockBuilder(OrderSearchResultInterface::class)
197  ->getMockForAbstractClass();
198  $orderSearchResult->expects($this->once())
199  ->method('getItems')
200  ->willReturn($this->getOrders());
201  $this->orderRepository->expects($this->once())
202  ->method('getList')
203  ->willReturn($orderSearchResult);
204  }
205 
210  private function initCurrencies()
211  {
212  $this->eurCurrency = $this->getMockBuilder(Currency::class)
213  ->disableOriginalConstructor()
214  ->setMethods(['convert'])
215  ->getMock();
216 
217  $this->uahCurrency = $this->getMockBuilder(Currency::class)
218  ->disableOriginalConstructor()
219  ->setMethods(['convert'])
220  ->getMock();
221  }
222 
227  private function getOrders()
228  {
229  $eurOrder = $this->getMockBuilder(Order::class)
230  ->disableOriginalConstructor()
231  ->setMethods(['getBaseGrandTotal', 'getBaseCurrencyCode'])
232  ->getMock();
233 
234  $eurOrder->expects($this->once())
235  ->method('getBaseGrandTotal')
236  ->willReturn(self::$eurAmount);
237  $eurOrder->expects($this->once())
238  ->method('getBaseCurrencyCode')
239  ->willReturn('EUR');
240 
241  $uahOrder = $this->getMockBuilder(Order::class)
242  ->disableOriginalConstructor()
243  ->setMethods(['getBaseGrandTotal', 'getBaseCurrencyCode'])
244  ->getMock();
245 
246  $uahOrder->expects($this->once())
247  ->method('getBaseGrandTotal')
248  ->willReturn(self::$uahAmount);
249  $uahOrder->expects($this->once())
250  ->method('getBaseCurrencyCode')
251  ->willReturn('UAH');
252 
253  $usdOrder = $this->getMockBuilder(Order::class)
254  ->disableOriginalConstructor()
255  ->setMethods(['getBaseGrandTotal', 'getBaseCurrencyCode'])
256  ->getMock();
257 
258  $usdOrder->expects($this->once())
259  ->method('getBaseGrandTotal')
260  ->willReturn(self::$usdAmount);
261  $usdOrder->expects($this->once())
262  ->method('getBaseCurrencyCode')
263  ->willReturn('USD');
264 
265  return [$usdOrder, $eurOrder, $uahOrder];
266  }
267 }
$searchCriteria