PageRenderTime 48ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/kevindendievel/instruis
PHP | 193 lines | 124 code | 30 blank | 39 comment | 1 complexity | 93687d6841d2627de6f1bcaa7beff5ca 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. sleep(1);
  98. clearstatcache();
  99. $log->write('warning', 'Test warning fourth');
  100. // rotate count reached so file count should not increase
  101. $files = glob($path . 'error.log.*');
  102. $this->assertEquals(2, count($files));
  103. $result = file_get_contents($path . 'error.log');
  104. $this->assertRegExp('/Warning: Test warning fourth/', $result);
  105. $result = file_get_contents(array_pop($files));
  106. $this->assertRegExp('/Warning: Test warning third/', $result);
  107. $result = file_get_contents(array_pop($files));
  108. $this->assertRegExp('/Warning: Test warning second/', $result);
  109. file_put_contents($path . 'debug.log', "this text is just greater than 35 bytes\n");
  110. $log = new FileLog(array(
  111. 'path' => $path,
  112. 'size' => 35,
  113. 'rotate' => 0
  114. ));
  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. }