1<?php
2
3/**
4 * Declares functions that need to be available in the global scope
5 *
6 * @package MDB2
7 * @category Database
8 * @author Daniel Convissor <danielc@php.net>
9 */
10
11/**
12 * Builds the tables used by the test suite.
13 *
14 * @param array|MDB2_Driver_Common $ci  either a MDB2_Driver_Common object or
15 *                   an associative array with two elements.  The "dsn"
16 *                   element must contain an array of DSN information.
17 *                   The "options" element must be an array of connection
18 *                   options.
19 */
20function build_schema($ci) {
21    $file = __DIR__ . '/schema.xml';
22
23    if (is_object($ci)) {
24        if (! $ci instanceof MDB2_Driver_Common) {
25            die("Must be a MDB2_Driver_Common object.\n");
26        }
27        $db = $ci;
28        $database = $ci->getDatabase();
29        $phptype = $ci->phptype;
30        $original_options = $ci->options;
31    } else {
32        if (!is_array($ci['dsn'])) {
33            die('$ci["dsn"] must use the array DSN format.' . "\n");
34        }
35        $db = $ci['dsn'];
36        $database = $db['database'];
37        $phptype = $db['phptype'];
38
39        if (!array_key_exists('options', $ci)) {
40            die('$ci["options"] is missing.' . "\n");
41        }
42        $original_options = $ci['options'];
43    }
44
45    $variables = array(
46        'name'   => $database,
47        'create' => true,
48    );
49
50    $options = array(
51        'log_line_break'   => '<br />',
52        'idxname_format'   => '%s',
53        'debug'            => true,
54        'quote_identifier' => true,
55        'force_defaults'   => false,
56        'portability'      => false
57    );
58    $options = array_merge($options, $original_options);
59
60    $err_base = "TEST SCHEMA BUILD ERROR FOR $phptype: ";
61
62    $schema = MDB2_Schema::factory($db, $options);
63    if (MDB2::isError($schema)) {
64        die($err_base . $schema->getMessage() . ' '
65            . $schema->getUserInfo() . "\n");
66    }
67
68    $definition = $schema->parseDatabaseDefinitionFile($file, $variables, true, true);
69    if (MDB2::isError($definition)) {
70        die($err_base . $definition->getMessage() . ' - '
71            . $definition->getUserInfo() . "\n");
72    } else {
73        $operation = $schema->createDatabase($definition);
74        if (MDB2::isError($operation)) {
75            die($err_base . $operation->getMessage() . ' '
76                . $operation->getUserInfo() . "\n");
77        }
78    }
79}
80
81/**
82 * Determines if the desired MDB2_Driver class is available IN THE LOCATION
83 * WE ARE TESTING
84 *
85 * Because there's not much point in testing some other installation.
86 *
87 * @param string $phptype  the "phptype" of the driver we're looking for
88 * @return bool
89 * @uses MDB2_TEST_MDB2_PATH  to determine the path
90 */
91function is_driver_available($phptype) {
92    return file_exists(MDB2_TEST_MDB2_PATH . "/MDB2/Driver/$phptype.php");
93}
94
95/**
96 * Produces a multi-diemnsional array containing the connection information
97 * for each DBMS to be tested
98 *
99 * The connection information for each DBMS is an associative array with two
100 * elements.  The "dsn" element must contain an array of DSN information.
101 * The "options" element must be an array of connection options.
102 *
103 * Used by Standard_Abstract::provider()
104 *
105 * @return array
106 */
107function mdb2_test_db_object_provider() {
108    static $dbs;
109
110    if (!isset($dbs)) {
111        $dsns = unserialize(MDB2_TEST_SERIALIZED_DSNS);
112        $dbs = array();
113        foreach ($dsns as $driver => $ci) {
114            $dbs[$driver] = array(
115                $ci,
116            );
117
118            // Disable messages from other packages while building schema.
119            $prior = error_reporting(E_ALL & ~E_STRICT & ~E_DEPRECATED);
120            build_schema($ci);
121            error_reporting($prior);
122        }
123    }
124
125    return $dbs;
126}
127