1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Quote\Test\Unit\Observer;
class SubmitObserverTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Quote\Observer\SubmitObserver
*/
protected $model;
/**
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $loggerMock;
/**
* @var \Magento\Sales\Model\Order\Email\Sender\OrderSender|\PHPUnit_Framework_MockObject_MockObject
*/
protected $orderSenderMock;
/**
* @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject
*/
protected $observerMock;
/**
* @var \Magento\Quote\Model\Quote|\PHPUnit_Framework_MockObject_MockObject
*/
protected $quoteMock;
/**
* @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject
*/
protected $orderMock;
/**
* @var \Magento\Quote\Model\Quote\Payment|\PHPUnit_Framework_MockObject_MockObject
*/
protected $paymentMock;
protected function setUp()
{
$this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
$this->quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
$this->orderMock = $this->createMock(\Magento\Sales\Model\Order::class);
$this->paymentMock = $this->createMock(\Magento\Quote\Model\Quote\Payment::class);
$this->orderSenderMock =
$this->createMock(\Magento\Sales\Model\Order\Email\Sender\OrderSender::class);
$eventMock = $this->getMockBuilder(\Magento\Framework\Event::class)
->disableOriginalConstructor()
->setMethods(['getQuote', 'getOrder'])
->getMock();
$this->observerMock = $this->createPartialMock(\Magento\Framework\Event\Observer::class, ['getEvent']);
$this->observerMock->expects($this->any())->method('getEvent')->willReturn($eventMock);
$eventMock->expects($this->once())->method('getQuote')->willReturn($this->quoteMock);
$eventMock->expects($this->once())->method('getOrder')->willReturn($this->orderMock);
$this->quoteMock->expects($this->once())->method('getPayment')->willReturn($this->paymentMock);
$this->model = new \Magento\Quote\Observer\SubmitObserver(
$this->loggerMock,
$this->orderSenderMock
);
}
public function testSendEmail()
{
$this->paymentMock->expects($this->once())->method('getOrderPlaceRedirectUrl')->willReturn('');
$this->orderMock->expects($this->once())->method('getCanSendNewEmailFlag')->willReturn(true);
$this->orderSenderMock->expects($this->once())->method('send')->willReturn(true);
$this->loggerMock->expects($this->never())->method('critical');
$this->model->execute($this->observerMock);
}
public function testFailToSendEmail()
{
$this->paymentMock->expects($this->once())->method('getOrderPlaceRedirectUrl')->willReturn('');
$this->orderMock->expects($this->once())->method('getCanSendNewEmailFlag')->willReturn(true);
$this->orderSenderMock->expects($this->once())->method('send')->willThrowException(
new \Exception('Some email sending Error')
);
$this->loggerMock->expects($this->once())->method('critical');
$this->model->execute($this->observerMock);
}
public function testSendEmailWhenRedirectUrlExists()
{
$this->paymentMock->expects($this->once())->method('getOrderPlaceRedirectUrl')->willReturn(false);
$this->orderMock->expects($this->once())->method('getCanSendNewEmailFlag');
$this->orderSenderMock->expects($this->never())->method('send');
$this->loggerMock->expects($this->never())->method('critical');
$this->model->execute($this->observerMock);
}
}