From 78962a9be3756fd399a6a1757f30f4c3c65ea1ee Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel Date: Tue, 21 Dec 2010 22:21:54 +0100 Subject: [PATCH] Allow to activate logging. --- framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module.php | 6 ++-- .../Kolab_Cli/lib/Horde/Kolab/Cli/Module/Base.php | 34 ++++++++++++++++++---- .../lib/Horde/Kolab/Cli/Module/Folder.php | 6 ++-- framework/Kolab_Cli/package.xml | 4 +++ .../Kolab_Cli/test/Horde/Kolab/Cli/TestCase.php | 18 ++++++++++++ .../Horde/Kolab/Cli/Unit/Cli/Module/BaseTest.php | 28 ++++++++++++++++-- .../test/Horde/Kolab/Cli/Unit/Cli/OptionsTest.php | 11 +++++++ 7 files changed, 93 insertions(+), 14 deletions(-) diff --git a/framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module.php b/framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module.php index 531762a7d..650791b54 100644 --- a/framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module.php +++ b/framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module.php @@ -33,10 +33,10 @@ extends Horde_Cli_Modular_Module /** * Handle the options and arguments. * - * @param mixed $options An array of options. - * @param mixed $arguments An array of arguments. + * @param mixed &$options An array of options. + * @param mixed &$arguments An array of arguments. * * @return NULL */ - public function handleArguments($options, $arguments); + public function handleArguments(&$options, &$arguments); } \ No newline at end of file diff --git a/framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module/Base.php b/framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module/Base.php index 702b710da..2d48916ae 100644 --- a/framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module/Base.php +++ b/framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module/Base.php @@ -65,7 +65,8 @@ Choices are: - pear [IMAP]: The PEAR-Net_IMAP driver - roundcube [IMAP]: The roundcube IMAP driver - mock [Mem.]: A dummy driver that uses memory." - ) + ), + 'default' => 'horde' ) ), new Horde_Argv_Option( @@ -89,7 +90,8 @@ Choices are: '--host', array( 'action' => 'store', - 'help' => Horde_Kolab_Cli_Translation::t('The host that holds the data.') + 'help' => Horde_Kolab_Cli_Translation::t('The host that holds the data.'), + 'default' => 'localhost' ) ), new Horde_Argv_Option( @@ -100,6 +102,14 @@ Choices are: 'help' => Horde_Kolab_Cli_Translation::t('Produce time measurements to indicate how long the processing takes.') ) ), + new Horde_Argv_Option( + '-l', + '--log', + array( + 'action' => 'store', + 'help' => Horde_Kolab_Cli_Translation::t('Write a log file in the provided LOG location.') + ) + ), ); } @@ -146,19 +156,31 @@ Choices are: /** * Handle the options and arguments. * - * @param mixed $options An array of options. - * @param mixed $arguments An array of arguments. + * @param mixed &$options An array of options. + * @param mixed &$arguments An array of arguments. * * @return NULL */ - public function handleArguments($options, $arguments) + public function handleArguments(&$options, &$arguments) { - if (in_array($options['driver'], array('roundcube', 'php', 'pear'))) { + if (isset($options['driver']) + && in_array($options['driver'], array('roundcube', 'php', 'pear'))) { if (defined('E_DEPRECATED')) { error_reporting(E_ALL & ~E_STRICT & ~E_DEPRECATED & ~E_NOTICE); } else { error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE); } } + if (isset($options['log'])) { + if (class_exists('Horde_Log_Logger')) { + $options['logger'] = new Horde_Log_Logger( + new Horde_Log_Handler_Stream( + $options['log'] + ) + ); + } else { + file_put_contents($options['log'], 'The Horde_Log_Logger class is not available!'); + } + } } } \ No newline at end of file diff --git a/framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module/Folder.php b/framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module/Folder.php index d74516039..ab5cc9f5e 100644 --- a/framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module/Folder.php +++ b/framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module/Folder.php @@ -96,12 +96,12 @@ implements Horde_Kolab_Cli_Module /** * Handle the options and arguments. * - * @param mixed $options An array of options. - * @param mixed $arguments An array of arguments. + * @param mixed &$options An array of options. + * @param mixed &$arguments An array of arguments. * * @return NULL */ - public function handleArguments($options, $arguments) + public function handleArguments(&$options, &$arguments) { } diff --git a/framework/Kolab_Cli/package.xml b/framework/Kolab_Cli/package.xml index 1ff398e70..62ff61fda 100644 --- a/framework/Kolab_Cli/package.xml +++ b/framework/Kolab_Cli/package.xml @@ -103,6 +103,10 @@ Support pear.horde.org + + Log + pear.horde.org + diff --git a/framework/Kolab_Cli/test/Horde/Kolab/Cli/TestCase.php b/framework/Kolab_Cli/test/Horde/Kolab/Cli/TestCase.php index 7cf5d24be..5be1fc8ac 100644 --- a/framework/Kolab_Cli/test/Horde/Kolab/Cli/TestCase.php +++ b/framework/Kolab_Cli/test/Horde/Kolab/Cli/TestCase.php @@ -30,6 +30,15 @@ class Horde_Kolab_Cli_TestCase extends PHPUnit_Framework_TestCase { + private $_log_file; + + public function tearDown() + { + if ($this->_log_file !== null && file_exists($this->_log_file)) { + unlink($this->_log_file); + } + } + protected function runCli() { ob_start(); @@ -43,4 +52,13 @@ extends PHPUnit_Framework_TestCase ob_end_clean(); return $output; } + + protected function getLogFile() + { + $this->_log_file = sys_get_temp_dir() . DIRECTORY_SEPARATOR + . 'Kolab_Cli_' . mt_rand() . '.log'; + return $this->_log_file; + } + + } diff --git a/framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/Cli/Module/BaseTest.php b/framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/Cli/Module/BaseTest.php index 3d94a8c71..0b04d0bc7 100644 --- a/framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/Cli/Module/BaseTest.php +++ b/framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/Cli/Module/BaseTest.php @@ -37,12 +37,14 @@ extends Horde_Kolab_Cli_TestCase { public function setUp() { + parent::setUp(); $this->_storeErrorHandling(); } public function tearDown() { $this->_restoreErrorHandling(); + parent::tearDown(); } public function testNotice() @@ -52,18 +54,40 @@ extends Horde_Kolab_Cli_TestCase public function testMissingNoticeWithRoundcubeDriver() { + $options = array('driver' => 'roundcube'); + $arguments = array(); $base = new Horde_Kolab_Cli_Module_Base(); - $base->handleArguments(array('driver' => 'roundcube'), array()); + $base->handleArguments($options, $arguments); $this->assertFalse((bool) (error_reporting() & E_NOTICE)); } public function testMissingNoticeWithHordeDriver() { + $options = array('driver' => 'horde'); + $arguments = array(); $base = new Horde_Kolab_Cli_Module_Base(); - $base->handleArguments(array('driver' => 'horde'), array()); + $base->handleArguments($options, $arguments); $this->assertTrue((bool) (error_reporting() & E_NOTICE)); } + public function testLoggerOption() + { + $options = array('log' => $this->getLogFile()); + $arguments = array(); + $base = new Horde_Kolab_Cli_Module_Base(); + $base->handleArguments($options, $arguments); + $this->assertContains('logger', array_keys($options)); + } + + public function testLoggerClass() + { + $options = array('log' => $this->getLogFile()); + $arguments = array(); + $base = new Horde_Kolab_Cli_Module_Base(); + $base->handleArguments($options, $arguments); + $this->assertInstanceOf('Horde_Log_Logger', $options['logger']); + } + private function _storeErrorHandling() { $this->_error_handling = error_reporting(); diff --git a/framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/Cli/OptionsTest.php b/framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/Cli/OptionsTest.php index 44569c858..6b1b51216 100644 --- a/framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/Cli/OptionsTest.php +++ b/framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/Cli/OptionsTest.php @@ -100,4 +100,15 @@ extends Horde_Kolab_Cli_TestCase $this->runCli() ); } + + public function testOptionLog() + { + $_SERVER['argv'] = array( + 'klb' + ); + $this->assertRegExp( + '/-l[ ]*LOG,[ ]*--log=LOG/', + $this->runCli() + ); + } } -- 2.11.0