Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Automatic console logging streams were not respecting --quiet

  • Loading branch information...
commit ed4493da0c9492d4d91eda000924fb7c582b9dc8 1 parent 591022f
José Lorenzo Rodríguez lorenzo authored
Showing with 63 additions and 12 deletions.
  1. +30 −11 lib/Cake/Console/Shell.php
  2. +33 −1 lib/Cake/Test/Case/Console/ShellTest.php
41 lib/Cake/Console/Shell.php
View
@@ -165,23 +165,13 @@ public function __construct($stdout = null, $stderr = null, $stdin = null) {
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 @@ public function runCommand($command, $argv) {
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 @@ protected function _pluginPath($pluginName) {
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,
+ ));
+ }
}
34 lib/Cake/Test/Case/Console/ShellTest.php
View
@@ -80,6 +80,10 @@ public function mergeVars($properties, $class, $normalize = true) {
return $this->_mergeVars($properties, $class, $normalize);
}
+ public function useLogger($enable = true) {
+ $this->_useLogger($enable);
+ }
+
}
/**
@@ -825,7 +829,7 @@ public function testFileAndConsoleLogging() {
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 @@ public function testFileAndConsoleLogging() {
$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'));
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.