GiftMessageManagerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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\GiftMessageManager;
  9. class GiftMessageManagerTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var GiftMessageManager
  13. */
  14. protected $model;
  15. /**
  16. * @var \PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $messageFactoryMock;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $quoteMock;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $quoteItemMock;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $quoteAddressMock;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $quoteAddressItemMock;
  35. /**
  36. * @var \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $giftMessageMock;
  39. protected function setUp()
  40. {
  41. $this->messageFactoryMock =
  42. $this->createPartialMock(\Magento\GiftMessage\Model\MessageFactory::class, ['create', '__wakeup']);
  43. $this->quoteMock = $this->createPartialMock(
  44. \Magento\Quote\Model\Quote::class,
  45. [
  46. 'setGiftMessageId',
  47. 'getGiftMessageId',
  48. 'save',
  49. 'getItemById',
  50. 'getAddressById',
  51. 'getBillingAddress',
  52. 'getShippingAddress',
  53. '__wakeup',
  54. 'getCustomerId'
  55. ]
  56. );
  57. $this->quoteItemMock = $this->createPartialMock(
  58. \Magento\Quote\Model\Quote\Item::class,
  59. [
  60. 'setGiftMessageId',
  61. 'getGiftMessageId',
  62. 'save',
  63. '__wakeup'
  64. ]
  65. );
  66. $this->quoteAddressMock = $this->createPartialMock(
  67. \Magento\Quote\Model\Quote\Address::class,
  68. [
  69. 'getGiftMessageId',
  70. 'setGiftMessageId',
  71. 'getItemById',
  72. 'save',
  73. '__wakeup'
  74. ]
  75. );
  76. $this->quoteAddressItemMock = $this->createPartialMock(
  77. \Magento\Quote\Model\Quote\Address\Item::class,
  78. [
  79. 'getGiftMessageId',
  80. 'setGiftMessageId',
  81. 'save',
  82. '__wakeup'
  83. ]
  84. );
  85. $this->giftMessageMock = $this->createPartialMock(
  86. \Magento\GiftMessage\Model\Message::class,
  87. [
  88. 'setSender',
  89. 'setRecipient',
  90. 'setMessage',
  91. 'setCustomerId',
  92. 'getSender',
  93. 'getRecipient',
  94. 'getMessage',
  95. 'getId',
  96. 'delete',
  97. 'save',
  98. '__wakeup',
  99. 'load'
  100. ]
  101. );
  102. $this->model = new \Magento\GiftMessage\Model\GiftMessageManager($this->messageFactoryMock);
  103. }
  104. public function testAddWhenGiftMessagesIsNoArray()
  105. {
  106. $giftMessages = '';
  107. $this->messageFactoryMock->expects($this->never())->method('create');
  108. $this->model->add($giftMessages, $this->quoteMock);
  109. }
  110. public function testAddWithSaveMessageIdAndEmptyMessageException()
  111. {
  112. $giftMessages = [
  113. 'quote' => [
  114. 0 => [
  115. 'from' => 'sender',
  116. 'to' => 'recipient',
  117. 'message' => ' ',
  118. ],
  119. ],
  120. ];
  121. $this->messageFactoryMock->expects($this->once())
  122. ->method('create')
  123. ->will($this->returnValue($this->giftMessageMock));
  124. $this->quoteMock->expects($this->once())->method('getGiftMessageId')->will($this->returnValue(null));
  125. $this->giftMessageMock->expects($this->never())->method('load');
  126. $this->giftMessageMock->expects($this->once())->method('getId')->will($this->returnValue(1));
  127. $this->giftMessageMock->expects($this->once())->method('delete');
  128. $this->quoteMock->expects($this->once())
  129. ->method('setGiftMessageId')
  130. ->with(0)
  131. ->will($this->returnValue($this->quoteMock));
  132. $exception = new \Exception();
  133. $this->quoteMock->expects($this->once())->method('save')->will($this->throwException($exception));
  134. $this->model->add($giftMessages, $this->quoteMock);
  135. }
  136. public function testAddWithSaveMessageIdException()
  137. {
  138. $entityId = 12;
  139. $giftMessages = [
  140. 'quote_item' => [
  141. 12 => [
  142. 'from' => 'sender',
  143. 'to' => 'recipient',
  144. 'message' => 'message',
  145. ],
  146. ],
  147. ];
  148. $customerId = 42;
  149. $this->messageFactoryMock->expects($this->once())
  150. ->method('create')
  151. ->will($this->returnValue($this->giftMessageMock));
  152. $this->quoteMock->expects($this->once())
  153. ->method('getItemById')
  154. ->with($entityId)
  155. ->will($this->returnValue($this->quoteItemMock));
  156. $this->quoteItemMock->expects($this->once())->method('getGiftMessageId')->will($this->returnValue(null));
  157. $this->giftMessageMock->expects($this->once())
  158. ->method('setSender')
  159. ->with('sender')
  160. ->will($this->returnValue($this->giftMessageMock));
  161. $this->giftMessageMock->expects($this->once())
  162. ->method('setRecipient')
  163. ->with('recipient')
  164. ->will($this->returnValue($this->giftMessageMock));
  165. $this->quoteMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
  166. $this->giftMessageMock->expects($this->once())
  167. ->method('setCustomerId')
  168. ->with($customerId)
  169. ->will($this->returnValue($this->giftMessageMock));
  170. $this->giftMessageMock->expects($this->once())
  171. ->method('setMessage')
  172. ->with('message')
  173. ->will($this->returnValue($this->giftMessageMock));
  174. $this->giftMessageMock->expects($this->once())->method('save');
  175. $this->giftMessageMock->expects($this->once())->method('getId')->will($this->returnValue(33));
  176. $this->quoteItemMock->expects($this->once())
  177. ->method('setGiftMessageId')
  178. ->with(33)
  179. ->will($this->returnValue($this->quoteItemMock));
  180. $exception = new \Exception();
  181. $this->quoteItemMock->expects($this->once())->method('save')->will($this->throwException($exception));
  182. $this->model->add($giftMessages, $this->quoteMock);
  183. }
  184. public function testAddWithQuoteAddress()
  185. {
  186. $entityId = 1;
  187. $giftMessages = [
  188. 'quote_address' => [
  189. 1 => [
  190. 'from' => 'sender',
  191. 'to' => 'recipient',
  192. 'message' => 'message',
  193. ],
  194. ],
  195. ];
  196. $customerId = 42;
  197. $this->messageFactoryMock->expects($this->once())
  198. ->method('create')
  199. ->will($this->returnValue($this->giftMessageMock));
  200. $this->quoteMock->expects($this->once())
  201. ->method('getAddressById')
  202. ->with($entityId)
  203. ->will($this->returnValue($this->quoteAddressMock));
  204. $this->quoteAddressMock->expects($this->once())->method('getGiftMessageId')->will($this->returnValue(null));
  205. $this->giftMessageMock->expects($this->once())
  206. ->method('setSender')
  207. ->with('sender')
  208. ->will($this->returnValue($this->giftMessageMock));
  209. $this->giftMessageMock->expects($this->once())
  210. ->method('setRecipient')
  211. ->with('recipient')
  212. ->will($this->returnValue($this->giftMessageMock));
  213. $this->giftMessageMock->expects($this->once())
  214. ->method('setMessage')
  215. ->with('message')
  216. ->will($this->returnValue($this->giftMessageMock));
  217. $this->quoteMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
  218. $this->giftMessageMock->expects($this->once())
  219. ->method('setCustomerId')
  220. ->with($customerId)
  221. ->will($this->returnValue($this->giftMessageMock));
  222. $this->giftMessageMock->expects($this->once())->method('save');
  223. $this->giftMessageMock->expects($this->once())->method('getId')->will($this->returnValue(33));
  224. $this->quoteAddressMock->expects($this->once())
  225. ->method('setGiftMessageId')
  226. ->with(33)
  227. ->will($this->returnValue($this->quoteAddressMock));
  228. $this->quoteAddressMock->expects($this->once())->method('save');
  229. $this->model->add($giftMessages, $this->quoteMock);
  230. }
  231. public function testAddWithQuoteAddressItem()
  232. {
  233. $entityId = 1;
  234. $giftMessages = [
  235. 'quote_address_item' => [
  236. 1 => [
  237. 'from' => 'sender',
  238. 'to' => 'recipient',
  239. 'message' => 'message',
  240. 'address' => 'address',
  241. ],
  242. ],
  243. ];
  244. $customerId = 42;
  245. $this->messageFactoryMock->expects($this->once())
  246. ->method('create')
  247. ->will($this->returnValue($this->giftMessageMock));
  248. $this->quoteMock->expects($this->once())
  249. ->method('getAddressById')
  250. ->with('address')
  251. ->will($this->returnValue($this->quoteAddressMock));
  252. $this->quoteAddressMock->expects($this->once())
  253. ->method('getItemById')
  254. ->with($entityId)
  255. ->will($this->returnValue($this->quoteAddressItemMock));
  256. $this->quoteAddressItemMock->expects($this->once())->method('getGiftMessageId')->will($this->returnValue(0));
  257. $this->giftMessageMock->expects($this->once())
  258. ->method('setSender')
  259. ->with('sender')
  260. ->will($this->returnValue($this->giftMessageMock));
  261. $this->giftMessageMock->expects($this->once())
  262. ->method('setRecipient')
  263. ->with('recipient')
  264. ->will($this->returnValue($this->giftMessageMock));
  265. $this->giftMessageMock->expects($this->once())
  266. ->method('setMessage')
  267. ->with('message')
  268. ->will($this->returnValue($this->giftMessageMock));
  269. $this->quoteMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
  270. $this->giftMessageMock->expects($this->once())
  271. ->method('setCustomerId')
  272. ->with($customerId)
  273. ->will($this->returnValue($this->giftMessageMock));
  274. $this->giftMessageMock->expects($this->once())->method('save');
  275. $this->giftMessageMock->expects($this->once())->method('getId')->will($this->returnValue(33));
  276. $this->quoteAddressItemMock->expects($this->once())
  277. ->method('setGiftMessageId')
  278. ->with(33)
  279. ->will($this->returnValue($this->quoteAddressItemMock));
  280. $this->quoteAddressItemMock->expects($this->once())
  281. ->method('save')
  282. ->will($this->returnValue($this->quoteAddressItemMock));
  283. $this->model->add($giftMessages, $this->quoteMock);
  284. }
  285. /**
  286. * @expectedException \Magento\Framework\Exception\CouldNotSaveException
  287. * @expectedExceptionMessage The gift message couldn't be added to Cart.
  288. */
  289. public function testSetMessageCouldNotAddGiftMessageException()
  290. {
  291. $this->giftMessageMock->expects($this->once())->method('getSender')->will($this->returnValue('sender'));
  292. $this->giftMessageMock->expects($this->once())->method('getRecipient')->will($this->returnValue('recipient'));
  293. $this->giftMessageMock->expects($this->once())->method('getMessage')->will($this->returnValue('Message'));
  294. $this->messageFactoryMock->expects($this->once())
  295. ->method('create')
  296. ->willThrowException(new \Exception());
  297. $this->model->setMessage($this->quoteMock, 'item', $this->giftMessageMock);
  298. }
  299. }