2
0

RotatingFileHandlerTest.php 3.4 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\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. public function testRotationDeletesOldFiles()
  32. {
  33. touch($old1 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400).'.rot');
  34. touch($old2 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 2).'.rot');
  35. touch($old3 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 3).'.rot');
  36. touch($old4 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 4).'.rot');
  37. $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
  38. $handler->write(array('message' => 'test'));
  39. $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
  40. $handler->close();
  41. $this->assertTrue(file_exists($log));
  42. $this->assertTrue(file_exists($old1));
  43. $this->assertFalse(file_exists($old2));
  44. $this->assertFalse(file_exists($old3));
  45. $this->assertFalse(file_exists($old4));
  46. $this->assertEquals('test', file_get_contents($log));
  47. }
  48. public function testRotationNotTriggeredTwiceTheSameDay()
  49. {
  50. touch($old1 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400).'.rot');
  51. touch($old2 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 2).'.rot');
  52. touch($old3 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 3).'.rot');
  53. touch($old4 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 4).'.rot');
  54. $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
  55. file_put_contents($log, 'test');
  56. $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
  57. $handler->write(array('message' => 'test'));
  58. $handler->close();
  59. $this->assertTrue(file_exists($log));
  60. $this->assertTrue(file_exists($old1));
  61. $this->assertTrue(file_exists($old2));
  62. $this->assertTrue(file_exists($old3));
  63. $this->assertTrue(file_exists($old4));
  64. $this->assertEquals('testtest', file_get_contents($log));
  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->write(array('message' => 'test'));
  72. $this->assertEquals('footest', file_get_contents($log));
  73. }
  74. public function tearDown()
  75. {
  76. foreach (glob(__DIR__.'/Fixtures/*.rot') as $file) {
  77. unlink($file);
  78. }
  79. }
  80. }