From: Gunnar Wrobel Date: Thu, 9 Dec 2010 10:04:24 +0000 (+0100) Subject: Test the timing decorator. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=700769a51fb2b573c64682e350ba7bee50770617;p=horde.git Test the timing decorator. --- 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 index 000000000..40d924af1 --- /dev/null +++ b/framework/Kolab_Format/lib/Horde/Kolab/Format/Decorator/Base.php @@ -0,0 +1,116 @@ + + * @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 + * @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 index 000000000..1559b80c9 --- /dev/null +++ b/framework/Kolab_Format/lib/Horde/Kolab/Format/Decorator/Timed.php @@ -0,0 +1,103 @@ + + * @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 + * @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 diff --git a/framework/Kolab_Format/lib/Horde/Kolab/Format/Factory.php b/framework/Kolab_Format/lib/Horde/Kolab/Format/Factory.php index e13ba465f..b0380c078 100644 --- a/framework/Kolab_Format/lib/Horde/Kolab/Format/Factory.php +++ b/framework/Kolab_Format/lib/Horde/Kolab/Format/Factory.php @@ -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. + *
+     * 'version' - The format version.
+     * 
+ * + * @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 + ); + } } diff --git a/framework/Kolab_Format/package.xml b/framework/Kolab_Format/package.xml index 970fdd546..b5661d0ea 100644 --- a/framework/Kolab_Format/package.xml +++ b/framework/Kolab_Format/package.xml @@ -30,7 +30,7 @@ yes 2010-12-09 - + 1.1.0 1.1.0 @@ -70,6 +70,10 @@ + + + + @@ -435,6 +439,10 @@ Date pear.horde.org + + Support + pear.horde.org + @@ -448,6 +456,8 @@ + + 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 index 000000000..6639510b1 --- /dev/null +++ b/framework/Kolab_Format/test/Horde/Kolab/Format/Unit/Decorator/TimedTest.php @@ -0,0 +1,95 @@ + + * @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 + * @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() + ); + } +}