RotatingFileHandlerTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 InvalidArgumentException;
  12. use Monolog\Test\TestCase;
  13. use PHPUnit_Framework_Error_Deprecated;
  14. /**
  15. * @covers Monolog\Handler\RotatingFileHandler
  16. */
  17. class RotatingFileHandlerTest extends TestCase
  18. {
  19. /**
  20. * This var should be private but then the anonymous function
  21. * in the `setUp` method won't be able to set it. `$this` cant't
  22. * be used in the anonymous function in `setUp` because PHP 5.3
  23. * does not support it.
  24. */
  25. public $lastError;
  26. public function setUp()
  27. {
  28. $dir = __DIR__.'/Fixtures';
  29. chmod($dir, 0777);
  30. if (!is_writable($dir)) {
  31. $this->markTestSkipped($dir.' must be writable to test the RotatingFileHandler.');
  32. }
  33. $this->lastError = null;
  34. $self = $this;
  35. // workaround with &$self used for PHP 5.3
  36. set_error_handler(function($code, $message) use (&$self) {
  37. $self->lastError = array(
  38. 'code' => $code,
  39. 'message' => $message,
  40. );
  41. });
  42. }
  43. private function assertErrorWasTriggered($code, $message)
  44. {
  45. if (empty($this->lastError)) {
  46. $this->fail(
  47. sprintf(
  48. 'Failed asserting that error with code `%d` and message `%s` was triggered',
  49. $code,
  50. $message
  51. )
  52. );
  53. }
  54. $this->assertEquals($code, $this->lastError['code'], sprintf('Expected an error with code %d to be triggered, got `%s` instead', $code, $this->lastError['code']));
  55. $this->assertEquals($message, $this->lastError['message'], sprintf('Expected an error with message `%d` to be triggered, got `%s` instead', $message, $this->lastError['message']));
  56. }
  57. public function testRotationCreatesNewFile()
  58. {
  59. touch(__DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400).'.rot');
  60. $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot');
  61. $handler->setFormatter($this->getIdentityFormatter());
  62. $handler->handle($this->getRecord());
  63. $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
  64. $this->assertTrue(file_exists($log));
  65. $this->assertEquals('test', file_get_contents($log));
  66. }
  67. /**
  68. * @dataProvider rotationTests
  69. */
  70. public function testRotation($createFile, $dateFormat, $timeCallback)
  71. {
  72. touch($old1 = __DIR__.'/Fixtures/foo-'.date($dateFormat, $timeCallback(-1)).'.rot');
  73. touch($old2 = __DIR__.'/Fixtures/foo-'.date($dateFormat, $timeCallback(-2)).'.rot');
  74. touch($old3 = __DIR__.'/Fixtures/foo-'.date($dateFormat, $timeCallback(-3)).'.rot');
  75. touch($old4 = __DIR__.'/Fixtures/foo-'.date($dateFormat, $timeCallback(-4)).'.rot');
  76. $log = __DIR__.'/Fixtures/foo-'.date($dateFormat).'.rot';
  77. if ($createFile) {
  78. touch($log);
  79. }
  80. $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
  81. $handler->setFormatter($this->getIdentityFormatter());
  82. $handler->setFilenameFormat('{filename}-{date}', $dateFormat);
  83. $handler->handle($this->getRecord());
  84. $handler->close();
  85. $this->assertTrue(file_exists($log));
  86. $this->assertTrue(file_exists($old1));
  87. $this->assertEquals($createFile, file_exists($old2));
  88. $this->assertEquals($createFile, file_exists($old3));
  89. $this->assertEquals($createFile, file_exists($old4));
  90. $this->assertEquals('test', file_get_contents($log));
  91. }
  92. public function rotationTests()
  93. {
  94. $now = time();
  95. $dayCallback = function($ago) use ($now) {
  96. return $now + 86400 * $ago;
  97. };
  98. $monthCallback = function($ago) {
  99. return gmmktime(0, 0, 0, date('n') + $ago, date('d'), date('Y'));
  100. };
  101. $yearCallback = function($ago) {
  102. return gmmktime(0, 0, 0, date('n'), date('d'), date('Y') + $ago);
  103. };
  104. return array(
  105. 'Rotation is triggered when the file of the current day is not present'
  106. => array(true, RotatingFileHandler::FILE_PER_DAY, $dayCallback),
  107. 'Rotation is not triggered when the file of the current day is already present'
  108. => array(false, RotatingFileHandler::FILE_PER_DAY, $dayCallback),
  109. 'Rotation is triggered when the file of the current month is not present'
  110. => array(true, RotatingFileHandler::FILE_PER_MONTH, $monthCallback),
  111. 'Rotation is not triggered when the file of the current month is already present'
  112. => array(false, RotatingFileHandler::FILE_PER_MONTH, $monthCallback),
  113. 'Rotation is triggered when the file of the current year is not present'
  114. => array(true, RotatingFileHandler::FILE_PER_YEAR, $yearCallback),
  115. 'Rotation is not triggered when the file of the current year is already present'
  116. => array(false, RotatingFileHandler::FILE_PER_YEAR, $yearCallback),
  117. );
  118. }
  119. /**
  120. * @dataProvider dateFormatProvider
  121. */
  122. public function testAllowOnlyFixedDefinedDateFormats($dateFormat, $valid)
  123. {
  124. $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
  125. if (!$valid) {
  126. $this->setExpectedExceptionRegExp(InvalidArgumentException::class, '~^Invalid date format~');
  127. }
  128. $handler->setFilenameFormat('{filename}-{date}', $dateFormat);
  129. }
  130. public function dateFormatProvider()
  131. {
  132. return array(
  133. array(RotatingFileHandler::FILE_PER_DAY, true),
  134. array(RotatingFileHandler::FILE_PER_MONTH, true),
  135. array(RotatingFileHandler::FILE_PER_YEAR, true),
  136. array('m-d-Y', false),
  137. array('Y-m-d-h-i', false)
  138. );
  139. }
  140. /**
  141. * @dataProvider filenameFormatProvider
  142. */
  143. public function testDisallowFilenameFormatsWithoutDate($filenameFormat, $valid)
  144. {
  145. $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
  146. if (!$valid) {
  147. $this->setExpectedExceptionRegExp(InvalidArgumentException::class, '~^Invalid filename format~');
  148. }
  149. $handler->setFilenameFormat($filenameFormat, RotatingFileHandler::FILE_PER_DAY);
  150. }
  151. public function filenameFormatProvider()
  152. {
  153. return array(
  154. array('{filename}', false),
  155. array('{filename}-{date}', true),
  156. array('{date}', true),
  157. array('foobar-{date}', true),
  158. array('foo-{date}-bar', true),
  159. array('{date}-foobar', true),
  160. array('foobar', false),
  161. );
  162. }
  163. public function testReuseCurrentFile()
  164. {
  165. $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
  166. file_put_contents($log, "foo");
  167. $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot');
  168. $handler->setFormatter($this->getIdentityFormatter());
  169. $handler->handle($this->getRecord());
  170. $this->assertEquals('footest', file_get_contents($log));
  171. }
  172. public function tearDown()
  173. {
  174. foreach (glob(__DIR__.'/Fixtures/*.rot') as $file) {
  175. unlink($file);
  176. }
  177. restore_error_handler();
  178. }
  179. }