Automatic console logging streams were not respecting --quiet

This commit is contained in:
Jose Lorenzo Rodriguez 2012-07-06 16:00:13 -04:30
parent 591022f182
commit ed4493da0c
2 changed files with 63 additions and 12 deletions

View file

@ -165,23 +165,13 @@ class Shell extends Object {
if ($this->stdout == null) {
$this->stdout = new ConsoleOutput('php://stdout');
}
CakeLog::config('stdout', array(
'engine' => 'ConsoleLog',
'types' => array('notice', 'info'),
'stream' => $this->stdout,
));
if ($this->stderr == null) {
$this->stderr = new ConsoleOutput('php://stderr');
}
CakeLog::config('stderr', array(
'engine' => 'ConsoleLog',
'types' => array('emergency', 'alert', 'critical', 'error', 'warning', 'debug'),
'stream' => $this->stderr,
));
if ($this->stdin == null) {
$this->stdin = new ConsoleInput('php://stdin');
}
$this->_useLogger();
$parent = get_parent_class($this);
if ($this->tasks !== null && $this->tasks !== false) {
$this->_mergeVars(array('tasks'), $parent, true);
@ -379,6 +369,10 @@ class Shell extends Object {
return false;
}
if (!empty($this->params['quiet'])) {
$this->_useLogger(false);
}
$this->command = $command;
if (!empty($this->params['help'])) {
return $this->_displayHelp($command);
@ -825,4 +819,29 @@ class Shell extends Object {
return current(App::path('plugins')) . $pluginName . DS;
}
/**
* Used to enable or disable logging stream output to stdout and stderr
* If you don't wish to see in your stdout or stderr everything that is logged
* through CakeLog, call this function with first param as false
*
* @param boolean $enable wheter to enable CakeLog output or not
* @return void
**/
protected function _useLogger($enable = true) {
if (!$enable) {
CakeLog::drop('stdout');
CakeLog::drop('stderr');
return;
}
CakeLog::config('stdout', array(
'engine' => 'ConsoleLog',
'types' => array('notice', 'info'),
'stream' => $this->stdout,
));
CakeLog::config('stderr', array(
'engine' => 'ConsoleLog',
'types' => array('emergency', 'alert', 'critical', 'error', 'warning', 'debug'),
'stream' => $this->stderr,
));
}
}

View file

@ -80,6 +80,10 @@ class ShellTestShell extends Shell {
return $this->_mergeVars($properties, $class, $normalize);
}
public function useLogger($enable = true) {
$this->_useLogger($enable);
}
}
/**
@ -825,7 +829,7 @@ TEXT;
require_once CORE_TEST_CASES . DS . 'Log' . DS . 'Engine' . DS . 'ConsoleLogTest.php';
$mock = $this->getMock('ConsoleLog', array('write'), array(
array('types' => 'error'),
));
));
TestCakeLog::config('console', array(
'engine' => 'ConsoleLog',
'stream' => 'php://stderr',
@ -840,4 +844,32 @@ TEXT;
$this->assertContains($this->Shell->testMessage, $contents);
}
/**
* Tests that _useLogger works properly
*
* @return void
**/
public function testProtectedUseLogger() {
CakeLog::drop('stdout');
CakeLog::drop('stderr');
$this->Shell->useLogger(true);
$this->assertNotEmpty(CakeLog::stream('stdout'));
$this->assertNotEmpty(CakeLog::stream('stderr'));
$this->Shell->useLogger(false);
$this->assertFalse(CakeLog::stream('stdout'));
$this->assertFalse(CakeLog::stream('stderr'));
}
/**
* Test file and console and logging quiet output
*/
public function testQuietLog() {
$output = $this->getMock('ConsoleOutput', array(), array(), '', false);
$error = $this->getMock('ConsoleOutput', array(), array(), '', false);
$in = $this->getMock('ConsoleInput', array(), array(), '', false);
$this->Shell = $this->getMock('ShellTestShell', array('_useLogger'), array($output, $error, $in));
$this->Shell->expects($this->once())->method('_useLogger')->with(false);
$this->Shell->runCommand('foo', array('--quiet'));
}
}