ConsoleLogTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * ConsoleLogTest 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('ConsoleLog', 'Log/Engine');
  20. class TestConsoleLog extends ConsoleLog {
  21. }
  22. class TestCakeLog extends CakeLog {
  23. public static function replace($key, &$engine) {
  24. self::$_Collection->{$key} = $engine;
  25. }
  26. }
  27. /**
  28. * ConsoleLogTest class
  29. *
  30. * @package Cake.Test.Case.Log.Engine
  31. */
  32. class ConsoleLogTest extends CakeTestCase {
  33. public function setUp() {
  34. parent::setUp();
  35. CakeLog::config('debug', array(
  36. 'engine' => 'FileLog',
  37. 'types' => array('notice', 'info', 'debug'),
  38. 'file' => 'debug',
  39. ));
  40. CakeLog::config('error', array(
  41. 'engine' => 'FileLog',
  42. 'types' => array('error', 'warning'),
  43. 'file' => 'error',
  44. ));
  45. }
  46. public function tearDown() {
  47. parent::tearDown();
  48. if (file_exists(LOGS . 'error.log')) {
  49. unlink(LOGS . 'error.log');
  50. }
  51. if (file_exists(LOGS . 'debug.log')) {
  52. unlink(LOGS . 'debug.log');
  53. }
  54. }
  55. /**
  56. * Test writing to ConsoleOutput
  57. */
  58. public function testConsoleOutputWrites() {
  59. TestCakeLog::config('test_console_log', array(
  60. 'engine' => 'TestConsoleLog',
  61. ));
  62. $mock = $this->getMock('TestConsoleLog', array('write'), array(
  63. array('types' => 'error'),
  64. ));
  65. TestCakeLog::replace('test_console_log', $mock);
  66. $message = 'Test error message';
  67. $mock->expects($this->once())
  68. ->method('write');
  69. TestCakeLog::write(LOG_ERR, $message);
  70. }
  71. /**
  72. * Test logging to both ConsoleLog and FileLog
  73. */
  74. public function testCombinedLogWriting() {
  75. TestCakeLog::config('test_console_log', array(
  76. 'engine' => 'TestConsoleLog',
  77. ));
  78. $mock = $this->getMock('TestConsoleLog', array('write'), array(
  79. array('types' => 'error'),
  80. ));
  81. TestCakeLog::replace('test_console_log', $mock);
  82. // log to both file and console
  83. $message = 'Test error message';
  84. $mock->expects($this->once())
  85. ->method('write');
  86. TestCakeLog::write(LOG_ERR, $message);
  87. $this->assertTrue(file_exists(LOGS . 'error.log'), 'error.log missing');
  88. $logOutput = file_get_contents(LOGS . 'error.log');
  89. $this->assertContains($message, $logOutput);
  90. // TestConsoleLog is only interested in `error` type
  91. $message = 'Test info message';
  92. $mock->expects($this->never())
  93. ->method('write');
  94. TestCakeLog::write(LOG_INFO, $message);
  95. // checks that output is correctly written in the correct logfile
  96. $this->assertTrue(file_exists(LOGS . 'error.log'), 'error.log missing');
  97. $this->assertTrue(file_exists(LOGS . 'debug.log'), 'debug.log missing');
  98. $logOutput = file_get_contents(LOGS . 'error.log');
  99. $this->assertNotContains($message, $logOutput);
  100. $logOutput = file_get_contents(LOGS . 'debug.log');
  101. $this->assertContains($message, $logOutput);
  102. }
  103. /**
  104. * test default value of stream 'outputAs'
  105. */
  106. public function testDefaultOutputAs() {
  107. TestCakeLog::config('test_console_log', array(
  108. 'engine' => 'TestConsoleLog',
  109. ));
  110. if (DS == '\\' && !(bool)env('ANSICON')) {
  111. $expected = ConsoleOutput::PLAIN;
  112. } else {
  113. $expected = ConsoleOutput::COLOR;
  114. }
  115. $stream = TestCakeLog::stream('test_console_log');
  116. $config = $stream->config();
  117. $this->assertEquals($expected, $config['outputAs']);
  118. }
  119. }