LogEntryRotatorTest.php 4.73 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
<?php
/**
 * @copyright  Vertex. All rights reserved.  https://www.vertexinc.com/
 * @author     Mediotype                     https://www.mediotype.com/
 */

namespace Vertex\Tax\Test\Integration\Logging;

use Magento\Framework\Api\SearchCriteriaInterface;
use Vertex\Tax\Api\Data\LogEntryInterface;
use Vertex\Tax\Api\Data\LogEntryInterfaceFactory;
use Vertex\Tax\Api\LogEntryRepositoryInterface;
use Vertex\Tax\Cron\LogRotate;
use Vertex\Tax\Test\Integration\TestCase;

/**
 * Test that log rotation works as designed
 */
class LogEntryRotatorTest extends TestCase
{
    /** @var LogRotate */
    private $logRotate;

    /** @var LogEntryRepositoryInterface */
    private $repository;

    /** @var SearchCriteriaInterface */
    private $emptyCriteria;

    /**
     * @inheritdoc
     */
    protected function setUp()
    {
        parent::setUp();

        $this->logRotate = $this->getObject(LogRotate::class);
        $this->repository = $this->getObject(LogEntryRepositoryInterface::class);
        $this->emptyCriteria = $this->getObject(SearchCriteriaInterface::class);
    }

    /**
     * Test that when the entry lifetime is 1 logs older than 1 day are rotated
     *
     * @magentoConfigFixture default/tax/vertex_logging/enable_rotation 1
     * @magentoConfigFixture default_store tax/vertex_logging/entry_lifetime 1
     * @magentoDataFixture loadFixture
     * @magentoDbIsolation enabled
     *
     * @return void
     * @throws \Magento\Framework\Exception\CouldNotDeleteException
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function testDeletionWithLifetimeOfOne()
    {
        // Sanity Assertion
        $beforeResults = $this->repository->getList($this->emptyCriteria);
        if ($beforeResults->getTotalCount() !== 3) {
            $this->fail('Fixture should have created three entries.  Total amount: '. $beforeResults->getTotalCount());
        }

        $this->logRotate->execute();

        $afterResults = $this->repository->getList($this->emptyCriteria);
        $this->assertEquals(1, $afterResults->getTotalCount());
    }

    /**
     * Test that when the entry lifetime is 2 logs older than 2 days are rotated
     *
     * @magentoConfigFixture default/tax/vertex_logging/enable_rotation 1
     * @magentoConfigFixture default_store tax/vertex_logging/entry_lifetime 2
     * @magentoDataFixture loadFixture
     * @magentoDbIsolation enabled
     *
     * @return void
     * @throws \Magento\Framework\Exception\CouldNotDeleteException
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function testDeletionWithLifetimeOfTwo()
    {
        // Sanity Assertion
        $beforeResults = $this->repository->getList($this->emptyCriteria);
        if ($beforeResults->getTotalCount() !== 3) {
            $this->fail('Fixture should have created three entries.  Total amount: '. $beforeResults->getTotalCount());
        }

        $this->logRotate->execute();

        $afterResults = $this->repository->getList($this->emptyCriteria);
        $this->assertEquals(2, $afterResults->getTotalCount());
    }

    /**
     * Load in test log entries
     *
     * @return void
     * @throws \Magento\Framework\Exception\CouldNotSaveException
     */
    public static function loadFixture()
    {
        /** @var \Magento\Framework\ObjectManagerInterface $om */
        $om = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

        /** @var LogEntryRepositoryInterface $repository */
        $repository = $om->get(LogEntryRepositoryInterface::class);

        /** @var LogEntryInterfaceFactory $factory */
        $factory = $om->get(LogEntryInterfaceFactory::class);

        $periodTwoHours = new \DateInterval('PT2H');
        $periodTwoDaysTwoHours = new \DateInterval('P1DT2H');
        $periodThreeDaysTwoHours = new \DateInterval('P2DT2H');

        $now = new \DateTimeImmutable();
        $twoHoursAgo = $now->sub($periodTwoHours);
        $oneDayTwoHoursAgo = $now->sub($periodTwoDaysTwoHours);
        $twoDaysTwoHoursAgo = $now->sub($periodThreeDaysTwoHours);

        /** @var LogEntryInterface $twoHoursAgoEntry */
        $twoHoursAgoEntry = $factory->create();
        $twoHoursAgoEntry->setDate($twoHoursAgo->format('Y-m-d H:i:s'));
        $repository->save($twoHoursAgoEntry);

        /** @var LogEntryInterface $oneDayTwoHoursAgoEntry */
        $oneDayTwoHoursAgoEntry = $factory->create();
        $oneDayTwoHoursAgoEntry->setDate($oneDayTwoHoursAgo->format('Y-m-d H:i:s'));
        $repository->save($oneDayTwoHoursAgoEntry);

        /** @var LogEntryInterface $twoDaysTwoHoursAgoEntry */
        $twoDaysTwoHoursAgoEntry = $factory->create();
        $twoDaysTwoHoursAgoEntry->setDate($twoDaysTwoHoursAgo->format('Y-m-d H:i:s'));
        $repository->save($twoDaysTwoHoursAgoEntry);
    }
}