CommentsHistoryUpdaterTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Test\Unit\Model;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\Sales\Api\Data\OrderStatusHistoryInterface;
  9. use Magento\Sales\Model\Order\Status\HistoryFactory;
  10. use Magento\Signifyd\Api\Data\CaseInterface;
  11. use Magento\Signifyd\Model\CommentsHistoryUpdater;
  12. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  13. use Magento\Sales\Api\OrderStatusHistoryRepositoryInterface;
  14. /**
  15. * Contains tests for comments history updater class.
  16. */
  17. class CommentsHistoryUpdaterTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var int
  21. */
  22. private static $orderId = 123;
  23. /**
  24. * @var string
  25. */
  26. private static $message = 'Case is created.';
  27. /**
  28. * @var string
  29. */
  30. private static $status = 'On Hold';
  31. /**
  32. * @var CommentsHistoryUpdater
  33. */
  34. private $updater;
  35. /**
  36. * @var HistoryFactory|MockObject
  37. */
  38. private $historyFactory;
  39. /**
  40. * @var CaseInterface|MockObject
  41. */
  42. private $caseEntity;
  43. /**
  44. * @var OrderStatusHistoryInterface|MockObject
  45. */
  46. private $historyEntity;
  47. /**
  48. * @var OrderStatusHistoryRepositoryInterface|MockObject
  49. */
  50. private $historyRepository;
  51. /**
  52. * @inheritdoc
  53. */
  54. protected function setUp()
  55. {
  56. $objectManager = new ObjectManager($this);
  57. $this->historyFactory = $this->getMockBuilder(HistoryFactory::class)
  58. ->disableOriginalConstructor()
  59. ->setMethods(['create', 'save'])
  60. ->getMock();
  61. $this->historyRepository = $this->getMockBuilder(OrderStatusHistoryRepositoryInterface::class)
  62. ->getMockForAbstractClass();
  63. $this->caseEntity = $this->getMockBuilder(CaseInterface::class)
  64. ->disableOriginalConstructor()
  65. ->setMethods(['getOrderId'])
  66. ->getMockForAbstractClass();
  67. $this->initCommentMock();
  68. $this->updater = $objectManager->getObject(CommentsHistoryUpdater::class, [
  69. 'historyFactory' => $this->historyFactory,
  70. 'historyRepository' => $this->historyRepository
  71. ]);
  72. }
  73. /**
  74. * Checks a test case when updater throws an exception while saving history comment.
  75. *
  76. * @covers \Magento\Signifyd\Model\CommentsHistoryUpdater::addComment
  77. * @expectedException \Exception
  78. */
  79. public function testAddCommentWithException()
  80. {
  81. $this->caseEntity->expects(self::once())
  82. ->method('getOrderId')
  83. ->willReturn(self::$orderId);
  84. $this->historyEntity->method('setStatus')
  85. ->with('')
  86. ->willReturnSelf();
  87. $this->historyRepository->expects(self::once())
  88. ->method('save')
  89. ->with($this->historyEntity)
  90. ->willThrowException(new \Exception('Cannot save comment message.'));
  91. $this->updater->addComment($this->caseEntity, __(self::$message));
  92. }
  93. /**
  94. * Checks a test case when updater successfully saves history comment.
  95. *
  96. * @covers \Magento\Signifyd\Model\CommentsHistoryUpdater::addComment
  97. */
  98. public function testAddComment()
  99. {
  100. $this->caseEntity->expects(self::once())
  101. ->method('getOrderId')
  102. ->willReturn(self::$orderId);
  103. $this->historyEntity->method('setStatus')
  104. ->with(self::$status)
  105. ->willReturnSelf();
  106. $this->historyRepository->expects(self::once())
  107. ->method('save')
  108. ->with($this->historyEntity)
  109. ->willReturnSelf();
  110. $this->updater->addComment($this->caseEntity, __(self::$message), self::$status);
  111. }
  112. /**
  113. * Checks a test when message does not specified.
  114. *
  115. * @covers \Magento\Signifyd\Model\CommentsHistoryUpdater::addComment
  116. */
  117. public function testAddCommentWithoutMessage()
  118. {
  119. $this->caseEntity->expects(self::never())
  120. ->method('getOrderId');
  121. $this->historyFactory->expects(self::never())
  122. ->method('save');
  123. $phrase = '';
  124. $this->updater->addComment($this->caseEntity, __($phrase));
  125. }
  126. /**
  127. * Creates mock object for history entity.
  128. *
  129. * @return void
  130. */
  131. private function initCommentMock()
  132. {
  133. $this->historyEntity = $this->getMockBuilder(OrderStatusHistoryInterface::class)
  134. ->disableOriginalConstructor()
  135. ->setMethods(['setParentId', 'setComment', 'setEntityName', 'save'])
  136. ->getMockForAbstractClass();
  137. $this->historyFactory->method('create')
  138. ->willReturn($this->historyEntity);
  139. $this->historyEntity->method('setParentId')
  140. ->with(self::$orderId)
  141. ->willReturnSelf();
  142. $this->historyEntity->method('setComment')
  143. ->with(self::$message)
  144. ->willReturnSelf();
  145. $this->historyEntity->method('setEntityName')
  146. ->with('order')
  147. ->willReturnSelf();
  148. }
  149. }