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