• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

misantron / dbunit / 4562384438

pending completion
4562384438

push

github

Aleksandr Ivanov
Fix build status badge display

635 of 1200 relevant lines covered (52.92%)

5.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.11
/src/Database/Metadata/AbstractMetadata.php
1
<?php
2

3
/*
4
 * This file is part of DbUnit.
5
 *
6
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace PHPUnit\DbUnit\Database\Metadata;
13

14
use PDO;
15
use PHPUnit\DbUnit\Exception\RuntimeException;
16
use ReflectionClass;
17

18
/**
19
 * Provides a basic constructor for all meta data classes and a factory for
20
 * generating the appropriate meta data class.
21
 */
22
abstract class AbstractMetadata implements Metadata
23
{
24
    protected static $metaDataClassMap = [
25
        'pgsql'    => PgSQL::class,
26
        'mysql'    => MySQL::class,
27
        'oci'      => Oci::class,
28
        'sqlite'   => Sqlite::class,
29
        'sqlite2'  => Sqlite::class,
30
        'sqlsrv'   => SqlSrv::class,
31
        'firebird' => Firebird::class,
32
        'dblib'    => Dblib::class,
33
    ];
34

35
    /**
36
     * The PDO connection used to retreive database meta data
37
     *
38
     * @var PDO
39
     */
40
    protected $pdo;
41

42
    /**
43
     * The default schema name for the meta data object.
44
     *
45
     * @var string
46
     */
47
    protected $schema;
48

49
    /**
50
     * The character used to quote schema objects.
51
     */
52
    protected $schemaObjectQuoteChar = '"';
53

54
    /**
55
     * The command used to perform a TRUNCATE operation.
56
     */
57
    protected $truncateCommand = 'TRUNCATE';
58

59
    /**
60
     * Creates a meta data object based on the driver of given $pdo object and
61
     * $schema name.
62
     *
63
     * @param PDO    $pdo
64
     * @param string $schema
65
     *
66
     * @return AbstractMetadata
67
     */
68
    public static function createMetaData(PDO $pdo, $schema = '')
69
    {
70
        $driverName = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
29✔
71

72
        if (isset(self::$metaDataClassMap[$driverName])) {
29✔
73
            $className = self::$metaDataClassMap[$driverName];
29✔
74

75
            if ($className instanceof ReflectionClass) {
29✔
76
                return $className->newInstance($pdo, $schema);
29✔
77
            }
78

79
            return self::registerClassWithDriver($className, $driverName)->newInstance($pdo, $schema);
1✔
80
        }
81

82
        throw new RuntimeException("Could not find a meta data driver for {$driverName} pdo driver.");
×
83
    }
84

85
    /**
86
     * Validates and registers the given $className with the given $pdoDriver.
87
     * It should be noted that this function will not attempt to include /
88
     * require the file. The $pdoDriver can be determined by the value of the
89
     * PDO::ATTR_DRIVER_NAME attribute for a pdo object.
90
     *
91
     * A reflection of the $className is returned.
92
     *
93
     * @param string $className
94
     * @param string $pdoDriver
95
     *
96
     * @return ReflectionClass
97
     */
98
    public static function registerClassWithDriver($className, $pdoDriver)
99
    {
100
        if (!class_exists($className)) {
1✔
101
            throw new RuntimeException("Specified class for {$pdoDriver} driver ({$className}) does not exist.");
×
102
        }
103

104
        $reflection = new ReflectionClass($className);
1✔
105

106
        if ($reflection->isSubclassOf(self::class)) {
1✔
107
            return self::$metaDataClassMap[$pdoDriver] = $reflection;
1✔
108
        }
109

110
        throw new RuntimeException("Specified class for {$pdoDriver} driver ({$className}) does not extend PHPUnit_Extensions_Database_DB_MetaData.");
×
111
    }
112

113
    /**
114
     * Creates a new database meta data object using the given pdo connection
115
     * and schema name.
116
     *
117
     * @param PDO    $pdo
118
     * @param string $schema
119
     */
120
    final public function __construct(PDO $pdo, $schema = '')
121
    {
122
        $this->pdo    = $pdo;
29✔
123
        $this->schema = $schema;
29✔
124
    }
125

126
    /**
127
     * Returns the schema for the connection.
128
     *
129
     * @return string
130
     */
131
    public function getSchema()
132
    {
133
        return $this->schema;
×
134
    }
135

136
    /**
137
     * Returns a quoted schema object. (table name, column name, etc)
138
     *
139
     * @param string $object
140
     *
141
     * @return string
142
     */
143
    public function quoteSchemaObject($object)
144
    {
145
        $parts       = explode('.', $object);
16✔
146
        $quotedParts = [];
16✔
147

148
        foreach ($parts as $part) {
16✔
149
            $quotedParts[] = $this->schemaObjectQuoteChar .
16✔
150
                str_replace($this->schemaObjectQuoteChar, $this->schemaObjectQuoteChar . $this->schemaObjectQuoteChar, $part) .
16✔
151
                $this->schemaObjectQuoteChar;
16✔
152
        }
153

154
        return implode('.', $quotedParts);
16✔
155
    }
156

157
    /**
158
     * Seperates the schema and the table from a fully qualified table name.
159
     *
160
     * Returns an associative array containing the 'schema' and the 'table'.
161
     *
162
     * @param string $fullTableName
163
     *
164
     * @return array
165
     */
166
    public function splitTableName($fullTableName)
167
    {
168
        if (($dot = strpos($fullTableName, '.')) !== false) {
×
169
            return [
×
170
                'schema' => substr($fullTableName, 0, $dot),
×
171
                'table'  => substr($fullTableName, $dot + 1),
×
172
            ];
×
173
        }
174

175
        return [
×
176
                'schema' => null,
×
177
                'table'  => $fullTableName,
×
178
            ];
×
179
    }
180

181
    /**
182
     * Returns the command for the database to truncate a table.
183
     *
184
     * @return string
185
     */
186
    public function getTruncateCommand()
187
    {
188
        return $this->truncateCommand;
14✔
189
    }
190

191
    /**
192
     * Returns true if the rdbms allows cascading
193
     *
194
     * @return bool
195
     */
196
    public function allowsCascading()
197
    {
198
        return false;
×
199
    }
200

201
    /**
202
     * Disables primary keys if the rdbms does not allow setting them otherwise
203
     *
204
     * @param string $tableName
205
     */
206
    public function disablePrimaryKeys($tableName): void
207
    {
208
        return;
11✔
209
    }
210

211
    /**
212
     * Reenables primary keys after they have been disabled
213
     *
214
     * @param string $tableName
215
     */
216
    public function enablePrimaryKeys($tableName): void
217
    {
218
        return;
11✔
219
    }
220
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc