PageRenderTime 27ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/Front_End/vendor/monolog/monolog/tests/Monolog/Handler/RotatingFileHandlerTest.php

https://gitlab.com/Sigpot/AirSpot
PHP | 211 lines | 159 code | 25 blank | 27 comment | 5 complexity | cd30678304d5c339392889d3e8b16cd6 MD5 | raw file
  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\TestCase;
  12. use PHPUnit_Framework_Error_Deprecated;
  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 = array(
  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, date('n') + $ago, date('d'), date('Y'));
  99. };
  100. $yearCallback = function($ago) {
  101. return gmmktime(0, 0, 0, date('n'), date('d'), date('Y') + $ago);
  102. };
  103. return array(
  104. 'Rotation is triggered when the file of the current day is not present'
  105. => array(true, RotatingFileHandler::FILE_PER_DAY, $dayCallback),
  106. 'Rotation is not triggered when the file of the current day is already present'
  107. => array(false, RotatingFileHandler::FILE_PER_DAY, $dayCallback),
  108. 'Rotation is triggered when the file of the current month is not present'
  109. => array(true, RotatingFileHandler::FILE_PER_MONTH, $monthCallback),
  110. 'Rotation is not triggered when the file of the current month is already present'
  111. => array(false, RotatingFileHandler::FILE_PER_MONTH, $monthCallback),
  112. 'Rotation is triggered when the file of the current year is not present'
  113. => array(true, RotatingFileHandler::FILE_PER_YEAR, $yearCallback),
  114. 'Rotation is not triggered when the file of the current year is already present'
  115. => array(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. $handler->setFilenameFormat('{filename}-{date}', $dateFormat);
  125. if (!$valid) {
  126. $this->assertErrorWasTriggered(
  127. E_USER_DEPRECATED,
  128. 'Invalid date format - format must be one of RotatingFileHandler::FILE_PER_DAY ("Y-m-d"), '.
  129. 'RotatingFileHandler::FILE_PER_MONTH ("Y-m") or RotatingFileHandler::FILE_PER_YEAR ("Y"), '.
  130. 'or you can set one of the date formats using slashes, underscores and/or dots instead of dashes.'
  131. );
  132. }
  133. }
  134. public function dateFormatProvider()
  135. {
  136. return array(
  137. array(RotatingFileHandler::FILE_PER_DAY, true),
  138. array(RotatingFileHandler::FILE_PER_MONTH, true),
  139. array(RotatingFileHandler::FILE_PER_YEAR, true),
  140. array('m-d-Y', false),
  141. array('Y-m-d-h-i', false)
  142. );
  143. }
  144. /**
  145. * @dataProvider filenameFormatProvider
  146. */
  147. public function testDisallowFilenameFormatsWithoutDate($filenameFormat, $valid)
  148. {
  149. $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
  150. $handler->setFilenameFormat($filenameFormat, RotatingFileHandler::FILE_PER_DAY);
  151. if (!$valid) {
  152. $this->assertErrorWasTriggered(
  153. E_USER_DEPRECATED,
  154. 'Invalid filename format - format should contain at least `{date}`, because otherwise rotating is impossible.'
  155. );
  156. }
  157. }
  158. public function filenameFormatProvider()
  159. {
  160. return array(
  161. array('{filename}', false),
  162. array('{filename}-{date}', true),
  163. array('{date}', true),
  164. array('foobar-{date}', true),
  165. array('foo-{date}-bar', true),
  166. array('{date}-foobar', true),
  167. array('foobar', false),
  168. );
  169. }
  170. public function testReuseCurrentFile()
  171. {
  172. $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
  173. file_put_contents($log, "foo");
  174. $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot');
  175. $handler->setFormatter($this->getIdentityFormatter());
  176. $handler->handle($this->getRecord());
  177. $this->assertEquals('footest', file_get_contents($log));
  178. }
  179. public function tearDown()
  180. {
  181. foreach (glob(__DIR__.'/Fixtures/*.rot') as $file) {
  182. unlink($file);
  183. }
  184. restore_error_handler();
  185. }
  186. }