Skip to content

Commit 0c0777d

Browse files
author
Dmytro Voskoboinikov
committed
MAGETWO-35620: Implement Observer
1 parent 1e8bcdf commit 0c0777d

File tree

5 files changed

+511
-422
lines changed

5 files changed

+511
-422
lines changed

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

Lines changed: 127 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class CreditmemoSenderTest extends \PHPUnit_Framework_TestCase
1414
*/
1515
protected $sender;
1616

17+
/**
18+
* @var \Magento\Sales\Model\Order\Email\Sender|\PHPUnit_Framework_MockObject_MockObject
19+
*/
20+
protected $senderMock;
21+
1722
/**
1823
* @var \PHPUnit_Framework_MockObject_MockObject
1924
*/
@@ -40,7 +45,7 @@ class CreditmemoSenderTest extends \PHPUnit_Framework_TestCase
4045
protected $orderMock;
4146

4247
/**
43-
* @var \PHPUnit_Framework_MockObject_MockObject
48+
* @var \Magento\Sales\Model\Order\Creditmemo|\PHPUnit_Framework_MockObject_MockObject
4449
*/
4550
protected $creditmemoMock;
4651
/**
@@ -53,8 +58,23 @@ class CreditmemoSenderTest extends \PHPUnit_Framework_TestCase
5358
*/
5459
protected $creditmemoResource;
5560

