InvoiceServiceTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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\Service;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. /**
  9. * Class InvoiceServiceTest
  10. */
  11. class InvoiceServiceTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * Repository
  15. *
  16. * @var \Magento\Sales\Api\InvoiceRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $repositoryMock;
  19. /**
  20. * Repository
  21. *
  22. * @var \Magento\Sales\Api\InvoiceCommentRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $commentRepositoryMock;
  25. /**
  26. * Search Criteria Builder
  27. *
  28. * @var \Magento\Framework\Api\SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $searchCriteriaBuilderMock;
  31. /**
  32. * Filter Builder
  33. *
  34. * @var \Magento\Framework\Api\FilterBuilder|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $filterBuilderMock;
  37. /**
  38. * Invoice Notifier
  39. *
  40. * @var \Magento\Sales\Model\Order\InvoiceNotifier|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $invoiceNotifierMock;
  43. /**
  44. * @var \Magento\Sales\Model\Service\InvoiceService
  45. */
  46. protected $invoiceService;
  47. /**
  48. * SetUp
  49. */
  50. protected function setUp()
  51. {
  52. $objectManager = new ObjectManagerHelper($this);
  53. $this->repositoryMock = $this->getMockForAbstractClass(
  54. \Magento\Sales\Api\InvoiceRepositoryInterface::class,
  55. ['get'],
  56. '',
  57. false
  58. );
  59. $this->commentRepositoryMock = $this->getMockForAbstractClass(
  60. \Magento\Sales\Api\InvoiceCommentRepositoryInterface::class,
  61. ['getList'],
  62. '',
  63. false
  64. );
  65. $this->searchCriteriaBuilderMock = $this->createPartialMock(
  66. \Magento\Framework\Api\SearchCriteriaBuilder::class,
  67. ['create', 'addFilters']
  68. );
  69. $this->filterBuilderMock = $this->createPartialMock(
  70. \Magento\Framework\Api\FilterBuilder::class,
  71. ['setField', 'setValue', 'setConditionType', 'create']
  72. );
  73. $this->invoiceNotifierMock = $this->createPartialMock(
  74. \Magento\Sales\Model\Order\InvoiceNotifier::class,
  75. ['notify']
  76. );
  77. $this->invoiceService = $objectManager->getObject(
  78. \Magento\Sales\Model\Service\InvoiceService::class,
  79. [
  80. 'repository' => $this->repositoryMock,
  81. 'commentRepository' => $this->commentRepositoryMock,
  82. 'criteriaBuilder' => $this->searchCriteriaBuilderMock,
  83. 'filterBuilder' => $this->filterBuilderMock,
  84. 'notifier' => $this->invoiceNotifierMock
  85. ]
  86. );
  87. }
  88. /**
  89. * Run test setCapture method
  90. */
  91. public function testSetCapture()
  92. {
  93. $id = 145;
  94. $returnValue = true;
  95. $invoiceMock = $this->createPartialMock(\Magento\Sales\Model\Order\Invoice::class, ['capture']);
  96. $this->repositoryMock->expects($this->once())
  97. ->method('get')
  98. ->with($id)
  99. ->will($this->returnValue($invoiceMock));
  100. $invoiceMock->expects($this->once())
  101. ->method('capture')
  102. ->will($this->returnValue($returnValue));
  103. $this->assertTrue($this->invoiceService->setCapture($id));
  104. }
  105. /**
  106. * Run test getCommentsList method
  107. */
  108. public function testGetCommentsList()
  109. {
  110. $id = 25;
  111. $returnValue = 'return-value';
  112. $filterMock = $this->createMock(\Magento\Framework\Api\Filter::class);
  113. $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
  114. $this->filterBuilderMock->expects($this->once())
  115. ->method('setField')
  116. ->with('parent_id')
  117. ->will($this->returnSelf());
  118. $this->filterBuilderMock->expects($this->once())
  119. ->method('setValue')
  120. ->with($id)
  121. ->will($this->returnSelf());
  122. $this->filterBuilderMock->expects($this->once())
  123. ->method('setConditionType')
  124. ->with('eq')
  125. ->will($this->returnSelf());
  126. $this->filterBuilderMock->expects($this->once())
  127. ->method('create')
  128. ->will($this->returnValue($filterMock));
  129. $this->searchCriteriaBuilderMock->expects($this->once())
  130. ->method('addFilters')
  131. ->with([$filterMock]);
  132. $this->searchCriteriaBuilderMock->expects($this->once())
  133. ->method('create')
  134. ->will($this->returnValue($searchCriteriaMock));
  135. $this->commentRepositoryMock->expects($this->once())
  136. ->method('getList')
  137. ->with($searchCriteriaMock)
  138. ->will($this->returnValue($returnValue));
  139. $this->assertEquals($returnValue, $this->invoiceService->getCommentsList($id));
  140. }
  141. /**
  142. * Run test notify method
  143. */
  144. public function testNotify()
  145. {
  146. $id = 123;
  147. $returnValue = 'return-value';
  148. $modelMock = $this->getMockForAbstractClass(
  149. \Magento\Sales\Model\AbstractModel::class,
  150. [],
  151. '',
  152. false
  153. );
  154. $this->repositoryMock->expects($this->once())
  155. ->method('get')
  156. ->with($id)
  157. ->will($this->returnValue($modelMock));
  158. $this->invoiceNotifierMock->expects($this->once())
  159. ->method('notify')
  160. ->with($modelMock)
  161. ->will($this->returnValue($returnValue));
  162. $this->assertEquals($returnValue, $this->invoiceService->notify($id));
  163. }
  164. /**
  165. * Run test setVoid method
  166. */
  167. public function testSetVoid()
  168. {
  169. $id = 145;
  170. $returnValue = true;
  171. $invoiceMock = $this->createPartialMock(\Magento\Sales\Model\Order\Invoice::class, ['void']);
  172. $this->repositoryMock->expects($this->once())
  173. ->method('get')
  174. ->with($id)
  175. ->will($this->returnValue($invoiceMock));
  176. $invoiceMock->expects($this->once())
  177. ->method('void')
  178. ->will($this->returnValue($returnValue));
  179. $this->assertTrue($this->invoiceService->setVoid($id));
  180. }
  181. }