-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
ConnectionHelper.php
158 lines (146 loc) · 5.71 KB
/
ConnectionHelper.php
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @since 4.3.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\TestSuite;
use Cake\Database\Connection;
use Cake\Database\DriverFeatureEnum;
use Cake\Database\Log\QueryLogger;
use Cake\Datasource\ConnectionManager;
use Closure;
/**
* Helper for managing test connections
*
* @internal
*/
class ConnectionHelper
{
/**
* Adds `test_<connection name>` aliases for all non-test connections.
*
* This forces all models to use the test connection instead. For example,
* if a model is confused to use connection `files` then it will be aliased
* to `test_files`.
*
* The `default` connection is aliased to `test`.
*
* @return void
*/
public function addTestAliases(): void
{
ConnectionManager::alias('test', 'default');
foreach (ConnectionManager::configured() as $connection) {
if ($connection === 'test' || $connection === 'default') {
continue;
}
if (str_starts_with($connection, 'test_')) {
$original = substr($connection, 5);
ConnectionManager::alias($connection, $original);
} else {
$test = 'test_' . $connection;
ConnectionManager::alias($test, $connection);
}
}
}
/**
* Enables query logging for all database connections.
*
* @param array<int, string>|null $connections Connection names or null for all.
* @return void
*/
public function enableQueryLogging(?array $connections = null): void
{
$connections ??= ConnectionManager::configured();
foreach ($connections as $connection) {
$connection = ConnectionManager::get($connection);
$message = '--Starting test run ' . date('Y-m-d H:i:s');
if (
$connection instanceof Connection &&
$connection->getDriver()->log($message) === false
) {
$connection->getDriver()->setLogger(new QueryLogger());
$connection->getDriver()->log($message);
}
}
}
/**
* Drops all tables.
*
* @param string $connectionName Connection name
* @param array<string>|null $tables List of tables names or null for all.
* @return void
*/
public function dropTables(string $connectionName, ?array $tables = null): void
{
$connection = ConnectionManager::get($connectionName);
assert($connection instanceof Connection);
$collection = $connection->getSchemaCollection();
$allTables = $collection->listTablesWithoutViews();
$tables = $tables !== null ? array_intersect($tables, $allTables) : $allTables;
/** @var array<\Cake\Database\Schema\TableSchema> $schemas Specify type for psalm */
$schemas = array_map(fn ($table) => $collection->describe($table), $tables);
$dialect = $connection->getDriver()->schemaDialect();
foreach ($schemas as $schema) {
foreach ($dialect->dropConstraintSql($schema) as $statement) {
$connection->execute($statement);
}
}
foreach ($schemas as $schema) {
foreach ($dialect->dropTableSql($schema) as $statement) {
$connection->execute($statement);
}
}
}
/**
* Truncates all tables.
*
* @param string $connectionName Connection name
* @param array<string>|null $tables List of tables names or null for all.
* @return void
*/
public function truncateTables(string $connectionName, ?array $tables = null): void
{
$connection = ConnectionManager::get($connectionName);
assert($connection instanceof Connection);
$collection = $connection->getSchemaCollection();
$allTables = $collection->listTablesWithoutViews();
$tables = $tables !== null ? array_intersect($tables, $allTables) : $allTables;
/** @var array<\Cake\Database\Schema\TableSchema> $schemas Specify type for psalm */
$schemas = array_map(fn ($table) => $collection->describe($table), $tables);
$this->runWithoutConstraints($connection, function (Connection $connection) use ($schemas): void {
$dialect = $connection->getDriver()->schemaDialect();
foreach ($schemas as $schema) {
foreach ($dialect->truncateTableSql($schema) as $statement) {
$connection->execute($statement);
}
}
});
}
/**
* Runs callback with constraints disabled correctly per-database
*
* @param \Cake\Database\Connection $connection Database connection
* @param \Closure $callback callback
* @return void
*/
public function runWithoutConstraints(Connection $connection, Closure $callback): void
{
if ($connection->getDriver()->supports(DriverFeatureEnum::DISABLE_CONSTRAINT_WITHOUT_TRANSACTION)) {
$connection->disableConstraints(fn (Connection $connection) => $callback($connection));
} else {
$connection->transactional(function (Connection $connection) use ($callback): void {
$connection->disableConstraints(fn (Connection $connection) => $callback($connection));
});
}
}
}