Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CommentRepositoryTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
12 use Magento\Sales\Api\Data\ShipmentCommentInterfaceFactory;
13 use Magento\Sales\Api\Data\ShipmentCommentSearchResultInterfaceFactory;
19 use Psr\Log\LoggerInterface;
20 
26 class CommentRepositoryTest extends \PHPUnit\Framework\TestCase
27 {
31  private $commentResource;
32 
36  private $commentFactory;
37 
41  private $searchResultFactory;
42 
46  private $collectionProcessor;
47 
51  private $commentRepository;
52 
56  private $shipmentCommentSender;
57 
61  private $shipmentRepositoryMock;
62 
66  private $shipmentMock;
67 
71  private $commentMock;
72 
76  private $loggerMock;
77 
78  protected function setUp()
79  {
80  $this->commentResource = $this->getMockBuilder(ShipmentCommentResourceInterface::class)
81  ->disableOriginalConstructor()
82  ->getMock();
83  $this->commentFactory = $this->getMockBuilder(ShipmentCommentInterfaceFactory::class)
84  ->disableOriginalConstructor()
85  ->getMock();
86  $this->searchResultFactory = $this->getMockBuilder(ShipmentCommentSearchResultInterfaceFactory::class)
87  ->disableOriginalConstructor()
88  ->getMock();
89  $this->collectionProcessor = $this->getMockBuilder(CollectionProcessorInterface::class)
90  ->disableOriginalConstructor()
91  ->getMock();
92  $this->shipmentRepositoryMock = $this->getMockBuilder(ShipmentRepositoryInterface::class)
93  ->disableOriginalConstructor()
94  ->getMock();
95  $this->shipmentCommentSender = $this->getMockBuilder(ShipmentCommentSender::class)
96  ->disableOriginalConstructor()
97  ->getMock();
98  $this->loggerMock = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
99 
100  $this->shipmentMock = $this->getMockBuilder(Shipment::class)->disableOriginalConstructor()->getMock();
101  $this->commentMock = $this->getMockBuilder(Comment::class)->disableOriginalConstructor()->getMock();
102 
103  $this->commentRepository = new CommentRepository(
104  $this->commentResource,
105  $this->commentFactory,
106  $this->searchResultFactory,
107  $this->collectionProcessor,
108  $this->shipmentCommentSender,
109  $this->shipmentRepositoryMock,
110  $this->loggerMock
111  );
112  }
113 
114  public function testSave()
115  {
116  $comment = "Comment text";
117  $shipmentId = 123;
118  $this->commentResource->expects($this->once())
119  ->method('save')
120  ->with($this->commentMock)
121  ->willReturnSelf();
122  $this->commentMock->expects($this->once())
123  ->method('getIsCustomerNotified')
124  ->willReturn(1);
125  $this->commentMock->expects($this->once())
126  ->method('getParentId')
127  ->willReturn($shipmentId);
128  $this->commentMock->expects($this->once())
129  ->method('getComment')
130  ->willReturn($comment);
131 
132  $this->shipmentRepositoryMock->expects($this->once())
133  ->method('get')
134  ->with($shipmentId)
135  ->willReturn($this->shipmentMock);
136  $this->shipmentCommentSender->expects($this->once())
137  ->method('send')
138  ->with($this->shipmentMock, true, $comment);
139  $this->assertEquals($this->commentMock, $this->commentRepository->save($this->commentMock));
140  }
141 
146  public function testSaveWithException()
147  {
148  $this->commentResource->expects($this->once())
149  ->method('save')
150  ->with($this->commentMock)
151  ->willThrowException(
152  new \Magento\Framework\Exception\CouldNotSaveException(__('Could not save the shipment comment.'))
153  );
154 
155  $this->commentRepository->save($this->commentMock);
156  }
157 
158  public function testSaveSendCatchException()
159  {
160  $comment = "Comment text";
161  $creditmemoId = 123;
162  $this->commentResource->expects($this->once())
163  ->method('save')
164  ->with($this->commentMock)
165  ->willReturnSelf();
166  $this->commentMock->expects($this->once())
167  ->method('getIsCustomerNotified')
168  ->willReturn(1);
169  $this->commentMock->expects($this->once())
170  ->method('getParentId')
171  ->willReturn($creditmemoId);
172  $this->commentMock->expects($this->once())
173  ->method('getComment')
174  ->willReturn($comment);
175 
176  $this->shipmentRepositoryMock->expects($this->once())
177  ->method('get')
178  ->with($creditmemoId)
179  ->willReturn($this->shipmentMock);
180  $this->shipmentCommentSender->expects($this->once())
181  ->method('send')
182  ->willThrowException(new \Exception());
183  $this->loggerMock->expects($this->once())
184  ->method('critical');
185 
186  $this->commentRepository->save($this->commentMock);
187  }
188 }
__()
Definition: __.php:13