61+
/**
62+
* Global configuration storage mock.
63+
*
64+
* @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
65+
*/
66+
protected $globalConfig;
67+
5668
protected function setUp()
5769
{
70+
$this->senderMock = $this->getMock(
71+
'Magento\Sales\Model\Order\Email\Sender',
72+
['send', 'sendCopyTo'],
73+
[],
74+
'',
75+
false
76+
);
77+
5878
$this->senderBuilderFactoryMock = $this->getMock(
5979
'\Magento\Sales\Model\Order\Email\SenderBuilderFactory',
6080
['create'],
@@ -76,7 +96,15 @@ protected function setUp()
7696

7797
$this->creditmemoResource = $this->getMock(
7898
'\Magento\Sales\Model\Resource\Order\Creditmemo',
99+
['saveAttribute'],
79100
[],
101+
'',
102+
false
103+
);
104+
105+
$this->globalConfig = $this->getMock(
106+
'Magento\Framework\App\Config',
107+
['getValue'],
80108
[],
81109
'',
82110
false
@@ -123,13 +151,17 @@ protected function setUp()
123151
'',
124152
false
125153
);
126-
$this->orderMock->expects($this->once())
154+
$this->orderMock->expects($this->any())
127155
->method('getPayment')
128156
->will($this->returnValue($paymentInfoMock));
129157

130158
$this->creditmemoMock = $this->getMock(
131159
'\Magento\Sales\Model\Order\Creditmemo',
132-
['getStore', '__wakeup', 'getOrder'],
160+
[
161+
'getStore', '__wakeup', 'getOrder',
162+
'setSendEmail', 'setEmailSent', 'getCustomerNoteNotify',
163+
'getCustomerNote'
164+
],
133165
[],
134166
'',
135167
false
@@ -146,122 +178,117 @@ protected function setUp()
146178
$this->identityContainerMock,
147179
$this->senderBuilderFactoryMock,
148180
$this->paymentHelper,
149-
$this->creditmemoResource
181+
$this->creditmemoResource,
182+
$this->globalConfig
150183
);
151184
}
152185

153-
public function testSendFalse()
154-
{
155-
$result = $this->sender->send($this->creditmemoMock);
156-
$this->assertFalse($result);
157-
}
158-
159-
public function testSendTrueWithCustomerCopy()
186+
/**
187+
* @param int $configValue
188+
* @param bool|null $forceSyncMode
189+
* @param bool|null $customerNoteNotify
190+
* @param bool|null $emailSendingResult
191+
* @dataProvider sendDataProvider
192+
* @return void
193+
*/
194+
public function testSend($configValue, $forceSyncMode, $customerNoteNotify, $emailSendingResult)
160195
{
161-
$billingAddress = 'billing_address';
162196
$comment = 'comment_test';
197+
$billingAddress = 'billing_address';
198+
$configPath = 'sales_email/general/async_sending';
163199

164-
$this->orderMock->expects($this->once())
165-
->method('getCustomerIsGuest')
166-
->will($this->returnValue(false));
167-
$this->orderMock->expects($this->any())
168-
->method('getBillingAddress')
169-
->will($this->returnValue($billingAddress));
170-
171-
$this->identityContainerMock->expects($this->once())
172-
->method('isEnabled')
173-
->will($this->returnValue(true));
174-
$this->templateContainerMock->expects($this->once())
175-
->method('setTemplateVars')
176-
->with(
177-
$this->equalTo(
178-
[
179-
'order' => $this->orderMock,
180-
'invoice' => $this->creditmemoMock,
181-
'comment' => $comment,
182-
'billing' => $billingAddress,
183-
'payment_html' => 'payment',
184-
'store' => $this->storeMock,
185-
]
186-
)
187-
);
188-
$paymentInfoMock = $this->getMock(
189-
'\Magento\Payment\Model\Info',
190-
[],
191-
[],
192-
'',
193-
false
194-
);
195-
$this->orderMock->expects($this->once())
196-
->method('getPayment')
197-
->will($this->returnValue($paymentInfoMock));
200+
$this->creditmemoMock->expects($this->once())
201+
->method('setSendEmail')
202+
->with(true);
198203

199-
$senderMock = $this->getMock(
200-
'Magento\Sales\Model\Order\Email\Sender',
201-
['send', 'sendCopyTo'],
202-
[],
203-
'',
204-
false
205-
);
206-
$senderMock->expects($this->once())
207-
->method('send');
208-
$senderMock->expects($this->never())
209-
->method('sendCopyTo');
204+
$this->globalConfig->expects($this->once())
205+
->method('getValue')
206+
->with($configPath)
207+
->willReturn($configValue);
210208

211-
$this->senderBuilderFactoryMock->expects($this->once())
212-
->method('create')
213-
->will($this->returnValue($senderMock));
209+
if (!$configValue || $forceSyncMode) {
210+
$this->orderMock->expects($this->any())
211+
->method('getBillingAddress')
212+
->willReturn($billingAddress);
214213

215-
$result = $this->sender->send($this->creditmemoMock, true, $comment);
216-
$this->assertTrue($result);
217-
}
214+
$this->creditmemoMock->expects($this->once())
215+
->method('getCustomerNoteNotify')
216+
->willReturn($customerNoteNotify);
218217

219-
public function testSendTrueWithoutCustomerCopy()
220-
{
221-
$billingAddress = 'billing_address';
222-
$comment = 'comment_test';
218+
$this->creditmemoMock->expects($this->any())
219+
->method('getCustomerNote')
220+
->willReturn($comment);
223221

224-
$this->orderMock->expects($this->once())
225-
->method('getCustomerIsGuest')
226-
->will($this->returnValue(false));
227-
$this->orderMock->expects($this->any())
228-
->method('getBillingAddress')
229-
->will($this->returnValue($billingAddress));
230-
231-
$this->identityContainerMock->expects($this->once())
232-
->method('isEnabled')
233-
->will($this->returnValue(true));
234-
$this->templateContainerMock->expects($this->once())
235-
->method('setTemplateVars')
236-
->with(
237-
$this->equalTo(
222+
$this->templateContainerMock->expects($this->once())
223+
->method('setTemplateVars')
224+
->with(
238225
[
239226
'order' => $this->orderMock,
240227
'invoice' => $this->creditmemoMock,
228+
'comment' => $customerNoteNotify ? $comment : '',
241229
'billing' => $billingAddress,
242230
'payment_html' => 'payment',
243-
'comment' => $comment,
244231
'store' => $this->storeMock,
245232
]
246-
)
247-
);
248-
$senderMock = $this->getMock(
249-
'Magento\Sales\Model\Order\Email\Sender',
250-
['send', 'sendCopyTo'],
251-
[],
252-
'',
253-
false
254-
);
255-
$senderMock->expects($this->never())
256-
->method('send');
257-
$senderMock->expects($this->once())
258-
->method('sendCopyTo');
233+
);
234+
235+
$this->identityContainerMock->expects($this->once())
236+
->method('isEnabled')
237+
->willReturn($emailSendingResult);
238+
239+
if ($emailSendingResult) {
240+
$this->senderBuilderFactoryMock->expects($this->once())
241+
->method('create')
242+
->willReturn($this->senderMock);
243+
244+
$this->senderMock->expects($this->once())->method('send');
245+
246+
$this->senderMock->expects($this->once())->method('sendCopyTo');
259247

260-
$this->senderBuilderFactoryMock->expects($this->once())
261-
->method('create')
262-
->will($this->returnValue($senderMock));
248+
$this->creditmemoMock->expects($this->once())
249+
->method('setEmailSent')
250+
->with(true);
263251

264-
$result = $this->sender->send($this->creditmemoMock, false, $comment);
265-
$this->assertTrue($result);
252+
$this->creditmemoResource->expects($this->once())
253+
->method('saveAttribute')
254+
->with($this->creditmemoMock, ['send_email', 'email_sent']);
255+
256+
$this->assertTrue(
257+
$this->sender->send($this->creditmemoMock)
258+
);
259+
} else {
260+
$this->creditmemoResource->expects($this->once())
261+
->method('saveAttribute')
262+
->with($this->creditmemoMock, 'send_email');
263+
264+
$this->assertFalse(
265+
$this->sender->send($this->creditmemoMock)
266+
);
267+
}
268+
} else {
269+
$this->creditmemoResource->expects($this->once())
270+
->method('saveAttribute')
271+
->with($this->creditmemoMock, 'send_email');
272+
273+
$this->assertFalse(
274+
$this->sender->send($this->creditmemoMock)
275+
);
276+
}
277+
}
278+
279+
/**
280+
* @return array
281+
*/
282+
public function sendDataProvider()
283+
{
284+
return [
285+
[0, 0, 1, true],
286+
[0, 0, 0, true],
287+
[0, 0, 1, false],
288+
[0, 0, 0, false],
289+
[0, 1, 1, true],
290+
[0, 1, 0, true],
291+
[1, null, null, null]
292+
];
266293
}
267294
}

0 commit comments

Comments
 (0)