InvoiceNotifierTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\Order;
  7. use Magento\Framework\Exception\MailException;
  8. use Magento\Sales\Model\Order\InvoiceNotifier;
  9. use Magento\Sales\Model\ResourceModel\Order\Status\History\CollectionFactory;
  10. /**
  11. * Class InvoiceNotifierTest
  12. */
  13. class InvoiceNotifierTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var CollectionFactory |\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $historyCollectionFactory;
  19. /**
  20. * @var \Magento\Sales\Model\Order\InvoiceNotifier
  21. */
  22. protected $notifier;
  23. /**
  24. * @var \Magento\Sales\Model\Order\Invoice|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $invoice;
  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 $invoiceSenderMock;
  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->invoice = $this->createPartialMock(
  42. \Magento\Sales\Model\Order\Invoice::class,
  43. ['__wakeUp', 'getEmailSent']
  44. );
  45. $this->invoiceSenderMock = $this->createPartialMock(
  46. \Magento\Sales\Model\Order\Email\Sender\InvoiceSender::class,
  47. ['send']
  48. );
  49. $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
  50. $this->notifier = new InvoiceNotifier(
  51. $this->historyCollectionFactory,
  52. $this->loggerMock,
  53. $this->invoiceSenderMock
  54. );
  55. }
  56. /**
  57. * Test case for successful email sending
  58. */
  59. public function testNotifySuccess()
  60. {
  61. $historyCollection = $this->createPartialMock(
  62. \Magento\Sales\Model\ResourceModel\Order\Status\History\Collection::class,
  63. ['getUnnotifiedForInstance', 'save', 'setIsCustomerNotified']
  64. );
  65. $historyItem = $this->createPartialMock(
  66. \Magento\Sales\Model\Order\Status\History::class,
  67. ['setIsCustomerNotified', 'save', '__wakeUp']
  68. );
  69. $historyItem->expects($this->at(0))
  70. ->method('setIsCustomerNotified')
  71. ->with(1);
  72. $historyItem->expects($this->at(1))
  73. ->method('save');
  74. $historyCollection->expects($this->once())
  75. ->method('getUnnotifiedForInstance')
  76. ->with($this->invoice)
  77. ->will($this->returnValue($historyItem));
  78. $this->invoice->expects($this->once())
  79. ->method('getEmailSent')
  80. ->will($this->returnValue(true));
  81. $this->historyCollectionFactory->expects($this->once())
  82. ->method('create')
  83. ->will($this->returnValue($historyCollection));
  84. $this->invoiceSenderMock->expects($this->once())
  85. ->method('send')
  86. ->with($this->equalTo($this->invoice));
  87. $this->assertTrue($this->notifier->notify($this->invoice));
  88. }
  89. /**
  90. * Test case when email has not been sent
  91. */
  92. public function testNotifyFail()
  93. {
  94. $this->invoice->expects($this->once())
  95. ->method('getEmailSent')
  96. ->will($this->returnValue(false));
  97. $this->assertFalse($this->notifier->notify($this->invoice));
  98. }
  99. /**
  100. * Test case when Mail Exception has been thrown
  101. */
  102. public function testNotifyException()
  103. {
  104. $exception = new MailException(__('Email has not been sent'));
  105. $this->invoiceSenderMock->expects($this->once())
  106. ->method('send')
  107. ->with($this->equalTo($this->invoice))
  108. ->will($this->throwException($exception));
  109. $this->loggerMock->expects($this->once())
  110. ->method('critical')
  111. ->with($this->equalTo($exception));
  112. $this->assertFalse($this->notifier->notify($this->invoice));
  113. }
  114. }