RotatingFileHandlerTest.php 8.8 KB

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