Skip to content

Commit

Permalink
Start extracting a better representation of the Kolab data object.
Browse files Browse the repository at this point in the history
  • Loading branch information
wrobel committed Sep 29, 2012
1 parent dbde575 commit 0d2544c
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 6 deletions.
30 changes: 30 additions & 0 deletions framework/Kolab_Storage/lib/Horde/Kolab/Storage/Data/Exception.php
@@ -0,0 +1,30 @@
<?php
/**
* The exception marker for data specific Horde_Kolab_Storage errors.
*
* PHP version 5
*
* @category Kolab
* @package Kolab_Storage
* @author Gunnar Wrobel <wrobel@pardus.de>
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @link http://pear.horde.org/index.php?package=Kolab_Storage
*/

/**
* The exception marker for data specific Horde_Kolab_Storage errors.
*
* Copyright 2012 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Kolab
* @package Kolab_Storage
* @author Gunnar Wrobel <wrobel@pardus.de>
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @link http://pear.horde.org/index.php?package=Kolab_Storage
*/
class Horde_Kolab_Storage_Data_Exception extends Horde_Exception_Wrapped
{
}
Expand Up @@ -240,17 +240,19 @@ public function createKolabPart($object, array $options)
$kolab = new Horde_Mime_Part();
$kolab->setType($this->getMimeType($options['type']));
if (empty($options['raw'])) {
$format = new Horde_Kolab_Storage_Data_Object_Content(
$this->_factory->createFormat(
'Xml', $options['type'], $options['version']
)
);
if (isset($options['previous'])) {
$previous = array('previous' => $options['previous']);
$content = $format->modify($object, $options['previous']);
} else {
$previous = array();
$content = $format->create($object);
}
try {
$kolab->setContents(
$this->_factory->createFormat(
'Xml', $options['type'], $options['version']
)->save($object, $previous),
array('encoding' => 'quoted-printable')
$content, array('encoding' => 'quoted-printable')
);
} catch (Horde_Kolab_Format_Exception $e) {
throw new Horde_Kolab_Storage_Exception(
Expand Down
@@ -0,0 +1,85 @@
<?php
/**
* Generates the core Kolab content.
*
* PHP version 5
*
* @category Kolab
* @package Kolab_Storage
* @author Gunnar Wrobel <wrobel@pardus.de>
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @link http://pear.horde.org/index.php?package=Kolab_Storage
*/

/**
* Generates the core Kolab content.
*
* Copyright 2011-2012 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Kolab
* @package Kolab_Storage
* @author Gunnar Wrobel <wrobel@pardus.de>
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @link http://pear.horde.org/index.php?package=Kolab_Storage
*/
class Horde_Kolab_Storage_Data_Object_Content
{
/**
* Kolab format handler.
*
* @var Horde_Kolab_Format
*/
private $_format;

/**
* Constructor.
*
* @param Horde_Kolab_Format $format The Kolab format handler.
*/
public function __construct(Horde_Kolab_Format $format)
{
$this->_format = $format;
}

/**
* Create the Kolab content as a string.
*
* @param array $object The object data.
*
* @return string The Kolab content.
*/
public function create(array $object)
{
try {
return $this->_format->save($object);
} catch (Horde_Kolab_Format_Exception $e) {
throw new Horde_Kolab_Storage_Data_Exception(
'Failed saving Kolab object!', 0, $e
);
}
}

/**
* Modify a previous Kolab object.
*
* @param array $object The new object data.
* @param string $previous The previous data.
*
* @return string The new Kolab content.
*/
public function modify(array $object, $previous)
{
try {
return $this->_format->save(
$object, array('previous' => $previous)
);
} catch (Horde_Kolab_Format_Exception $e) {
throw new Horde_Kolab_Storage_Data_Exception(
'Failed saving Kolab object!', 0, $e
);
}
}
}
@@ -0,0 +1,93 @@
<?php
/**
* Test the Kolab content handler.
*
* PHP version 5
*
* @category Kolab
* @package Kolab_Storage
* @subpackage UnitTests
* @author Gunnar Wrobel <wrobel@pardus.de>
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @link http://pear.horde.org/index.php?package=Kolab_Storage
*/

/**
* Prepare the test setup.
*/
require_once __DIR__ . '/../../../Autoload.php';

/**
* Test the Kolab content handler.
*
* Copyright 2011-2012 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Kolab
* @package Kolab_Storage
* @subpackage UnitTests
* @author Gunnar Wrobel <wrobel@pardus.de>
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @link http://pear.horde.org/index.php?package=Kolab_Storage
*/
class Horde_Kolab_Storage_Unit_Data_Object_ContentTest
extends PHPUnit_Framework_TestCase
{
public function testCreate()
{
$format = $this->getMock('Horde_Kolab_Format');
$format->expects($this->once())
->method('save')
->with(array('foo' => 'foo'))
->will($this->returnValue('<event/>'));
$content = new Horde_Kolab_Storage_Data_Object_Content($format);
$this->assertEquals(
'<event/>', $content->create(array('foo' => 'foo'))
);
}

/**
* @expectedException Horde_Kolab_Storage_Data_Exception
*/
public function testCreateWithException()
{
$format = $this->getMock('Horde_Kolab_Format');
$format->expects($this->once())
->method('save')
->with(array('foo' => 'foo'))
->will($this->throwException(new Horde_Kolab_Format_Exception()));
$content = new Horde_Kolab_Storage_Data_Object_Content($format);
$content->create(array('foo' => 'foo'));
}

public function testModify()
{
$format = $this->getMock('Horde_Kolab_Format');
$format->expects($this->once())
->method('save')
->with(array('foo' => 'foo'), array('previous' => '<event/>'))
->will($this->returnValue('<event><modified/></event>'));
$content = new Horde_Kolab_Storage_Data_Object_Content($format);
$this->assertEquals(
'<event><modified/></event>',
$content->modify(array('foo' => 'foo'), '<event/>')
);
}

/**
* @expectedException Horde_Kolab_Storage_Data_Exception
*/
public function testModifyWithException()
{
$format = $this->getMock('Horde_Kolab_Format');
$format->expects($this->once())
->method('save')
->with(array('foo' => 'foo'), array('previous' => '<event/>'))
->will($this->throwException(new Horde_Kolab_Format_Exception()));
$content = new Horde_Kolab_Storage_Data_Object_Content($format);
$content->modify(array('foo' => 'foo'), '<event/>');
}

}

0 comments on commit 0d2544c

Please sign in to comment.