|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\GiftMessage\Test\Unit\Observer; |
| 10 | + |
| 11 | +use Magento\Framework\Event; |
| 12 | +use Magento\Framework\Event\Observer; |
| 13 | +use Magento\Framework\Message\MessageInterface; |
| 14 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 15 | +use Magento\GiftMessage\Helper\Message as MessageHelper; |
| 16 | +use Magento\GiftMessage\Model\Message as MessageModel; |
| 17 | +use Magento\GiftMessage\Model\MessageFactory; |
| 18 | +use Magento\GiftMessage\Observer\SalesEventOrderItemToQuoteItemObserver; |
| 19 | +use Magento\Quote\Model\Quote\Item as QuoteItem; |
| 20 | +use Magento\Sales\Model\Order; |
| 21 | +use Magento\Sales\Model\Order\Item as OrderItem; |
| 22 | +use Magento\Store\Model\Store; |
| 23 | +use PHPUnit\Framework\MockObject\MockObject; |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | + |
| 26 | +/** |
| 27 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 28 | + */ |
| 29 | +class SalesEventOrderItemToQuoteItemObserverTest extends TestCase |
| 30 | +{ |
| 31 | + /** |
| 32 | + * Stub message id |
| 33 | + */ |
| 34 | + private const STUB_MESSAGE_ID = 1; |
| 35 | + |
| 36 | + /** |
| 37 | + * Stub new message id |
| 38 | + */ |
| 39 | + private const STUB_NEW_MESSAGE_ID = 2; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var SalesEventOrderItemToQuoteItemObserver |
| 43 | + */ |
| 44 | + private $observer; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var MessageFactory|MockObject |
| 48 | + */ |
| 49 | + private $messageFactoryMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var MessageHelper|MockObject |
| 53 | + */ |
| 54 | + private $giftMessageHelperMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var Observer|MockObject |
| 58 | + */ |
| 59 | + private $observerMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var Event|MockObject |
| 63 | + */ |
| 64 | + private $eventMock; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var Order|MockObject |
| 68 | + */ |
| 69 | + private $orderMock; |
| 70 | + |
| 71 | + /** |
| 72 | + * @var OrderItem|MockObject |
| 73 | + */ |
| 74 | + private $orderItemMock; |
| 75 | + |
| 76 | + /** |
| 77 | + * @var Store|MockObject |
| 78 | + */ |
| 79 | + private $storeMock; |
| 80 | + |
| 81 | + /** |
| 82 | + * @var MessageInterface|MockObject |
| 83 | + */ |
| 84 | + private $messageMock; |
| 85 | + |
| 86 | + /** |
| 87 | + * @var QuoteItem|MockObject |
| 88 | + */ |
| 89 | + private $quoteItemMock; |
| 90 | + |
| 91 | + /** |
| 92 | + * Prepare environment for test |
| 93 | + */ |
| 94 | + public function setUp(): void |
| 95 | + { |
| 96 | + $this->messageFactoryMock = $this->getMockBuilder(MessageFactory::class) |
| 97 | + ->disableOriginalConstructor() |
| 98 | + ->setMethods(['create']) |
| 99 | + ->getMock(); |
| 100 | + $this->giftMessageHelperMock = $this->createMock(MessageHelper::class); |
| 101 | + $this->observerMock = $this->createMock(Observer::class); |
| 102 | + $this->eventMock = $this->createPartialMock(Event::class, ['getOrderItem', 'getQuoteItem']); |
| 103 | + $this->orderItemMock = $this->createPartialMock( |
| 104 | + OrderItem::class, |
| 105 | + ['getOrder', 'getStoreId', 'getGiftMessageId'] |
| 106 | + ); |
| 107 | + $this->quoteItemMock = $this->createPartialMock(QuoteItem::class, ['setGiftMessageId']); |
| 108 | + $this->orderMock = $this->createPartialMock(Order::class, ['getReordered']); |
| 109 | + $this->storeMock = $this->createMock(Store::class); |
| 110 | + $this->messageMock = $this->createMock(MessageModel::class); |
| 111 | + |
| 112 | + $this->eventMock->expects($this->atLeastOnce()) |
| 113 | + ->method('getOrderItem') |
| 114 | + ->willReturn($this->orderItemMock); |
| 115 | + |
| 116 | + $this->orderItemMock->expects($this->atLeastOnce()) |
| 117 | + ->method('getOrder') |
| 118 | + ->willReturn($this->orderMock); |
| 119 | + |
| 120 | + $this->observerMock->expects($this->atLeastOnce()) |
| 121 | + ->method('getEvent') |
| 122 | + ->willReturn($this->eventMock); |
| 123 | + |
| 124 | + $objectManager = new ObjectManager($this); |
| 125 | + |
| 126 | + $this->observer = $objectManager->getObject( |
| 127 | + SalesEventOrderItemToQuoteItemObserver::class, |
| 128 | + [ |
| 129 | + 'messageFactory' => $this->messageFactoryMock, |
| 130 | + 'giftMessageMessage' => $this->giftMessageHelperMock |
| 131 | + ] |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Test when the order is reorder |
| 137 | + */ |
| 138 | + public function testReorder() |
| 139 | + { |
| 140 | + $this->orderMock->expects($this->once()) |
| 141 | + ->method('getReordered') |
| 142 | + ->willReturn(true); |
| 143 | + |
| 144 | + $this->giftMessageHelperMock->expects($this->never()) |
| 145 | + ->method('isMessagesAllowed'); |
| 146 | + |
| 147 | + $this->eventMock |
| 148 | + ->expects($this->never()) |
| 149 | + ->method('getQuoteItem') |
| 150 | + ->willReturn($this->quoteItemMock); |
| 151 | + |
| 152 | + /** Run observer */ |
| 153 | + $this->observer->execute($this->observerMock); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Test when the order is new reorder and gift message is not allowed |
| 158 | + */ |
| 159 | + public function testNewOrderWhenGiftMessageIsNotAllowed() |
| 160 | + { |
| 161 | + $this->orderMock->expects($this->once()) |
| 162 | + ->method('getReordered') |
| 163 | + ->willReturn(false); |
| 164 | + |
| 165 | + $this->giftMessageHelperMock->expects($this->once()) |
| 166 | + ->method('isMessagesAllowed') |
| 167 | + ->willReturn(false); |
| 168 | + |
| 169 | + $this->eventMock |
| 170 | + ->expects($this->never()) |
| 171 | + ->method('getQuoteItem') |
| 172 | + ->willReturn($this->quoteItemMock); |
| 173 | + |
| 174 | + /** Run observer */ |
| 175 | + $this->observer->execute($this->observerMock); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Test when the order is new reorder and gift message is allowed |
| 180 | + */ |
| 181 | + public function testNewOrderWhenGiftMessageIsAllowed() |
| 182 | + { |
| 183 | + $this->orderMock->expects($this->once()) |
| 184 | + ->method('getReordered') |
| 185 | + ->willReturn(false); |
| 186 | + |
| 187 | + $this->giftMessageHelperMock->expects($this->once()) |
| 188 | + ->method('isMessagesAllowed') |
| 189 | + ->willReturn(true); |
| 190 | + |
| 191 | + $this->eventMock |
| 192 | + ->expects($this->atLeastOnce()) |
| 193 | + ->method('getQuoteItem') |
| 194 | + ->willReturn($this->quoteItemMock); |
| 195 | + |
| 196 | + $this->orderItemMock->expects($this->once()) |
| 197 | + ->method('getGiftMessageId') |
| 198 | + ->willReturn(self::STUB_MESSAGE_ID); |
| 199 | + |
| 200 | + $this->messageFactoryMock->expects($this->once()) |
| 201 | + ->method('create') |
| 202 | + ->willReturn($this->messageMock); |
| 203 | + $this->messageMock->expects($this->once()) |
| 204 | + ->method('load') |
| 205 | + ->with(self::STUB_MESSAGE_ID) |
| 206 | + ->willReturnSelf(); |
| 207 | + $this->messageMock->expects($this->once()) |
| 208 | + ->method('setId') |
| 209 | + ->with(null) |
| 210 | + ->willReturnSelf(); |
| 211 | + $this->messageMock->expects($this->once()) |
| 212 | + ->method('save') |
| 213 | + ->willReturnSelf(); |
| 214 | + $this->messageMock->expects($this->once()) |
| 215 | + ->method('getId') |
| 216 | + ->willReturn(self::STUB_NEW_MESSAGE_ID); |
| 217 | + $this->quoteItemMock->expects($this->once()) |
| 218 | + ->method('setGiftMessageId') |
| 219 | + ->with(self::STUB_NEW_MESSAGE_ID) |
| 220 | + ->willReturnSelf(); |
| 221 | + |
| 222 | + /** Run observer */ |
| 223 | + $this->observer->execute($this->observerMock); |
| 224 | + } |
| 225 | +} |
0 commit comments