FileLogTest.php 7.1 KB

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