-
Notifications
You must be signed in to change notification settings - Fork 9.4k
[GiftMessage] Cover Observer SalesEventOrderItemToQuoteItemObserver by Unit Test #26082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
magento-engcom-team
merged 2 commits into
magento:2.4-develop
from
edenduong:2.4-testcover/cover_order_item_to_quote
Dec 19, 2019
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
196 changes: 196 additions & 0 deletions
196
...ode/Magento/GiftMessage/Test/Unit/Observer/SalesEventOrderItemToQuoteItemObserverTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\GiftMessage\Test\Unit\Observer; | ||
|
||
use Magento\Framework\Event; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Message\MessageInterface; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use Magento\GiftMessage\Helper\Message as MessageHelper; | ||
use Magento\GiftMessage\Model\Message as MessageModel; | ||
use Magento\GiftMessage\Model\MessageFactory; | ||
use Magento\GiftMessage\Observer\SalesEventOrderItemToQuoteItemObserver; | ||
use Magento\Quote\Model\Quote\Item as QuoteItem; | ||
use Magento\Sales\Model\Order; | ||
use Magento\Sales\Model\Order\Item as OrderItem; | ||
use Magento\Store\Model\Store; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) | ||
*/ | ||
class SalesEventOrderItemToQuoteItemObserverTest extends TestCase | ||
{ | ||
/** | ||
* Stub message id | ||
*/ | ||
private const STUB_MESSAGE_ID = 1; | ||
|
||
/** | ||
* Stub new message id | ||
*/ | ||
private const STUB_NEW_MESSAGE_ID = 2; | ||
|
||
/** | ||
* @var SalesEventOrderItemToQuoteItemObserver | ||
*/ | ||
private $observer; | ||
|
||
/** | ||
* @var MessageFactory|MockObject | ||
*/ | ||
private $messageFactoryMock; | ||
|
||
/** | ||
* @var MessageHelper|MockObject | ||
*/ | ||
private $giftMessageHelperMock; | ||
|
||
/** | ||
* @var Observer|MockObject | ||
*/ | ||
private $observerMock; | ||
|
||
/** | ||
* @var Event|MockObject | ||
*/ | ||
private $eventMock; | ||
|
||
/** | ||
* @var Order|MockObject | ||
*/ | ||
private $orderMock; | ||
|
||
/** | ||
* @var OrderItem|MockObject | ||
*/ | ||
private $orderItemMock; | ||
|
||
/** | ||
* @var Store|MockObject | ||
*/ | ||
private $storeMock; | ||
|
||
/** | ||
* @var MessageInterface|MockObject | ||
*/ | ||
private $messageMock; | ||
|
||
/** | ||
* @var QuoteItem|MockObject | ||
*/ | ||
private $quoteItemMock; | ||
|
||
/** | ||
* Prepare environment for test | ||
*/ | ||
public function setUp(): void | ||
{ | ||
$this->messageFactoryMock = $this->createMock(MessageFactory::class); | ||
$this->giftMessageHelperMock = $this->createMock(MessageHelper::class); | ||
$this->observerMock = $this->createMock(Observer::class); | ||
$this->eventMock = $this->createPartialMock(Event::class, ['getOrderItem', 'getQuoteItem']); | ||
$this->orderItemMock = $this->createPartialMock( | ||
OrderItem::class, | ||
['getOrder', 'getStoreId', 'getGiftMessageId'] | ||
); | ||
$this->quoteItemMock = $this->createPartialMock(QuoteItem::class, ['setGiftMessageId']); | ||
$this->orderMock = $this->createPartialMock(Order::class, ['getReordered']); | ||
$this->storeMock = $this->createMock(Store::class); | ||
$this->messageMock = $this->createMock(MessageModel::class); | ||
|
||
$objectManager = new ObjectManager($this); | ||
|
||
$this->observer = $objectManager->getObject( | ||
SalesEventOrderItemToQuoteItemObserver::class, | ||
[ | ||
'messageFactory' => $this->messageFactoryMock, | ||
'giftMessageMessage' => $this->giftMessageHelperMock | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Tests duplicating gift message from order item to quote item | ||
* | ||
* @param bool $orderIsReordered | ||
* @param bool $isMessagesAllowed | ||
* @dataProvider giftMessageDataProvider | ||
*/ | ||
public function testExecute($orderIsReordered, $isMessagesAllowed) | ||
{ | ||
$this->eventMock->expects($this->atLeastOnce()) | ||
->method('getOrderItem') | ||
->willReturn($this->orderItemMock); | ||
|
||
$this->orderItemMock->expects($this->atLeastOnce()) | ||
->method('getOrder') | ||
->willReturn($this->orderMock); | ||
|
||
$this->observerMock->expects($this->atLeastOnce()) | ||
->method('getEvent') | ||
->willReturn($this->eventMock); | ||
edenduong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (!$orderIsReordered && $isMessagesAllowed) { | ||
edenduong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->eventMock | ||
->expects($this->atLeastOnce()) | ||
->method('getQuoteItem') | ||
->willReturn($this->quoteItemMock); | ||
$this->orderMock->expects($this->once()) | ||
->method('getReordered') | ||
->willReturn($orderIsReordered); | ||
$this->orderItemMock->expects($this->once()) | ||
->method('getGiftMessageId') | ||
->willReturn(self::STUB_MESSAGE_ID); | ||
$this->giftMessageHelperMock->expects($this->once()) | ||
->method('isMessagesAllowed') | ||
->willReturn($isMessagesAllowed); | ||
$this->messageFactoryMock->expects($this->once()) | ||
->method('create') | ||
->willReturn($this->messageMock); | ||
$this->messageMock->expects($this->once()) | ||
->method('load') | ||
->with(self::STUB_MESSAGE_ID) | ||
->willReturnSelf(); | ||
$this->messageMock->expects($this->once()) | ||
->method('setId') | ||
->with(null) | ||
->willReturnSelf(); | ||
$this->messageMock->expects($this->once()) | ||
->method('save') | ||
->willReturnSelf(); | ||
$this->messageMock->expects($this->once()) | ||
->method('getId') | ||
->willReturn(self::STUB_NEW_MESSAGE_ID); | ||
$this->quoteItemMock->expects($this->once()) | ||
->method('setGiftMessageId') | ||
->with(self::STUB_NEW_MESSAGE_ID) | ||
->willReturnSelf(); | ||
} | ||
|
||
/** Run observer */ | ||
$this->observer->execute($this->observerMock); | ||
} | ||
|
||
/** | ||
* Providing gift message data for test | ||
* | ||
* @return array | ||
*/ | ||
public function giftMessageDataProvider() | ||
{ | ||
return [ | ||
'order is not reordered, messages is allowed' => [false, true], | ||
'order is reordered, messages is allowed' => [true, true], | ||
'order is reordered, messages is not allowed' => [true, false], | ||
'order is not reordered, messages is not allowed' => [false, false] | ||
]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.