Extract the command line parsing.
authorGunnar Wrobel <p@rdus.de>
Thu, 8 Jul 2010 11:40:12 +0000 (13:40 +0200)
committerGunnar Wrobel <p@rdus.de>
Wed, 25 Aug 2010 17:21:36 +0000 (19:21 +0200)
framework/Kolab_Filter/lib/Horde/Kolab/Filter/Base.php
framework/Kolab_Filter/lib/Horde/Kolab/Filter/Cli.php [new file with mode: 0644]
framework/Kolab_Filter/lib/Horde/Kolab/Filter/Cli/Parser.php [new file with mode: 0644]
framework/Kolab_Filter/lib/Horde/Kolab/Filter/Exception.php
framework/Kolab_Filter/lib/Horde/Kolab/Filter/Exception/Usage.php [new file with mode: 0644]
framework/Kolab_Filter/package.xml
framework/Kolab_Filter/test/Horde/Kolab/Filter/FilterTest.php [deleted file]
framework/Kolab_Filter/test/Horde/Kolab/Filter/Integration/CliTest.php [new file with mode: 0644]

index 55434f1..d7240f2 100644 (file)
@@ -74,6 +74,13 @@ class Horde_Kolab_Filter_Base
     var $_sasl_username;
 
     /**
+     * Comman line parser.
+     *
+     * @param Horde_Kolab_Filter_Cli 
+     */
+    private $_cli;
+
+    /**
      * The log backend that needs to implement the debug(), info() and err()
      * methods.
      *
@@ -87,8 +94,10 @@ class Horde_Kolab_Filter_Base
      * @param mixed $logger The logger.
      */
     public function __construct(
+        Horde_Kolab_Filter_Cli $cli,
         $logger
     ) {
+        $this->_cli    = $cli;
         $this->_logger = $logger;
     }
 
@@ -187,57 +196,12 @@ class Horde_Kolab_Filter_Base
     {
         global $conf;
 
-        /* Get command line options. */
-        $p = new Horde_Kolab_Filter_Argv_Parser(
-            array('optionList' =>
-                  array(
-                      new Horde_Argv_Option('-s',
-                                            '--sender',
-                                            array('help' => 'The message sender.',
-                                                  'type' => 'string',
-                                                  'nargs' => 1)),
-                      new Horde_Argv_Option('-r',
-                                            '--recipient',
-                                            array('help' => 'A message recipient.',
-                                                  'action' => 'append',
-                                                  'type' => 'string')),
-                      new Horde_Argv_Option('-H',
-                                            '--host',
-                                            array('help' => 'The host running this script.')),
-                      new Horde_Argv_Option('-c',
-                                            '--client',
-                                            array('help' => 'The client sending the message.')),
-                      new Horde_Argv_Option('-u',
-                                            '--user',
-                                            array('help' => 'ID of the currently authenticated user.',
-                                                  'default' => '')),
-                      new Horde_Argv_Option('-C',
-                                            '--config',
-                                            array('help' => 'Path to the configuration file for this filter.'))
-                  )));
-
-        try {
-            list($values, $args) = $p->parseArgs();
-        } catch (InvalidArgumentException $e) {
-            $msg = $e->getMessage() . "\n\n" . $p->getUsage();
-            return PEAR::raiseError($msg, OUT_STDOUT | EX_USAGE);
-        }
+        $values = $this->_cli->getOptions();
 
         if (!empty($values['config']) && file_exists($values['config'])) {
             require_once $values['config'];
         }
 
-        if (empty($values['recipient'])) {
-            throw new Horde_Kolab_Filter_Exception(
-                sprintf(
-                    "Please provide one or more recipients.\n\n%s",
-                    $p->getUsage()
-                ), 
-                Horde_Kolab_Filter_Exception::OUT_STDOUT |
-                Horde_Kolab_Filter_Exception::EX_USAGE
-            );
-        }
-
         $this->_sender = strtolower($values['sender']);
         $this->_recipients = array_map('strtolower', $values['recipient']);
         $this->_client_address = $values['client'];
@@ -298,15 +262,3 @@ class Horde_Kolab_Filter_Base
     }
 }
 
