Untitled

mail@pastecode.io avatar
unknown
php
2 years ago
4.8 kB
2
Indexable
Never
<?php

declare(strict_types=1);

namespace Creativestyle\CustomizationMegaportKaspersky\Test\Integration\Model\Queue;

/**
 * @magentoAppArea frontend
 */
class GenerateLicenseForOrderItemTest extends \Creativestyle\CustomizationMegaportKaspersky\Test\Integration\CommonTestAbstract
{
    protected ?\Creativestyle\CustomizationMegaportKaspersky\Model\Queue\GenerateLicenseForOrderItem $generateLicenseForOrderItem;
    protected ?\Magento\Framework\ObjectManagerInterface $objectManager;
    protected ?\Magento\Catalog\Api\ProductRepositoryInterface $productRepository;

    protected function setUp(): void
    {
        $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
        $this->generateLicenseForOrderItem = $this->objectManager->create(\Creativestyle\CustomizationMegaportKaspersky\Model\Queue\GenerateLicenseForOrderItem::class);
        $this->productRepository = $this->objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
    }

    /**
     * @magentoDbIsolation enabled
     * @magentoAppIsolation enabled
     * @magentoDataFixture Magento/Sales/_files/order.php
     */
    public function testExecuteWithNonLicenseOrderItem()
    {
        $order = $this->objectManager->create(\Magento\Sales\Model\Order::class)->loadByIncrementId('100000001');
        $orderItems = $order->getAllItems();
        $orderItem = reset($orderItems);

        $exceptionMessage = sprintf('Item ID %s can not be processed.', $orderItem->getId());

        $this->expectException(\Magento\Framework\Exception\InputException::class);
        $this->expectExceptionMessage($exceptionMessage);

        $this->generateLicenseForOrderItem->execute([$orderItem->getId()]);
    }

    /**
     * @magentoDbIsolation enabled
     * @magentoAppIsolation enabled
     * @magentoDataFixture Creativestyle_CustomizationMegaportKaspersky::Test/Integration/_files/order_with_kaspersky_license.php
     */
    public function testExecuteWithLicenseOrderItem()
    {
        $order = $this->objectManager->create(\Magento\Sales\Model\Order::class)->loadByIncrementId('100000999');
        $orderItem = $this->getLicenseOrderItemFromOrder($order);

        $result = $this->generateLicenseForOrderItem->execute([$orderItem->getId()]);
        $this->assertInstanceOf(
            \Creativestyle\CustomizationMegaportKaspersky\Model\Queue\GenerateLicenseForOrderItem::class,
            $result,
            'Method GenerateLicenseForOrderItem:execute($liceseOrderItemsIds) class does not return self'
        );
    }

    /**
     * @magentoDbIsolation enabled
     * @magentoAppIsolation enabled
     * @magentoDataFixture Creativestyle_CustomizationMegaportKaspersky::Test/Integration/_files/order_with_kaspersky_license.php
     */
    public function testExecuteTwiceForLicenseItem()
    {
        $order = $this->objectManager->create(\Magento\Sales\Model\Order::class)->loadByIncrementId('100000999');
        $orderItem = $this->getLicenseOrderItemFromOrder($order);
        $exceptionMessage = sprintf('For Item ID %s a license has been already generated.', $orderItem->getId());

        $this->expectException(\Magento\Framework\Exception\CouldNotSaveException::class);
        $this->expectExceptionMessage($exceptionMessage);

        $this->generateLicenseForOrderItem->execute([$orderItem->getId()]);
        $this->generateLicenseForOrderItem->execute([$orderItem->getId()]);
    }

    /**
     * @magentoDbIsolation enabled
     * @magentoAppIsolation enabled
     * @magentoAppArea adminhtml
     * @magentoDataFixtureBeforeTransaction Creativestyle_CustomizationMegaportKaspersky::Test/Integration/_files/order_with_kaspersky_license.php
     */
    public function testExectuteIfProductIsDeletedFromCatalog()
    {
        $order = $this->objectManager->create(\Magento\Sales\Model\Order::class)->loadByIncrementId('100000999');
        $orderItem = $this->getLicenseOrderItemFromOrder($order);

        $registry = $this->objectManager->create(\Magento\Framework\Registry::class);
        $registry->unregister('isSecureArea');
        $registry->register('isSecureArea', true);

        $this->productRepository->deleteById($orderItem->getSku());

        $this->expectException(\Magento\Framework\Exception\NoSuchEntityException::class);
        $this->expectExceptionMessage('The product that was requested doesn\'t exist. Verify the product and try again.');

        $licenseServiceStub = $this->getMockBuilder(\Creativestyle\CustomizationMegaportKaspersky\Service\LicenseService::class)
            ->disableOriginalConstructor()
            ->getMock();
        $licenseServiceStub->expects($this->never())
            ->method('getClient');

        $this->generateLicenseForOrderItem->execute([$orderItem->getId()]);
    }
}