Skip to content
Browse files

Add possibility to dynamcially create a DB for tests under mgd2

  • Loading branch information...
1 parent 648d2ea commit 730de8ce01129de0413677f2bb8aefa820f6c885 @flack committed
Showing with 120 additions and 91 deletions.
  1. +1 −53 config-default.inc.php
  2. +2 −38 quick_init.php
  3. +22 −0 test/rootfile.php
  4. +95 −0 tools/bootstrap.php
View
54 config-default.inc.php
@@ -6,59 +6,7 @@
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License
*/
-/**
- * Simple default topic hierarchy setup for OpenPSA
- *
- * @package midcom
- */
-function openpsa_prepare_topics()
-{
- $openpsa_topics = array
- (
- 'Calendar' => 'org.openpsa.calendar',
- 'Contacts' => 'org.openpsa.contacts',
- 'Documents' => 'org.openpsa.documents',
- 'Expenses' => 'org.openpsa.expenses',
- 'Invoices' => 'org.openpsa.invoices',
- 'Products' => 'org.openpsa.products',
- 'Projects' => 'org.openpsa.projects',
- 'Reports' => 'org.openpsa.reports',
- 'Sales' => 'org.openpsa.sales',
- 'User Management' => 'org.openpsa.user',
- 'Wiki' => 'net.nemein.wiki',
- );
- $qb = new midgard_query_builder('midgard_topic');
- $qb->add_constraint('name', '=', 'openpsa');
- $qb->add_constraint('up', '=', 0);
- $topics = $qb->execute();
- if ($topics)
- {
- return $topics[0]->guid;
- }
-
- // Create a new root topic for OpenPSA
- $root_topic = new midgard_topic();
- $root_topic->name = 'openpsa';
- $root_topic->component = 'org.openpsa.mypage';
- $root_topic->extra = 'OpenPSA';
- if (!$root_topic->create())
- {
- throw new Exception('Failed to create root topic for OpenPSA: ' . midgard_connection::get_instance()->get_error_string());
- }
-
- foreach ($openpsa_topics as $title => $component)
- {
- $topic = new midgard_topic();
- $topic->name = strtolower($title);
- $topic->component = $component;
- $topic->extra = $title;
- $topic->up = $root_topic->id;
- $topic->create();
- }
-
- return $root_topic->guid;
-}
-
+require_once 'tools/bootstrap.php';
$GLOBALS['midcom_config_local']['log_level'] = 5;
$GLOBALS['midcom_config_local']['log_filename'] = dirname(midgard_connection::get_instance()->config->logfilename) . '/midcom.log';
$GLOBALS['midcom_config_local']['midcom_root_topic_guid'] = openpsa_prepare_topics();
View
40 quick_init.php
@@ -34,43 +34,7 @@
exit(1);
}
-if (!$config->create_blobdir())
-{
- echo "Failed to create file attachment storage directory to {$config->blobdir}:" . $midgard->get_error_string() . "\n";
- exit(1);
-}
+require_once 'tools/bootstrap.php';
-// Create storage
-if (!midgard_storage::create_base_storage())
-{
- if ($midgard->get_error_string() != 'MGD_ERR_OK')
- {
- echo "Failed to create base database structures" . $midgard->get_error_string() . "\n";
- exit(1);
- }
-}
-else
-{
- echo "Database initialized, preparing storage for MgdSchema classes:\n";
-}
-
-$re = new ReflectionExtension('midgard2');
-$classes = $re->getClasses();
-foreach ($classes as $refclass)
-{
- $parent_class = $refclass->getParentClass();
- if (!$parent_class)
- {
- continue;
- }
- if ($parent_class->getName() != 'midgard_object')
- {
- continue;
- }
- $type = $refclass->getName();
-
- midgard_storage::create_class_storage($type);
- midgard_storage::update_class_storage($type);
- echo " Created storage for {$type}\n";
-}
+openpsa_prepare_database($config);
?>
View
22 test/rootfile.php
@@ -51,6 +51,28 @@
$GLOBALS['midcom_config_local']['person_class'] = 'openpsa_person';
+ // Open connection
+ $midgard = midgard_connection::get_instance();
+
+ if (!$midgard->is_connected())
+ {
+ $config = new midgard_config();
+ $config->dbtype = 'SQLite';
+ $config->database = 'openpsa_test';
+ $config->blobdir = "/tmp/openpsa_test";
+ $config->tablecreate = true;
+ $config->tableupdate = true;
+ $config->loglevel = 'critical';
+
+ if (!$midgard->open_config($config))
+ {
+ throw new Exception('Could not open Midgard connection to test database: ' . $midgard->get_error_string());
+ }
+ require_once dirname(__FILE__) . '/../tools/bootstrap.php';
+ openpsa_prepare_database($config);
+ $GLOBALS['midcom_config_local']['midcom_root_topic_guid'] = openpsa_prepare_topics();
+ }
+
// Initialize the $_MIDGARD superglobal
$_MIDGARD = $mgd_defaults;
}
View
95 tools/bootstrap.php
@@ -0,0 +1,95 @@
+<?php
+
+/**
+ * Prepares a mgd2 database
+ */
+function openpsa_prepare_database($config)
+{
+ if (!$config->create_blobdir())
+ {
+ throw new Exception("Failed to create file attachment storage directory to {$config->blobdir}:" . midgard_connection::get_instance()->get_error_string());
+ }
+
+ // Create storage
+ if (!midgard_storage::create_base_storage())
+ {
+ if (midgard_connection::get_instance()->get_error_string() != 'MGD_ERR_OK')
+ {
+ throw new Exception("Failed to create base database structures" . midgard_connection::get_instance()->get_error_string());
+ }
+ }
+
+ $re = new ReflectionExtension('midgard2');
+ $classes = $re->getClasses();
+ foreach ($classes as $refclass)
+ {
+ $parent_class = $refclass->getParentClass();
+ if (!$parent_class)
+ {
+ continue;
+ }
+ if ($parent_class->getName() != 'midgard_object')
+ {
+ continue;
+ }
+ $type = $refclass->getName();
+
+ midgard_storage::create_class_storage($type);
+ midgard_storage::update_class_storage($type);
+ }
+}
+
+/**
+ * Simple default topic hierarchy setup for OpenPSA
+ *
+ * @package midcom
+ */
+function openpsa_prepare_topics()
+{
+ $openpsa_topics = array
+ (
+ 'Calendar' => 'org.openpsa.calendar',
+ 'Contacts' => 'org.openpsa.contacts',
+ 'Documents' => 'org.openpsa.documents',
+ 'Expenses' => 'org.openpsa.expenses',
+ 'Invoices' => 'org.openpsa.invoices',
+ 'Products' => 'org.openpsa.products',
+ 'Projects' => 'org.openpsa.projects',
+ 'Reports' => 'org.openpsa.reports',
+ 'Sales' => 'org.openpsa.sales',
+ 'User Management' => 'org.openpsa.user',
+ 'Wiki' => 'net.nemein.wiki',
+ );
+ $qb = new midgard_query_builder('midgard_topic');
+ $qb->add_constraint('name', '=', 'openpsa');
+ $qb->add_constraint('up', '=', 0);
+ $topics = $qb->execute();
+ if ($topics)
+ {
+ return $topics[0]->guid;
+ }
+
+ // Create a new root topic for OpenPSA
+ $root_topic = new midgard_topic();
+ $root_topic->name = 'openpsa';
+ $root_topic->component = 'org.openpsa.mypage';
+ $root_topic->extra = 'OpenPSA';
+ if (!$root_topic->create())
+ {
+ throw new Exception('Failed to create root topic for OpenPSA: ' . midgard_connection::get_instance()->get_error_string());
+ }
+
+ foreach ($openpsa_topics as $title => $component)
+ {
+ $topic = new midgard_topic();
+ $topic->name = strtolower($title);
+ $topic->component = $component;
+ $topic->extra = $title;
+ $topic->up = $root_topic->id;
+ $topic->create();
+ }
+
+ return $root_topic->guid;
+}
+
+?>

0 comments on commit 730de8c

Please sign in to comment.
Something went wrong with that request. Please try again.