123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?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);
- }
- }
|