MailTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Contact\Test\Unit\Model;
  8. use Magento\Contact\Model\ConfigInterface;
  9. use Magento\Contact\Model\Mail;
  10. use Magento\Store\Model\StoreManagerInterface;
  11. use Magento\Store\Api\Data\StoreInterface;
  12. class MailTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $configMock;
  18. /**
  19. * @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $urlMock;
  22. /**
  23. * @var \Magento\Framework\Mail\Template\TransportBuilder|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $transportBuilderMock;
  26. /**
  27. * @var \Magento\Framework\Translate\Inline\StateInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $inlineTranslationMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $storeManagerMock;
  34. /**
  35. * @var Mail
  36. */
  37. private $mail;
  38. protected function setUp()
  39. {
  40. $this->configMock = $this->getMockBuilder(ConfigInterface::class)->getMockForAbstractClass();
  41. $this->urlMock = $this->createMock(\Magento\Framework\UrlInterface::class);
  42. $this->transportBuilderMock = $this->getMockBuilder(
  43. \Magento\Framework\Mail\Template\TransportBuilder::class
  44. )->disableOriginalConstructor(
  45. )->getMock();
  46. $this->inlineTranslationMock = $this->getMockBuilder(
  47. \Magento\Framework\Translate\Inline\StateInterface::class
  48. )->disableOriginalConstructor(
  49. )->getMock();
  50. $this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
  51. $this->mail = new Mail(
  52. $this->configMock,
  53. $this->transportBuilderMock,
  54. $this->inlineTranslationMock,
  55. $this->storeManagerMock
  56. );
  57. }
  58. public function testSendMail()
  59. {
  60. $email = 'reply-to@example.com';
  61. $templateVars = ['comment' => 'Comment'];
  62. $transport = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
  63. $store = $this->createMock(StoreInterface::class);
  64. $store->expects($this->once())->method('getId')->willReturn(555);
  65. $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($store);
  66. $this->transportBuilderMock->expects($this->once())
  67. ->method('setTemplateIdentifier')
  68. ->will($this->returnSelf());
  69. $this->transportBuilderMock->expects($this->once())
  70. ->method('setTemplateOptions')
  71. ->with([
  72. 'area' => 'frontend',
  73. 'store' => 555,
  74. ])
  75. ->will($this->returnSelf());
  76. $this->transportBuilderMock->expects($this->once())
  77. ->method('setTemplateVars')
  78. ->with($templateVars)
  79. ->will($this->returnSelf());
  80. $this->transportBuilderMock->expects($this->once())
  81. ->method('setFrom')
  82. ->will($this->returnSelf());
  83. $this->transportBuilderMock->expects($this->once())
  84. ->method('addTo')
  85. ->will($this->returnSelf());
  86. $this->transportBuilderMock->expects($this->once())
  87. ->method('setReplyTo')
  88. ->with($email)
  89. ->will($this->returnSelf());
  90. $this->transportBuilderMock->expects($this->once())
  91. ->method('getTransport')
  92. ->willReturn($transport);
  93. $transport->expects($this->once())
  94. ->method('sendMessage');
  95. $this->inlineTranslationMock->expects($this->once())
  96. ->method('resume');
  97. $this->inlineTranslationMock->expects($this->once())
  98. ->method('suspend');
  99. $this->mail->send($email, $templateVars);
  100. }
  101. }