-class Horde_Kolab_Filter_Argv_Parser extends Horde_Argv_Parser
-{
-    public function parserError($msg)
-    {
-        throw new InvalidArgumentException(sprintf("%s: error: %s\n", $this->getProgName(), $msg));
-    }
-
-    public function parserExit($status = 0, $msg = null)
-    {
-        throw new InvalidArgumentException(sprintf("%s: error: %s\n", $this->getProgName(), $msg));
-    }
-}
diff --git a/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Cli.php b/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Cli.php
new file mode 100644 (file)
index 0000000..70cc291
--- /dev/null
@@ -0,0 +1,149 @@
+<?php
+/**
+ * The command line handling for the Kolab_Filter package.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package  Kolab_Filter
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+
+/**
+ * The command line handling for the Kolab_Filter package.
+ *
+ * Copyright 2008 Klarälvdalens Datakonsult AB
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you did not
+ * receive this file, see
+ * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+ *
+ * @category Kolab
+ * @package  Kolab_Filter
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+class Horde_Kolab_Filter_Cli
+{
+    /**
+     * The CLI argument parser.
+     *
+     * @var Horde_Argv_Parser
+     */
+    private $_parser;
+
+    /**
+     * The CLI options.
+     *
+     * @var Horde_Argv_Values
+     */
+    private $_options;
+
+    /**
+     * The CLI arguments.
+     *
+     * @var array
+     */
+    private $_arguments;
+
+    /**
+     * Constructor.
+     */
+    public function __construct()
+    {
+        $this->_parser = new Horde_Kolab_Filter_Cli_Parser(
+            array('optionList' =>
+                  array(
+                      new Horde_Argv_Option(
+                          '-s',
+                          '--sender',
+                          array(
+                              'help' => 'The message sender.',
+                              'type' => 'string',
+                              'nargs' => 1
+                          )
+                      ),
+                      new Horde_Argv_Option(
+                          '-r',
+                          '--recipient',
+                          array(
+                              'help' => 'A message recipient.',
+                              'action' => 'append',
+                              'type' => 'string'
+                          )
+                      ),
+                      new Horde_Argv_Option(
+                          '-H',
+                          '--host',
+                          array(
+                              'help' => 'The host running this script.'
+                          )
+                      ),
+                      new Horde_Argv_Option(
+                          '-c',
+                          '--client',
+                          array(
+                              'help' => 'The client sending the message.'
+                          )
+                      ),
+                      new Horde_Argv_Option(
+                          '-u',
+                          '--user',
+                          array(
+                              'help' => 'ID of the currently authenticated user.',
+                              'default' => ''
+                          )
+                      ),
+                      new Horde_Argv_Option(
+                          '-C',
+                          '--config',
+                          array(
+                              'help' => 'Path to the configuration file for this filter.'
+                          )
+                      )
+                  )
+            )
+        );
+    }
+
+    /**
+     * Parse the command line arguments.
+     *
+     * @return NULL
+     */
+    public function parse()
+    {
+        try {
+            list($this->_options, $this->_arguments) = $this->_parser->parseArgs();
+        } catch (InvalidArgumentException $e) {
+            throw new Horde_Kolab_Filter_Exception_Usage(
+                $e->getMessage() . "\n\n" . $this->_parser->getUsage()
+            );
+        }
+
+        if (empty($this->_options['recipient'])) {
+            throw new Horde_Kolab_Filter_Exception_Usage(
+                sprintf(
+                    "Please provide one or more recipients.\n\n%s",
+                    $this->_parser->getUsage()
+                )
+            );
+        }
+    }
+
+    /**
+     * Return the command line options.
+     *
+     * @return Horde_Argv_Values The command line values.
+     */
+    public function getOptions()
+    {
+        if ($this->_options === null) {
+            $this->parse();
+        }
+        return $this->_options;
+    }
+}
\ No newline at end of file
diff --git a/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Cli/Parser.php b/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Cli/Parser.php
new file mode 100644 (file)
index 0000000..8c1c1ed
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+/**
+ * A parser variant that does not automatically exit on a parser error.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package  Kolab_Filter
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+
+/**
+ * A parser variant that does not automatically exit on a parser error.
+ *
+ * Copyright 2008 Klarälvdalens Datakonsult AB
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you did not
+ * receive this file, see
+ * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+ *
+ * @category Kolab
+ * @package  Kolab_Filter
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+class Horde_Kolab_Filter_Cli_Parser extends Horde_Argv_Parser
+{
+    public function parserError($msg)
+    {
+        return $this->parserExit(0, $msg);
+    }
+
+    public function parserExit($status = 0, $msg = null)
+    {
+        throw new InvalidArgumentException(
+            sprintf("%s: error: %s\n", $this->getProgName(), $msg)
+        );
+    }
+}
index 6f01eb1..7aca1a0 100644 (file)
@@ -26,7 +26,7 @@
  * @link     http://pear.horde.org/index.php?package=Kolab_Filter
  */
 class Horde_Kolab_Filter_Exception
