|
| 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 | +} |
0 commit comments