Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CommentsHistoryUpdaterTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Sales\Model\Order\Status\HistoryFactory;
13 use PHPUnit_Framework_MockObject_MockObject as MockObject;
15 
19 class CommentsHistoryUpdaterTest extends \PHPUnit\Framework\TestCase
20 {
24  private static $orderId = 123;
25 
29  private static $message = 'Case is created.';
30 
34  private static $status = 'On Hold';
35 
39  private $updater;
40 
44  private $historyFactory;
45 
49  private $caseEntity;
50 
54  private $historyEntity;
55 
59  private $historyRepository;
60 
64  protected function setUp()
65  {
66  $objectManager = new ObjectManager($this);
67 
68  $this->historyFactory = $this->getMockBuilder(HistoryFactory::class)
69  ->disableOriginalConstructor()
70  ->setMethods(['create', 'save'])
71  ->getMock();
72 
73  $this->historyRepository = $this->getMockBuilder(OrderStatusHistoryRepositoryInterface::class)
74  ->getMockForAbstractClass();
75 
76  $this->caseEntity = $this->getMockBuilder(CaseInterface::class)
77  ->disableOriginalConstructor()
78  ->setMethods(['getOrderId'])
79  ->getMockForAbstractClass();
80 
81  $this->initCommentMock();
82 
83  $this->updater = $objectManager->getObject(CommentsHistoryUpdater::class, [
84  'historyFactory' => $this->historyFactory,
85  'historyRepository' => $this->historyRepository
86  ]);
87  }
88 
95  public function testAddCommentWithException()
96  {
97  $this->caseEntity->expects(self::once())
98  ->method('getOrderId')
99  ->willReturn(self::$orderId);
100 
101  $this->historyEntity->method('setStatus')
102  ->with('')
103  ->willReturnSelf();
104  $this->historyRepository->expects(self::once())
105  ->method('save')
106  ->with($this->historyEntity)
107  ->willThrowException(new \Exception('Cannot save comment message.'));
108 
109  $this->updater->addComment($this->caseEntity, __(self::$message));
110  }
111 
117  public function testAddComment()
118  {
119  $this->caseEntity->expects(self::once())
120  ->method('getOrderId')
121  ->willReturn(self::$orderId);
122 
123  $this->historyEntity->method('setStatus')
124  ->with(self::$status)
125  ->willReturnSelf();
126  $this->historyRepository->expects(self::once())
127  ->method('save')
128  ->with($this->historyEntity)
129  ->willReturnSelf();
130 
131  $this->updater->addComment($this->caseEntity, __(self::$message), self::$status);
132  }
133 
140  {
141  $this->caseEntity->expects(self::never())
142  ->method('getOrderId');
143 
144  $this->historyFactory->expects(self::never())
145  ->method('save');
146 
147  $phrase = '';
148  $this->updater->addComment($this->caseEntity, __($phrase));
149  }
150 
156  private function initCommentMock()
157  {
158  $this->historyEntity = $this->getMockBuilder(OrderStatusHistoryInterface::class)
159  ->disableOriginalConstructor()
160  ->setMethods(['setParentId', 'setComment', 'setEntityName', 'save'])
161  ->getMockForAbstractClass();
162 
163  $this->historyFactory->method('create')
164  ->willReturn($this->historyEntity);
165 
166  $this->historyEntity->method('setParentId')
167  ->with(self::$orderId)
168  ->willReturnSelf();
169  $this->historyEntity->method('setComment')
170  ->with(self::$message)
171  ->willReturnSelf();
172  $this->historyEntity->method('setEntityName')
173  ->with('order')
174  ->willReturnSelf();
175  }
176 }
$objectManager
Definition: bootstrap.php:17
__()
Definition: __.php:13
$message
$status
Definition: order_status.php:8