OrderNotifierTest.php 4.0 KB

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