Skip to content

Commit 5be8f4e

Browse files
committed
MC-33893: 2.3.4 Invoice always sent (if exist) when order placed.
1 parent ce34f98 commit 5be8f4e

File tree

2 files changed

+215
-12
lines changed

2 files changed

+215
-12
lines changed
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Test\Unit\Observer;
9+
10+
use Magento\Framework\Event;
11+
use Magento\Framework\Event\Observer;
12+
use Magento\Quote\Model\Quote;
13+
use Magento\Quote\Model\Quote\Payment;
14+
use Magento\Quote\Observer\SubmitObserver;
15+
use Magento\Sales\Model\Order;
16+
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
17+
use Magento\Sales\Model\Order\Email\Sender\OrderSender;
18+
use Magento\Sales\Model\Order\Invoice;
19+
use Magento\Sales\Model\ResourceModel\Order\Invoice\Collection;
20+
use PHPUnit\Framework\MockObject\MockObject;
21+
use PHPUnit\Framework\TestCase;
22+
use Psr\Log\LoggerInterface;
23+
use Magento\Sales\Model\Order\Email\Container\InvoiceIdentity;
24+
use Magento\Quote\Observer\SendInvoiceEmailObserver;
25+
26+
/**
27+
* Test for sending invoice email during order place on frontend
28+
*
29+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
30+
*/
31+
class SendInvoiceEmailObserverTest extends TestCase
32+
{
33+
/**
34+
* @var SendInvoiceEmailObserver
35+
*/
36+
private $model;
37+
38+
/**
39+
* @var LoggerInterface|MockObject
40+
*/
41+
private $loggerMock;
42+
43+
/**
44+
* @var InvoiceSender|MockObject
45+
*/
46+
private $invoiceSenderMock;
47+
48+
/**
49+
* @var InvoiceIdentity|MockObject
50+
*/
51+
private $invoiceIdentityMock;
52+
53+
/**
54+
* @var Observer|MockObject
55+
*/
56+
private $observerMock;
57+
58+
/**
59+
* @var Quote|MockObject
60+
*/
61+
private $quoteMock;
62+
63+
/**
64+
* @var Order|MockObject
65+
*/
66+
private $orderMock;
67+
68+
/**
69+
* @var Payment|MockObject
70+
*/
71+
private $paymentMock;
72+
73+
/**
74+
* @inheirtDoc
75+
*/
76+
protected function setUp(): void
77+
{
78+
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
79+
$this->quoteMock = $this->createMock(Quote::class);
80+
$this->orderMock = $this->createMock(Order::class);
81+
$this->paymentMock = $this->createMock(Payment::class);
82+
$this->invoiceSenderMock = $this->createMock(InvoiceSender::class);
83+
$this->invoiceIdentityMock = $this->getMockBuilder(InvoiceIdentity::class)
84+
->disableOriginalConstructor()
85+
->setMethods(['isEnabled'])
86+
->getMock();
87+
$eventMock = $this->getMockBuilder(Event::class)
88+
->disableOriginalConstructor()
89+
->setMethods(['getQuote', 'getOrder'])
90+
->getMock();
91+
$this->observerMock = $this->createPartialMock(Observer::class, ['getEvent']);
92+
$this->observerMock->expects($this->any())->method('getEvent')->willReturn($eventMock);
93+
$eventMock->expects($this->any())->method('getQuote')->willReturn($this->quoteMock);
94+
$eventMock->expects($this->any())->method('getOrder')->willReturn($this->orderMock);
95+
$this->quoteMock->expects($this->any())->method('getPayment')->willReturn($this->paymentMock);
96+
$this->model = new SendInvoiceEmailObserver(
97+
$this->loggerMock,
98+
$this->invoiceSenderMock,
99+
$this->invoiceIdentityMock
100+
);
101+
}
102+
103+
/**
104+
* Tests successful email sending.
105+
*/
106+
public function testSendEmail()
107+
{
108+
$this->invoiceIdentityMock
109+
->expects($this->once())
110+
->method('isEnabled')
111+
->willReturn(true);
112+
113+
$this->paymentMock->method('getOrderPlaceRedirectUrl')->willReturn('');
114+
115+
$invoice = $this->createMock(Invoice::class);
116+
$invoiceCollection = $this->createMock(Collection::class);
117+
$invoiceCollection->method('getItems')
118+
->willReturn([$invoice]);
119+
$this->orderMock->method('getInvoiceCollection')
120+
->willReturn($invoiceCollection);
121+
$this->quoteMock
122+
->expects($this->any())
123+
->method('getPayment')
124+
->willReturn($this->paymentMock);
125+
126+
$this->orderMock->method('getCanSendNewEmailFlag')->willReturn(true);
127+
$this->invoiceSenderMock->expects($this->once())
128+
->method('send')
129+
->with($invoice)
130+
->willReturn(true);
131+
$this->loggerMock->expects($this->never())
132+
->method('critical');
133+
134+
$this->model->execute($this->observerMock);
135+
}
136+
137+
/**
138+
* Tests email sending disabled by configuration.
139+
*/
140+
public function testSendEmailDisabled()
141+
{
142+
$this->invoiceIdentityMock
143+
->expects($this->once())
144+
->method('isEnabled')
145+
->willReturn(false);
146+
147+
$this->paymentMock
148+
->expects($this->never())
149+
->method('getOrderPlaceRedirectUrl');
150+
$this->orderMock
151+
->expects($this->never())
152+
->method('getInvoiceCollection');
153+
154+
$this->quoteMock
155+
->expects($this->never())
156+
->method('getPayment');
157+
158+
$this->orderMock
159+
->expects($this->never())
160+
->method('getCanSendNewEmailFlag');
161+
$this->loggerMock->expects($this->never())
162+
->method('critical');
163+
164+
$this->model->execute($this->observerMock);
165+
}
166+
167+
/**
168+
* Tests failing email sending.
169+
*/
170+
public function testFailToSendEmail()
171+
{
172+
$this->invoiceIdentityMock
173+
->expects($this->once())
174+
->method('isEnabled')
175+
->willReturn(true);
176+
$this->paymentMock->expects($this->once())->method('getOrderPlaceRedirectUrl')->willReturn('');
177+
178+
$invoice = $this->createMock(Invoice::class);
179+
$invoiceCollection = $this->createMock(Collection::class);
180+
$invoiceCollection->method('getItems')
181+
->willReturn([$invoice]);
182+
$this->orderMock->method('getInvoiceCollection')
183+
->willReturn($invoiceCollection);
184+
185+
$this->orderMock->expects($this->once())->method('getCanSendNewEmailFlag')->willReturn(true);
186+
$this->invoiceSenderMock->expects($this->once())->method('send')->willThrowException(
187+
new \Exception('Some email sending Error')
188+
);
189+
$this->loggerMock->expects($this->once())->method('critical');
190+
$this->model->execute($this->observerMock);
191+
}
192+
193+
/**
194+
* Tests send email when redirect.
195+
*/
196+
public function testSendEmailWhenRedirectUrlExists()
197+
{
198+
$this->invoiceIdentityMock
199+
->expects($this->once())
200+
->method('isEnabled')
201+
->willReturn(true);
202+
203+
$this->paymentMock->expects($this->once())->method('getOrderPlaceRedirectUrl')->willReturn(false);
204+
$this->orderMock->expects($this->once())->method('getCanSendNewEmailFlag');
205+
$this->invoiceSenderMock->expects($this->never())->method('send');
206+
$this->loggerMock->expects($this->never())->method('critical');
207+
$this->model->execute($this->observerMock);
208+
}
209+
}

