FileLog.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * FileLogTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake.tests.cases.libs.log
  16. * @since CakePHP(tm) v 1.3
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('FileLog', 'Log/Engine');
  20. /**
  21. * CakeLogTest class
  22. *
  23. * @package cake.tests.cases.libs
  24. */
  25. class FileLogTest extends CakeTestCase {
  26. /**
  27. * testLogFileWriting method
  28. *
  29. * @access public
  30. * @return void
  31. */
  32. function testLogFileWriting() {
  33. if (file_exists(LOGS . 'error.log')) {
  34. unlink(LOGS . 'error.log');
  35. }
  36. $log = new FileLog();
  37. $log->write('warning', 'Test warning');
  38. $this->assertTrue(file_exists(LOGS . 'error.log'));
  39. $result = file_get_contents(LOGS . 'error.log');
  40. $this->assertPattern('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning/', $result);
  41. unlink(LOGS . 'error.log');
  42. if (file_exists(LOGS . 'debug.log')) {
  43. unlink(LOGS . 'debug.log');
  44. }
  45. $log->write('debug', 'Test warning');
  46. $this->assertTrue(file_exists(LOGS . 'debug.log'));
  47. $result = file_get_contents(LOGS . 'debug.log');
  48. $this->assertPattern('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Test warning/', $result);
  49. unlink(LOGS . 'debug.log');
  50. if (file_exists(LOGS . 'random.log')) {
  51. unlink(LOGS . 'random.log');
  52. }
  53. $log->write('random', 'Test warning');
  54. $this->assertTrue(file_exists(LOGS . 'random.log'));
  55. $result = file_get_contents(LOGS . 'random.log');
  56. $this->assertPattern('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Random: Test warning/', $result);
  57. unlink(LOGS . 'random.log');
  58. }
  59. /**
  60. * test using the path setting to write logs in other places.
  61. *
  62. * @return void
  63. */
  64. function testPathSetting() {
  65. $path = TMP . 'tests' . DS;
  66. if (file_exists(LOGS . 'error.log')) {
  67. unlink(LOGS . 'error.log');
  68. }
  69. $log = new FileLog(compact('path'));
  70. $log->write('warning', 'Test warning');
  71. $this->assertTrue(file_exists($path . 'error.log'));
  72. unlink($path . 'error.log');
  73. }
  74. }