mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-22 01:58:47 +02:00
N°2793 Test log rotation
This commit is contained in:
@@ -200,7 +200,7 @@ abstract class RotatingLogFileNameBuilder implements iLogFileNameBuilder
|
||||
* @uses static::$oLogFileLastModified
|
||||
* @uses GetFileSuffix
|
||||
*/
|
||||
protected function GetRotatedFileName($oLogFileLastModified)
|
||||
public function GetRotatedFileName($oLogFileLastModified)
|
||||
{
|
||||
$aPathParts = pathinfo($this->sLogFileFullPath);
|
||||
$this->sFilePath = $aPathParts['dirname'];
|
||||
|
||||
@@ -17,6 +17,18 @@ use DateTime;
|
||||
*/
|
||||
class LogFileNameBuilderTest extends ItopTestCase
|
||||
{
|
||||
/**
|
||||
* @param $sLogFile
|
||||
* @param \DateTime $oNewModificationDate
|
||||
*
|
||||
* @uses \clearstatcache() if not called, the next filemtime() call will return the PHP cached date instead of the real one
|
||||
*/
|
||||
private function ChangeFileModificationDate($sLogFile, DateTime $oNewModificationDate)
|
||||
{
|
||||
touch($sLogFile, $oNewModificationDate->getTimestamp());
|
||||
clearstatcache(true, $sLogFile);
|
||||
}
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
@@ -24,6 +36,51 @@ class LogFileNameBuilderTest extends ItopTestCase
|
||||
require_once APPROOT.'core/log.class.inc.php';
|
||||
}
|
||||
|
||||
public function testCheckAndRotateLogFile()
|
||||
{
|
||||
$sLogFile = __DIR__.DIRECTORY_SEPARATOR.'fileNameBuilder.test.log';
|
||||
$oFileBuilder = new DailyRotatingLogFileNameBuilder($sLogFile);
|
||||
|
||||
if (file_exists($sLogFile))
|
||||
{
|
||||
unlink($sLogFile);
|
||||
}
|
||||
|
||||
$bIsFileExists = $oFileBuilder->IsLogFileExists();
|
||||
$this->assertFalse($bIsFileExists, 'Test log file does not exist');
|
||||
|
||||
$hLogFile = fopen($sLogFile, 'a');
|
||||
$sDate = date('Y-m-d H:i:s');
|
||||
$sTestClassName = self::class;
|
||||
fwrite($hLogFile, "$sDate | This is a line generated by $sTestClassName\n");
|
||||
fclose($hLogFile);
|
||||
$iLogDateLastModifiedTimeStamp = filemtime($sLogFile);
|
||||
$oLogFileLastModified = DateTime::createFromFormat('U', $iLogDateLastModifiedTimeStamp);
|
||||
|
||||
$sRotatedLogFile = $oFileBuilder->GetRotatedFileName($oLogFileLastModified);
|
||||
$oFileBuilder->CheckAndRotateLogFile();
|
||||
$this->assertFileExists($sLogFile, 'Test log file modification date is today, original file still exists after rotation call');
|
||||
$this->assertFileNotExists($sRotatedLogFile, 'No rotation occurred yet');
|
||||
|
||||
// changing modification date, but do not reset cached filebuilder date => no change
|
||||
$oTimeYesterday = new DateTime('yesterday');
|
||||
touch($sLogFile, $oTimeYesterday->getTimestamp());
|
||||
$sRotatedLogFile = $oFileBuilder->GetRotatedFileName($oTimeYesterday);
|
||||
$oFileBuilder->CheckAndRotateLogFile();
|
||||
$this->assertFileExists($sLogFile, 'Test log file modification date is yesterday but filebuilder use its cache, original file still exists after rotation call');
|
||||
$this->assertFileNotExists($sRotatedLogFile, 'No rotation occurred yet');
|
||||
|
||||
// changing modification date AND resetting filebuilder date cache
|
||||
$oTimeYesterday = new DateTime('yesterday');
|
||||
$this->ChangeFileModificationDate($sLogFile, $oTimeYesterday);
|
||||
$oFileBuilder->ResetLastModifiedDateForFile();
|
||||
$sRotatedLogFile = $oFileBuilder->GetRotatedFileName($oTimeYesterday);
|
||||
$oFileBuilder->CheckAndRotateLogFile();
|
||||
$this->assertFileNotExists($sLogFile, 'Test log file modification date is yesterday, file rotated !');
|
||||
$this->assertFileExists($sRotatedLogFile, 'Rotation was done');
|
||||
unlink($sRotatedLogFile);
|
||||
}
|
||||
|
||||
public function ShouldRotateProvider()
|
||||
{
|
||||
return array(
|
||||
|
||||
Reference in New Issue
Block a user