1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-02-23 22:42:38 +01:00
php-monolog/tests/Monolog/Handler/RotatingFileHandlerTest.php

100 lines
3.1 KiB
PHP
Raw Normal View History

2011-04-05 23:19:18 +02:00
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Handler;
use Monolog\TestCase;
2011-04-05 23:19:18 +02:00
/**
* @covers Monolog\Handler\RotatingFileHandler
*/
class RotatingFileHandlerTest extends TestCase
2011-04-05 23:19:18 +02:00
{
public function setUp()
{
$dir = __DIR__.'/Fixtures';
chmod($dir, 0777);
if (!is_writable($dir)) {
$this->markTestSkipped($dir.' must be writeable to test the RotatingFileHandler.');
}
}
public function testRotationCreatesNewFile()
{
touch(__DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400).'.rot');
$handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot');
$handler->setFormatter($this->getIdentityFormatter());
$handler->handle($this->getRecord());
2011-04-05 23:19:18 +02:00
$log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
$this->assertTrue(file_exists($log));
$this->assertEquals('test', file_get_contents($log));
}
2011-04-05 23:36:18 +02:00
/**
* @dataProvider rotationTests
*/
public function testRotation($createFile)
2011-04-05 23:19:18 +02:00
{
touch($old1 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400).'.rot');
touch($old2 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 2).'.rot');
touch($old3 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 3).'.rot');
touch($old4 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 4).'.rot');
2011-04-05 23:36:18 +02:00
$log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
if ($createFile) {
touch($log);
}
2011-04-05 23:19:18 +02:00
$handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
$handler->setFormatter($this->getIdentityFormatter());
$handler->handle($this->getRecord());
2011-04-05 23:19:18 +02:00
$handler->close();
$this->assertTrue(file_exists($log));
$this->assertTrue(file_exists($old1));
2011-04-05 23:36:18 +02:00
$this->assertEquals($createFile, file_exists($old2));
$this->assertEquals($createFile, file_exists($old3));
$this->assertEquals($createFile, file_exists($old4));
2011-04-05 23:19:18 +02:00
$this->assertEquals('test', file_get_contents($log));
}
2011-04-05 23:36:18 +02:00
public function rotationTests()
2011-04-05 23:19:18 +02:00
{
2011-04-05 23:36:18 +02:00
return array(
'Rotation is triggered when the file of the current day is not present'
=> array(true),
'Rotation is not triggered when the file is already present'
=> array(false),
);
2011-04-05 23:19:18 +02:00
}
public function testReuseCurrentFile()
{
$log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
file_put_contents($log, "foo");
$handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot');
$handler->setFormatter($this->getIdentityFormatter());
$handler->handle($this->getRecord());
2011-04-05 23:19:18 +02:00
$this->assertEquals('footest', file_get_contents($log));
}
public function tearDown()
{
foreach (glob(__DIR__.'/Fixtures/*.rot') as $file) {
unlink($file);
}
}
}