LogEntryRotatorTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Test\Integration\Logging;
  7. use Magento\Framework\Api\SearchCriteriaInterface;
  8. use Vertex\Tax\Api\Data\LogEntryInterface;
  9. use Vertex\Tax\Api\Data\LogEntryInterfaceFactory;
  10. use Vertex\Tax\Api\LogEntryRepositoryInterface;
  11. use Vertex\Tax\Cron\LogRotate;
  12. use Vertex\Tax\Test\Integration\TestCase;
  13. /**
  14. * Test that log rotation works as designed
  15. */
  16. class LogEntryRotatorTest extends TestCase
  17. {
  18. /** @var LogRotate */
  19. private $logRotate;
  20. /** @var LogEntryRepositoryInterface */
  21. private $repository;
  22. /** @var SearchCriteriaInterface */
  23. private $emptyCriteria;
  24. /**
  25. * @inheritdoc
  26. */
  27. protected function setUp()
  28. {
  29. parent::setUp();
  30. $this->logRotate = $this->getObject(LogRotate::class);
  31. $this->repository = $this->getObject(LogEntryRepositoryInterface::class);
  32. $this->emptyCriteria = $this->getObject(SearchCriteriaInterface::class);
  33. }
  34. /**
  35. * Test that when the entry lifetime is 1 logs older than 1 day are rotated
  36. *
  37. * @magentoConfigFixture default/tax/vertex_logging/enable_rotation 1
  38. * @magentoConfigFixture default_store tax/vertex_logging/entry_lifetime 1
  39. * @magentoDataFixture loadFixture
  40. * @magentoDbIsolation enabled
  41. *
  42. * @return void
  43. * @throws \Magento\Framework\Exception\CouldNotDeleteException
  44. * @throws \Magento\Framework\Exception\LocalizedException
  45. */
  46. public function testDeletionWithLifetimeOfOne()
  47. {
  48. // Sanity Assertion
  49. $beforeResults = $this->repository->getList($this->emptyCriteria);
  50. if ($beforeResults->getTotalCount() !== 3) {
  51. $this->fail('Fixture should have created three entries. Total amount: '. $beforeResults->getTotalCount());
  52. }
  53. $this->logRotate->execute();
  54. $afterResults = $this->repository->getList($this->emptyCriteria);
  55. $this->assertEquals(1, $afterResults->getTotalCount());
  56. }
  57. /**
  58. * Test that when the entry lifetime is 2 logs older than 2 days are rotated
  59. *
  60. * @magentoConfigFixture default/tax/vertex_logging/enable_rotation 1
  61. * @magentoConfigFixture default_store tax/vertex_logging/entry_lifetime 2
  62. * @magentoDataFixture loadFixture
  63. * @magentoDbIsolation enabled
  64. *
  65. * @return void
  66. * @throws \Magento\Framework\Exception\CouldNotDeleteException
  67. * @throws \Magento\Framework\Exception\LocalizedException
  68. */
  69. public function testDeletionWithLifetimeOfTwo()
  70. {
  71. // Sanity Assertion
  72. $beforeResults = $this->repository->getList($this->emptyCriteria);
  73. if ($beforeResults->getTotalCount() !== 3) {
  74. $this->fail('Fixture should have created three entries. Total amount: '. $beforeResults->getTotalCount());
  75. }
  76. $this->logRotate->execute();
  77. $afterResults = $this->repository->getList($this->emptyCriteria);
  78. $this->assertEquals(2, $afterResults->getTotalCount());
  79. }
  80. /**
  81. * Load in test log entries
  82. *
  83. * @return void
  84. * @throws \Magento\Framework\Exception\CouldNotSaveException
  85. */
  86. public static function loadFixture()
  87. {
  88. /** @var \Magento\Framework\ObjectManagerInterface $om */
  89. $om = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  90. /** @var LogEntryRepositoryInterface $repository */
  91. $repository = $om->get(LogEntryRepositoryInterface::class);
  92. /** @var LogEntryInterfaceFactory $factory */
  93. $factory = $om->get(LogEntryInterfaceFactory::class);
  94. $periodTwoHours = new \DateInterval('PT2H');
  95. $periodTwoDaysTwoHours = new \DateInterval('P1DT2H');
  96. $periodThreeDaysTwoHours = new \DateInterval('P2DT2H');
  97. $now = new \DateTimeImmutable();
  98. $twoHoursAgo = $now->sub($periodTwoHours);
  99. $oneDayTwoHoursAgo = $now->sub($periodTwoDaysTwoHours);
  100. $twoDaysTwoHoursAgo = $now->sub($periodThreeDaysTwoHours);
  101. /** @var LogEntryInterface $twoHoursAgoEntry */
  102. $twoHoursAgoEntry = $factory->create();
  103. $twoHoursAgoEntry->setDate($twoHoursAgo->format('Y-m-d H:i:s'));
  104. $repository->save($twoHoursAgoEntry);
  105. /** @var LogEntryInterface $oneDayTwoHoursAgoEntry */
  106. $oneDayTwoHoursAgoEntry = $factory->create();
  107. $oneDayTwoHoursAgoEntry->setDate($oneDayTwoHoursAgo->format('Y-m-d H:i:s'));
  108. $repository->save($oneDayTwoHoursAgoEntry);
  109. /** @var LogEntryInterface $twoDaysTwoHoursAgoEntry */
  110. $twoDaysTwoHoursAgoEntry = $factory->create();
  111. $twoDaysTwoHoursAgoEntry->setDate($twoDaysTwoHoursAgo->format('Y-m-d H:i:s'));
  112. $repository->save($twoDaysTwoHoursAgoEntry);
  113. }
  114. }