GuestItemRepositoryTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\GiftMessage\Test\Unit\Model;
  8. use Magento\GiftMessage\Model\ItemRepository;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class GuestItemRepositoryTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var ItemRepository
  16. */
  17. protected $itemRepository;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $quoteRepositoryMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $messageFactoryMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $quoteMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $messageMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $quoteItemMock;
  38. /**
  39. * @var string
  40. */
  41. protected $cartId = 13;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $storeManagerMock;
  46. /**
  47. * @var \PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $giftMessageManagerMock;
  50. /**
  51. * @var \PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $helperMock;
  54. /**
  55. * @var \PHPUnit_Framework_MockObject_MockObject
  56. */
  57. protected $storeMock;
  58. protected function setUp()
  59. {
  60. $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
  61. $this->messageFactoryMock = $this->createPartialMock(
  62. \Magento\GiftMessage\Model\MessageFactory::class,
  63. [
  64. 'create',
  65. '__wakeup'
  66. ]
  67. );
  68. $this->messageMock = $this->createMock(\Magento\GiftMessage\Model\Message::class);
  69. $this->quoteItemMock = $this->createPartialMock(
  70. \Magento\Quote\Model\Quote\Item::class,
  71. [
  72. 'getGiftMessageId',
  73. '__wakeup'
  74. ]
  75. );
  76. $this->quoteMock = $this->createPartialMock(
  77. \Magento\Quote\Model\Quote::class,
  78. [
  79. 'getGiftMessageId',
  80. 'getItemById',
  81. '__wakeup',
  82. ]
  83. );
  84. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  85. $this->giftMessageManagerMock =
  86. $this->createMock(\Magento\GiftMessage\Model\GiftMessageManager::class);
  87. $this->helperMock = $this->createMock(\Magento\GiftMessage\Helper\Message::class);
  88. $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  89. $this->itemRepository = new \Magento\GiftMessage\Model\ItemRepository(
  90. $this->quoteRepositoryMock,
  91. $this->storeManagerMock,
  92. $this->giftMessageManagerMock,
  93. $this->helperMock,
  94. $this->messageFactoryMock
  95. );
  96. $this->quoteRepositoryMock->expects($this->once())
  97. ->method('getActive')
  98. ->with($this->cartId)
  99. ->will($this->returnValue($this->quoteMock));
  100. }
  101. /**
  102. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  103. * @expectedExceptionMessage No item with the provided ID was found in the Cart. Verify the ID and try again.
  104. */
  105. public function testGetWithNoSuchEntityException()
  106. {
  107. $itemId = 2;
  108. $this->quoteMock->expects($this->once())->method('getItemById')->with($itemId)->will($this->returnValue(null));
  109. $this->itemRepository->get($this->cartId, $itemId);
  110. }
  111. public function testGetWithoutMessageId()
  112. {
  113. $messageId = 0;
  114. $itemId = 2;
  115. $this->quoteMock->expects($this->once())
  116. ->method('getItemById')
  117. ->with($itemId)
  118. ->will($this->returnValue($this->quoteItemMock));
  119. $this->quoteItemMock->expects($this->once())->method('getGiftMessageId')->will($this->returnValue($messageId));
  120. $this->assertNull($this->itemRepository->get($this->cartId, $itemId));
  121. }
  122. public function testGet()
  123. {
  124. $messageId = 123;
  125. $itemId = 2;
  126. $this->quoteMock->expects($this->once())
  127. ->method('getItemById')
  128. ->with($itemId)
  129. ->will($this->returnValue($this->quoteItemMock));
  130. $this->quoteItemMock->expects($this->once())->method('getGiftMessageId')->will($this->returnValue($messageId));
  131. $this->messageFactoryMock->expects($this->once())
  132. ->method('create')
  133. ->will($this->returnValue($this->messageMock));
  134. $this->messageMock->expects($this->once())
  135. ->method('load')
  136. ->with($messageId)
  137. ->will($this->returnValue($this->messageMock));
  138. $this->assertEquals($this->messageMock, $this->itemRepository->get($this->cartId, $itemId));
  139. }
  140. /**
  141. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  142. */
  143. public function testSaveWithNoSuchEntityException()
  144. {
  145. $itemId = 1;
  146. $this->quoteMock->expects($this->once())->method('getItemById')->with($itemId)->will($this->returnValue(null));
  147. $this->itemRepository->save($this->cartId, $this->messageMock, $itemId);
  148. $this->expectExceptionMessage(
  149. 'No product with the "1" itemId exists in the Cart. Verify your information and try again.'
  150. );
  151. }
  152. /**
  153. * @expectedException \Magento\Framework\Exception\State\InvalidTransitionException
  154. * @expectedExceptionMessage Gift messages can't be used for virtual products.
  155. */
  156. public function testSaveWithInvalidTransitionException()
  157. {
  158. $itemId = 1;
  159. $quoteItem = $this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, ['getIsVirtual', '__wakeup']);
  160. $this->quoteMock->expects($this->once())
  161. ->method('getItemById')
  162. ->with($itemId)
  163. ->will($this->returnValue($quoteItem));
  164. $quoteItem->expects($this->once())->method('getIsVirtual')->will($this->returnValue(1));
  165. $this->itemRepository->save($this->cartId, $this->messageMock, $itemId);
  166. }
  167. public function testSave()
  168. {
  169. $itemId = 1;
  170. $quoteItem = $this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, ['getIsVirtual', '__wakeup']);
  171. $this->quoteMock->expects($this->once())
  172. ->method('getItemById')
  173. ->with($itemId)
  174. ->will($this->returnValue($quoteItem));
  175. $quoteItem->expects($this->once())->method('getIsVirtual')->will($this->returnValue(0));
  176. $this->storeManagerMock->expects($this->once())->method('getStore')->will($this->returnValue($this->storeMock));
  177. $this->helperMock->expects($this->once())
  178. ->method('isMessagesAllowed')
  179. ->with('items', $this->quoteMock, $this->storeMock)
  180. ->will($this->returnValue(true));
  181. $this->giftMessageManagerMock->expects($this->once())
  182. ->method('setMessage')
  183. ->with($this->quoteMock, 'quote_item', $this->messageMock, $itemId)
  184. ->will($this->returnValue($this->giftMessageManagerMock));
  185. $this->messageMock->expects($this->once())->method('getMessage')->willReturn('message');
  186. $this->assertTrue($this->itemRepository->save($this->cartId, $this->messageMock, $itemId));
  187. }
  188. }