2
0

RotatingFileHandlerTest.php 7.5 KB

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