From ed4493da0c9492d4d91eda000924fb7c582b9dc8 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Fri, 6 Jul 2012 16:00:13 -0430 Subject: [PATCH] Automatic console logging streams were not respecting --quiet --- lib/Cake/Console/Shell.php | 41 +++++++++++++++++------- lib/Cake/Test/Case/Console/ShellTest.php | 34 +++++++++++++++++++- 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/lib/Cake/Console/Shell.php b/lib/Cake/Console/Shell.php index a23a38fb3..801410ba5 100644 --- a/lib/Cake/Console/Shell.php +++ b/lib/Cake/Console/Shell.php @@ -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, + )); + } } diff --git a/lib/Cake/Test/Case/Console/ShellTest.php b/lib/Cake/Test/Case/Console/ShellTest.php index 4d7248243..875b23dcc 100644 --- a/lib/Cake/Test/Case/Console/ShellTest.php +++ b/lib/Cake/Test/Case/Console/ShellTest.php @@ -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')); + } + }