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\InvoiceCommentInterfaceFactory;
13 use Magento\Sales\Api\Data\InvoiceCommentSearchResultInterfaceFactory;
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 $invoiceCommentSender;
57 
61  private $invoiceRepositoryMock;
62 
66  private $invoiceMock;
67 
71  private $commentMock;
72 
76  private $loggerMock;
77 
78  protected function setUp()
79  {
80  $this->commentResource = $this->getMockBuilder(InvoiceCommentResourceInterface::class)
81  ->disableOriginalConstructor()
82  ->getMock();
83  $this->commentFactory = $this->getMockBuilder(InvoiceCommentInterfaceFactory::class)
84  ->disableOriginalConstructor()
85  ->getMock();
86  $this->searchResultFactory = $this->getMockBuilder(InvoiceCommentSearchResultInterfaceFactory::class)
87  ->disableOriginalConstructor()
88  ->getMock();
89  $this->collectionProcessor = $this->getMockBuilder(CollectionProcessorInterface::class)
90  ->disableOriginalConstructor()
91  ->getMock();
92  $this->invoiceRepositoryMock = $this->getMockBuilder(InvoiceRepositoryInterface::class)
93  ->disableOriginalConstructor()
94  ->getMock();
95  $this->invoiceCommentSender = $this->getMockBuilder(InvoiceCommentSender::class)
96  ->disableOriginalConstructor()
97  ->getMock();
98  $this->loggerMock = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
99 
100  $this->invoiceMock = $this->getMockBuilder(Invoice::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->invoiceCommentSender,
109  $this->invoiceRepositoryMock,
110  $this->loggerMock
111  );
112  }
113 
114  public function testSave()
115  {
116  $comment = "Comment text";
117  $invoiceId = 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($invoiceId);
128  $this->commentMock->expects($this->once())
129  ->method('getComment')
130  ->willReturn($comment);
131 
132  $this->invoiceRepositoryMock->expects($this->once())
133  ->method('get')
134  ->with($invoiceId)
135  ->willReturn($this->invoiceMock);
136  $this->invoiceCommentSender->expects($this->once())
137  ->method('send')
138  ->with($this->invoiceMock, true, $comment)
139  ->willReturn(true);
140  $this->commentRepository->save($this->commentMock);
141  }
142 
147  public function testSaveWithException()
148  {
149  $this->commentResource->expects($this->once())
150  ->method('save')
151  ->with($this->commentMock)
152  ->willThrowException(
153  new \Magento\Framework\Exception\CouldNotSaveException(__('Could not save the invoice comment.'))
154  );
155 
156  $this->commentRepository->save($this->commentMock);
157  }
158 
159  public function testSaveSendCatchException()
160  {
161  $comment = "Comment text";
162  $creditmemoId = 123;
163  $this->commentResource->expects($this->once())
164  ->method('save')
165  ->with($this->commentMock)
166  ->willReturnSelf();
167  $this->commentMock->expects($this->once())
168  ->method('getIsCustomerNotified')
169  ->willReturn(1);
170  $this->commentMock->expects($this->once())
171  ->method('getParentId')
172  ->willReturn($creditmemoId);
173  $this->commentMock->expects($this->once())
174  ->method('getComment')
175  ->willReturn($comment);
176 
177  $this->invoiceRepositoryMock->expects($this->once())
178  ->method('get')
179  ->with($creditmemoId)
180  ->willReturn($this->invoiceMock);
181  $this->invoiceCommentSender->expects($this->once())
182  ->method('send')
183  ->willThrowException(new \Exception());
184  $this->loggerMock->expects($this->once())
185  ->method('critical');
186 
187  $this->commentRepository->save($this->commentMock);
188  }
189 }
__()
Definition: __.php:13