Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GiftMessageManagerTest.php
Go to the documentation of this file.
1 <?php
9 
11 
12 class GiftMessageManagerTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $model;
18 
23 
27  protected $quoteMock;
28 
32  protected $quoteItemMock;
33 
37  protected $quoteAddressMock;
38 
43 
47  protected $giftMessageMock;
48 
49  protected function setUp()
50  {
51  $this->messageFactoryMock =
52  $this->createPartialMock(\Magento\GiftMessage\Model\MessageFactory::class, ['create', '__wakeup']);
53 
54  $this->quoteMock = $this->createPartialMock(
55  \Magento\Quote\Model\Quote::class,
56  [
57  'setGiftMessageId',
58  'getGiftMessageId',
59  'save',
60  'getItemById',
61  'getAddressById',
62  'getBillingAddress',
63  'getShippingAddress',
64  '__wakeup',
65  'getCustomerId'
66  ]
67  );
68  $this->quoteItemMock = $this->createPartialMock(
69  \Magento\Quote\Model\Quote\Item::class,
70  [
71  'setGiftMessageId',
72  'getGiftMessageId',
73  'save',
74  '__wakeup'
75  ]
76  );
77 
78  $this->quoteAddressMock = $this->createPartialMock(
79  \Magento\Quote\Model\Quote\Address::class,
80  [
81  'getGiftMessageId',
82  'setGiftMessageId',
83  'getItemById',
84  'save',
85  '__wakeup'
86  ]
87  );
88 
89  $this->quoteAddressItemMock = $this->createPartialMock(
90  \Magento\Quote\Model\Quote\Address\Item::class,
91  [
92  'getGiftMessageId',
93  'setGiftMessageId',
94  'save',
95  '__wakeup'
96  ]
97  );
98 
99  $this->giftMessageMock = $this->createPartialMock(
100  \Magento\GiftMessage\Model\Message::class,
101  [
102  'setSender',
103  'setRecipient',
104  'setMessage',
105  'setCustomerId',
106  'getSender',
107  'getRecipient',
108  'getMessage',
109  'getId',
110  'delete',
111  'save',
112  '__wakeup',
113  'load'
114  ]
115  );
116 
117  $this->model = new \Magento\GiftMessage\Model\GiftMessageManager($this->messageFactoryMock);
118  }
119 
121  {
122  $giftMessages = '';
123  $this->messageFactoryMock->expects($this->never())->method('create');
124  $this->model->add($giftMessages, $this->quoteMock);
125  }
126 
128  {
129  $giftMessages = [
130  'quote' => [
131  0 => [
132  'from' => 'sender',
133  'to' => 'recipient',
134  'message' => ' ',
135  ],
136  ],
137  ];
138 
139  $this->messageFactoryMock->expects($this->once())
140  ->method('create')
141  ->will($this->returnValue($this->giftMessageMock));
142  $this->quoteMock->expects($this->once())->method('getGiftMessageId')->will($this->returnValue(null));
143  $this->giftMessageMock->expects($this->never())->method('load');
144  $this->giftMessageMock->expects($this->once())->method('getId')->will($this->returnValue(1));
145  $this->giftMessageMock->expects($this->once())->method('delete');
146  $this->quoteMock->expects($this->once())
147  ->method('setGiftMessageId')
148  ->with(0)
149  ->will($this->returnValue($this->quoteMock));
150  $exception = new \Exception();
151  $this->quoteMock->expects($this->once())->method('save')->will($this->throwException($exception));
152 
153  $this->model->add($giftMessages, $this->quoteMock);
154  }
155 
157  {
158  $entityId = 12;
159  $giftMessages = [
160  'quote_item' => [
161  12 => [
162  'from' => 'sender',
163  'to' => 'recipient',
164  'message' => 'message',
165  ],
166  ],
167  ];
168  $customerId = 42;
169 
170  $this->messageFactoryMock->expects($this->once())
171  ->method('create')
172  ->will($this->returnValue($this->giftMessageMock));
173  $this->quoteMock->expects($this->once())
174  ->method('getItemById')
175  ->with($entityId)
176  ->will($this->returnValue($this->quoteItemMock));
177  $this->quoteItemMock->expects($this->once())->method('getGiftMessageId')->will($this->returnValue(null));
178  $this->giftMessageMock->expects($this->once())
179  ->method('setSender')
180  ->with('sender')
181  ->will($this->returnValue($this->giftMessageMock));
182  $this->giftMessageMock->expects($this->once())
183  ->method('setRecipient')
184  ->with('recipient')
185  ->will($this->returnValue($this->giftMessageMock));
186  $this->quoteMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
187  $this->giftMessageMock->expects($this->once())
188  ->method('setCustomerId')
189  ->with($customerId)
190  ->will($this->returnValue($this->giftMessageMock));
191  $this->giftMessageMock->expects($this->once())
192  ->method('setMessage')
193  ->with('message')
194  ->will($this->returnValue($this->giftMessageMock));
195  $this->giftMessageMock->expects($this->once())->method('save');
196  $this->giftMessageMock->expects($this->once())->method('getId')->will($this->returnValue(33));
197  $this->quoteItemMock->expects($this->once())
198  ->method('setGiftMessageId')
199  ->with(33)
200  ->will($this->returnValue($this->quoteItemMock));
201  $exception = new \Exception();
202  $this->quoteItemMock->expects($this->once())->method('save')->will($this->throwException($exception));
203 
204  $this->model->add($giftMessages, $this->quoteMock);
205  }
206 
207  public function testAddWithQuoteAddress()
208  {
209  $entityId = 1;
210  $giftMessages = [
211  'quote_address' => [
212  1 => [
213  'from' => 'sender',
214  'to' => 'recipient',
215  'message' => 'message',
216  ],
217  ],
218  ];
219  $customerId = 42;
220 
221  $this->messageFactoryMock->expects($this->once())
222  ->method('create')
223  ->will($this->returnValue($this->giftMessageMock));
224  $this->quoteMock->expects($this->once())
225  ->method('getAddressById')
226  ->with($entityId)
227  ->will($this->returnValue($this->quoteAddressMock));
228  $this->quoteAddressMock->expects($this->once())->method('getGiftMessageId')->will($this->returnValue(null));
229  $this->giftMessageMock->expects($this->once())
230  ->method('setSender')
231  ->with('sender')
232  ->will($this->returnValue($this->giftMessageMock));
233  $this->giftMessageMock->expects($this->once())
234  ->method('setRecipient')
235  ->with('recipient')
236  ->will($this->returnValue($this->giftMessageMock));
237  $this->giftMessageMock->expects($this->once())
238  ->method('setMessage')
239  ->with('message')
240  ->will($this->returnValue($this->giftMessageMock));
241  $this->quoteMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
242  $this->giftMessageMock->expects($this->once())
243  ->method('setCustomerId')
244  ->with($customerId)
245  ->will($this->returnValue($this->giftMessageMock));
246  $this->giftMessageMock->expects($this->once())->method('save');
247  $this->giftMessageMock->expects($this->once())->method('getId')->will($this->returnValue(33));
248  $this->quoteAddressMock->expects($this->once())
249  ->method('setGiftMessageId')
250  ->with(33)
251  ->will($this->returnValue($this->quoteAddressMock));
252  $this->quoteAddressMock->expects($this->once())->method('save');
253  $this->model->add($giftMessages, $this->quoteMock);
254  }
255 
256  public function testAddWithQuoteAddressItem()
257  {
258  $entityId = 1;
259  $giftMessages = [
260  'quote_address_item' => [
261  1 => [
262  'from' => 'sender',
263  'to' => 'recipient',
264  'message' => 'message',
265  'address' => 'address',
266  ],
267  ],
268  ];
269  $customerId = 42;
270 
271  $this->messageFactoryMock->expects($this->once())
272  ->method('create')
273  ->will($this->returnValue($this->giftMessageMock));
274  $this->quoteMock->expects($this->once())
275  ->method('getAddressById')
276  ->with('address')
277  ->will($this->returnValue($this->quoteAddressMock));
278  $this->quoteAddressMock->expects($this->once())
279  ->method('getItemById')
280  ->with($entityId)
281  ->will($this->returnValue($this->quoteAddressItemMock));
282  $this->quoteAddressItemMock->expects($this->once())->method('getGiftMessageId')->will($this->returnValue(0));
283  $this->giftMessageMock->expects($this->once())
284  ->method('setSender')
285  ->with('sender')
286  ->will($this->returnValue($this->giftMessageMock));
287  $this->giftMessageMock->expects($this->once())
288  ->method('setRecipient')
289  ->with('recipient')
290  ->will($this->returnValue($this->giftMessageMock));
291  $this->giftMessageMock->expects($this->once())
292  ->method('setMessage')
293  ->with('message')
294  ->will($this->returnValue($this->giftMessageMock));
295  $this->quoteMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
296  $this->giftMessageMock->expects($this->once())
297  ->method('setCustomerId')
298  ->with($customerId)
299  ->will($this->returnValue($this->giftMessageMock));
300  $this->giftMessageMock->expects($this->once())->method('save');
301  $this->giftMessageMock->expects($this->once())->method('getId')->will($this->returnValue(33));
302  $this->quoteAddressItemMock->expects($this->once())
303  ->method('setGiftMessageId')
304  ->with(33)
305  ->will($this->returnValue($this->quoteAddressItemMock));
306  $this->quoteAddressItemMock->expects($this->once())
307  ->method('save')
308  ->will($this->returnValue($this->quoteAddressItemMock));
309 
310  $this->model->add($giftMessages, $this->quoteMock);
311  }
312 
318  {
319  $this->giftMessageMock->expects($this->once())->method('getSender')->will($this->returnValue('sender'));
320  $this->giftMessageMock->expects($this->once())->method('getRecipient')->will($this->returnValue('recipient'));
321  $this->giftMessageMock->expects($this->once())->method('getMessage')->will($this->returnValue('Message'));
322 
323  $this->messageFactoryMock->expects($this->once())
324  ->method('create')
325  ->willThrowException(new \Exception());
326 
327  $this->model->setMessage($this->quoteMock, 'item', $this->giftMessageMock);
328  }
329 }