Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MailTest.php
Go to the documentation of this file.
1 <?php
8 
13 
14 class MailTest extends \PHPUnit\Framework\TestCase
15 {
16 
20  private $configMock;
21 
25  private $urlMock;
26 
30  private $transportBuilderMock;
31 
35  private $inlineTranslationMock;
36 
40  private $storeManagerMock;
41 
45  private $mail;
46 
47  protected function setUp()
48  {
49  $this->configMock = $this->getMockBuilder(ConfigInterface::class)->getMockForAbstractClass();
50  $this->urlMock = $this->createMock(\Magento\Framework\UrlInterface::class);
51  $this->transportBuilderMock = $this->getMockBuilder(
52  \Magento\Framework\Mail\Template\TransportBuilder::class
53  )->disableOriginalConstructor(
54  )->getMock();
55  $this->inlineTranslationMock = $this->getMockBuilder(
56  \Magento\Framework\Translate\Inline\StateInterface::class
57  )->disableOriginalConstructor(
58  )->getMock();
59 
60  $this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
61 
62  $this->mail = new Mail(
63  $this->configMock,
64  $this->transportBuilderMock,
65  $this->inlineTranslationMock,
66  $this->storeManagerMock
67  );
68  }
69 
70  public function testSendMail()
71  {
73  $templateVars = ['comment' => 'Comment'];
74 
75  $transport = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
76 
77  $store = $this->createMock(StoreInterface::class);
78  $store->expects($this->once())->method('getId')->willReturn(555);
79 
80  $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($store);
81 
82  $this->transportBuilderMock->expects($this->once())
83  ->method('setTemplateIdentifier')
84  ->will($this->returnSelf());
85 
86  $this->transportBuilderMock->expects($this->once())
87  ->method('setTemplateOptions')
88  ->with([
89  'area' => 'frontend',
90  'store' => 555,
91  ])
92  ->will($this->returnSelf());
93 
94  $this->transportBuilderMock->expects($this->once())
95  ->method('setTemplateVars')
96  ->with($templateVars)
97  ->will($this->returnSelf());
98 
99  $this->transportBuilderMock->expects($this->once())
100  ->method('setFrom')
101  ->will($this->returnSelf());
102 
103  $this->transportBuilderMock->expects($this->once())
104  ->method('addTo')
105  ->will($this->returnSelf());
106 
107  $this->transportBuilderMock->expects($this->once())
108  ->method('setReplyTo')
109  ->with($email)
110  ->will($this->returnSelf());
111 
112  $this->transportBuilderMock->expects($this->once())
113  ->method('getTransport')
114  ->willReturn($transport);
115 
116  $transport->expects($this->once())
117  ->method('sendMessage');
118 
119  $this->inlineTranslationMock->expects($this->once())
120  ->method('resume');
121 
122  $this->inlineTranslationMock->expects($this->once())
123  ->method('suspend');
124 
125  $this->mail->send($email, $templateVars);
126  }
127 }
$email
Definition: details.phtml:13