/** * @expectedException \Magento\Framework\Exception\CouldNotSaveException * @expectedExceptionMessage Could not add gift message to shopping cart */ public function testSetMessageCouldNotAddGiftMessageException() { $cartId = 665; $this->quoteRepositoryMock->expects($this->once())->method('get')->with($cartId)->will($this->returnValue($this->quoteMock)); $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(1)); $this->quoteMock->expects($this->once())->method('isVirtual')->will($this->returnValue(false)); $this->quoteMock->expects($this->once())->method('getBillingAddress')->will($this->returnValue($this->billingAddressMock)); $this->billingAddressMock->expects($this->once())->method('getCountryId')->will($this->returnValue(12)); $this->quoteMock->expects($this->once())->method('getShippingAddress')->will($this->returnValue($this->shippingAddressMock)); $this->shippingAddressMock->expects($this->once())->method('getCountryId')->will($this->returnValue(13)); $this->storeManagerMock->expects($this->once())->method('getStore')->will($this->returnValue($this->storeMock)); $this->helperMock->expects($this->once())->method('getIsMessagesAvailable')->with('', $this->quoteMock, $this->storeMock)->will($this->returnValue(true)); $this->giftMessageMock->expects($this->once())->method('getSender')->will($this->returnValue('sender')); $this->giftMessageMock->expects($this->once())->method('getRecipient')->will($this->returnValue('recipient')); $this->giftMessageMock->expects($this->once())->method('getMessage')->will($this->returnValue('Message')); $message['quote'][null] = ['from' => 'sender', 'to' => 'recipient', 'message' => 'Message']; $exception = new \Magento\Framework\Exception\CouldNotSaveException('Could not add gift message to shopping cart'); $this->giftMessageManagerMock->expects($this->once())->method('add')->with($message, $this->quoteMock)->will($this->throwException($exception)); $this->service->setForQuote($cartId, $this->giftMessageMock); }