Test the timing decorator.
authorGunnar Wrobel <p@rdus.de>
Thu, 9 Dec 2010 10:04:24 +0000 (11:04 +0100)
committerGunnar Wrobel <p@rdus.de>
Mon, 13 Dec 2010 10:17:13 +0000 (11:17 +0100)
framework/Kolab_Format/lib/Horde/Kolab/Format/Decorator/Base.php [new file with mode: 0644]
framework/Kolab_Format/lib/Horde/Kolab/Format/Decorator/Timed.php [new file with mode: 0644]
framework/Kolab_Format/lib/Horde/Kolab/Format/Factory.php
framework/Kolab_Format/package.xml
framework/Kolab_Format/test/Horde/Kolab/Format/Unit/Decorator/TimedTest.php [new file with mode: 0644]

diff --git a/framework/Kolab_Format/lib/Horde/Kolab/Format/Decorator/Base.php b/framework/Kolab_Format/lib/Horde/Kolab/Format/Decorator/Base.php
new file mode 100644 (file)
index 0000000..40d924a
--- /dev/null
@@ -0,0 +1,116 @@
+<?php
+/**
+ * A base decorator definition.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package  Kolab_Format
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Format
+ */
+
+/**
+ * A base decorator definition.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * 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_Format
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Format
+ */
+class Horde_Kolab_Format_Decorator_Base
+implements Horde_Kolab_Format
+{
+    /**
+     * The decorated Kolab format handler.
+     *
+     * @var Horde_Kolab_Format
+     */
+    private $_handler;
+
+    /**
+     * Constructor.
+     *
+     * @param Horde_Kolab_Format $handler The handler to be decorated.
+     */
+    public function __construct(Horde_Kolab_Format $handler)
+    {
+        $this->_handler = $handler;
+    }
+
+    /**
+     * Return the decorated handler.
+     *
+     * @return Horde_Kolab_Format The handler.
+     */
+    public function getHandler()
+    {
+        return $this->_handler;
+    }
+
+    /**
+     * Return the name of the resulting document.
+     *
+     * @return string The name that may be used as filename.
+     */
+    public function getName()
+    {
+        return $this->_handler->getName();
+    }
+
+    /**
+     * Return the mime type of the resulting document.
+     *
+     * @return string The mime type of the result.
+     */
+    public function getMimeType()
+    {
+        return $this->_handler->getMimeType();
+    }
+
+    /**
+     * Return the disposition of the resulting document.
+     *
+     * @return string The disportion of this document.
+     */
+    public function getDisposition()
+    {
+        return $this->_handler->getDisposition();
+    }
+
+    /**
+     * Load an object based on the given XML string.
+     *
+     * @param string &$xmltext The XML of the message as string.
+     *
+     * @return array The data array representing the object.
+     *
+     * @throws Horde_Kolab_Format_Exception
+     */
+    public function load(&$xmltext)
+    {
+        return $this->_handler->load($xmltext);
+    }
+
+    /**
+     * Convert the data to a XML string.
+     *
+     * @param array &$object The data array representing the note.
+     *
+     * @return string The data as XML string.
+     *
+     * @throws Horde_Kolab_Format_Exception
+     */
+    public function save($object)
+    {
+        return $this->_handler->save($object);
+    }
+}
\ No newline at end of file
diff --git a/framework/Kolab_Format/lib/Horde/Kolab/Format/Decorator/Timed.php b/framework/Kolab_Format/lib/Horde/Kolab/Format/Decorator/Timed.php
new file mode 100644 (file)
index 0000000..1559b80
--- /dev/null
@@ -0,0 +1,103 @@
+<?php
+/**
+ * Determines how much time is spent while loading/saving the Kolab objects.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package  Kolab_Format
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Format
+ */
+
+/**
+ * Determines how much time is spent while loading/saving the Kolab objects.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * 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_Format
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Format
+ */
+class Horde_Kolab_Format_Decorator_Timed
+extends Horde_Kolab_Format_Decorator_Base
+{
+    /**
+     * The timer used for recording the amount of time spent.
+     *
+     * @var Horde_Support_Timer
+     */
+    private $_timer;
+
+    /**
+     * Time spent handling objects.
+     *
+     * @var float
+     */
+    static private $_spent = 0.0;
+
+    /**
+     * Constructor.
+     *
+     * @param Horde_Kolab_Format  $handler The handler to be decorated.
+     * @param Horde_Support_Timer $timer   The timer.
+     */
+    public function __construct(
+        Horde_Kolab_Format $handler,
+        Horde_Support_Timer $timer
+    ) {
+        parent::__construct($handler);
+        $this->_timer = $timer;
+    }
+
+    /**
+     * Load an object based on the given XML string.
+     *
+     * @param string &$xmltext The XML of the message as string.
+     *
+     * @return array The data array representing the object.
+     *
+     * @throws Horde_Kolab_Format_Exception
+     */
+    public function load(&$xmltext)
+    {
+        $this->_timer->push();
+        $result = $this->getHandler()->load($xmltext);
+        self::$_spent += $this->_timer->pop();
+        return $result;
+    }
+
+    /**
+     * Convert the data to a XML string.
+     *
+     * @param array &$object The data array representing the note.
+     *
+     * @return string The data as XML string.
+     *
+     * @throws Horde_Kolab_Format_Exception
+     */
+    public function save($object)
+    {
+        $this->_timer->push();
+        $result = $this->getHandler()->save($object);
+        self::$_spent += $this->_timer->pop();
+        return $result;
+    }
+
+    /**
+     * Report the time spent for loading/saving objects.
+     *
+     * @return float The amount of time.
+     */
+    public function timeSpent()
+    {
+        return self::$_spent;
+    }
+}
\ No newline at end of file
index e13ba46..b0380c0 100644 (file)
@@ -69,4 +69,33 @@ class Horde_Kolab_Format_Factory
         }
         return $this->_instances[$class];
     }
