Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OrderServiceTest.php
Go to the documentation of this file.
1 <?php
7 
13 class OrderServiceTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $orderService;
19 
24 
29 
34 
39 
43  protected $filterBuilderMock;
44 
48  protected $filterMock;
49 
53  protected $orderNotifierMock;
54 
58  protected $orderMock;
59 
64 
69 
73  protected $eventManagerMock;
74 
79 
80  protected function setUp()
81  {
82  $this->orderRepositoryMock = $this->getMockBuilder(
83  \Magento\Sales\Api\OrderRepositoryInterface::class
84  )
85  ->disableOriginalConstructor()
86  ->getMock();
87  $this->orderStatusHistoryRepositoryMock = $this->getMockBuilder(
88  \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface::class
89  )
90  ->disableOriginalConstructor()
91  ->getMock();
92  $this->searchCriteriaBuilderMock = $this->getMockBuilder(
93  \Magento\Framework\Api\SearchCriteriaBuilder::class
94  )
95  ->disableOriginalConstructor()
96  ->getMock();
97  $this->searchCriteriaMock = $this->getMockBuilder(
98  \Magento\Framework\Api\SearchCriteria::class
99  )
100  ->disableOriginalConstructor()
101  ->getMock();
102  $this->filterBuilderMock = $this->getMockBuilder(
103  \Magento\Framework\Api\FilterBuilder::class
104  )
105  ->disableOriginalConstructor()
106  ->getMock();
107  $this->filterMock = $this->getMockBuilder(
108  \Magento\Framework\Api\Filter::class
109  )
110  ->disableOriginalConstructor()
111  ->getMock();
112  $this->orderNotifierMock = $this->getMockBuilder(
113  \Magento\Sales\Model\OrderNotifier::class
114  )
115  ->disableOriginalConstructor()
116  ->getMock();
117  $this->orderMock = $this->getMockBuilder(
118  \Magento\Sales\Model\Order::class
119  )
120  ->disableOriginalConstructor()
121  ->getMock();
122  $this->orderStatusHistoryMock = $this->getMockBuilder(
123  \Magento\Sales\Model\Order\Status\History::class
124  )
125  ->disableOriginalConstructor()
126  ->getMock();
127  $this->orderSearchResultMock = $this->getMockBuilder(
128  \Magento\Sales\Api\Data\OrderStatusHistorySearchResultInterface::class
129  )
130  ->disableOriginalConstructor()
131  ->getMock();
132  $this->eventManagerMock = $this->getMockBuilder(
133  \Magento\Framework\Event\ManagerInterface::class
134  )
135  ->disableOriginalConstructor()
136  ->getMock();
137  $this->orderCommentSender = $this->getMockBuilder(
138  \Magento\Sales\Model\Order\Email\Sender\OrderCommentSender::class
139  )
140  ->disableOriginalConstructor()
141  ->getMock();
142 
143  $this->orderService = new \Magento\Sales\Model\Service\OrderService(
144  $this->orderRepositoryMock,
145  $this->orderStatusHistoryRepositoryMock,
146  $this->searchCriteriaBuilderMock,
147  $this->filterBuilderMock,
148  $this->orderNotifierMock,
149  $this->eventManagerMock,
150  $this->orderCommentSender
151  );
152  }
153 
157  public function testCancel()
158  {
159  $this->orderRepositoryMock->expects($this->once())
160  ->method('get')
161  ->with(123)
162  ->willReturn($this->orderMock);
163  $this->orderMock->expects($this->once())
164  ->method('cancel')
165  ->willReturn($this->orderMock);
166  $this->orderMock->expects($this->once())
167  ->method('canCancel')
168  ->willReturn(true);
169  $this->assertTrue($this->orderService->cancel(123));
170  }
171 
175  public function testCancelFailed()
176  {
177  $this->orderRepositoryMock->expects($this->once())
178  ->method('get')
179  ->with(123)
180  ->willReturn($this->orderMock);
181  $this->orderMock->expects($this->never())
182  ->method('cancel')
183  ->willReturn($this->orderMock);
184  $this->orderMock->expects($this->once())
185  ->method('canCancel')
186  ->willReturn(false);
187  $this->assertFalse($this->orderService->cancel(123));
188  }
189 
190  public function testGetCommentsList()
191  {
192  $this->filterBuilderMock->expects($this->once())
193  ->method('setField')
194  ->with('parent_id')
195  ->willReturnSelf();
196  $this->filterBuilderMock->expects($this->once())
197  ->method('setValue')
198  ->with(123)
199  ->willReturnSelf();
200  $this->filterBuilderMock->expects($this->once())
201  ->method('setConditionType')
202  ->with('eq')
203  ->willReturnSelf();
204  $this->filterBuilderMock->expects($this->once())
205  ->method('create')
206  ->willReturn($this->filterMock);
207  $this->searchCriteriaBuilderMock->expects($this->once())
208  ->method('addFilters')
209  ->with([$this->filterMock])
210  ->willReturn($this->filterBuilderMock);
211  $this->searchCriteriaBuilderMock->expects($this->once())
212  ->method('create')
213  ->willReturn($this->searchCriteriaMock);
214  $this->orderStatusHistoryRepositoryMock->expects($this->once())
215  ->method('getList')
216  ->with($this->searchCriteriaMock)
217  ->willReturn($this->orderSearchResultMock);
218  $this->assertEquals($this->orderSearchResultMock, $this->orderService->getCommentsList(123));
219  }
220 
221  public function testAddComment()
222  {
223  $clearComment = "Comment text here...";
224  $this->orderRepositoryMock->expects($this->once())
225  ->method('get')
226  ->with(123)
227  ->willReturn($this->orderMock);
228  $this->orderMock->expects($this->once())
229  ->method('addStatusHistory')
230  ->with($this->orderStatusHistoryMock)
231  ->willReturn($this->orderMock);
232  $this->orderStatusHistoryMock->expects($this->once())
233  ->method('getComment')
234  ->willReturn("<h1>" . $clearComment);
235  $this->orderRepositoryMock->expects($this->once())
236  ->method('save')
237  ->with($this->orderMock)
238  ->willReturn([]);
239  $this->orderCommentSender->expects($this->once())
240  ->method('send')
241  ->with($this->orderMock, false, $clearComment);
242  $this->assertTrue($this->orderService->addComment(123, $this->orderStatusHistoryMock));
243  }
244 
245  public function testNotify()
246  {
247  $this->orderRepositoryMock->expects($this->once())
248  ->method('get')
249  ->with(123)
250  ->willReturn($this->orderMock);
251  $this->orderNotifierMock->expects($this->once())
252  ->method('notify')
253  ->with($this->orderMock)
254  ->willReturn(true);
255  $this->assertTrue($this->orderService->notify(123));
256  }
257 
258  public function testGetStatus()
259  {
260  $this->orderRepositoryMock->expects($this->once())
261  ->method('get')
262  ->with(123)
263  ->willReturn($this->orderMock);
264  $this->orderMock->expects($this->once())
265  ->method('getStatus')
266  ->willReturn('test-status');
267  $this->assertEquals('test-status', $this->orderService->getStatus(123));
268  }
269 
270  public function testHold()
271  {
272  $this->orderRepositoryMock->expects($this->once())
273  ->method('get')
274  ->with(123)
275  ->willReturn($this->orderMock);
276  $this->orderRepositoryMock->expects($this->once())
277  ->method('save')
278  ->with($this->orderMock)
279  ->willReturn($this->orderMock);
280  $this->orderMock->expects($this->once())
281  ->method('hold')
282  ->willReturn($this->orderMock);
283  $this->assertTrue($this->orderService->hold(123));
284  }
285 
286  public function testUnHold()
287  {
288  $this->orderRepositoryMock->expects($this->once())
289  ->method('get')
290  ->with(123)
291  ->willReturn($this->orderMock);
292  $this->orderRepositoryMock->expects($this->once())
293  ->method('save')
294  ->with($this->orderMock)
295  ->willReturn($this->orderMock);
296  $this->orderMock->expects($this->once())
297  ->method('unHold')
298  ->willReturn($this->orderMock);
299  $this->assertTrue($this->orderService->unHold(123));
300  }
301 }