PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

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