-extends Exception
+extends Horde_Exception
 {
     /**
      * Failure constants from postfix src/global/sys_exits.h
diff --git a/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Exception/Usage.php b/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Exception/Usage.php
new file mode 100644 (file)
index 0000000..b86af8c
--- /dev/null
@@ -0,0 +1,48 @@
+<?php
+/**
+ * This class provides an error thrown when the user supplied invalid command
+ * line parameters.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package  Kolab_Filter
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+
+/**
+ * This class provides an error thrown when the user supplied invalid command
+ * line parameters.
+ *
+ * Copyright 2010 Klarälvdalens Datakonsult AB
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Kolab
+ * @package  Kolab_Filter
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+class Horde_Kolab_Filter_Exception_Usage
+extends Horde_Kolab_Filter_Exception
+{
+    /**
+     * Construct the exception
+     *
+     * @param string $msg
+     * @param Exception $previous
+     */
+    public function __construct($msg = '', Exception $previous = null)
+    {
+        parent::__construct(
+            $msg,
+            Horde_Kolab_Filter_Exception::OUT_STDOUT |
+            Horde_Kolab_Filter_Exception::EX_USAGE,
+            $previous
+        );
+    }
+}
index 39d1a98..1d192a0 100644 (file)
@@ -74,9 +74,16 @@ http://pear.php.net/dtd/package-2.0.xsd">
        <file name="Itip.php" role="php" />
       </dir> <!-- /lib/Horde/Kolab/Resource -->
       <dir name="Filter">
+       <file name="Cli.php" role="php" />
+       <dir name="Cli">
+        <file name="Parser.php" role="php" />
+       </dir> <!-- /lib/Horde/Kolab/Filter/Cli -->
        <file name="Content.php" role="php" />
        <file name="Base.php" role="php" />
        <file name="Exception.php" role="php" />
+       <dir name="Exception">
+        <file name="Usage.php" role="php" />
+       </dir> <!-- /lib/Horde/Kolab/Filter/Exception -->
        <file name="Incoming.php" role="php" />
        <file name="Outlook.php" role="php" />
        <file name="Response.php" role="php" />
@@ -192,12 +199,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <min>1.4.0b1</min>
    </pearinstaller>
    <package>
-    <name>Horde</name>
+    <name>Core</name>
     <channel>pear.horde.org</channel>
-    <min>0.0.2</min>
    </package>
    <package>
-    <name>Icalendar</name>
+    <name>Horde_iCalendar</name>
     <channel>pear.horde.org</channel>
     <min>0.0.3</min>
    </package>
@@ -206,6 +212,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <channel>pear.horde.org</channel>
    </package>
    <package>
+    <name>Exception</name>
+    <channel>pear.horde.org</channel>
+   </package>
+   <package>
     <name>Horde_MIME</name>
     <channel>pear.horde.org</channel>
     <min>0.0.2</min>
