<?php
/**
 * Copyright 2016 Adobe
 * All Rights Reserved.
 */
declare(strict_types=1);

namespace Magento\Sales\Test\Unit\Model\Order;

use Magento\Framework\EntityManager\HydratorInterface;
use Magento\Framework\EntityManager\HydratorPool;
use Magento\Sales\Api\Data\ShipmentCommentCreationInterface;
use Magento\Sales\Api\Data\ShipmentInterface;
use Magento\Sales\Api\Data\ShipmentItemCreationInterface;
use Magento\Sales\Api\Data\ShipmentTrackCreationInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Shipment;
use Magento\Sales\Model\Order\Shipment\Track;
use Magento\Sales\Model\Order\Shipment\TrackFactory;
use Magento\Sales\Model\Order\ShipmentDocumentFactory;
use Magento\Sales\Model\Order\ShipmentDocumentFactory\ExtensionAttributesProcessor;
use Magento\Sales\Model\Order\ShipmentFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Magento\Framework\TestFramework\Unit\Helper\MockCreationTrait;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class ShipmentDocumentFactoryTest extends TestCase
{
    use MockCreationTrait;

    /**
     * @var MockObject|ShipmentFactory
     */
    private $shipmentFactoryMock;

    /**
     * @var MockObject|Order
     */
    private $orderMock;

    /**
     * @var MockObject|ShipmentItemCreationInterface
     */
    private $itemMock;

    /**
     * @var MockObject|ShipmentCommentCreationInterface
     */
    private $commentMock;

    /**
     * @var MockObject|ShipmentInterface
     */
    private $shipmentMock;

    /**
     * @var ShipmentDocumentFactory
     */
    private $shipmentDocumentFactory;

    /**
     * @var MockObject|HydratorPool
     */
    private $hydratorPoolMock;

    /**
     * @var MockObject|TrackFactory
     */
    private $trackFactoryMock;

    /**
     * @var MockObject|HydratorInterface
     */
    private $hydratorMock;

    /**
     * @var MockObject|ExtensionAttributesProcessor
     */
    private $extensionAttributeProcessorMock;

    /**
     * @var MockObject|Track
     */
    private $trackMock;

    protected function setUp(): void
    {
        $this->shipmentFactoryMock = $this->createMock(ShipmentFactory::class);

        $this->orderMock = $this->createMock(Order::class);

        $this->itemMock = $this->createMock(ShipmentItemCreationInterface::class);

        $this->commentMock = $this->createMock(ShipmentCommentCreationInterface::class);

        $this->shipmentMock = $this->createPartialMockWithReflection(
            Shipment::class,
            ['addComment', 'addTrack', 'setCustomerNote', 'setCustomerNoteNotify']
        );

        $this->hydratorPoolMock = $this->createMock(HydratorPool::class);

        $this->trackFactoryMock = $this->getMockBuilder(TrackFactory::class)
            ->onlyMethods(['create'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->trackMock = $this->createMock(Track::class);

        $this->hydratorMock = $this->createMock(HydratorInterface::class);

        $this->extensionAttributeProcessorMock = $this->createMock(ExtensionAttributesProcessor::class);

        $this->shipmentDocumentFactory = new ShipmentDocumentFactory(
            $this->shipmentFactoryMock,
            $this->hydratorPoolMock,
            $this->trackFactoryMock,
            $this->extensionAttributeProcessorMock
        );
    }

    public function testCreate()
    {
        $trackNum = "123456789";
        $trackData = [$trackNum];
        $tracks = [$this->trackMock];
        $appendComment = true;
        $packages = [];
        $items = [1 => 10];

        $this->extensionAttributeProcessorMock->expects($this->never())->method('execute');
        $this->itemMock->expects($this->once())->method('getOrderItemId')->willReturn(1);
        $this->itemMock->expects($this->once())->method('getQty')->willReturn(10);
        $this->itemMock->expects($this->once())
            ->method('getOrderItemId')
            ->willReturn(1);

        $this->itemMock->expects($this->once())
            ->method('getQty')
            ->willReturn(10);

        $this->shipmentFactoryMock->expects($this->once())
            ->method('create')
            ->with(
                $this->orderMock,
                $items
            )
            ->willReturn($this->shipmentMock);

        $this->shipmentMock->expects($this->once())
            ->method('addTrack')
            ->willReturnSelf();

        $this->hydratorPoolMock->expects($this->once())
            ->method('getHydrator')
            ->with(ShipmentTrackCreationInterface::class)
            ->willReturn($this->hydratorMock);

        $this->hydratorMock->expects($this->once())
            ->method('extract')
            ->with($this->trackMock)
            ->willReturn($trackData);

        $this->trackFactoryMock->expects($this->once())
            ->method('create')
            ->with(['data' => $trackData])
            ->willReturn($this->trackMock);

        if ($appendComment) {
            $comment = "New comment!";
            $visibleOnFront = true;
            $this->commentMock->expects($this->exactly(2))
                ->method('getComment')
                ->willReturn($comment);

            $this->commentMock->expects($this->once())
                ->method('getIsVisibleOnFront')
                ->willReturn($visibleOnFront);

            $this->shipmentMock->expects($this->once())
                ->method('addComment')
                ->with($comment, $appendComment, $visibleOnFront)
                ->willReturnSelf();

            $this->shipmentMock->expects($this->once())
                ->method('setCustomerNoteNotify')
                ->with(true);
        }

        $this->assertEquals(
            $this->shipmentDocumentFactory->create(
                $this->orderMock,
                [$this->itemMock],
                $tracks,
                $this->commentMock,
                $appendComment,
                $packages
            ),
            $this->shipmentMock
        );
    }
}
