commentRepositoryMock = $this->getMockForAbstractClass( \Magento\Sales\Api\ShipmentCommentRepositoryInterface::class, ['getList'], '', false ); $this->searchCriteriaBuilderMock = $this->createPartialMock( \Magento\Framework\Api\SearchCriteriaBuilder::class, ['create', 'addFilters'] ); $this->filterBuilderMock = $this->createPartialMock( \Magento\Framework\Api\FilterBuilder::class, ['setField', 'setValue', 'setConditionType', 'create'] ); $this->repositoryMock = $this->getMockForAbstractClass( \Magento\Sales\Api\ShipmentRepositoryInterface::class, ['get'], '', false ); $this->notifierMock = $this->createPartialMock(\Magento\Shipping\Model\ShipmentNotifier::class, ['notify']); $this->shipmentService = $objectManager->getObject( \Magento\Sales\Model\Service\ShipmentService::class, [ 'commentRepository' => $this->commentRepositoryMock, 'criteriaBuilder' => $this->searchCriteriaBuilderMock, 'filterBuilder' => $this->filterBuilderMock, 'repository' => $this->repositoryMock, 'notifier' => $this->notifierMock, ] ); } /** * Run test getLabel method */ public function testGetLabel() { $id = 145; $returnValue = 'return-value'; $shipmentMock = $this->createPartialMock(\Magento\Sales\Model\Order\Shipment::class, ['getShippingLabel']); $this->repositoryMock->expects($this->once()) ->method('get') ->with($id) ->will($this->returnValue($shipmentMock)); $shipmentMock->expects($this->once()) ->method('getShippingLabel') ->will($this->returnValue($returnValue)); $this->assertEquals($returnValue, $this->shipmentService->getLabel($id)); } /** * Run test getCommentsList method */ public function testGetCommentsList() { $id = 25; $returnValue = 'return-value'; $filterMock = $this->createMock(\Magento\Framework\Api\Filter::class); $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class); $this->filterBuilderMock->expects($this->once()) ->method('setField') ->with('parent_id') ->will($this->returnSelf()); $this->filterBuilderMock->expects($this->once()) ->method('setValue') ->with($id) ->will($this->returnSelf()); $this->filterBuilderMock->expects($this->once()) ->method('setConditionType') ->with('eq') ->will($this->returnSelf()); $this->filterBuilderMock->expects($this->once()) ->method('create') ->will($this->returnValue($filterMock)); $this->searchCriteriaBuilderMock->expects($this->once()) ->method('addFilters') ->with([$filterMock]); $this->searchCriteriaBuilderMock->expects($this->once()) ->method('create') ->will($this->returnValue($searchCriteriaMock)); $this->commentRepositoryMock->expects($this->once()) ->method('getList') ->with($searchCriteriaMock) ->will($this->returnValue($returnValue)); $this->assertEquals($returnValue, $this->shipmentService->getCommentsList($id)); } /** * Run test notify method */ public function testNotify() { $id = 123; $returnValue = 'return-value'; $modelMock = $this->getMockForAbstractClass( \Magento\Sales\Model\AbstractModel::class, [], '', false ); $this->repositoryMock->expects($this->once()) ->method('get') ->with($id) ->will($this->returnValue($modelMock)); $this->notifierMock->expects($this->once()) ->method('notify') ->with($modelMock) ->will($this->returnValue($returnValue)); $this->assertEquals($returnValue, $this->shipmentService->notify($id)); } }