123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\GiftMessage\Test\Unit\Model;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Framework\Exception\State\InvalidTransitionException;
- use Magento\Framework\Exception\CouldNotSaveException;
- /**
- * Test class for \Magento\GiftMessage\Model\OrderItemRepository
- * * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class OrderItemRepositoryTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\GiftMessage\Model\OrderItemRepository|\PHPUnit_Framework_MockObject_MockObject
- */
- private $orderItemRepository;
- /**
- * @var \Magento\Sales\Model\OrderFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $orderFactoryMock;
- /**
- * @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject
- */
- private $orderMock;
- /**
- * @var \Magento\GiftMessage\Helper\Message|\PHPUnit_Framework_MockObject_MockObject
- */
- private $helperMock;
- /**
- * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $storeManagerMock;
- /**
- * @var \Magento\Store\Api\Data\StoreInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $storeMock;
- /**
- * @var \Magento\GiftMessage\Model\MessageFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $messageFactoryMock;
- /**
- * @var \Magento\GiftMessage\Model\Save|\PHPUnit_Framework_MockObject_MockObject
- */
- private $giftMessageSaveModelMock;
- protected function setUp()
- {
- $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
- ->disableOriginalConstructor()
- ->setMethods(['load', 'getItemById', 'getIsVirtual'])
- ->getMock();
- $this->orderFactoryMock = $this->getMockBuilder(\Magento\Sales\Model\OrderFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->helperMock = $this->getMockBuilder(\Magento\GiftMessage\Helper\Message::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
- ->disableOriginalConstructor()
- ->setMethods(['getStore'])
- ->getMockForAbstractClass();
- $this->storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
- ->disableOriginalConstructor()
- ->setMethods([])
- ->getMock();
- $this->messageFactoryMock = $this->getMockBuilder(\Magento\GiftMessage\Model\MessageFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->giftMessageSaveModelMock = $this->getMockBuilder(\Magento\GiftMessage\Model\Save::class)
- ->disableOriginalConstructor()
- ->setMethods(['setGiftmessages', 'saveAllInOrder'])
- ->getMock();
- $this->storeManagerMock->expects($this->any())
- ->method('getStore')
- ->willReturn($this->storeMock);
- $this->orderItemRepository = $helper->getObject(
- \Magento\GiftMessage\Model\OrderItemRepository::class,
- [
- 'orderFactory' => $this->orderFactoryMock,
- 'storeManager' => $this->storeManagerMock,
- 'helper' => $this->helperMock,
- 'messageFactory' => $this->messageFactoryMock,
- 'giftMessageSaveModel' => $this->giftMessageSaveModelMock
- ]
- );
- }
- /**
- * @covers \Magento\GiftMessage\Model\OrderItemRepository::get
- */
- public function testGet()
- {
- $orderId = 1;
- $orderItemId = 2;
- $messageId = 3;
- $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(['getGiftMessageId'])
- ->getMock();
- $messageMock = $this->getMockBuilder(\Magento\GiftMessage\Model\Message::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->orderFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->orderMock);
- $this->orderMock->expects($this->once())
- ->method('load')
- ->willReturnSelf();
- $this->orderMock->expects($this->once())
- ->method('getItemById')
- ->with($orderItemId)
- ->willReturn($orderItemMock);
- $this->helperMock->expects($this->once())
- ->method('isMessagesAllowed')
- ->with('order_item', $orderItemMock, $this->storeMock)
- ->willReturn(true);
- $orderItemMock->expects($this->once())
- ->method('getGiftMessageId')
- ->willReturn($messageId);
- $this->messageFactoryMock->expects($this->any())
- ->method('create')
- ->willReturn($messageMock);
- $messageMock->expects($this->once())
- ->method('load')
- ->with($messageId)
- ->willReturnSelf();
- $this->assertEquals($messageMock, $this->orderItemRepository->get($orderId, $orderItemId));
- }
- /**
- * @covers \Magento\GiftMessage\Model\OrderItemRepository::get
- */
- public function testGetNoSuchEntityExceptionOnGetItemById()
- {
- $orderId = 1;
- $orderItemId = 2;
- $this->orderFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->orderMock);
- $this->orderMock->expects($this->once())
- ->method('load')
- ->willReturnSelf();
- $this->orderMock->expects($this->once())
- ->method('getItemById')
- ->with($orderItemId)
- ->willReturn(null);
- $this->helperMock->expects($this->never())->method('isMessagesAllowed');
- try {
- $this->orderItemRepository->get($orderId, $orderItemId);
- $this->fail('Expected NoSuchEntityException not caught');
- } catch (NoSuchEntityException $exception) {
- $this->assertEquals(
- 'No item with the provided ID was found in the Order. Verify the ID and try again.',
- $exception->getMessage()
- );
- }
- }
- /**
- * @covers \Magento\GiftMessage\Model\OrderItemRepository::get
- */
- public function testGetNoSuchEntityExceptionOnIsMessageAllowed()
- {
- $orderId = 1;
- $orderItemId = 2;
- $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(['getGiftMessageId'])
- ->getMock();
- $this->orderFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->orderMock);
- $this->orderMock->expects($this->once())
- ->method('load')
- ->willReturnSelf();
- $this->orderMock->expects($this->once())
- ->method('getItemById')
- ->with($orderItemId)
- ->willReturn($orderItemMock);
- $this->helperMock->expects($this->once())
- ->method('isMessagesAllowed')
- ->with('order_item', $orderItemMock, $this->storeMock)
- ->willReturn(false);
- $orderItemMock->expects($this->never())->method('getGiftMessageId');
- try {
- $this->orderItemRepository->get($orderId, $orderItemId);
- $this->fail('Expected NoSuchEntityException not caught');
- } catch (NoSuchEntityException $exception) {
- $this->assertEquals(
- "No item with the provided ID was found in the Order, or a gift message isn't allowed. "
- . "Verify and try again.",
- $exception->getMessage()
- );
- }
- }
- /**
- * @covers \Magento\GiftMessage\Model\OrderItemRepository::get
- */
- public function testGetNoSuchEntityExceptionOnGetGiftMessageId()
- {
- $orderId = 1;
- $orderItemId = 2;
- $messageId = null;
- $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(['getGiftMessageId'])
- ->getMock();
- $this->orderFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->orderMock);
- $this->orderMock->expects($this->once())
- ->method('load')
- ->willReturnSelf();
- $this->orderMock->expects($this->once())
- ->method('getItemById')
- ->with($orderItemId)
- ->willReturn($orderItemMock);
- $this->helperMock->expects($this->once())
- ->method('isMessagesAllowed')
- ->with('order_item', $orderItemMock, $this->storeMock)
- ->willReturn(true);
- $orderItemMock->expects($this->once())
- ->method('getGiftMessageId')
- ->willReturn($messageId);
- $this->messageFactoryMock->expects($this->never())->method('create');
- try {
- $this->orderItemRepository->get($orderId, $orderItemId);
- $this->fail('Expected NoSuchEntityException not caught');
- } catch (NoSuchEntityException $exception) {
- $this->assertEquals(
- 'No item with the provided ID was found in the Order. Verify the ID and try again.',
- $exception->getMessage()
- );
- }
- }
- /**
- * @covers \Magento\GiftMessage\Model\OrderItemRepository::save
- */
- public function testSave()
- {
- $orderId = 1;
- $orderItemId = 2;
- $message[$orderItemId] = [
- 'type' => 'order_item',
- 'sender' => 'sender_value',
- 'recipient' => 'recipient_value',
- 'message' => 'message_value',
- ];
- $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(['getGiftMessageId'])
- ->getMock();
- $messageMock = $this->getMockBuilder(\Magento\GiftMessage\Api\Data\MessageInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->orderFactoryMock->expects($this->any())
- ->method('create')
- ->willReturn($this->orderMock);
- $this->orderMock->expects($this->any())
- ->method('load')
- ->willReturnSelf();
- $this->orderMock->expects($this->once())
- ->method('getItemById')
- ->with($orderItemId)
- ->willReturn($orderItemMock);
- $this->orderMock->expects($this->once())
- ->method('getIsVirtual')
- ->willReturn(false);
- $this->helperMock->expects($this->once())
- ->method('isMessagesAllowed')
- ->with('order_item', $orderItemMock, $this->storeMock)
- ->willReturn(true);
- $messageMock->expects($this->once())
- ->method('getSender')
- ->willReturn('sender_value');
- $messageMock->expects($this->once())
- ->method('getRecipient')
- ->willReturn('recipient_value');
- $messageMock->expects($this->once())
- ->method('getMessage')
- ->willReturn('message_value');
- $this->giftMessageSaveModelMock->expects($this->once())
- ->method('setGiftmessages')
- ->with($message);
- $this->giftMessageSaveModelMock->expects($this->once())
- ->method('saveAllInOrder');
- $this->assertTrue($this->orderItemRepository->save($orderId, $orderItemId, $messageMock));
- }
- /**
- * @covers \Magento\GiftMessage\Model\OrderItemRepository::save
- */
- public function testSaveNoSuchEntityException()
- {
- $orderId = 1;
- $orderItemId = 2;
- $messageMock = $this->getMockBuilder(\Magento\GiftMessage\Api\Data\MessageInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->orderFactoryMock->expects($this->any())
- ->method('create')
- ->willReturn($this->orderMock);
- $this->orderMock->expects($this->any())
- ->method('load')
- ->willReturnSelf();
- $this->orderMock->expects($this->once())
- ->method('getItemById')
- ->with($orderItemId)
- ->willReturn(null);
- $this->orderMock->expects($this->never())
- ->method('getIsVirtual');
- try {
- $this->orderItemRepository->save($orderId, $orderItemId, $messageMock);
- $this->fail('Expected NoSuchEntityException not caught');
- } catch (NoSuchEntityException $exception) {
- $this->assertEquals(
- 'No item with the provided ID was found in the Order. Verify the ID and try again.',
- $exception->getMessage()
- );
- }
- }
- /**
- * @covers \Magento\GiftMessage\Model\OrderItemRepository::save
- */
- public function testSaveInvalidTransitionException()
- {
- $orderId = 1;
- $orderItemId = 2;
- $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(['getGiftMessageId'])
- ->getMock();
- $messageMock = $this->getMockBuilder(\Magento\GiftMessage\Api\Data\MessageInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->orderFactoryMock->expects($this->any())
- ->method('create')
- ->willReturn($this->orderMock);
- $this->orderMock->expects($this->any())
- ->method('load')
- ->willReturnSelf();
- $this->orderMock->expects($this->once())
- ->method('getItemById')
- ->with($orderItemId)
- ->willReturn($orderItemMock);
- $this->orderMock->expects($this->once())
- ->method('getIsVirtual')
- ->willReturn(true);
- $this->helperMock->expects($this->never())
- ->method('isMessagesAllowed');
- try {
- $this->orderItemRepository->save($orderId, $orderItemId, $messageMock);
- $this->fail('Expected InvalidTransitionException not caught');
- } catch (InvalidTransitionException $exception) {
- $this->assertEquals("Gift messages can't be used for virtual products.", $exception->getMessage());
- }
- }
- /**
- * @covers \Magento\GiftMessage\Model\OrderItemRepository::save
- */
- public function testSaveCouldNotSaveException()
- {
- $orderId = 1;
- $orderItemId = 2;
- $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(['getGiftMessageId'])
- ->getMock();
- $messageMock = $this->getMockBuilder(\Magento\GiftMessage\Api\Data\MessageInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->orderFactoryMock->expects($this->any())
- ->method('create')
- ->willReturn($this->orderMock);
- $this->orderMock->expects($this->any())
- ->method('load')
- ->willReturnSelf();
- $this->orderMock->expects($this->once())
- ->method('getItemById')
- ->with($orderItemId)
- ->willReturn($orderItemMock);
- $this->orderMock->expects($this->once())
- ->method('getIsVirtual')
- ->willReturn(false);
- $this->helperMock->expects($this->once())
- ->method('isMessagesAllowed')
- ->with('order_item', $orderItemMock, $this->storeMock)
- ->willReturn(false);
- $messageMock->expects($this->never())
- ->method('getSender');
- try {
- $this->orderItemRepository->save($orderId, $orderItemId, $messageMock);
- $this->fail('Expected CouldNotSaveException not caught');
- } catch (CouldNotSaveException $exception) {
- $this->assertEquals("The gift message isn't available.", $exception->getMessage());
- }
- }
- /**
- * @covers \Magento\GiftMessage\Model\OrderItemRepository::save
- */
- public function testSaveCouldNotSaveExceptionOnSaveAllInOrder()
- {
- $orderId = 1;
- $orderItemId = 2;
- $message[$orderItemId] = [
- 'type' => 'order_item',
- 'sender' => 'sender_value',
- 'recipient' => 'recipient_value',
- 'message' => 'message_value',
- ];
- $excep = new \Exception('Exception message');
- $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(['getGiftMessageId'])
- ->getMock();
- $messageMock = $this->getMockBuilder(\Magento\GiftMessage\Api\Data\MessageInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->orderFactoryMock->expects($this->any())
- ->method('create')
- ->willReturn($this->orderMock);
- $this->orderMock->expects($this->any())
- ->method('load')
- ->willReturnSelf();
- $this->orderMock->expects($this->once())
- ->method('getItemById')
- ->with($orderItemId)
- ->willReturn($orderItemMock);
- $this->orderMock->expects($this->once())
- ->method('getIsVirtual')
- ->willReturn(false);
- $this->helperMock->expects($this->once())
- ->method('isMessagesAllowed')
- ->with('order_item', $orderItemMock, $this->storeMock)
- ->willReturn(true);
- $messageMock->expects($this->once())
- ->method('getSender')
- ->willReturn('sender_value');
- $messageMock->expects($this->once())
- ->method('getRecipient')
- ->willReturn('recipient_value');
- $messageMock->expects($this->once())
- ->method('getMessage')
- ->willReturn('message_value');
- $this->giftMessageSaveModelMock->expects($this->once())
- ->method('setGiftmessages')
- ->with($message);
- $this->giftMessageSaveModelMock->expects($this->once())
- ->method('saveAllInOrder')
- ->will($this->throwException($excep));
- try {
- $this->orderItemRepository->save($orderId, $orderItemId, $messageMock);
- $this->fail('Expected CouldNotSaveException not caught');
- } catch (CouldNotSaveException $exception) {
- $this->assertEquals(
- 'The gift message couldn\'t be added to the "' . $excep->getMessage() . '" order.',
- $exception->getMessage()
- );
- }
- }
- }
|