Skip to content

Commit

Permalink
[BUGFIX] Use correct DateInterval objects in RotatingFileWriterTest
Browse files Browse the repository at this point in the history
The `RotatingFileWriterTest` previously used a timestamp-based
calculation to mock file names. This approach conflicts with the
`DateInterval`-based calculation and might lead to false-negative test
results. This commit updates the test cases by using the interval-based
calculation now.

Resolves: #103243
Releases: main
Change-Id: I9b08b797377eb504af7707acf520fae896b6cbb1
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/83177
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
  • Loading branch information
andreaskienast authored and lolli42 committed Mar 2, 2024
1 parent cf4d853 commit a774f59
Showing 1 changed file with 23 additions and 33 deletions.
56 changes: 23 additions & 33 deletions typo3/sysext/core/Tests/Unit/Log/Writer/RotatingFileWriterTest.php
Expand Up @@ -93,11 +93,24 @@ public function writingLogWithoutLatestRotationAndNonEmptyLogRotates(): void
self::assertCount(1, $rotatedFiles);
}

#[DataProvider('writingLogWithLatestRotationInTimeFrameDoesNotRotateDataProvider')]
public static function intervalDataProvider(): array
{
return [
[Interval::DAILY],
[Interval::WEEKLY],
[Interval::MONTHLY],
[Interval::YEARLY],
];
}

#[DataProvider('intervalDataProvider')]
#[Test]
public function writingLogWithLatestRotationInTimeFrameDoesNotRotate(Interval $interval, int $rotationTimestamp): void
public function writingLogWithLatestRotationInTimeFrameDoesNotRotate(Interval $interval): void
{
$rotationDate = (new \DateTimeImmutable('@' . $rotationTimestamp))->format('YmdHis');
$testingTolerance = 100;
$rotationDate = (new \DateTime('@' . (time() + $testingTolerance)))
->sub(new \DateInterval($interval->getDateInterval()))
->format('YmdHis');
$logFileName = $this->getDefaultFileName();

file_put_contents($logFileName, 'fooo');
Expand All @@ -114,24 +127,15 @@ public function writingLogWithLatestRotationInTimeFrameDoesNotRotate(Interval $i
self::assertCount(1, $rotatedFiles);
}

public static function writingLogWithLatestRotationInTimeFrameDoesNotRotateDataProvider(): array
{
$secondsOfADay = 86400;
$tolerance = 100;

return [
[Interval::DAILY, time() - $secondsOfADay + $tolerance],
[Interval::WEEKLY, time() - $secondsOfADay * 7 + $tolerance],
[Interval::MONTHLY, time() - $secondsOfADay * 30 + $tolerance],
[Interval::YEARLY, time() - $secondsOfADay * 365 + $tolerance],
];
}

#[DataProvider('writingLogWithExpiredLatestRotationInTimeFrameRotatesDataProvider')]
#[DataProvider('intervalDataProvider')]
#[Test]
public function writingLogWithExpiredLatestRotationInTimeFrameRotates(Interval $interval, int $rotationTimestamp): void
public function writingLogWithExpiredLatestRotationInTimeFrameRotates(Interval $interval): void
{
$rotationDate = (new \DateTimeImmutable('@' . $rotationTimestamp))->format('YmdHis');
// Helper variable to ensure the next rotation interval kicks in
$boost = 100;
$rotationDate = (new \DateTime('@' . (time() - $boost)))
->sub(new \DateInterval($interval->getDateInterval()))
->format('YmdHis');
$logFileName = $this->getDefaultFileName();

file_put_contents($logFileName, 'fooo');
Expand All @@ -148,20 +152,6 @@ public function writingLogWithExpiredLatestRotationInTimeFrameRotates(Interval $
self::assertCount(2, $rotatedFiles);
}

public static function writingLogWithExpiredLatestRotationInTimeFrameRotatesDataProvider(): array
{
$secondsOfADay = 86400;
// Helper variable to ensure the next rotation interval kicks in
$boost = 100;

return [
[Interval::DAILY, time() - $secondsOfADay - $boost],
[Interval::WEEKLY, time() - $secondsOfADay * 7 - $boost],
[Interval::MONTHLY, time() - $secondsOfADay * 31 - $boost], // dumb test, always expect months with 31 days
[Interval::YEARLY, time() - $secondsOfADay * 366 - $boost], // dumb test, always expect years with 366 days
];
}

#[Test]
public function rotationRespectsMaxAmountOfFiles(): void
{
Expand Down

0 comments on commit a774f59

Please sign in to comment.