RotatingFileHandlerTest.php 3.1KB

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