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