• 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

71.43
/src/Database/DefaultConnection.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;
13

14
use PHPUnit\DbUnit\Database\Metadata\AbstractMetadata;
15
use PHPUnit\DbUnit\Database\Metadata\Metadata;
16
use PHPUnit\DbUnit\DataSet\IDataSet;
17
use PHPUnit\DbUnit\DataSet\ITable;
18
use PHPUnit\DbUnit\DataSet\QueryTable;
19

20
/**
21
 * Provides a basic interface for communicating with a database.
22
 */
23
class DefaultConnection implements Connection
24
{
25
    /**
26
     * @var \PDO
27
     */
28
    protected $connection;
29

30
    /**
31
     * The metadata object used to retrieve table meta data from the database.
32
     *
33
     * @var Metadata
34
     */
35
    protected $metaData;
36

37
    /**
38
     * Creates a new database connection
39
     *
40
     * @param \PDO    $connection
41
     * @param string  $schema
42
     */
43
    public function __construct(\PDO $connection, string $schema = '')
44
    {
45
        $this->connection = $connection;
29✔
46
        $this->metaData = AbstractMetadata::createMetaData($connection, $schema);
29✔
47
        $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
29✔
48
    }
49

50
    /**
51
     * Close this connection.
52
     */
53
    public function close(): void
54
    {
55
        unset($this->connection, $this->metaData);
×
56
    }
57

58
    /**
59
     * Returns a database metadata object that can be used to retrieve table
60
     * meta data from the database.
61
     *
62
     * @return Metadata
63
     */
64
    public function getMetaData(): Metadata
65
    {
66
        return $this->metaData;
17✔
67
    }
68

69
    /**
70
     * Returns the schema for the connection.
71
     *
72
     * @return string
73
     */
74
    public function getSchema(): string
75
    {
76
        return $this->getMetaData()->getSchema();
×
77
    }
78

79
    /**
80
     * Creates a dataset containing the specified table names. If no table
81
     * names are specified then it will created a dataset over the entire
82
     * database.
83
     *
84
     * @param array $tableNames
85
     *
86
     * @return IDataSet
87
     *
88
     * @todo Implement the filtered data set.
89
     */
90
    public function createDataSet(array $tableNames = null): IDataSet
91
    {
92
        if (empty($tableNames)) {
14✔
93
            return new DataSet($this);
14✔
94
        }
95

96
        return new FilteredDataSet($this, $tableNames);
3✔
97
    }
98

99
    /**
100
     * Creates a table with the result of the specified SQL statement.
101
     *
102
     * @param string $resultName
103
     * @param string $sql
104
     *
105
     * @return QueryTable
106
     */
107
    public function createQueryTable($resultName, $sql): ITable
108
    {
109
        return new QueryTable($resultName, $sql, $this);
×
110
    }
111

112
    /**
113
     * Returns this connection database configuration
114
     */
115
    public function getConfig(): void
116
    {
117
    }
×
118

119
    /**
120
     * Returns a PDO Connection
121
     *
122
     * @return \PDO
123
     */
124
    public function getConnection(): \PDO
125
    {
126
        return $this->connection;
27✔
127
    }
128

129
    /**
130
     * Returns the number of rows in the given table. You can specify an
131
     * optional where clause to return a subset of the table.
132
     *
133
     * @param string $tableName
134
     * @param string $whereClause
135
     *
136
     * @return int
137
     */
138
    public function getRowCount($tableName, $whereClause = null): int
139
    {
140
        $query = "SELECT COUNT(*) FROM {$this->quoteSchemaObject($tableName)}";
2✔
141

142
        if (isset($whereClause)) {
2✔
143
            $query .= " WHERE {$whereClause}";
×
144
        }
145

146
        return (int) $this->connection->query($query)->fetchColumn();
2✔
147
    }
148

149
    /**
150
     * Returns a quoted schema object. (table name, column name, etc)
151
     *
152
     * @param string $object
153
     *
154
     * @return string
155
     */
156
    public function quoteSchemaObject(string $object): string
157
    {
158
        return $this->getMetaData()->quoteSchemaObject($object);
16✔
159
    }
160

161
    /**
162
     * Returns the command used to truncate a table.
163
     *
164
     * @return string
165
     */
166
    public function getTruncateCommand(): string
167
    {
168
        return $this->getMetaData()->getTruncateCommand();
14✔
169
    }
170

171
    /**
172
     * Returns true if the connection allows cascading
173
     *
174
     * @return bool
175
     */
176
    public function allowsCascading(): bool
177
    {
178
        return $this->getMetaData()->allowsCascading();
×
179
    }
180

181
    /**
182
     * Disables primary keys if connection does not allow setting them otherwise
183
     *
184
     * @param string $tableName
185
     */
186
    public function disablePrimaryKeys(string $tableName): void
187
    {
188
        $this->getMetaData()->disablePrimaryKeys($tableName);
11✔
189
    }
190

191
    /**
192
     * Reenables primary keys after they have been disabled
193
     *
194
     * @param string $tableName
195
     */
196
    public function enablePrimaryKeys(string $tableName): void
197
    {
198
        $this->getMetaData()->enablePrimaryKeys($tableName);
11✔
199
    }
200
}
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