From: Gunnar Wrobel
Date: Tue, 29 Sep 2009 15:23:36 +0000 (+0200)
Subject: The mock driver.
X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=1c91915a864f158e7ba702f2dd5be8c0f42e42f5;p=horde.git
The mock driver.
---
diff --git a/framework/History/lib/Horde/History/Factory.php b/framework/History/lib/Horde/History/Factory.php
index c475912f8..cdfa8e6b6 100644
--- a/framework/History/lib/Horde/History/Factory.php
+++ b/framework/History/lib/Horde/History/Factory.php
@@ -61,8 +61,9 @@ class Horde_History_Factory
case 'Sql':
return Horde_History_Factory::getHistorySql($injector, $config->params);
case 'Mock':
- default:
return Horde_History_Factory::getHistoryMock($config->params);
+ default:
+ throw new Horde_Exception(sprintf("Driver %s not supported!", $config->driver));
}
}
@@ -109,6 +110,24 @@ class Horde_History_Factory
}
/**
+ * Create a concrete Horde_History_Mock instance.
+ *
+ * @param Horde_Injector $injector The environment for creating the instance.
+ * @param array $params The db connection parameters if the
+ * environment does not already provide a
+ * connection.
+ *
+ * @return Horde_History_Mock The new Horde_History_Mock instance.
+ *
+ * @throws Horde_Exception If the injector provides no configuration or
+ * creating the database connection failed.
+ */
+ static protected function getHistoryMock(array $params)
+ {
+ return new Horde_History_Mock();
+ }
+
+ /**
* Create a database connection.
*
* @param array $params The database connection parameters.
diff --git a/framework/History/lib/Horde/History/Mock.php b/framework/History/lib/Horde/History/Mock.php
new file mode 100644
index 000000000..dde9254fd
--- /dev/null
+++ b/framework/History/lib/Horde/History/Mock.php
@@ -0,0 +1,248 @@
+
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=History
+ */
+
+/**
+ * The Horde_History_Mock:: class provides a method of tracking changes in Horde
+ * objects, stored in memory.
+ *
+ * Copyright 2009 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 Horde
+ * @package History
+ * @author Gunnar Wrobel
+ * 'field' - the history field being compared (i.e. 'action').
+ * 'op' - the operator to compare this field with.
+ * 'value' - the value to check for (i.e. 'add').
+ *
+ * @param string $parent The parent history to start searching at. If
+ * non-empty, will be searched for with a LIKE
+ * '$parent:%' clause.
+ *
+ * @return array An array of history object ids, or an empty array if
+ * none matched the criteria.
+ *
+ * @throws Horde_Exception
+ */
+ public function getByTimestamp($cmp, $ts, $filters = array(),
+ $parent = null)
+ {
+ $result = array();
+
+ foreach ($this->_data as $id => $element) {
+
+ $ignore = false;
+
+ switch ($cmp) {
+ case '<=':
+ case '=<':
+ if ($element['history_ts'] > $ts) {
+ $ignore = true;
+ };
+ break;
+ case '<':
+ if ($element['history_ts'] >= $ts) {
+ $ignore = true;
+ };
+ break;
+ case '=':
+ if ($element['history_ts'] != $ts) {
+ $ignore = true;
+ };
+ break;
+ case '>':
+ if ($element['history_ts'] <= $ts) {
+ $ignore = true;
+ };
+ break;
+ case '>=':
+ case '=>':
+ if ($element['history_ts'] < $ts) {
+ $ignore = true;
+ };
+ break;
+ default:
+ throw new Horde_Exception(sprintf("Comparison %s not implemented!", $cmp));
+ }
+
+ if ($ignore) {
+ continue;
+ }
+
+ /* Add additional filters, if there are any. */
+ if ($filters) {
+ foreach ($filters as $filter) {
+ if ($filter['op'] != '=') {
+ throw new Horde_Exception(sprintf("Comparison %s not implemented!", $filter['op']));
+ }
+ if ($element['history_' . $filter['field']] != $filter['value']) {
+ $ignore = true;
+ }
+ }
+ }
+
+ if ($ignore) {
+ continue;
+ }
+
+ if ($parent) {
+ if (substr($element['history_uid'], 0, strlen($parent) + 1) != $parent . ':') {
+ continue;
+ }
+ }
+
+ $result[$element['history_uid']] = $id;
+ }
+ return $result;
+ }
+
+ /**
+ * Remove one or more history entries by name.
+ *
+ * @param array $names The history entries to remove.
+ *
+ * @return boolean True if the operation succeeded.
+ *
+ * @throws Horde_Exception
+ */
+ public function removeByNames($names)
+ {
+ if (!count($names)) {
+ return true;
+ }
+
+ $ids = array();
+ foreach ($this->_data as $id => $element) {
+ if (in_array($element['history_uid'], $names)) {
+ $ids[] = $id;
+ }
+ }
+
+ foreach ($ids as $id) {
+ unset($this->_data[$id]);
+ }
+ return true;
+ }
+}
diff --git a/framework/History/package.xml b/framework/History/package.xml
index f31f2c512..0c104eea9 100644
--- a/framework/History/package.xml
+++ b/framework/History/package.xml
@@ -34,6 +34,7 @@ http://pear.php.net/dtd/package-2.0.xsd">