RotatingFileHandlerTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\Logger;
  12. class RotatingFileHandlerTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function setUp()
  15. {
  16. $dir = __DIR__.'/Fixtures';
  17. chmod($dir, 0777);
  18. if (!is_writable($dir)) {
  19. $this->markTestSkipped($dir.' must be writeable to test the RotatingFileHandler.');
  20. }
  21. }
  22. public function testRotationCreatesNewFile()
  23. {
  24. touch(__DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400).'.rot');
  25. $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot');
  26. $handler->write(array('message' => 'test'));
  27. $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
  28. $this->assertTrue(file_exists($log));
  29. $this->assertEquals('test', file_get_contents($log));
  30. }
  31. /**
  32. * @dataProvider rotationTests
  33. */
  34. public function testRotation($createFile)
  35. {
  36. touch($old1 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400).'.rot');
  37. touch($old2 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 2).'.rot');
  38. touch($old3 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 3).'.rot');
  39. touch($old4 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 4).'.rot');
  40. $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
  41. if ($createFile) {
  42. touch($log);
  43. }
  44. $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
  45. $handler->write(array('message' => 'test'));
  46. $handler->close();
  47. $this->assertTrue(file_exists($log));
  48. $this->assertTrue(file_exists($old1));
  49. $this->assertEquals($createFile, file_exists($old2));
  50. $this->assertEquals($createFile, file_exists($old3));
  51. $this->assertEquals($createFile, file_exists($old4));
  52. $this->assertEquals('test', file_get_contents($log));
  53. }
  54. public function rotationTests()
  55. {
  56. return array(
  57. 'Rotation is triggered when the file of the current day is not present'
  58. => array(true),
  59. 'Rotation is not triggered when the file is already present'
  60. => array(false),
  61. );
  62. }
  63. public function testReuseCurrentFile()
  64. {
  65. $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
  66. file_put_contents($log, "foo");
  67. $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot');
  68. $handler->write(array('message' => 'test'));
  69. $this->assertEquals('footest', file_get_contents($log));
  70. }
  71. public function tearDown()
  72. {
  73. foreach (glob(__DIR__.'/Fixtures/*.rot') as $file) {
  74. unlink($file);
  75. }
  76. }
  77. }