File: Db.php

package info (click to toggle)
php-horde-test 2.6.4%2Bdebian0-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 556 kB
  • sloc: xml: 2,328; php: 730; makefile: 18
file content (68 lines) | stat: -rw-r--r-- 2,241 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
 * Generates test database connectors.
 *
 * PHP version 5
 *
 * @category Horde
 * @package  Test
 * @author   Gunnar Wrobel <wrobel@pardus.de>
 * @license  http://www.horde.org/licenses/lgpl21 LGPL
 * @link     http://www.horde.org/components/Horde_Test
 */

/**
 * Generates test database connectors.
 *
 * Copyright 2011-2017 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 Horde
 * @package  Test
 * @author   Gunnar Wrobel <wrobel@pardus.de>
 * @license  http://www.horde.org/licenses/lgpl21 LGPL
 * @link     http://www.horde.org/components/Horde_Test
 */
class Horde_Test_Factory_Db
{
    /**
     * Create a connector to an in-memory sqlite DB.
     *
     * @param array $params Additional options.
     * <pre>
     * 'migrations' - (array) An list of migrations that should be run.
     *                Each element must contain the keys 'migrationsPath'
     *                and 'schemaTableName'.
     *                DEFAULT: empty
     * </pre>
     *
     * @return Horde_Db_Adapter_Pdo_Sqlite The DB adapter.
     */
    public function create($params = array())
    {
        if (!extension_loaded('pdo') ||
            !in_array('sqlite', PDO::getAvailableDrivers())) {
            throw new Horde_Test_Exception('No sqlite extension or no sqlite PDO driver');
        }
        if (!class_exists('Horde_Db_Adapter_Pdo_Sqlite')) {
            throw new Horde_Test_Exception('The "Horde_Db_Adapter_Pdo_Sqlite" class is unavailable!');
        }
        $db = new Horde_Db_Adapter_Pdo_Sqlite(array('dbname' => ':memory:', 'charset' => 'utf-8'));
        if (isset($params['migrations'])) {
            if (isset($params['migrations']['migrationsPath'])) {
                $migrations = array($params['migrations']);
            } else {
                $migrations = $params['migrations'];
            }
            foreach ($migrations as $migration) {
                $migrator = new Horde_Db_Migration_Migrator(
                    $db, null, $migration
                );
                $migrator->up();
            }
        }
        return $db;
    }
}