Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CreditmemoNotifierTest.php
Go to the documentation of this file.
1 <?php
8 
10 
12 use Magento\Sales\Model\ResourceModel\Order\Status\History\CollectionFactory;
13 
17 class CreditmemoNotifierTest extends \PHPUnit\Framework\TestCase
18 {
23 
27  protected $notifier;
28 
32  protected $creditmemo;
33 
37  protected $loggerMock;
38 
43 
44  protected function setUp()
45  {
46  $this->historyCollectionFactory = $this->createPartialMock(
47  \Magento\Sales\Model\ResourceModel\Order\Status\History\CollectionFactory::class,
48  ['create']
49  );
50  $this->creditmemo = $this->createPartialMock(
51  \Magento\Sales\Model\Order\Creditmemo::class,
52  ['__wakeUp', 'getEmailSent']
53  );
54  $this->creditmemoSenderMock = $this->createPartialMock(
55  \Magento\Sales\Model\Order\Email\Sender\CreditmemoSender::class,
56  ['send']
57  );
58  $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
59  $this->notifier = new CreditmemoNotifier(
60  $this->historyCollectionFactory,
61  $this->loggerMock,
62  $this->creditmemoSenderMock
63  );
64  }
65 
69  public function testNotifySuccess()
70  {
71  $historyCollection = $this->createPartialMock(
72  \Magento\Sales\Model\ResourceModel\Order\Status\History\Collection::class,
73  ['getUnnotifiedForInstance', 'save', 'setIsCustomerNotified']
74  );
75  $historyItem = $this->createPartialMock(
76  \Magento\Sales\Model\Order\Status\History::class,
77  ['setIsCustomerNotified', 'save', '__wakeUp']
78  );
79  $historyItem->expects($this->at(0))
80  ->method('setIsCustomerNotified')
81  ->with(1);
82  $historyItem->expects($this->at(1))
83  ->method('save');
84  $historyCollection->expects($this->once())
85  ->method('getUnnotifiedForInstance')
86  ->with($this->creditmemo)
87  ->will($this->returnValue($historyItem));
88  $this->creditmemo->expects($this->once())
89  ->method('getEmailSent')
90  ->will($this->returnValue(true));
91  $this->historyCollectionFactory->expects($this->once())
92  ->method('create')
93  ->will($this->returnValue($historyCollection));
94 
95  $this->creditmemoSenderMock->expects($this->once())
96  ->method('send')
97  ->with($this->equalTo($this->creditmemo));
98 
99  $this->assertTrue($this->notifier->notify($this->creditmemo));
100  }
101 
105  public function testNotifyFail()
106  {
107  $this->creditmemo->expects($this->once())
108  ->method('getEmailSent')
109  ->will($this->returnValue(false));
110  $this->assertFalse($this->notifier->notify($this->creditmemo));
111  }
112 
116  public function testNotifyException()
117  {
118  $exception = new MailException(__('Email has not been sent'));
119  $this->creditmemoSenderMock->expects($this->once())
120  ->method('send')
121  ->with($this->equalTo($this->creditmemo))
122  ->will($this->throwException($exception));
123  $this->loggerMock->expects($this->once())
124  ->method('critical')
125  ->with($this->equalTo($exception));
126  $this->assertFalse($this->notifier->notify($this->creditmemo));
127  }
128 }
__()
Definition: __.php:13