PageRenderTime 250ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/Cake/Test/Case/Log/Engine/FileLogTest.php

https://bitbucket.org/gencer/cakephp
PHP | 194 lines | 126 code | 31 blank | 37 comment | 1 complexity | ded2dc6c887e47030d3d252f7b4575db MD5 | raw file
  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. * @package Cake.Test.Case.Log.Engine
  15. * @since CakePHP(tm) v 1.3
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('FileLog', 'Log/Engine');
  19. /**
  20. * CakeLogTest class
  21. *
  22. * @package Cake.Test.Case.Log.Engine
  23. */
  24. class FileLogTest extends CakeTestCase {
  25. /**
  26. * testLogFileWriting method
  27. *
  28. * @return void
  29. */
  30. public function testLogFileWriting() {
  31. $this->_deleteLogs(LOGS);
  32. $log = new FileLog();
  33. $log->write('warning', 'Test warning');
  34. $this->assertTrue(file_exists(LOGS . 'error.log'));
  35. $result = file_get_contents(LOGS . 'error.log');
  36. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning/', $result);
  37. $log->write('debug', 'Test warning');
  38. $this->assertTrue(file_exists(LOGS . 'debug.log'));
  39. $result = file_get_contents(LOGS . 'debug.log');
  40. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Test warning/', $result);
  41. $log->write('random', 'Test warning');
  42. $this->assertTrue(file_exists(LOGS . 'random.log'));
  43. $result = file_get_contents(LOGS . 'random.log');
  44. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Random: Test warning/', $result);
  45. }
  46. /**
  47. * test using the path setting to write logs in other places.
  48. *
  49. * @return void
  50. */
  51. public function testPathSetting() {
  52. $path = TMP . 'tests' . DS;
  53. $this->_deleteLogs($path);
  54. $log = new FileLog(compact('path'));
  55. $log->write('warning', 'Test warning');
  56. $this->assertTrue(file_exists($path . 'error.log'));
  57. }
  58. /**
  59. * test log rotation
  60. *
  61. * @return void
  62. */
  63. public function testRotation() {
  64. $path = TMP . 'tests' . DS;
  65. $this->_deleteLogs($path);
  66. file_put_contents($path . 'error.log', "this text is under 35 bytes\n");
  67. $log = new FileLog(array(
  68. 'path' => $path,
  69. 'size' => 35,
  70. 'rotate' => 2
  71. ));
  72. $log->write('warning', 'Test warning one');
  73. $this->assertTrue(file_exists($path . 'error.log'));
  74. $result = file_get_contents($path . 'error.log');
  75. $this->assertRegExp('/Warning: Test warning one/', $result);
  76. $this->assertEquals(0, count(glob($path . 'error.log.*')));
  77. clearstatcache();
  78. $log->write('warning', 'Test warning second');
  79. $files = glob($path . 'error.log.*');
  80. $this->assertEquals(1, count($files));
  81. $result = file_get_contents($files[0]);
  82. $this->assertRegExp('/this text is under 35 bytes/', $result);
  83. $this->assertRegExp('/Warning: Test warning one/', $result);
  84. sleep(1);
  85. clearstatcache();
  86. $log->write('warning', 'Test warning third');
  87. $result = file_get_contents($path . 'error.log');
  88. $this->assertRegExp('/Warning: Test warning third/', $result);
  89. $files = glob($path . 'error.log.*');
  90. $this->assertEquals(2, count($files));
  91. $result = file_get_contents($files[0]);
  92. $this->assertRegExp('/this text is under 35 bytes/', $result);
  93. $result = file_get_contents($files[1]);
  94. $this->assertRegExp('/Warning: Test warning second/', $result);
  95. file_put_contents($path . 'error.log.0000000000', "The oldest log file with over 35 bytes.\n");
  96. sleep(1);
  97. clearstatcache();
  98. $log->write('warning', 'Test warning fourth');
  99. // rotate count reached so file count should not increase
  100. $files = glob($path . 'error.log.*');
  101. $this->assertEquals(2, count($files));
  102. $result = file_get_contents($path . 'error.log');
  103. $this->assertRegExp('/Warning: Test warning fourth/', $result);
  104. $result = file_get_contents(array_pop($files));
  105. $this->assertRegExp('/Warning: Test warning third/', $result);
  106. $result = file_get_contents(array_pop($files));
  107. $this->assertRegExp('/Warning: Test warning second/', $result);
  108. file_put_contents($path . 'debug.log', "this text is just greater than 35 bytes\n");
  109. $log = new FileLog(array(
  110. 'path' => $path,
  111. 'size' => 35,
  112. 'rotate' => 0
  113. ));
  114. file_put_contents($path . 'debug.log.0000000000', "The oldest log file with over 35 bytes.\n");
  115. $log->write('debug', 'Test debug');
  116. $this->assertTrue(file_exists($path . 'debug.log'));
  117. $result = file_get_contents($path . 'debug.log');
  118. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Test debug/', $result);
  119. $this->assertFalse(strstr($result, 'greater than 5 bytes'));
  120. $this->assertEquals(0, count(glob($path . 'debug.log.*')));
  121. }
  122. public function testMaskSetting() {
  123. if (DS === '\\') {
  124. $this->markTestSkipped('File permission testing does not work on Windows.');
  125. }
  126. $path = TMP . 'tests' . DS;
  127. $this->_deleteLogs($path);
  128. $log = new FileLog(array('path' => $path, 'mask' => 0666));
  129. $log->write('warning', 'Test warning one');
  130. $result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
  131. $expected = '0666';
  132. $this->assertEquals($expected, $result);
  133. unlink($path . 'error.log');
  134. $log = new FileLog(array('path' => $path, 'mask' => 0644));
  135. $log->write('warning', 'Test warning two');
  136. $result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
  137. $expected = '0644';
  138. $this->assertEquals($expected, $result);
  139. unlink($path . 'error.log');
  140. $log = new FileLog(array('path' => $path, 'mask' => 0640));
  141. $log->write('warning', 'Test warning three');
  142. $result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
  143. $expected = '0640';
  144. $this->assertEquals($expected, $result);
  145. unlink($path . 'error.log');
  146. }
  147. /**
  148. * helper function to clears all log files in specified directory
  149. *
  150. * @return void
  151. */
  152. protected function _deleteLogs($dir) {
  153. $files = array_merge(glob($dir . '*.log'), glob($dir . '*.log.*'));
  154. foreach ($files as $file) {
  155. unlink($file);
  156. }
  157. }
  158. }