2012-05-08 15:55:47 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ConsoleLogTest file
|
|
|
|
*
|
2017-06-10 22:15:34 +00:00
|
|
|
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2012-05-08 15:55:47 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2012-05-08 15:55:47 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2012-05-08 15:55:47 +00:00
|
|
|
* @package Cake.Test.Case.Log.Engine
|
|
|
|
* @since CakePHP(tm) v 1.3
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2012-05-08 15:55:47 +00:00
|
|
|
*/
|
2013-05-30 22:11:14 +00:00
|
|
|
|
2012-05-08 15:55:47 +00:00
|
|
|
App::uses('ConsoleLog', 'Log/Engine');
|
|
|
|
|
2013-05-30 22:11:14 +00:00
|
|
|
/**
|
2016-08-10 10:22:09 +00:00
|
|
|
* TestConsoleLog
|
2013-05-30 22:11:14 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Log.Engine
|
|
|
|
*/
|
2012-05-08 15:55:47 +00:00
|
|
|
class TestConsoleLog extends ConsoleLog {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-30 22:11:14 +00:00
|
|
|
/**
|
2016-08-10 10:22:09 +00:00
|
|
|
* TestCakeLog
|
2013-05-30 22:11:14 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Log.Engine
|
|
|
|
*/
|
2012-05-08 15:55:47 +00:00
|
|
|
class TestCakeLog extends CakeLog {
|
|
|
|
|
|
|
|
public static function replace($key, &$engine) {
|
2015-07-21 08:22:53 +00:00
|
|
|
static::$_Collection->{$key} = $engine;
|
2012-05-08 15:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ConsoleLogTest class
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Log.Engine
|
|
|
|
*/
|
|
|
|
class ConsoleLogTest extends CakeTestCase {
|
|
|
|
|
2022-12-28 07:03:39 +00:00
|
|
|
public function setUp() : void {
|
2012-05-08 15:55:47 +00:00
|
|
|
parent::setUp();
|
|
|
|
CakeLog::config('debug', array(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
2012-05-08 15:55:47 +00:00
|
|
|
'types' => array('notice', 'info', 'debug'),
|
|
|
|
'file' => 'debug',
|
|
|
|
));
|
|
|
|
CakeLog::config('error', array(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
2012-05-08 15:55:47 +00:00
|
|
|
'types' => array('error', 'warning'),
|
|
|
|
'file' => 'error',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2022-12-28 07:03:39 +00:00
|
|
|
public function tearDown() : void {
|
2012-05-08 15:55:47 +00:00
|
|
|
parent::tearDown();
|
|
|
|
if (file_exists(LOGS . 'error.log')) {
|
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
}
|
|
|
|
if (file_exists(LOGS . 'debug.log')) {
|
|
|
|
unlink(LOGS . 'debug.log');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test writing to ConsoleOutput
|
2014-04-02 01:02:37 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2012-05-08 15:55:47 +00:00
|
|
|
*/
|
|
|
|
public function testConsoleOutputWrites() {
|
|
|
|
TestCakeLog::config('test_console_log', array(
|
2013-06-21 14:43:01 +00:00
|
|
|
'engine' => 'TestConsole',
|
2012-05-08 15:55:47 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$mock = $this->getMock('TestConsoleLog', array('write'), array(
|
|
|
|
array('types' => 'error'),
|
|
|
|
));
|
|
|
|
TestCakeLog::replace('test_console_log', $mock);
|
|
|
|
|
|
|
|
$message = 'Test error message';
|
|
|
|
$mock->expects($this->once())
|
|
|
|
->method('write');
|
2012-05-11 13:42:54 +00:00
|
|
|
TestCakeLog::write(LOG_ERR, $message);
|
2012-05-08 15:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test logging to both ConsoleLog and FileLog
|
2014-04-02 01:02:37 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2012-05-08 15:55:47 +00:00
|
|
|
*/
|
|
|
|
public function testCombinedLogWriting() {
|
|
|
|
TestCakeLog::config('test_console_log', array(
|
2013-06-21 14:43:01 +00:00
|
|
|
'engine' => 'TestConsole',
|
2012-05-08 15:55:47 +00:00
|
|
|
));
|
|
|
|
$mock = $this->getMock('TestConsoleLog', array('write'), array(
|
|
|
|
array('types' => 'error'),
|
|
|
|
));
|
|
|
|
TestCakeLog::replace('test_console_log', $mock);
|
|
|
|
|
|
|
|
// log to both file and console
|
|
|
|
$message = 'Test error message';
|
|
|
|
$mock->expects($this->once())
|
|
|
|
->method('write');
|
2012-05-11 13:42:54 +00:00
|
|
|
TestCakeLog::write(LOG_ERR, $message);
|
2012-05-08 15:55:47 +00:00
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'), 'error.log missing');
|
|
|
|
$logOutput = file_get_contents(LOGS . 'error.log');
|
2022-12-30 08:53:58 +00:00
|
|
|
$this->assertStringContainsString($message, $logOutput);
|
2012-05-08 15:55:47 +00:00
|
|
|
|
|
|
|
// TestConsoleLog is only interested in `error` type
|
|
|
|
$message = 'Test info message';
|
|
|
|
$mock->expects($this->never())
|
|
|
|
->method('write');
|
|
|
|
TestCakeLog::write(LOG_INFO, $message);
|
|
|
|
|
|
|
|
// checks that output is correctly written in the correct logfile
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'), 'error.log missing');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'debug.log'), 'debug.log missing');
|
|
|
|
$logOutput = file_get_contents(LOGS . 'error.log');
|
|
|
|
$this->assertNotContains($message, $logOutput);
|
|
|
|
$logOutput = file_get_contents(LOGS . 'debug.log');
|
2022-12-30 08:53:58 +00:00
|
|
|
$this->assertStringContainsString($message, $logOutput);
|
2012-05-08 15:55:47 +00:00
|
|
|
}
|
|
|
|
|
2012-07-02 14:19:02 +00:00
|
|
|
/**
|
|
|
|
* test default value of stream 'outputAs'
|
2014-04-02 01:02:37 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2012-07-02 14:19:02 +00:00
|
|
|
*/
|
|
|
|
public function testDefaultOutputAs() {
|
|
|
|
TestCakeLog::config('test_console_log', array(
|
2013-06-21 14:43:01 +00:00
|
|
|
'engine' => 'TestConsole',
|
2012-07-02 14:19:02 +00:00
|
|
|
));
|
2015-07-23 03:48:34 +00:00
|
|
|
if ((DS === '\\' && !(bool)env('ANSICON') && env('ConEmuANSI') !== 'ON') ||
|
2014-09-13 06:19:40 +00:00
|
|
|
(function_exists('posix_isatty') && !posix_isatty(null))
|
|
|
|
) {
|
2012-07-02 14:19:02 +00:00
|
|
|
$expected = ConsoleOutput::PLAIN;
|
|
|
|
} else {
|
|
|
|
$expected = ConsoleOutput::COLOR;
|
|
|
|
}
|
|
|
|
$stream = TestCakeLog::stream('test_console_log');
|
|
|
|
$config = $stream->config();
|
|
|
|
$this->assertEquals($expected, $config['outputAs']);
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:55:47 +00:00
|
|
|
}
|