FileLogTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 2005-2012, 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-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Log.Engine
  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.Test.Case.Log.Engine
  24. */
  25. class FileLogTest extends CakeTestCase {
  26. /**
  27. * testLogFileWriting method
  28. *
  29. * @return void
  30. */
  31. public function testLogFileWriting() {
  32. if (file_exists(LOGS . 'error.log')) {
  33. unlink(LOGS . 'error.log');
  34. }
  35. $log = new FileLog();
  36. $log->write('warning', 'Test warning');
  37. $this->assertTrue(file_exists(LOGS . 'error.log'));
  38. $result = file_get_contents(LOGS . 'error.log');
  39. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning/', $result);
  40. unlink(LOGS . 'error.log');
  41. if (file_exists(LOGS . 'debug.log')) {
  42. unlink(LOGS . 'debug.log');
  43. }
  44. $log->write('debug', 'Test warning');
  45. $this->assertTrue(file_exists(LOGS . 'debug.log'));
  46. $result = file_get_contents(LOGS . 'debug.log');
  47. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Test warning/', $result);
  48. unlink(LOGS . 'debug.log');
  49. if (file_exists(LOGS . 'random.log')) {
  50. unlink(LOGS . 'random.log');
  51. }
  52. $log->write('random', 'Test warning');
  53. $this->assertTrue(file_exists(LOGS . 'random.log'));
  54. $result = file_get_contents(LOGS . 'random.log');
  55. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Random: Test warning/', $result);
  56. unlink(LOGS . 'random.log');
  57. }
  58. /**
  59. * test using the path setting to write logs in other places.
  60. *
  61. * @return void
  62. */
  63. public function testPathSetting() {
  64. $path = TMP . 'tests' . DS;
  65. if (file_exists(LOGS . 'error.log')) {
  66. unlink(LOGS . 'error.log');
  67. }
  68. $log = new FileLog(compact('path'));
  69. $log->write('warning', 'Test warning');
  70. $this->assertTrue(file_exists($path . 'error.log'));
  71. unlink($path . 'error.log');
  72. }
  73. }