Allow to activate logging.
authorGunnar Wrobel <p@rdus.de>
Tue, 21 Dec 2010 21:21:54 +0000 (22:21 +0100)
committerGunnar Wrobel <p@rdus.de>
Tue, 4 Jan 2011 07:54:12 +0000 (08:54 +0100)
framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module.php
framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module/Base.php
framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module/Folder.php
framework/Kolab_Cli/package.xml
framework/Kolab_Cli/test/Horde/Kolab/Cli/TestCase.php
framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/Cli/Module/BaseTest.php
framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/Cli/OptionsTest.php

index 531762a..650791b 100644 (file)
@@ -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
index 702b710..2d48916 100644 (file)
@@ -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
index d745160..ab5cc9f 100644 (file)
@@ -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)
     {
     }
 
index 1ff398e..62ff61f 100644 (file)
     <name>Support</name>
     <channel>pear.horde.org</channel>
    </package>
+   <package>
+    <name>Log</name>
+    <channel>pear.horde.org</channel>
+   </package>
   </optional>
  </dependencies>
  <phprelease>
index 7cf5d24..5be1fc8 100644 (file)
 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;
+    }
+
+
 }
index 3d94a8c..0b04d0b 100644 (file)
@@ -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();
index 44569c8..6b1b512 100644 (file)
@@ -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()
+        );
+    }
 }