123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <?php
- /**
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\GiftMessage\Test\Unit\Model;
- use Magento\GiftMessage\Model\GiftMessageManager;
- class GiftMessageManagerTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var GiftMessageManager
- */
- protected $model;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $messageFactoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $quoteMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $quoteItemMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $quoteAddressMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $quoteAddressItemMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $giftMessageMock;
- protected function setUp()
- {
- $this->messageFactoryMock =
- $this->createPartialMock(\Magento\GiftMessage\Model\MessageFactory::class, ['create', '__wakeup']);
- $this->quoteMock = $this->createPartialMock(
- \Magento\Quote\Model\Quote::class,
- [
- 'setGiftMessageId',
- 'getGiftMessageId',
- 'save',
- 'getItemById',
- 'getAddressById',
- 'getBillingAddress',
- 'getShippingAddress',
- '__wakeup',
- 'getCustomerId'
- ]
- );
- $this->quoteItemMock = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Item::class,
- [
- 'setGiftMessageId',
- 'getGiftMessageId',
- 'save',
- '__wakeup'
- ]
- );
- $this->quoteAddressMock = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Address::class,
- [
- 'getGiftMessageId',
- 'setGiftMessageId',
- 'getItemById',
- 'save',
- '__wakeup'
- ]
- );
- $this->quoteAddressItemMock = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Address\Item::class,
- [
- 'getGiftMessageId',
- 'setGiftMessageId',
- 'save',
- '__wakeup'
- ]
- );
- $this->giftMessageMock = $this->createPartialMock(
- \Magento\GiftMessage\Model\Message::class,
- [
- 'setSender',
- 'setRecipient',
- 'setMessage',
- 'setCustomerId',
- 'getSender',
- 'getRecipient',
- 'getMessage',
- 'getId',
- 'delete',
- 'save',
- '__wakeup',
- 'load'
- ]
- );
- $this->model = new \Magento\GiftMessage\Model\GiftMessageManager($this->messageFactoryMock);
- }
- public function testAddWhenGiftMessagesIsNoArray()
- {
- $giftMessages = '';
- $this->messageFactoryMock->expects($this->never())->method('create');
- $this->model->add($giftMessages, $this->quoteMock);
- }
- public function testAddWithSaveMessageIdAndEmptyMessageException()
- {
- $giftMessages = [
- 'quote' => [
- 0 => [
- 'from' => 'sender',
- 'to' => 'recipient',
- 'message' => ' ',
- ],
- ],
- ];
- $this->messageFactoryMock->expects($this->once())
- ->method('create')
- ->will($this->returnValue($this->giftMessageMock));
- $this->quoteMock->expects($this->once())->method('getGiftMessageId')->will($this->returnValue(null));
- $this->giftMessageMock->expects($this->never())->method('load');
- $this->giftMessageMock->expects($this->once())->method('getId')->will($this->returnValue(1));
- $this->giftMessageMock->expects($this->once())->method('delete');
- $this->quoteMock->expects($this->once())
- ->method('setGiftMessageId')
- ->with(0)
- ->will($this->returnValue($this->quoteMock));
- $exception = new \Exception();
- $this->quoteMock->expects($this->once())->method('save')->will($this->throwException($exception));
- $this->model->add($giftMessages, $this->quoteMock);
- }
- public function testAddWithSaveMessageIdException()
- {
- $entityId = 12;
- $giftMessages = [
- 'quote_item' => [
- 12 => [
- 'from' => 'sender',
- 'to' => 'recipient',
- 'message' => 'message',
- ],
- ],
- ];
- $customerId = 42;
- $this->messageFactoryMock->expects($this->once())
- ->method('create')
- ->will($this->returnValue($this->giftMessageMock));
- $this->quoteMock->expects($this->once())
- ->method('getItemById')
- ->with($entityId)
- ->will($this->returnValue($this->quoteItemMock));
- $this->quoteItemMock->expects($this->once())->method('getGiftMessageId')->will($this->returnValue(null));
- $this->giftMessageMock->expects($this->once())
- ->method('setSender')
- ->with('sender')
- ->will($this->returnValue($this->giftMessageMock));
- $this->giftMessageMock->expects($this->once())
- ->method('setRecipient')
- ->with('recipient')
- ->will($this->returnValue($this->giftMessageMock));
- $this->quoteMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
- $this->giftMessageMock->expects($this->once())
- ->method('setCustomerId')
- ->with($customerId)
- ->will($this->returnValue($this->giftMessageMock));
- $this->giftMessageMock->expects($this->once())
- ->method('setMessage')
- ->with('message')
- ->will($this->returnValue($this->giftMessageMock));
- $this->giftMessageMock->expects($this->once())->method('save');
- $this->giftMessageMock->expects($this->once())->method('getId')->will($this->returnValue(33));
- $this->quoteItemMock->expects($this->once())
- ->method('setGiftMessageId')
- ->with(33)
- ->will($this->returnValue($this->quoteItemMock));
- $exception = new \Exception();
- $this->quoteItemMock->expects($this->once())->method('save')->will($this->throwException($exception));
- $this->model->add($giftMessages, $this->quoteMock);
- }
- public function testAddWithQuoteAddress()
- {
- $entityId = 1;
- $giftMessages = [
- 'quote_address' => [
- 1 => [
- 'from' => 'sender',
- 'to' => 'recipient',
- 'message' => 'message',
- ],
- ],
- ];
- $customerId = 42;
- $this->messageFactoryMock->expects($this->once())
- ->method('create')
- ->will($this->returnValue($this->giftMessageMock));
- $this->quoteMock->expects($this->once())
- ->method('getAddressById')
- ->with($entityId)
- ->will($this->returnValue($this->quoteAddressMock));
- $this->quoteAddressMock->expects($this->once())->method('getGiftMessageId')->will($this->returnValue(null));
- $this->giftMessageMock->expects($this->once())
- ->method('setSender')
- ->with('sender')
- ->will($this->returnValue($this->giftMessageMock));
- $this->giftMessageMock->expects($this->once())
- ->method('setRecipient')
- ->with('recipient')
- ->will($this->returnValue($this->giftMessageMock));
- $this->giftMessageMock->expects($this->once())
- ->method('setMessage')
- ->with('message')
- ->will($this->returnValue($this->giftMessageMock));
- $this->quoteMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
- $this->giftMessageMock->expects($this->once())
- ->method('setCustomerId')
- ->with($customerId)
- ->will($this->returnValue($this->giftMessageMock));
- $this->giftMessageMock->expects($this->once())->method('save');
- $this->giftMessageMock->expects($this->once())->method('getId')->will($this->returnValue(33));
- $this->quoteAddressMock->expects($this->once())
- ->method('setGiftMessageId')
- ->with(33)
- ->will($this->returnValue($this->quoteAddressMock));
- $this->quoteAddressMock->expects($this->once())->method('save');
- $this->model->add($giftMessages, $this->quoteMock);
- }
- public function testAddWithQuoteAddressItem()
- {
- $entityId = 1;
- $giftMessages = [
- 'quote_address_item' => [
- 1 => [
- 'from' => 'sender',
- 'to' => 'recipient',
- 'message' => 'message',
- 'address' => 'address',
- ],
- ],
- ];
- $customerId = 42;
- $this->messageFactoryMock->expects($this->once())
- ->method('create')
- ->will($this->returnValue($this->giftMessageMock));
- $this->quoteMock->expects($this->once())
- ->method('getAddressById')
- ->with('address')
- ->will($this->returnValue($this->quoteAddressMock));
- $this->quoteAddressMock->expects($this->once())
- ->method('getItemById')
- ->with($entityId)
- ->will($this->returnValue($this->quoteAddressItemMock));
- $this->quoteAddressItemMock->expects($this->once())->method('getGiftMessageId')->will($this->returnValue(0));
- $this->giftMessageMock->expects($this->once())
- ->method('setSender')
- ->with('sender')
- ->will($this->returnValue($this->giftMessageMock));
- $this->giftMessageMock->expects($this->once())
- ->method('setRecipient')
- ->with('recipient')
- ->will($this->returnValue($this->giftMessageMock));
- $this->giftMessageMock->expects($this->once())
- ->method('setMessage')
- ->with('message')
- ->will($this->returnValue($this->giftMessageMock));
- $this->quoteMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
- $this->giftMessageMock->expects($this->once())
- ->method('setCustomerId')
- ->with($customerId)
- ->will($this->returnValue($this->giftMessageMock));
- $this->giftMessageMock->expects($this->once())->method('save');
- $this->giftMessageMock->expects($this->once())->method('getId')->will($this->returnValue(33));
- $this->quoteAddressItemMock->expects($this->once())
- ->method('setGiftMessageId')
- ->with(33)
- ->will($this->returnValue($this->quoteAddressItemMock));
- $this->quoteAddressItemMock->expects($this->once())
- ->method('save')
- ->will($this->returnValue($this->quoteAddressItemMock));
- $this->model->add($giftMessages, $this->quoteMock);
- }
- /**
- * @expectedException \Magento\Framework\Exception\CouldNotSaveException
- * @expectedExceptionMessage The gift message couldn't be added to Cart.
- */
- public function testSetMessageCouldNotAddGiftMessageException()
- {
- $this->giftMessageMock->expects($this->once())->method('getSender')->will($this->returnValue('sender'));
- $this->giftMessageMock->expects($this->once())->method('getRecipient')->will($this->returnValue('recipient'));
- $this->giftMessageMock->expects($this->once())->method('getMessage')->will($this->returnValue('Message'));
- $this->messageFactoryMock->expects($this->once())
- ->method('create')
- ->willThrowException(new \Exception());
- $this->model->setMessage($this->quoteMock, 'item', $this->giftMessageMock);
- }
- }
|