|
| 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->createMock(MessageFactory::class); |
| 97 | + $this->giftMessageHelperMock = $this->createMock(MessageHelper::class); |
| 98 | + $this->observerMock = $this->createMock(Observer::class); |
| 99 | + $this->eventMock = $this->createPartialMock(Event::class, ['getOrderItem', 'getQuoteItem']); |
| 100 | + $this->orderItemMock = $this->createPartialMock( |
| 101 | + OrderItem::class, |
| 102 | + ['getOrder', 'getStoreId', 'getGiftMessageId'] |
| 103 | + ); |
| 104 | + $this->quoteItemMock = $this->createPartialMock(QuoteItem::class, ['setGiftMessageId']); |
| 105 | + $this->orderMock = $this->createPartialMock(Order::class, ['getReordered']); |
| 106 | + $this->storeMock = $this->createMock(Store::class); |
| 107 | + $this->messageMock = $this->createMock(MessageModel::class); |
| 108 | + |
| 109 | + $objectManager = new ObjectManager($this); |
| 110 | + |
| 111 | + $this->observer = $objectManager->getObject( |
| 112 | + SalesEventOrderItemToQuoteItemObserver::class, |
| 113 | + [ |
| 114 | + 'messageFactory' => $this->messageFactoryMock, |
| 115 | + 'giftMessageMessage' => $this->giftMessageHelperMock |
| 116 | + ] |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Tests duplicating gift message from order item to quote item |
| 122 | + * |
| 123 | + * @param bool $orderIsReordered |
| 124 | + * @param bool $isMessagesAllowed |
| 125 | + * @dataProvider giftMessageDataProvider |
| 126 | + */ |
| 127 | + public function testExecute($orderIsReordered, $isMessagesAllowed) |
| 128 | + { |
| 129 | + $this->eventMock->expects($this->atLeastOnce()) |
| 130 | + ->method('getOrderItem') |
| 131 | + ->willReturn($this->orderItemMock); |
| 132 | + |
| 133 | + $this->orderItemMock->expects($this->atLeastOnce()) |
| 134 | + ->method('getOrder') |
| 135 | + ->willReturn($this->orderMock); |
| 136 | + |
| 137 | + $this->observerMock->expects($this->atLeastOnce()) |
| 138 | + ->method('getEvent') |
| 139 | + ->willReturn($this->eventMock); |
| 140 | + |
| 141 | + if (!$orderIsReordered && $isMessagesAllowed) { |
| 142 | + $this->eventMock |
| 143 | + ->expects($this->atLeastOnce()) |
| 144 | + ->method('getQuoteItem') |
| 145 | + ->willReturn($this->quoteItemMock); |
| 146 | + $this->orderMock->expects($this->once()) |
| 147 | + ->method('getReordered') |
| 148 | + ->willReturn($orderIsReordered); |
| 149 | + $this->orderItemMock->expects($this->once()) |
| 150 | + ->method('getGiftMessageId') |
| 151 | + ->willReturn(self::STUB_MESSAGE_ID); |
| 152 | + $this->giftMessageHelperMock->expects($this->once()) |
| 153 | + ->method('isMessagesAllowed') |
| 154 | + ->willReturn($isMessagesAllowed); |
| 155 | + $this->messageFactoryMock->expects($this->once()) |
| 156 | + ->method('create') |
| 157 | + ->willReturn($this->messageMock); |
| 158 | + $this->messageMock->expects($this->once()) |
| 159 | + ->method('load') |
| 160 | + ->with(self::STUB_MESSAGE_ID) |
| 161 | + ->willReturnSelf(); |
| 162 | + $this->messageMock->expects($this->once()) |
| 163 | + ->method('setId') |
| 164 | + ->with(null) |
| 165 | + ->willReturnSelf(); |
| 166 | + $this->messageMock->expects($this->once()) |
| 167 | + ->method('save') |
| 168 | + ->willReturnSelf(); |
| 169 | + $this->messageMock->expects($this->once()) |
| 170 | + ->method('getId') |
| 171 | + ->willReturn(self::STUB_NEW_MESSAGE_ID); |
| 172 | + $this->quoteItemMock->expects($this->once()) |
| 173 | + ->method('setGiftMessageId') |
| 174 | + ->with(self::STUB_NEW_MESSAGE_ID) |
| 175 | + ->willReturnSelf(); |
| 176 | + } |
| 177 | + |
| 178 | + /** Run observer */ |
| 179 | + $this->observer->execute($this->observerMock); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Providing gift message data for test |
| 184 | + * |
| 185 | + * @return array |
| 186 | + */ |
| 187 | + public function giftMessageDataProvider() |
| 188 | + { |
| 189 | + return [ |
| 190 | + 'order is not reordered, messages is allowed' => [false, true], |
| 191 | + 'order is reordered, messages is allowed' => [true, true], |
| 192 | + 'order is reordered, messages is not allowed' => [true, false], |
| 193 | + 'order is not reordered, messages is not allowed' => [false, false] |
| 194 | + ]; |
| 195 | + } |
| 196 | +} |
0 commit comments