123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Sales\Test\Unit\Model;
- use Magento\Framework\App\ResourceConnection;
- use Magento\Framework\DB\Adapter\AdapterInterface;
- use Magento\Sales\Api\Data\OrderInterface;
- use Magento\Sales\Api\Data\ShipmentCommentCreationInterface;
- use Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface;
- use Magento\Sales\Api\Data\ShipmentInterface;
- use Magento\Sales\Api\Data\ShipmentPackageInterface;
- use Magento\Sales\Api\Data\ShipmentTrackCreationInterface;
- use Magento\Sales\Api\OrderRepositoryInterface;
- use Magento\Sales\Api\ShipmentRepositoryInterface;
- use Magento\Sales\Model\Order;
- use Magento\Sales\Model\Order\Config as OrderConfig;
- use Magento\Sales\Model\Order\OrderStateResolverInterface;
- use Magento\Sales\Model\Order\ShipmentDocumentFactory;
- use Magento\Sales\Model\Order\Shipment\NotifierInterface;
- use Magento\Sales\Model\Order\Shipment\OrderRegistrarInterface;
- use Magento\Sales\Model\Order\Validation\ShipOrderInterface;
- use Magento\Sales\Model\ValidatorResultInterface;
- use Magento\Sales\Model\ShipOrder;
- use Psr\Log\LoggerInterface;
- /**
- * Class ShipOrderTest
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @SuppressWarnings(PHPMD.TooManyFields)
- */
- class ShipOrderTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
- */
- private $resourceConnectionMock;
- /**
- * @var OrderRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $orderRepositoryMock;
- /**
- * @var ShipmentDocumentFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $shipmentDocumentFactoryMock;
- /**
- * @var ShipOrderInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $shipOrderValidatorMock;
- /**
- * @var OrderRegistrarInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $orderRegistrarMock;
- /**
- * @var OrderStateResolverInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $orderStateResolverMock;
- /**
- * @var OrderConfig|\PHPUnit_Framework_MockObject_MockObject
- */
- private $configMock;
- /**
- * @var ShipmentRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $shipmentRepositoryMock;
- /**
- * @var NotifierInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $notifierInterfaceMock;
- /**
- * @var ShipOrder|\PHPUnit_Framework_MockObject_MockObject
- */
- private $model;
- /**
- * @var ShipmentCreationArgumentsInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $shipmentCommentCreationMock;
- /**
- * @var ShipmentCommentCreationInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $shipmentCreationArgumentsMock;
- /**
- * @var OrderInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $orderMock;
- /**
- * @var ShipmentInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $shipmentMock;
- /**
- * @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $adapterMock;
- /**
- * @var ShipmentTrackCreationInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $trackMock;
- /**
- * @var ShipmentPackageInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $packageMock;
- /**
- * @var ValidatorResultInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $validationMessagesMock;
- /**
- * @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $loggerMock;
- protected function setUp()
- {
- $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->orderRepositoryMock = $this->getMockBuilder(OrderRepositoryInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->shipmentDocumentFactoryMock = $this->getMockBuilder(ShipmentDocumentFactory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->orderRegistrarMock = $this->getMockBuilder(OrderRegistrarInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->orderStateResolverMock = $this->getMockBuilder(OrderStateResolverInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->configMock = $this->getMockBuilder(OrderConfig::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->shipmentRepositoryMock = $this->getMockBuilder(ShipmentRepositoryInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->notifierInterfaceMock = $this->getMockBuilder(NotifierInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->shipmentCommentCreationMock = $this->getMockBuilder(ShipmentCommentCreationInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->shipmentCreationArgumentsMock = $this->getMockBuilder(ShipmentCreationArgumentsInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->orderMock = $this->getMockBuilder(OrderInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->shipmentMock = $this->getMockBuilder(ShipmentInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->packageMock = $this->getMockBuilder(ShipmentPackageInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->trackMock = $this->getMockBuilder(ShipmentTrackCreationInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->adapterMock = $this->getMockBuilder(AdapterInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->shipOrderValidatorMock = $this->getMockBuilder(ShipOrderInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->validationMessagesMock = $this->getMockBuilder(ValidatorResultInterface::class)
- ->disableOriginalConstructor()
- ->setMethods(['hasMessages', 'getMessages', 'addMessage'])
- ->getMock();
- $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->model = $helper->getObject(
- ShipOrder::class,
- [
- 'resourceConnection' => $this->resourceConnectionMock,
- 'orderRepository' => $this->orderRepositoryMock,
- 'shipmentDocumentFactory' => $this->shipmentDocumentFactoryMock,
- 'orderStateResolver' => $this->orderStateResolverMock,
- 'config' => $this->configMock,
- 'shipmentRepository' => $this->shipmentRepositoryMock,
- 'shipOrderValidator' => $this->shipOrderValidatorMock,
- 'notifierInterface' => $this->notifierInterfaceMock,
- 'logger' => $this->loggerMock,
- 'orderRegistrar' => $this->orderRegistrarMock
- ]
- );
- }
- /**
- * @param int $orderId
- * @param array $items
- * @param bool $notify
- * @param bool $appendComment
- * @throws \Magento\Sales\Exception\CouldNotShipException
- * @throws \Magento\Sales\Exception\DocumentValidationException
- * @dataProvider dataProvider
- */
- public function testExecute($orderId, $items, $notify, $appendComment)
- {
- $this->resourceConnectionMock->expects($this->once())
- ->method('getConnection')
- ->with('sales')
- ->willReturn($this->adapterMock);
- $this->orderRepositoryMock->expects($this->once())
- ->method('get')
- ->willReturn($this->orderMock);
- $this->shipmentDocumentFactoryMock->expects($this->once())
- ->method('create')
- ->with(
- $this->orderMock,
- $items,
- [$this->trackMock],
- $this->shipmentCommentCreationMock,
- ($appendComment && $notify),
- [$this->packageMock],
- $this->shipmentCreationArgumentsMock
- )->willReturn($this->shipmentMock);
- $this->shipOrderValidatorMock->expects($this->once())
- ->method('validate')
- ->with(
- $this->orderMock,
- $this->shipmentMock,
- $items,
- $notify,
- $appendComment,
- $this->shipmentCommentCreationMock,
- [$this->trackMock],
- [$this->packageMock]
- )
- ->willReturn($this->validationMessagesMock);
- $hasMessages = false;
- $this->validationMessagesMock->expects($this->once())
- ->method('hasMessages')->willReturn($hasMessages);
- $this->orderRegistrarMock->expects($this->once())
- ->method('register')
- ->with($this->orderMock, $this->shipmentMock)
- ->willReturn($this->orderMock);
- $this->orderStateResolverMock->expects($this->once())
- ->method('getStateForOrder')
- ->with($this->orderMock, [OrderStateResolverInterface::IN_PROGRESS])
- ->willReturn(Order::STATE_PROCESSING);
- $this->orderMock->expects($this->once())
- ->method('setState')
- ->with(Order::STATE_PROCESSING)
- ->willReturnSelf();
- $this->orderMock->expects($this->once())
- ->method('getState')
- ->willReturn(Order::STATE_PROCESSING);
- $this->configMock->expects($this->once())
- ->method('getStateDefaultStatus')
- ->with(Order::STATE_PROCESSING)
- ->willReturn('Processing');
- $this->orderMock->expects($this->once())
- ->method('setStatus')
- ->with('Processing')
- ->willReturnSelf();
- $this->shipmentRepositoryMock->expects($this->once())
- ->method('save')
- ->with($this->shipmentMock)
- ->willReturn($this->shipmentMock);
- $this->orderRepositoryMock->expects($this->once())
- ->method('save')
- ->with($this->orderMock)
- ->willReturn($this->orderMock);
- if ($notify) {
- $this->notifierInterfaceMock->expects($this->once())
- ->method('notify')
- ->with($this->orderMock, $this->shipmentMock, $this->shipmentCommentCreationMock);
- }
- $this->shipmentMock->expects($this->once())
- ->method('getEntityId')
- ->willReturn(2);
- $this->assertEquals(
- 2,
- $this->model->execute(
- $orderId,
- $items,
- $notify,
- $appendComment,
- $this->shipmentCommentCreationMock,
- [$this->trackMock],
- [$this->packageMock],
- $this->shipmentCreationArgumentsMock
- )
- );
- }
- /**
- * @expectedException \Magento\Sales\Api\Exception\DocumentValidationExceptionInterface
- */
- public function testDocumentValidationException()
- {
- $orderId = 1;
- $items = [1 => 2];
- $notify = true;
- $appendComment = true;
- $errorMessages = ['error1', 'error2'];
- $this->orderRepositoryMock->expects($this->once())
- ->method('get')
- ->willReturn($this->orderMock);
- $this->shipmentDocumentFactoryMock->expects($this->once())
- ->method('create')
- ->with(
- $this->orderMock,
- $items,
- [$this->trackMock],
- $this->shipmentCommentCreationMock,
- ($appendComment && $notify),
- [$this->packageMock],
- $this->shipmentCreationArgumentsMock
- )->willReturn($this->shipmentMock);
- $this->shipOrderValidatorMock->expects($this->once())
- ->method('validate')
- ->with(
- $this->orderMock,
- $this->shipmentMock,
- $items,
- $notify,
- $appendComment,
- $this->shipmentCommentCreationMock,
- [$this->trackMock],
- [$this->packageMock]
- )
- ->willReturn($this->validationMessagesMock);
- $hasMessages = true;
- $this->validationMessagesMock->expects($this->once())
- ->method('hasMessages')->willReturn($hasMessages);
- $this->validationMessagesMock->expects($this->once())
- ->method('getMessages')->willReturn($errorMessages);
- $this->model->execute(
- $orderId,
- $items,
- $notify,
- $appendComment,
- $this->shipmentCommentCreationMock,
- [$this->trackMock],
- [$this->packageMock],
- $this->shipmentCreationArgumentsMock
- );
- }
- /**
- * @expectedException \Magento\Sales\Api\Exception\CouldNotShipExceptionInterface
- */
- public function testCouldNotShipException()
- {
- $orderId = 1;
- $items = [1 => 2];
- $notify = true;
- $appendComment = true;
- $this->resourceConnectionMock->expects($this->once())
- ->method('getConnection')
- ->with('sales')
- ->willReturn($this->adapterMock);
- $this->orderRepositoryMock->expects($this->once())
- ->method('get')
- ->willReturn($this->orderMock);
- $this->shipmentDocumentFactoryMock->expects($this->once())
- ->method('create')
- ->with(
- $this->orderMock,
- $items,
- [$this->trackMock],
- $this->shipmentCommentCreationMock,
- ($appendComment && $notify),
- [$this->packageMock],
- $this->shipmentCreationArgumentsMock
- )->willReturn($this->shipmentMock);
- $this->shipOrderValidatorMock->expects($this->once())
- ->method('validate')
- ->with(
- $this->orderMock,
- $this->shipmentMock,
- $items,
- $notify,
- $appendComment,
- $this->shipmentCommentCreationMock,
- [$this->trackMock],
- [$this->packageMock]
- )
- ->willReturn($this->validationMessagesMock);
- $hasMessages = false;
- $this->validationMessagesMock->expects($this->once())
- ->method('hasMessages')->willReturn($hasMessages);
- $exception = new \Exception();
- $this->orderRegistrarMock->expects($this->once())
- ->method('register')
- ->with($this->orderMock, $this->shipmentMock)
- ->willThrowException($exception);
- $this->loggerMock->expects($this->once())
- ->method('critical')
- ->with($exception);
- $this->adapterMock->expects($this->once())
- ->method('rollBack');
- $this->model->execute(
- $orderId,
- $items,
- $notify,
- $appendComment,
- $this->shipmentCommentCreationMock,
- [$this->trackMock],
- [$this->packageMock],
- $this->shipmentCreationArgumentsMock
- );
- }
- /**
- * @return array
- */
- public function dataProvider()
- {
- return [
- 'TestWithNotifyTrue' => [1, [1 => 2], true, true],
- 'TestWithNotifyFalse' => [1, [1 => 2], false, true],
- ];
- }
- }
|