|
| 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\Model\Plugin; |
| 10 | + |
| 11 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 12 | +use Magento\GiftMessage\Model\Plugin\MergeQuoteItems; |
| 13 | +use Magento\Quote\Model\Quote\Item; |
| 14 | +use Magento\Quote\Model\Quote\Item\Processor; |
| 15 | +use PHPUnit\Framework\MockObject\MockObject; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * Unit Test for \Magento\GiftMessage\Model\Plugin\MergeQuoteItems |
| 20 | + */ |
| 21 | +class MergeQuoteItemsTest extends TestCase |
| 22 | +{ |
| 23 | + private const STUB_GIFT_MESSAGE = 'message'; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var MergeQuoteItems |
| 27 | + */ |
| 28 | + private $plugin; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var Processor|MockObject |
| 32 | + */ |
| 33 | + private $processorMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var Item|MockObject |
| 37 | + */ |
| 38 | + private $resultMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var Item|MockObject |
| 42 | + */ |
| 43 | + private $sourceMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * @inheritDoc |
| 47 | + */ |
| 48 | + protected function setUp(): void |
| 49 | + { |
| 50 | + $this->plugin = (new ObjectManagerHelper($this))->getObject(MergeQuoteItems::class); |
| 51 | + $this->processorMock = $this->createMock(Processor::class); |
| 52 | + $this->resultMock = $this->createPartialMock(Item::class, ['setGiftMessageId']); |
| 53 | + $this->sourceMock = $this->createPartialMock(Item::class, ['getGiftMessageId']); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Test case when a source item has a Gift message. |
| 58 | + */ |
| 59 | + public function testAfterMergeExpectsSetGiftMessageIdCalled(): void |
| 60 | + { |
| 61 | + $this->sourceMock->expects($this->once()) |
| 62 | + ->method('getGiftMessageId') |
| 63 | + ->willReturn(self::STUB_GIFT_MESSAGE); |
| 64 | + $this->resultMock->expects($this->once()) |
| 65 | + ->method('setGiftMessageId') |
| 66 | + ->with(self::STUB_GIFT_MESSAGE); |
| 67 | + |
| 68 | + $this->assertSame( |
| 69 | + $this->resultMock, |
| 70 | + $this->plugin->afterMerge($this->processorMock, $this->resultMock, $this->sourceMock) |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Test case when a source item doesn't have a Gift message. |
| 76 | + */ |
| 77 | + public function testAfterMergeWithoutGiftMessageId(): void |
| 78 | + { |
| 79 | + $this->sourceMock->expects($this->once())->method('getGiftMessageId')->willReturn(null); |
| 80 | + $this->resultMock->expects($this->never())->method('setGiftMessageId'); |
| 81 | + |
| 82 | + $this->assertSame( |
| 83 | + $this->resultMock, |
| 84 | + $this->plugin->afterMerge($this->processorMock, $this->resultMock, $this->sourceMock) |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
0 commit comments