ShipmentNotifierTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Shipping\Test\Unit\Model;
  7. use Magento\Framework\Exception\MailException;
  8. use Magento\Sales\Model\ResourceModel\Order\Status\History\CollectionFactory;
  9. use Magento\Shipping\Model\ShipmentNotifier;
  10. /**
  11. * Class ShipmentNotifierTest
  12. *
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class ShipmentNotifierTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var CollectionFactory |\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $historyCollectionFactory;
  21. /**
  22. * @var \Magento\Sales\Model\ShipmentNotifier
  23. */
  24. protected $notifier;
  25. /**
  26. * @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $shipment;
  29. /**
  30. * @var \Magento\Framework\ObjectManagerInterface |\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $loggerMock;
  33. /**
  34. * @var \Magento\Framework\ObjectManager\ObjectManager |\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $shipmentSenderMock;
  37. protected function setUp()
  38. {
  39. $this->historyCollectionFactory = $this->createPartialMock(
  40. \Magento\Sales\Model\ResourceModel\Order\Status\History\CollectionFactory::class,
  41. ['create']
  42. );
  43. $this->shipment = $this->createPartialMock(
  44. \Magento\Sales\Model\Order\Shipment::class,
  45. ['__wakeUp', 'getEmailSent']
  46. );
  47. $this->shipmentSenderMock = $this->createPartialMock(
  48. \Magento\Sales\Model\Order\Email\Sender\ShipmentSender::class,
  49. ['send']
  50. );
  51. $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
  52. $this->notifier = new ShipmentNotifier(
  53. $this->historyCollectionFactory,
  54. $this->loggerMock,
  55. $this->shipmentSenderMock
  56. );
  57. }
  58. /**
  59. * Test case for successful email sending
  60. */
  61. public function testNotifySuccess()
  62. {
  63. $historyCollection = $this->createPartialMock(
  64. \Magento\Sales\Model\ResourceModel\Order\Status\History\Collection::class,
  65. ['getUnnotifiedForInstance', 'save', 'setIsCustomerNotified']
  66. );
  67. $historyItem = $this->createPartialMock(
  68. \Magento\Sales\Model\Order\Status\History::class,
  69. ['setIsCustomerNotified', 'save', '__wakeUp']
  70. );
  71. $historyItem->expects($this->at(0))
  72. ->method('setIsCustomerNotified')
  73. ->with(1);
  74. $historyItem->expects($this->at(1))
  75. ->method('save');
  76. $historyCollection->expects($this->once())
  77. ->method('getUnnotifiedForInstance')
  78. ->with($this->shipment)
  79. ->will($this->returnValue($historyItem));
  80. $this->shipment->expects($this->once())
  81. ->method('getEmailSent')
  82. ->will($this->returnValue(true));
  83. $this->historyCollectionFactory->expects($this->once())
  84. ->method('create')
  85. ->will($this->returnValue($historyCollection));
  86. $this->shipmentSenderMock->expects($this->once())
  87. ->method('send')
  88. ->with($this->equalTo($this->shipment));
  89. $this->assertTrue($this->notifier->notify($this->shipment));
  90. }
  91. /**
  92. * Test case when email has not been sent
  93. */
  94. public function testNotifyFail()
  95. {
  96. $this->shipment->expects($this->once())
  97. ->method('getEmailSent')
  98. ->will($this->returnValue(false));
  99. $this->assertFalse($this->notifier->notify($this->shipment));
  100. }
  101. /**
  102. * Test case when Mail Exception has been thrown
  103. */
  104. public function testNotifyException()
  105. {
  106. $exception = new MailException(__('Email has not been sent'));
  107. $this->shipmentSenderMock->expects($this->once())
  108. ->method('send')
  109. ->with($this->equalTo($this->shipment))
  110. ->will($this->throwException($exception));
  111. $this->loggerMock->expects($this->once())
  112. ->method('critical')
  113. ->with($this->equalTo($exception));
  114. $this->assertFalse($this->notifier->notify($this->shipment));
  115. }
  116. }