CartRepositoryTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\CartRepository;
  9. class CartRepositoryTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var CartRepository
  13. */
  14. protected $cartRepository;
  15. /**
  16. * @var \PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $quoteRepositoryMock;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $messageFactoryMock;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $quoteMock;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $messageMock;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $quoteItemMock;
  35. /**
  36. * @var string
  37. */
  38. protected $cartId = 13;
  39. /**
  40. * @var \PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $storeManagerMock;
  43. /**
  44. * @var \PHPUnit_Framework_MockObject_MockObject
  45. */
  46. protected $giftMessageManagerMock;
  47. /**
  48. * @var \PHPUnit_Framework_MockObject_MockObject
  49. */
  50. protected $helperMock;
  51. /**
  52. * @var \PHPUnit_Framework_MockObject_MockObject
  53. */
  54. protected $storeMock;
  55. protected function setUp()
  56. {
  57. $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
  58. $this->messageFactoryMock = $this->createPartialMock(
  59. \Magento\GiftMessage\Model\MessageFactory::class,
  60. [
  61. 'create',
  62. '__wakeup'
  63. ]
  64. );
  65. $this->messageMock = $this->createMock(\Magento\GiftMessage\Model\Message::class);
  66. $this->quoteItemMock = $this->createPartialMock(
  67. \Magento\Quote\Model\Quote\Item::class,
  68. [
  69. 'getGiftMessageId',
  70. '__wakeup'
  71. ]
  72. );
  73. $this->quoteMock = $this->createPartialMock(
  74. \Magento\Quote\Model\Quote::class,
  75. [
  76. 'getGiftMessageId',
  77. 'getItemById',
  78. 'getItemsCount',
  79. 'isVirtual',
  80. '__wakeup',
  81. ]
  82. );
  83. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  84. $this->giftMessageManagerMock =
  85. $this->createMock(\Magento\GiftMessage\Model\GiftMessageManager::class);
  86. $this->helperMock = $this->createMock(\Magento\GiftMessage\Helper\Message::class);
  87. $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  88. $this->cartRepository = new \Magento\GiftMessage\Model\CartRepository(
  89. $this->quoteRepositoryMock,
  90. $this->storeManagerMock,
  91. $this->giftMessageManagerMock,
  92. $this->helperMock,
  93. $this->messageFactoryMock
  94. );
  95. $this->quoteRepositoryMock->expects($this->once())
  96. ->method('getActive')
  97. ->with($this->cartId)
  98. ->will($this->returnValue($this->quoteMock));
  99. }
  100. public function testGetWithOutMessageId()
  101. {
  102. $messageId = 0;
  103. $this->quoteMock->expects($this->once())->method('getGiftMessageId')->will($this->returnValue($messageId));
  104. $this->assertNull($this->cartRepository->get($this->cartId));
  105. }
  106. public function testGet()
  107. {
  108. $messageId = 156;
  109. $this->quoteMock->expects($this->once())->method('getGiftMessageId')->will($this->returnValue($messageId));
  110. $this->messageFactoryMock->expects($this->once())
  111. ->method('create')
  112. ->will($this->returnValue($this->messageMock));
  113. $this->messageMock->expects($this->once())->method('load')->will($this->returnValue($this->messageMock));
  114. $this->assertEquals($this->messageMock, $this->cartRepository->get($this->cartId));
  115. }
  116. /**
  117. * @expectedException \Magento\Framework\Exception\InputException
  118. * @expectedExceptionMessage Gift messages can't be used for an empty cart. Add an item and try again.
  119. */
  120. public function testSaveWithInputException()
  121. {
  122. $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(0));
  123. $this->cartRepository->save($this->cartId, $this->messageMock);
  124. }
  125. /**
  126. * @expectedException \Magento\Framework\Exception\State\InvalidTransitionException
  127. * @expectedExceptionMessage Gift messages can't be used for virtual products.
  128. */
  129. public function testSaveWithInvalidTransitionException()
  130. {
  131. $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(1));
  132. $this->quoteMock->expects($this->once())->method('isVirtual')->will($this->returnValue(true));
  133. $this->cartRepository->save($this->cartId, $this->messageMock);
  134. }
  135. public function testSave()
  136. {
  137. $this->quoteMock->expects($this->once())->method('isVirtual')->will($this->returnValue(false));
  138. $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(1));
  139. $this->storeManagerMock->expects($this->once())->method('getStore')->will($this->returnValue($this->storeMock));
  140. $this->helperMock->expects($this->once())
  141. ->method('isMessagesAllowed')
  142. ->with('quote', $this->quoteMock, $this->storeMock)
  143. ->will($this->returnValue(true));
  144. $this->giftMessageManagerMock->expects($this->once())
  145. ->method('setMessage')
  146. ->with($this->quoteMock, 'quote', $this->messageMock)
  147. ->will($this->returnValue($this->giftMessageManagerMock));
  148. $this->messageMock->expects($this->once())->method('getMessage')->willReturn('message');
  149. $this->assertTrue($this->cartRepository->save($this->cartId, $this->messageMock));
  150. }
  151. }