app/code/Magento/Quote/Test/Unit/Observer/SubmitObserverTest.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Psr\Log\LoggerInterface;
2323

2424
/**
25+
* Test for sending order email during order place on frontend
26+
*
2527
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2628
*/
2729
class SubmitObserverTest extends TestCase
@@ -41,11 +43,6 @@ class SubmitObserverTest extends TestCase
4143
*/
4244
private $orderSenderMock;
4345

44-
/**
45-
* @var InvoiceSender|MockObject
46-
*/
47-
private $invoiceSender;
48-
4946
/**
5047
* @var Observer|MockObject
5148
*/
@@ -66,14 +63,16 @@ class SubmitObserverTest extends TestCase
6663
*/
6764
private $paymentMock;
6865

66+
/**
67+
* @inheirtDoc
68+
*/
6969
protected function setUp(): void
7070
{
7171
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
7272
$this->quoteMock = $this->createMock(Quote::class);
7373
$this->orderMock = $this->createMock(Order::class);
7474
$this->paymentMock = $this->createMock(Payment::class);
7575
$this->orderSenderMock = $this->createMock(OrderSender::class);
76-
$this->invoiceSender = $this->createMock(InvoiceSender::class);
7776
$eventMock = $this->getMockBuilder(Event::class)
7877
->disableOriginalConstructor()
7978
->setMethods(['getQuote', 'getOrder'])
@@ -85,8 +84,7 @@ protected function setUp(): void
8584
$this->quoteMock->expects($this->once())->method('getPayment')->willReturn($this->paymentMock);
8685
$this->model = new SubmitObserver(
8786
$this->loggerMock,
88-
$this->orderSenderMock,
89-
$this->invoiceSender
87+
$this->orderSenderMock
9088
);
9189
}
9290

@@ -106,10 +104,6 @@ public function testSendEmail()
106104
$this->orderMock->method('getCanSendNewEmailFlag')->willReturn(true);
107105
$this->orderSenderMock->expects($this->once())
108106
->method('send')->willReturn(true);
109-
$this->invoiceSender->expects($this->once())
110-
->method('send')
111-
->with($invoice)
112-
->willReturn(true);
113107
$this->loggerMock->expects($this->never())
114108
->method('critical');
115109

0 commit comments

Comments
 (0)