1: <?php
2: /**
3: * Generates test database connectors.
4: *
5: * PHP version 5
6: *
7: * @category Horde
8: * @package Test
9: * @author Gunnar Wrobel <wrobel@pardus.de>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL
11: * @link http://www.horde.org/components/Horde_Test
12: */
13:
14: /**
15: * Generates test database connectors.
16: *
17: * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you
20: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
21: *
22: * @since Horde_Test 1.2.0
23: *
24: * @category Horde
25: * @package Test
26: * @author Gunnar Wrobel <wrobel@pardus.de>
27: * @license http://www.horde.org/licenses/lgpl21 LGPL
28: * @link http://www.horde.org/components/Horde_Test
29: */
30: class Horde_Test_Factory_Db
31: {
32: /**
33: * Create a connector to an in-memory sqlite DB.
34: *
35: * @params array $params Additional options.
36: * <pre>
37: * 'migrations' - (array) An list of migrations that should be run.
38: * Each element must contain the keys 'migrationsPath'
39: * and 'schemaTableName'.
40: * DEFAULT: empty
41: * </pre>
42: *
43: * @return Horde_Db_Adapter_Pdo_Sqlite The DB adapter.
44: */
45: public function create($params = array())
46: {
47: if (!extension_loaded('pdo') ||
48: !in_array('sqlite', PDO::getAvailableDrivers())) {
49: throw new Horde_Test_Exception('No sqlite extension or no sqlite PDO driver');
50: }
51: if (!class_exists('Horde_Db_Adapter_Pdo_Sqlite')) {
52: throw new Horde_Test_Exception('The "Horde_Db_Adapter_Pdo_Sqlite" class is unavailable!');
53: }
54: $db = new Horde_Db_Adapter_Pdo_Sqlite(array('dbname' => ':memory:', 'charset' => 'utf-8'));
55: if (isset($params['migrations'])) {
56: if (isset($params['migrations']['migrationsPath'])) {
57: $migrations = array($params['migrations']);
58: } else {
59: $migrations = $params['migrations'];
60: }
61: foreach ($migrations as $migration) {
62: $migrator = new Horde_Db_Migration_Migrator(
63: $db, null, $migration
64: );
65: $migrator->up();
66: }
67: }
68: return $db;
69: }
70: }