@@ -242,10 +252,13 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <install name="lib/Horde/Kolab/Resource/Freebusy.php" as="Horde/Kolab/Resource/Freebusy.php" />
    <install name="lib/Horde/Kolab/Resource/Freebusy/Kolab.php" as="Horde/Kolab/Resource/Freebusy/Kolab.php" />
    <install name="lib/Horde/Kolab/Resource/Freebusy/Mock.php" as="Horde/Kolab/Resource/Freebusy/Mock.php" />
-   <install name="lib/Horde/Kolab/Filter/Content.php" as="Horde/Kolab/Filter/Content.php" />
    <install name="lib/Horde/Kolab/Test/Filter.php" as="Horde/Kolab/Test/Filter.php" />
+   <install name="lib/Horde/Kolab/Filter/Cli.php" as="Horde/Kolab/Filter/Cli.php" />
+   <install name="lib/Horde/Kolab/Filter/Cli/Parser.php" as="Horde/Kolab/Filter/Cli/Parser.php" />
+   <install name="lib/Horde/Kolab/Filter/Content.php" as="Horde/Kolab/Filter/Content.php" />
    <install name="lib/Horde/Kolab/Filter/Base.php" as="Horde/Kolab/Filter/Base.php" />
    <install name="lib/Horde/Kolab/Filter/Exception.php" as="Horde/Kolab/Filter/Exception.php" />
+   <install name="lib/Horde/Kolab/Filter/Exception/Usage.php" as="Horde/Kolab/Filter/Exception/Usage.php" />
    <install name="lib/Horde/Kolab/Filter/Incoming.php" as="Horde/Kolab/Filter/Incoming.php" />
    <install name="lib/Horde/Kolab/Filter/Outlook.php" as="Horde/Kolab/Filter/Outlook.php" />
    <install name="lib/Horde/Kolab/Filter/Response.php" as="Horde/Kolab/Filter/Response.php" />
