RotatingFileHandlerTest.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\TestCase;
  12. /**
  13. * @covers Monolog\Handler\RotatingFileHandler
  14. */
  15. class RotatingFileHandlerTest extends TestCase
  16. {
  17. public function setUp()
  18. {
  19. $dir = __DIR__.'/Fixtures';
  20. chmod($dir, 0777);
  21. if (!is_writable($dir)) {
  22. $this->markTestSkipped($dir.' must be writeable to test the RotatingFileHandler.');
  23. }
  24. }
  25. public function testRotationCreatesNewFile()
  26. {
  27. touch(__DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400).'.rot');
  28. $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot');
  29. $handler->setFormatter($this->getIdentityFormatter());
  30. $handler->handle($this->getRecord());
  31. $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
  32. $this->assertTrue(file_exists($log));
  33. $this->assertEquals('test', file_get_contents($log));
  34. }
  35. /**
  36. * @dataProvider rotationTests
  37. */
  38. public function testRotation($createFile)
  39. {
  40. touch($old1 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400).'.rot');
  41. touch($old2 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 2).'.rot');
  42. touch($old3 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 3).'.rot');
  43. touch($old4 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 4).'.rot');
  44. $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
  45. if ($createFile) {
  46. touch($log);
  47. }
  48. $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
  49. $handler->setFormatter($this->getIdentityFormatter());
  50. $handler->handle($this->getRecord());
  51. $handler->close();
  52. $this->assertTrue(file_exists($log));
  53. $this->assertTrue(file_exists($old1));
  54. $this->assertEquals($createFile, file_exists($old2));
  55. $this->assertEquals($createFile, file_exists($old3));
  56. $this->assertEquals($createFile, file_exists($old4));
  57. $this->assertEquals('test', file_get_contents($log));
  58. }
  59. public function rotationTests()
  60. {
  61. return array(
  62. 'Rotation is triggered when the file of the current day is not present'
  63. => array(true),
  64. 'Rotation is not triggered when the file is already present'
  65. => array(false),
  66. );
  67. }
  68. public function testReuseCurrentFile()
  69. {
  70. $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
  71. file_put_contents($log, "foo");
  72. $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot');
  73. $handler->setFormatter($this->getIdentityFormatter());
  74. $handler->handle($this->getRecord());
  75. $this->assertEquals('footest', file_get_contents($log));
  76. }
  77. public function tearDown()
  78. {
  79. foreach (glob(__DIR__.'/Fixtures/*.rot') as $file) {
  80. unlink($file);
  81. }
  82. }
  83. }