RotatingFileHandlerTest.php 8.7 KB

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