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

codeigniter4 / CodeIgniter4 / 12673986434

08 Jan 2025 03:42PM UTC coverage: 84.455% (+0.001%) from 84.454%
12673986434

Pull #9385

github

web-flow
Merge 06e47f0ee into e475fd8fa
Pull Request #9385: refactor: Fix phpstan expr.resultUnused

20699 of 24509 relevant lines covered (84.45%)

190.57 hits per line

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

43.9
/system/Test/Mock/MockConnection.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Test\Mock;
15

16
use CodeIgniter\CodeIgniter;
17
use CodeIgniter\Database\BaseConnection;
18
use CodeIgniter\Database\BaseResult;
19
use CodeIgniter\Database\Query;
20
use CodeIgniter\Database\TableName;
21

22
/**
23
 * @extends BaseConnection<object|resource, object|resource>
24
 */
25
class MockConnection extends BaseConnection
26
{
27
    /**
28
     * @var array{connect?: mixed, execute?: bool|object}
29
     */
30
    protected $returnValues = [];
31

32
    /**
33
     * Database schema for Postgre and SQLSRV
34
     *
35
     * @var string
36
     */
37
    protected $schema;
38

39
    public $database;
40
    public $lastQuery;
41

42
    /**
43
     * @param mixed $return
44
     *
45
     * @return $this
46
     */
47
    public function shouldReturn(string $method, $return)
48
    {
49
        $this->returnValues[$method] = $return;
8✔
50

51
        return $this;
8✔
52
    }
53

54
    /**
55
     * Orchestrates a query against the database. Queries must use
56
     * Database\Statement objects to store the query and build it.
57
     * This method works with the cache.
58
     *
59
     * Should automatically handle different connections for read/write
60
     * queries if needed.
61
     *
62
     * @param mixed ...$binds
63
     *
64
     * @return BaseResult|bool|Query
65
     *
66
     * @todo BC set $queryClass default as null in 4.1
67
     */
68
    public function query(string $sql, $binds = null, bool $setEscapeFlags = true, string $queryClass = '')
69
    {
70
        $queryClass = str_replace('Connection', 'Query', static::class);
6✔
71

72
        $query = new $queryClass($this);
6✔
73

74
        $query->setQuery($sql, $binds, $setEscapeFlags);
6✔
75

76
        if ($this->swapPre !== '' && $this->DBPrefix !== '') {
6✔
77
            $query->swapPrefix($this->DBPrefix, $this->swapPre);
×
78
        }
79

80
        $startTime = microtime(true);
6✔
81

82
        $this->lastQuery = $query;
6✔
83

84
        // Run the query
85
        if (false === ($this->resultID = $this->simpleQuery($query->getQuery()))) {
6✔
86
            $query->setDuration($startTime, $startTime);
×
87

88
            // @todo deal with errors
89

90
            return false;
×
91
        }
92

93
        $query->setDuration($startTime);
6✔
94

95
        // resultID is not false, so it must be successful
96
        if ($query->isWriteType($sql)) {
6✔
97
            return true;
6✔
98
        }
99

100
        // query is not write-type, so it must be read-type query; return QueryResult
101
        $resultClass = str_replace('Connection', 'Result', static::class);
×
102

103
        return new $resultClass($this->connID, $this->resultID);
×
104
    }
105

106
    /**
107
     * Connect to the database.
108
     *
109
     * @return mixed
110
     */
111
    public function connect(bool $persistent = false)
112
    {
113
        $return = $this->returnValues['connect'] ?? true;
10✔
114

115
        if (is_array($return)) {
10✔
116
            // By removing the top item here, we can
117
            // get a different value for, say, testing failover connections.
118
            $return = array_shift($this->returnValues['connect']);
1✔
119
        }
120

121
        return $return;
10✔
122
    }
123

124
    /**
125
     * Keep or establish the connection if no queries have been sent for
126
     * a length of time exceeding the server's idle timeout.
127
     */
128
    public function reconnect(): bool
129
    {
130
        return true;
×
131
    }
132

133
    /**
134
     * Select a specific database table to use.
135
     *
136
     * @return bool
137
     */
138
    public function setDatabase(string $databaseName)
139
    {
140
        $this->database = $databaseName;
×
141

142
        return true;
×
143
    }
144

145
    /**
146
     * Returns a string containing the version of the database being used.
147
     */
148
    public function getVersion(): string
149
    {
150
        return CodeIgniter::CI_VERSION;
×
151
    }
152

153
    /**
154
     * Executes the query against the database.
155
     *
156
     * @return bool|object
157
     */
158
    protected function execute(string $sql)
159
    {
160
        return $this->returnValues['execute'];
6✔
161
    }
162

163
    /**
164
     * Returns the total number of rows affected by this query.
165
     */
166
    public function affectedRows(): int
167
    {
168
        return 1;
6✔
169
    }
170

171
    /**
172
     * Returns the last error code and message.
173
     *
174
     * Must return an array with keys 'code' and 'message':
175
     *
176
     *  return ['code' => null, 'message' => null);
177
     */
178
    public function error(): array
179
    {
180
        return [
×
181
            'code'    => 0,
×
182
            'message' => '',
×
183
        ];
×
184
    }
185

186
    /**
187
     * Insert ID
188
     */
189
    public function insertID(): int
190
    {
191
        return $this->connID->insert_id;
×
192
    }
193

194
    /**
195
     * Generates the SQL for listing tables in a platform-dependent manner.
196
     *
197
     * @param string|null $tableName If $tableName is provided will return only this table if exists.
198
     */
199
    protected function _listTables(bool $constrainByPrefix = false, ?string $tableName = null): string
200
    {
201
        return '';
×
202
    }
203

204
    /**
205
     * Generates a platform-specific query string so that the column names can be fetched.
206
     *
207
     * @param string|TableName $table
208
     */
209
    protected function _listColumns($table = ''): string
210
    {
211
        return '';
×
212
    }
213

214
    protected function _fieldData(string $table): array
215
    {
216
        return [];
×
217
    }
218

219
    protected function _indexData(string $table): array
220
    {
221
        return [];
×
222
    }
223

224
    protected function _foreignKeyData(string $table): array
225
    {
226
        return [];
×
227
    }
228

229
    /**
230
     * Close the connection.
231
     *
232
     * @return void
233
     */
234
    protected function _close()
235
    {
236
    }
×
237

238
    /**
239
     * Begin Transaction
240
     */
241
    protected function _transBegin(): bool
242
    {
243
        return true;
×
244
    }
245

246
    /**
247
     * Commit Transaction
248
     */
249
    protected function _transCommit(): bool
250
    {
251
        return true;
×
252
    }
253

254
    /**
255
     * Rollback Transaction
256
     */
257
    protected function _transRollback(): bool
258
    {
259
        return true;
×
260
    }
261
}
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

© 2026 Coveralls, Inc