Skip to content

Commit dcd6c1f

Browse files
author
Dmytro Voskoboinikov
committed
MAGETWO-35620: Implement Observer
1 parent b57b853 commit dcd6c1f

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sales\Test\Unit\Model\Observer;
7+
8+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
9+
10+
/**
11+
* Unit test of sales emails sending observer.
12+
*/
13+
class SendEmailsTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* Subject of testing.
17+
*
18+
* @var \Magento\Sales\Model\Observer\SendEmails
19+
*/
20+
protected $object;
21+
22+
/**
23+
* Email sender model mock.
24+
*
25+
* @var \Magento\Sales\Model\Order\Email\Sender|\PHPUnit_Framework_MockObject_MockObject
26+
*/
27+
protected $emailSender;
28+
29+
/**
30+
* Entity resource model mock.
31+
*
32+
* @var \Magento\Sales\Model\Resource\Entity|\PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
protected $entityResource;
35+
36+
/**
37+
* Entity collection model mock.
38+
*
39+
* @var \Magento\Sales\Model\Resource\Collection\AbstractCollection|\PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
protected $entityCollection;
42+
43+
/**
44+
* Global configuration storage mock.
45+
*
46+
* @var \Magento\Framework\App\Config|\PHPUnit_Framework_MockObject_MockObject
47+
*/
48+
protected $globalConfig;
49+
50+
protected function setUp()
51+
{
52+
$objectManager = new ObjectManager($this);
53+
54+
$this->emailSender = $this->getMock(
55+
'Magento\Sales\Model\Order\Email\Sender',
56+
['send'],
57+
[],
58+
'',
59+
false
60+
);
61+
62+
$this->entityResource = $this->getMockForAbstractClass(
63+
'Magento\Sales\Model\Resource\Entity',
64+
[],
65+
'',
66+
false,
67+
false,
68+
true,
69+
['save']
70+
);
71+
72+
$this->entityCollection = $this->getMockForAbstractClass(
73+
'Magento\Sales\Model\Resource\Collection\AbstractCollection',
74+
[],
75+
'',
76+
false,
77+
false,
78+
true,
79+
['addFieldToFilter', 'getItems']
80+
);
81+
82+
$this->globalConfig = $this->getMock(
83+
'Magento\Framework\App\Config',
84+
[],
85+
[],
86+
'',
87+
false
88+
);
89+
90+
$this->object = $objectManager->getObject(
91+
'Magento\Sales\Model\Observer\SendEmails',
92+
[
93+
'emailSender' => $this->emailSender,
94+
'entityResource' => $this->entityResource,
95+
'entityCollection' => $this->entityCollection,
96+
'globalConfig' => $this->globalConfig
97+
]
98+
);
99+
}
100+
101+
/**
102+
* @param int $configValue
103+
* @param array|null $collectionItems
104+
* @param bool|null $emailSendingResult
105+
* @dataProvider executeDataProvider
106+
* @return void
107+
*/
108+
public function testExecute($configValue, $collectionItems, $emailSendingResult)
109+
{
110+
$path = 'path/to/value/async_emails';
111+
112+
$this->globalConfig
113+
->expects($this->once())
114+
->method('getValue')
115+
->with($path)
116+
->willReturn($configValue);
117+
118+
if ($configValue) {
119+
$this->entityCollection
120+
->expects($this->at(0))
121+
->method('addFieldToFilter')
122+
->with('send_email', ['eq' => 1]);
123+
124+
$this->entityCollection
125+
->expects($this->at(1))
126+
->method('addFieldToFilter')
127+
->with('email_sent', ['null' => true]);
128+
129+
$this->entityCollection
130+
->expects($this->any())
131+
->method('getItems')
132+
->willReturn($collectionItems);
133+
134+
if ($collectionItems) {
135+
/** @var \Magento\Sales\Model\AbstractModel|\PHPUnit_Framework_MockObject_MockObject $collectionItem */
136+
$collectionItem = $collectionItems[0];
137+
138+
$this->emailSender
139+
->expects($this->once())
140+
->method('send')
141+
->with($collectionItem, true)
142+
->willReturn($emailSendingResult);
143+
144+
if ($emailSendingResult) {
145+
$collectionItem
146+
->expects($this->once())
147+
->method('setEmailSent')
148+
->with(true)
149+
->willReturn($collectionItem);
150+
151+
$this->entityResource
152+
->expects($this->once())
153+
->method('save')
154+
->with($collectionItem);
155+
}
156+
}
157+
}
158+
159+
$this->object->execute();
160+
}
161+
162+
/**
163+
* @return array
164+
*/
165+
public function executeDataProvider()
166+
{
167+
$entityModel = $this->getMockForAbstractClass(
168+
'Magento\Sales\Model\AbstractModel',
169+
[],
170+
'',
171+
false,
172+
false,
173+
true,
174+
['setEmailSent']
175+
);
176+
177+
return [
178+
[1, [$entityModel], true],
179+
[1, [$entityModel], false],
180+
[1, [], null],
181+
[0, null, null]
182+
];
183+
}
184+
}

0 commit comments

Comments
 (0)