+
+    /**
+     * Generates a Kolab object handler with a timer wrapped around it..
+     *
+     * @param string $format The format that the handler should work with.
+     * @param string $object The object type that should be handled.
+     * @param array  $params Additional parameters.
+     * <pre>
+     * 'version' - The format version.
+     * </pre>
+     *
+     * @return Horde_Kolab_Format The wrapped handler.
+     *
+     * @throws Horde_Kolab_Format_Exception If the specified handler does not
+     *                                      exist.
+     */
+    public function createTimed($format_type = '', $object_type = '', $params = null)
+    {
+        if (isset($params['handler'])) {
+            $handler = $params['handler'];
+        } else {
+            $handler = $this->create($format_type, $object_type, $params);
+        }
+        return new Horde_Kolab_Format_Decorator_Timed(
+            $handler,
+            new Horde_Support_Timer(),
+            isset($params['logger']) ? $params['logger'] : null
+        );
+    }
 }
index 970fdd5..b5661d0 100644 (file)
@@ -30,7 +30,7 @@
   <active>yes</active>
  </lead>
  <date>2010-12-09</date>
- <time>08:47:27</time>
+ <time>10:18:38</time>
  <version>
   <release>1.1.0</release>
   <api>1.1.0</api>
     <dir name="Horde">
      <dir name="Kolab">
       <dir name="Format">
+       <dir name="Decorator">
+        <file name="Base.php" role="php" />
+        <file name="Timed.php" role="php" />
+       </dir> <!-- /lib/Horde/Kolab/Format/Decorator -->
        <dir name="Xml">
         <file name="Annotation.php" role="php" />
         <file name="Contact.php" role="php" />
     <name>Date</name>
     <channel>pear.horde.org</channel>
    </package>
+   <package>
+    <name>Support</name>
+    <channel>pear.horde.org</channel>
+   </package>
   </optional>
  </dependencies>
  <phprelease>
    <install as="Horde/Kolab/Format/Factory.php" name="lib/Horde/Kolab/Format/Factory.php" />
    <install as="Horde/Kolab/Format/Translation.php" name="lib/Horde/Kolab/Format/Translation.php" />
    <install as="Horde/Kolab/Format/Xml.php" name="lib/Horde/Kolab/Format/Xml.php" />
+   <install as="Horde/Kolab/Format/Decorator/Base.php" name="lib/Horde/Kolab/Format/Decorator/Base.php" />
+   <install as="Horde/Kolab/Format/Decorator/Timed.php" name="lib/Horde/Kolab/Format/Decorator/Timed.php" />
    <install as="Horde/Kolab/Format/Xml/Annotation.php" name="lib/Horde/Kolab/Format/Xml/Annotation.php" />
    <install as="Horde/Kolab/Format/Xml/Contact.php" name="lib/Horde/Kolab/Format/Xml/Contact.php" />
    <install as="Horde/Kolab/Format/Xml/Distributionlist.php" name="lib/Horde/Kolab/Format/Xml/Distributionlist.php" />
diff --git a/framework/Kolab_Format/test/Horde/Kolab/Format/Unit/Decorator/TimedTest.php b/framework/Kolab_Format/test/Horde/Kolab/Format/Unit/Decorator/TimedTest.php
new file mode 100644 (file)
index 0000000..6639510
--- /dev/null
@@ -0,0 +1,95 @@
+<?php
+/**
+ * Test the decorator for time measurements.
+ *
+ * PHP version 5
+ *
+ * @category   Kolab
+ * @package    Kolab_Format
+ * @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_Format
+ */
+
+/**
+ * Prepare the test setup.
+ */
+require_once dirname(__FILE__) . '/../../Autoload.php';
+
+/**
+ * Test the decorator for time measurements.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * 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_Format
+ * @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_Format
+ */
+class Horde_Kolab_Format_Unit_Decorator_TimedTest
+extends Horde_Kolab_Format_TestCase
+{
+    public function testConstructor()
+    {
+        $this->getFactory()->createTimed('XML', 'contact');
+    }
+
+    public function testGetName()
+    {
+        $this->assertEquals(
+            'kolab.xml',
+            $this->getFactory()->createTimed('XML', 'contact')->getName()
+        );
+    }
+
+    public function testGetMimeType()
+    {
+        $this->assertEquals(
+            'application/x-vnd.kolab.contact',
+            $this->getFactory()->createTimed('XML', 'contact')->getMimeType()
+        );
+    }
+
+    public function testGetDisposition()
+    {
+        $this->assertEquals(
+            'attachment',
+            $this->getFactory()->createTimed('XML', 'contact')->getDisposition()
+        );
+    }
+
+    public function testTimeSpent()
+    {
+        $mock = $this->getMock('Horde_Kolab_Format');
+        $timed = $this->getFactory()->createTimed(
+            'XML', 'contact', array('handler' => $mock)
+        );
+        $a = '';
+        $timed->load($a);
+        $this->assertType(
+            'float',
+            $timed->timeSpent()
+        );
+    }
+
+    public function testTimeSpentIncreases()
+    {
+        $mock = $this->getMock('Horde_Kolab_Format');
+        $timed = $this->getFactory()->createTimed(
+            'XML', 'contact', array('handler' => $mock)
+        );
+        $a = '';
+        $timed->load($a);
+        $t_one = $timed->timeSpent();
+        $timed->save(array());
+        $this->assertTrue(
+            $t_one < $timed->timeSpent()
+        );
+    }
+}