Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OrderNotifierTest.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 OrderNotifierTest extends \PHPUnit\Framework\TestCase
18 {
23 
27  protected $notifier;
28 
32  protected $order;
33 
37  protected $loggerMock;
38 
42  protected $orderSenderMock;
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->order = $this->createPartialMock(\Magento\Sales\Model\Order::class, ['__wakeUp', 'getEmailSent']);
51  $this->orderSenderMock = $this->createPartialMock(
52  \Magento\Sales\Model\Order\Email\Sender\OrderSender::class,
53  ['send']
54  );
55  $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
56  $this->notifier = new OrderNotifier(
57  $this->historyCollectionFactory,
58  $this->loggerMock,
59  $this->orderSenderMock
60  );
61  }
62 
66  public function testNotifySuccess()
67  {
68  $historyCollection = $this->createPartialMock(
69  \Magento\Sales\Model\ResourceModel\Order\Status\History\Collection::class,
70  ['getUnnotifiedForInstance', 'save', 'setIsCustomerNotified']
71  );
72  $historyItem = $this->createPartialMock(
73  \Magento\Sales\Model\Order\Status\History::class,
74  ['setIsCustomerNotified', 'save', '__wakeUp']
75  );
76  $historyItem->expects($this->at(0))
77  ->method('setIsCustomerNotified')
78  ->with(1);
79  $historyItem->expects($this->at(1))
80  ->method('save');
81  $historyCollection->expects($this->once())
82  ->method('getUnnotifiedForInstance')
83  ->with($this->order)
84  ->will($this->returnValue($historyItem));
85  $this->order->expects($this->once())
86  ->method('getEmailSent')
87  ->will($this->returnValue(true));
88  $this->historyCollectionFactory->expects($this->once())
89  ->method('create')
90  ->will($this->returnValue($historyCollection));
91 
92  $this->orderSenderMock->expects($this->once())
93  ->method('send')
94  ->with($this->equalTo($this->order));
95 
96  $this->assertTrue($this->notifier->notify($this->order));
97  }
98 
102  public function testNotifyFail()
103  {
104  $this->order->expects($this->once())
105  ->method('getEmailSent')
106  ->will($this->returnValue(false));
107  $this->assertFalse($this->notifier->notify($this->order));
108  }
109 
113  public function testNotifyException()
114  {
115  $exception = new MailException(__('Email has not been sent'));
116  $this->orderSenderMock->expects($this->once())
117  ->method('send')
118  ->with($this->equalTo($this->order))
119  ->will($this->throwException($exception));
120  $this->loggerMock->expects($this->once())
121  ->method('critical')
122  ->with($this->equalTo($exception));
123  $this->assertFalse($this->notifier->notify($this->order));
124  }
125 }
__()
Definition: __.php:13