PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

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