ShipmentServiceTest.php 5.6 KB

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