Skip to content

Commit ab10641

Browse files
committed
Refactor unit test to cover issue case.
1 parent dbf2c29 commit ab10641

File tree

1 file changed

+57
-45
lines changed

1 file changed

+57
-45
lines changed

app/code/Magento/Sales/Test/Unit/Model/Order/Email/SenderBuilderTest.php

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ class SenderBuilderTest extends \PHPUnit\Framework\TestCase
3737

3838
protected function setUp()
3939
{
40-
$templateId = 'test_template_id';
41-
$templateOptions = ['option1', 'option2'];
42-
$templateVars = ['var1', 'var2'];
43-
$emailIdentity = 'email_identity_test';
44-
$emailCopyTo = ['example@mail.com'];
4540

4641
$this->templateContainerMock = $this->createPartialMock(
4742
\Magento\Sales\Model\Order\Email\Container\Template::class,
@@ -83,35 +78,6 @@ protected function setUp()
8378
]
8479
);
8580

86-
$this->templateContainerMock->expects($this->once())
87-
->method('getTemplateId')
88-
->will($this->returnValue($templateId));
89-
$this->transportBuilder->expects($this->once())
90-
->method('setTemplateIdentifier')
91-
->with($this->equalTo($templateId));
92-
$this->templateContainerMock->expects($this->once())
93-
->method('getTemplateOptions')
94-
->will($this->returnValue($templateOptions));
95-
$this->transportBuilder->expects($this->once())
96-
->method('setTemplateOptions')
97-
->with($this->equalTo($templateOptions));
98-
$this->templateContainerMock->expects($this->once())
99-
->method('getTemplateVars')
100-
->will($this->returnValue($templateVars));
101-
$this->transportBuilder->expects($this->once())
102-
->method('setTemplateVars')
103-
->with($this->equalTo($templateVars));
104-
105-
$this->identityContainerMock->expects($this->once())
106-
->method('getEmailIdentity')
107-
->will($this->returnValue($emailIdentity));
108-
$this->transportBuilder->expects($this->once())
109-
->method('setFromByScope')
110-
->with($this->equalTo($emailIdentity), 1);
111-
112-
$this->identityContainerMock->expects($this->once())
113-
->method('getEmailCopyTo')
114-
->will($this->returnValue($emailCopyTo));
11581

11682
$this->senderBuilder = new SenderBuilder(
11783
$this->templateContainerMock,
@@ -122,6 +88,7 @@ protected function setUp()
12288

12389
public function testSend()
12490
{
91+
$this->setExpectedCount(1);
12592
$customerName = 'test_name';
12693
$customerEmail = 'test_email';
12794
$identity = 'email_identity_test';
@@ -142,20 +109,20 @@ public function testSend()
142109
$this->identityContainerMock->expects($this->once())
143110
->method('getCustomerName')
144111
->will($this->returnValue($customerName));
145-
$this->identityContainerMock->expects($this->once())
112+
$this->identityContainerMock->expects($this->exactly(1))
146113
->method('getStore')
147114
->willReturn($this->storeMock);
148115
$this->storeMock->expects($this->once())
149116
->method('getId')
150117
->willReturn(1);
151-
$this->transportBuilder->expects($this->once())
118+
$this->transportBuilder->expects($this->exactly(1))
152119
->method('setFromByScope')
153120
->with($identity, 1);
154-
$this->transportBuilder->expects($this->once())
121+
$this->transportBuilder->expects($this->exactly(1))
155122
->method('addTo')
156123
->with($this->equalTo($customerEmail), $this->equalTo($customerName));
157124

158-
$this->transportBuilder->expects($this->once())
125+
$this->transportBuilder->expects($this->exactly(1))
159126
->method('getTransport')
160127
->will($this->returnValue($transportMock));
161128

@@ -164,6 +131,7 @@ public function testSend()
164131

165132
public function testSendCopyTo()
166133
{
134+
$this->setExpectedCount(2);
167135
$identity = 'email_identity_test';
168136
$transportMock = $this->createMock(
169137
\Magento\Sales\Test\Unit\Model\Order\Email\Stub\TransportInterfaceMock::class
@@ -172,22 +140,66 @@ public function testSendCopyTo()
172140
->method('getCustomerEmail');
173141
$this->identityContainerMock->expects($this->never())
174142
->method('getCustomerName');
175-
$this->transportBuilder->expects($this->once())
176-
->method('addTo')
177-
->with($this->equalTo('example@mail.com'));
178-
$this->transportBuilder->expects($this->once())
143+
$this->transportBuilder->expects($this->exactly(2))
144+
->method('addTo');
145+
$this->transportBuilder->expects($this->exactly(2))
179146
->method('setFromByScope')
180147
->with($identity, 1);
181-
$this->identityContainerMock->expects($this->once())
148+
$this->identityContainerMock->expects($this->exactly(2))
182149
->method('getStore')
183150
->willReturn($this->storeMock);
184-
$this->storeMock->expects($this->once())
151+
$this->storeMock->expects($this->exactly(2))
185152
->method('getId')
186153
->willReturn(1);
187-
$this->transportBuilder->expects($this->once())
154+
$this->transportBuilder->expects($this->exactly(2))
188155
->method('getTransport')
189156
->will($this->returnValue($transportMock));
190157

191158
$this->senderBuilder->sendCopyTo();
192159
}
160+
161+
/**
162+
* Sets expected count invocation.
163+
*
164+
* @param int $count
165+
*/
166+
private function setExpectedCount(int $count = 1)
167+
{
168+
169+
$templateId = 'test_template_id';
170+
$templateOptions = ['option1', 'option2'];
171+
$templateVars = ['var1', 'var2'];
172+
$emailIdentity = 'email_identity_test';
173+
$emailCopyTo = ['example@mail.com', 'example2@mail.com'];
174+
175+
$this->templateContainerMock->expects($this->exactly($count))
176+
->method('getTemplateId')
177+
->will($this->returnValue($templateId));
178+
$this->transportBuilder->expects($this->exactly($count))
179+
->method('setTemplateIdentifier')
180+
->with($this->equalTo($templateId));
181+
$this->templateContainerMock->expects($this->exactly($count))
182+
->method('getTemplateOptions')
183+
->will($this->returnValue($templateOptions));
184+
$this->transportBuilder->expects($this->exactly($count))
185+
->method('setTemplateOptions')
186+
->with($this->equalTo($templateOptions));
187+
$this->templateContainerMock->expects($this->exactly($count))
188+
->method('getTemplateVars')
189+
->will($this->returnValue($templateVars));
190+
$this->transportBuilder->expects($this->exactly($count))
191+
->method('setTemplateVars')
192+
->with($this->equalTo($templateVars));
193+
194+
$this->identityContainerMock->expects($this->exactly($count))
195+
->method('getEmailIdentity')
196+
->will($this->returnValue($emailIdentity));
197+
$this->transportBuilder->expects($this->exactly($count))
198+
->method('setFromByScope')
199+
->with($this->equalTo($emailIdentity), 1);
200+
201+
$this->identityContainerMock->expects($this->once())
202+
->method('getEmailCopyTo')
203+
->will($this->returnValue($emailCopyTo));
204+
}
193205
}

0 commit comments

Comments
 (0)