diff --git a/framework/Kolab_Filter/test/Horde/Kolab/Filter/FilterTest.php b/framework/Kolab_Filter/test/Horde/Kolab/Filter/FilterTest.php
deleted file mode 100644 (file)
index a033ab4..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-<?php
-/**
- * Test the base filter class within the Kolab filter implementation.
- *
- * PHP version 5
- *
- * @category   Kolab
- * @package    Kolab_Filter
- * @subpackage UnitTests
- * @author     Gunnar Wrobel <wrobel@pardus.de>
- * @license    http://www.fsf.org/copyleft/lgpl.html LGPL
- * @link       http://pear.horde.org/index.php?package=Kolab_Filter
- */
-
-/**
- * Prepare the test setup.
- */
-require_once dirname(__FILE__) . '/Autoload.php';
-
-/**
- * Test the filter class.
- *
- * Copyright 2008 Klarälvdalens Datakonsult AB
- *
- * See the enclosed file COPYING for license information (LGPL). If you
- * did not receive this file, see http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
- *
- * @category   Kolab
- * @package    Kolab_Filter
- * @subpackage UnitTests
- * @author     Gunnar Wrobel <wrobel@pardus.de>
- * @license    http://www.fsf.org/copyleft/lgpl.html LGPL
- * @link       http://pear.horde.org/index.php?package=Kolab_Filter
- */
-class Horde_Kolab_Filter_FilterTest extends PHPUnit_Framework_TestCase
-{
-
-    /**
-     * Set up testing.
-     */
-    protected function setUp()
-    {
-        $GLOBALS['conf']['log']['enabled']          = false;
-
-        $_SERVER['SERVER_NAME'] = 'localhost';
-        $_SERVER['REMOTE_ADDR'] = 'ADDR';
-        $_SERVER['REMOTE_HOST'] = 'HOST';
-    }
-
-
-    /**
-     * Test incorrect usage of the Filter
-     */
-    public function testIncorrectUsage()
-    {
-        $_SERVER['argv'] = array($_SERVER['argv'][0]);
-        $parser   = new Horde_Kolab_Filter_Incoming(
-            new Horde_Log_Logger(
-                new Horde_Log_Handler_Mock()
-            )
-        );
-        $inh = fopen(dirname(__FILE__) . '/fixtures/tiny.eml', 'r');
-        try {
-            $result = $parser->parse($inh, 'echo');
-        } catch (Horde_Kolab_Filter_Exception $e) {
-            $this->assertContains('Please provide one or more recipients.', $e->getMessage());
-            return;
-        }
-        $this->assertFail('No exception!');
-    }
-}
diff --git a/framework/Kolab_Filter/test/Horde/Kolab/Filter/Integration/CliTest.php b/framework/Kolab_Filter/test/Horde/Kolab/Filter/Integration/CliTest.php
new file mode 100644 (file)
index 0000000..72abeb6
--- /dev/null
@@ -0,0 +1,103 @@
+<?php
+/**
+ * Test the CLI handling.
+ *
+ * PHP version 5
+ *
+ * @category   Kolab
+ * @package    Kolab_Filter
+ * @subpackage UnitTests
+ * @author     Gunnar Wrobel <wrobel@pardus.de>
+ * @license    http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link       http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+
+/**
+ * Prepare the test setup.
+ */
+require_once dirname(__FILE__) . '/../Autoload.php';
+
+/**
+ * Test the CLI handling.
+ *
+ * Copyright 2008 Klarälvdalens Datakonsult AB
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+ *
+ * @category   Kolab
+ * @package    Kolab_Filter
+ * @subpackage UnitTests
+ * @author     Gunnar Wrobel <wrobel@pardus.de>
+ * @license    http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link       http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+class Horde_Kolab_Filter_Integration_CliTest extends PHPUnit_Framework_TestCase
+{
+
+    /**
+     * Set up testing.
+     */
+    protected function setUp()
+    {
+        $GLOBALS['conf']['log']['enabled']          = false;
+
+        $_SERVER['SERVER_NAME'] = 'localhost';
+        $_SERVER['REMOTE_ADDR'] = 'ADDR';
+        $_SERVER['REMOTE_HOST'] = 'HOST';
+    }
+
+
+    /**
+     * Test incorrect usage of the Filter.
+     */
+    public function testIncorrectUsage()
+    {
+        $_SERVER['argv'] = array($_SERVER['argv'][0]);
+        $parser   = new Horde_Kolab_Filter_Incoming(
+            new Horde_Kolab_Filter_Cli(),
+            new Horde_Log_Logger(
+                new Horde_Log_Handler_Mock()
+            )
+        );
+        $inh = fopen(dirname(__FILE__) . '/../fixtures/tiny.eml', 'r');
+        try {
+            $result = $parser->parse($inh, 'echo');
+        } catch (Horde_Kolab_Filter_Exception $e) {
+            $this->assertContains(
+                'Please provide one or more recipients.',
+                $e->getMessage()
+            );
+            return;
+        }
+        $this->assertFail('No exception!');
+    }
+
+    /**
+     * Test incorrect usage of the Filter by providing an invalid option.
+     */
+    public function testIncorrectUsageWithInvalidOption()
+    {
+        $_SERVER['argv'] = array(
+            $_SERVER['argv'][0],
+            '--recipient'
+        );
+        $parser   = new Horde_Kolab_Filter_Incoming(
+            new Horde_Kolab_Filter_Cli(),
+            new Horde_Log_Logger(
+                new Horde_Log_Handler_Mock()
+            )
+        );
+        $inh = fopen(dirname(__FILE__) . '/../fixtures/tiny.eml', 'r');
+        try {
+            $result = $parser->parse($inh, 'echo');
+        } catch (Horde_Kolab_Filter_Exception $e) {
+            $this->assertContains(
+                'error: --recipient option requires an argument',
+                $e->getMessage()
+            );
+            return;
+        }
+        $this->assertFail('No exception!');
+    }
+}