RotatingFileHandlerTest.php 3.0 KB

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