/tests/TestCase/Log/Engine/FileLogTest.php

https://github.com/LubosRemplik/cakephp · PHP · 252 lines · 153 code · 43 blank · 56 comment · 1 complexity · b27794616353b02b17d32c88aa3a6bb4 MD5 · raw file

  1. <?php
  2. /**
  3. * FileLogTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  13. * @link https://cakephp.org CakePHP(tm) Project
  14. * @since 1.3.0
  15. * @license https://opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Log\Engine;
  18. use Cake\Log\Engine\FileLog;
  19. use Cake\TestSuite\TestCase;
  20. use JsonSerializable;
  21. /**
  22. * used for testing when an object is passed to a logger
  23. */
  24. class StringObject
  25. {
  26. /**
  27. * String representation of the object
  28. *
  29. * @return string
  30. */
  31. public function __toString()
  32. {
  33. return 'Hey!';
  34. }
  35. }
  36. /**
  37. * used for testing when an serializable is passed to a logger
  38. */
  39. class JsonObject implements JsonSerializable
  40. {
  41. /**
  42. * String representation of the object
  43. *
  44. * @return string
  45. */
  46. public function jsonSerialize()
  47. {
  48. return ['hello' => 'world'];
  49. }
  50. }
  51. /**
  52. * FileLogTest class
  53. */
  54. class FileLogTest extends TestCase
  55. {
  56. /**
  57. * testLogFileWriting method
  58. *
  59. * @return void
  60. */
  61. public function testLogFileWriting()
  62. {
  63. $this->_deleteLogs(LOGS);
  64. $log = new FileLog(['path' => LOGS]);
  65. $log->log('warning', 'Test warning');
  66. $this->assertFileExists(LOGS . 'error.log');
  67. $result = file_get_contents(LOGS . 'error.log');
  68. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning/', $result);
  69. $log->log('debug', 'Test warning');
  70. $this->assertFileExists(LOGS . 'debug.log');
  71. $result = file_get_contents(LOGS . 'debug.log');
  72. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Test warning/', $result);
  73. $log->log('random', 'Test warning');
  74. $this->assertFileExists(LOGS . 'random.log');
  75. $result = file_get_contents(LOGS . 'random.log');
  76. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Random: Test warning/', $result);
  77. $object = new StringObject;
  78. $log->log('debug', $object);
  79. $this->assertFileExists(LOGS . 'debug.log');
  80. $result = file_get_contents(LOGS . 'debug.log');
  81. $this->assertContains('Debug: Hey!', $result);
  82. $object = new JsonObject;
  83. $log->log('debug', $object);
  84. $this->assertFileExists(LOGS . 'debug.log');
  85. $result = file_get_contents(LOGS . 'debug.log');
  86. $this->assertContains('Debug: ' . json_encode(['hello' => 'world']), $result);
  87. $log->log('debug', [1, 2]);
  88. $this->assertFileExists(LOGS . 'debug.log');
  89. $result = file_get_contents(LOGS . 'debug.log');
  90. $this->assertContains('Debug: ' . print_r([1, 2], true), $result);
  91. }
  92. /**
  93. * test using the path setting to log logs in other places.
  94. *
  95. * @return void
  96. */
  97. public function testPathSetting()
  98. {
  99. $path = TMP . 'tests' . DS;
  100. $this->_deleteLogs($path);
  101. $log = new FileLog(compact('path'));
  102. $log->log('warning', 'Test warning');
  103. $this->assertFileExists($path . 'error.log');
  104. }
  105. /**
  106. * test log rotation
  107. *
  108. * @return void
  109. */
  110. public function testRotation()
  111. {
  112. $path = TMP . 'tests' . DS;
  113. $this->_deleteLogs($path);
  114. file_put_contents($path . 'error.log', "this text is under 35 bytes\n");
  115. $log = new FileLog([
  116. 'path' => $path,
  117. 'size' => 35,
  118. 'rotate' => 2
  119. ]);
  120. $log->log('warning', 'Test warning one');
  121. $this->assertFileExists($path . 'error.log');
  122. $result = file_get_contents($path . 'error.log');
  123. $this->assertRegExp('/Warning: Test warning one/', $result);
  124. $this->assertCount(0, glob($path . 'error.log.*'));
  125. clearstatcache();
  126. $log->log('warning', 'Test warning second');
  127. $files = glob($path . 'error.log.*');
  128. $this->assertCount(1, $files);
  129. $result = file_get_contents($files[0]);
  130. $this->assertRegExp('/this text is under 35 bytes/', $result);
  131. $this->assertRegExp('/Warning: Test warning one/', $result);
  132. sleep(1);
  133. clearstatcache();
  134. $log->log('warning', 'Test warning third');
  135. $result = file_get_contents($path . 'error.log');
  136. $this->assertRegExp('/Warning: Test warning third/', $result);
  137. $files = glob($path . 'error.log.*');
  138. $this->assertCount(2, $files);
  139. $result = file_get_contents($files[0]);
  140. $this->assertRegExp('/this text is under 35 bytes/', $result);
  141. $result = file_get_contents($files[1]);
  142. $this->assertRegExp('/Warning: Test warning second/', $result);
  143. file_put_contents($path . 'error.log.0000000000', "The oldest log file with over 35 bytes.\n");
  144. sleep(1);
  145. clearstatcache();
  146. $log->log('warning', 'Test warning fourth');
  147. // rotate count reached so file count should not increase
  148. $files = glob($path . 'error.log.*');
  149. $this->assertCount(2, $files);
  150. $result = file_get_contents($path . 'error.log');
  151. $this->assertRegExp('/Warning: Test warning fourth/', $result);
  152. $result = file_get_contents(array_pop($files));
  153. $this->assertRegExp('/Warning: Test warning third/', $result);
  154. $result = file_get_contents(array_pop($files));
  155. $this->assertRegExp('/Warning: Test warning second/', $result);
  156. file_put_contents($path . 'debug.log', "this text is just greater than 35 bytes\n");
  157. $log = new FileLog([
  158. 'path' => $path,
  159. 'size' => 35,
  160. 'rotate' => 0
  161. ]);
  162. file_put_contents($path . 'debug.log.0000000000', "The oldest log file with over 35 bytes.\n");
  163. $log->log('debug', 'Test debug');
  164. $this->assertFileExists($path . 'debug.log');
  165. $result = file_get_contents($path . 'debug.log');
  166. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Test debug/', $result);
  167. $this->assertFalse(strstr($result, 'greater than 5 bytes'));
  168. $this->assertCount(0, glob($path . 'debug.log.*'));
  169. }
  170. public function testMaskSetting()
  171. {
  172. if (DS === '\\') {
  173. $this->markTestSkipped('File permission testing does not work on Windows.');
  174. }
  175. $path = TMP . 'tests' . DS;
  176. $this->_deleteLogs($path);
  177. $log = new FileLog(['path' => $path, 'mask' => 0666]);
  178. $log->log('warning', 'Test warning one');
  179. $result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
  180. $expected = '0666';
  181. $this->assertEquals($expected, $result);
  182. unlink($path . 'error.log');
  183. $log = new FileLog(['path' => $path, 'mask' => 0644]);
  184. $log->log('warning', 'Test warning two');
  185. $result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
  186. $expected = '0644';
  187. $this->assertEquals($expected, $result);
  188. unlink($path . 'error.log');
  189. $log = new FileLog(['path' => $path, 'mask' => 0640]);
  190. $log->log('warning', 'Test warning three');
  191. $result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
  192. $expected = '0640';
  193. $this->assertEquals($expected, $result);
  194. unlink($path . 'error.log');
  195. }
  196. /**
  197. * helper function to clears all log files in specified directory
  198. *
  199. * @param string $dir
  200. * @return void
  201. */
  202. protected function _deleteLogs($dir)
  203. {
  204. $files = array_merge(glob($dir . '*.log'), glob($dir . '*.log.*'));
  205. foreach ($files as $file) {
  206. unlink($file);
  207. }
  208. }
  209. }