mirror of
https://github.com/Combodo/iTop.git
synced 2026-03-05 00:54:12 +01:00
Now log file name is unchanged : current log is still /log/error.log \o/ Rotation check (using file last modification time) is done : * on each file write : we don't want to miss calls if session last from 23:59:59 to 00:01 for example ! Though the filemtime() call is done once per session to lower performance impacts * using a new background process (LogFileRotationProcess) File renaming on setup is therefore removed. Also the interface is renamed (from ILogFileNameBuilder to iLogFileNameBuilder) to conform to iTop convention.
80 lines
3.6 KiB
PHP
80 lines
3.6 KiB
PHP
<?php
|
|
|
|
|
|
namespace Combodo\iTop\Test\UnitTest\Core;
|
|
|
|
|
|
use Combodo\iTop\Test\UnitTest\ItopTestCase;
|
|
use DailyRotatingLogFileNameBuilder;
|
|
use DateTime;
|
|
use WeeklyRotatingLogFileNameBuilder;
|
|
|
|
class LogFileNameBuilderTest extends ItopTestCase
|
|
{
|
|
public function ShouldRotateProvider()
|
|
{
|
|
return array(
|
|
'WEEKLY Same week' => array('WeeklyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-01 00:00', false),
|
|
'WEEKLY 1 week diff, same month' => array('WeeklyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-08 00:00', true),
|
|
'WEEKLY 2 weeks diff, same month' => array('WeeklyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-15 00:00', true),
|
|
'WEEKLY 1 week diff, different month' => array('WeeklyRotatingLogFileNameBuilder', '2020-01-27 00:00', '2020-02-03 00:00', true),
|
|
'WEEKLY same week, different month' => array('WeeklyRotatingLogFileNameBuilder', '2020-01-27 00:00', '2020-02-02 00:00', false),
|
|
'WEEKLY 1 week diff, different year' => array('WeeklyRotatingLogFileNameBuilder', '2019-12-30 00:00', '2020-01-06 00:00', true),
|
|
'WEEKLY same week, different year' => array('WeeklyRotatingLogFileNameBuilder', '2019-12-30 00:00', '2020-01-05 00:00', true),
|
|
'DAILY Same day' => array('DailyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-01 15:42', false),
|
|
'DAILY Same week, different day' => array('DailyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-02 00:00', true),
|
|
'DAILY 1 week diff' => array('DailyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-08 00:00', true),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param string $sFileNameBuilderClass RotatingLogFileNameBuilder impl
|
|
* @param string $sDateModified format Y-m-d H:i
|
|
* @param string $sDateNow format Y-m-d H:i
|
|
* @param bool $bExpected
|
|
*
|
|
* @dataProvider ShouldRotateProvider
|
|
*/
|
|
public function testShouldRotate($sFileNameBuilderClass, $sDateModified, $sDateNow, $bExpected)
|
|
{
|
|
$oDateModified = DateTime::createFromFormat('Y-m-d H:i', $sDateModified);
|
|
$oDateNow = DateTime::createFromFormat('Y-m-d H:i', $sDateNow);
|
|
|
|
/** @var \RotatingLogFileNameBuilder $oFileBuilder */
|
|
$oFileBuilder = new $sFileNameBuilderClass();
|
|
$bShouldRotate = $oFileBuilder->ShouldRotate($oDateModified, $oDateNow);
|
|
|
|
$this->assertEquals($bExpected, $bShouldRotate);
|
|
}
|
|
|
|
public function CronNextOccurrenceProvider()
|
|
{
|
|
return array(
|
|
'DAILY morning' => array('DailyRotatingLogFileNameBuilder', '2020-02-01 05:00', '2020-02-02 00:00'),
|
|
'DAILY midnight' => array('DailyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-02 00:00'),
|
|
'WEEKLY monday 12:42' => array('WeeklyRotatingLogFileNameBuilder', '2020-02-03 12:42', '2020-02-10 00:00'),
|
|
'WEEKLY monday 00:00' => array('WeeklyRotatingLogFileNameBuilder', '2020-02-03 00:00', '2020-02-10 00:00'),
|
|
'WEEKLY tuesday 12:42' => array('WeeklyRotatingLogFileNameBuilder', '2020-02-04 12:42', '2020-02-10 00:00'),
|
|
'WEEKLY sunday 12:42' => array('WeeklyRotatingLogFileNameBuilder', '2020-02-02 12:42', '2020-02-03 00:00'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param string $sFileNameBuilderClass RotatingLogFileNameBuilder impl
|
|
* @param string $sDateNow format Y-m-d H:i
|
|
* @param string $sExpectedOccurrence format Y-m-d H:i
|
|
*
|
|
* @dataProvider CronNextOccurrenceProvider
|
|
*/
|
|
public function testCronNextOccurrence($sFileNameBuilderClass, $sDateNow, $sExpectedOccurrence)
|
|
{
|
|
$oDateNow = DateTime::createFromFormat('Y-m-d H:i', $sDateNow);
|
|
|
|
/** @var \RotatingLogFileNameBuilder $oFileBuilder */
|
|
$oFileBuilder = new $sFileNameBuilderClass();
|
|
$oActualOccurrence = $oFileBuilder->GetCronProcessNextOccurrence($oDateNow);
|
|
$sActualOccurrence = $oActualOccurrence->format('Y-m-d H:i');
|
|
|
|
$this->assertEquals($sExpectedOccurrence, $sActualOccurrence);
|
|
}
|
|
} |