-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
DbUtilsTest.php
208 lines (150 loc) · 6.31 KB
/
DbUtilsTest.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Database\Live;
use CodeIgniter\Database\Database;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Database\Seeds\CITestSeeder;
/**
* @internal
*/
#[Group('DatabaseLive')]
final class DbUtilsTest extends CIUnitTestCase
{
use DatabaseTestTrait;
protected $refresh = true;
protected $seed = CITestSeeder::class;
public function testUtilsBackup(): void
{
$util = (new Database())->loadUtils($this->db);
$this->expectException(DatabaseException::class);
$this->expectExceptionMessage('Unsupported feature of the database platform you are using.');
$util->backup();
}
public function testUtilsBackupWithParamsArray(): void
{
$util = (new Database())->loadUtils($this->db);
$params = [
'format' => 'json',
];
$this->expectException(DatabaseException::class);
$this->expectExceptionMessage('Unsupported feature of the database platform you are using.');
$util->backup($params);
}
public function testUtilsBackupWithParamsString(): void
{
$util = (new Database())->loadUtils($this->db);
$this->expectException(DatabaseException::class);
$this->expectExceptionMessage('Unsupported feature of the database platform you are using.');
$util->backup('db_jobs');
}
public function testUtilsListDatabases(): void
{
$util = (new Database())->loadUtils($this->db);
if (in_array($this->db->DBDriver, ['MySQLi', 'Postgre', 'SQLSRV', 'OCI8'], true)) {
$databases = $util->listDatabases();
$this->assertContains($this->db->getDatabase(), $databases);
} elseif ($this->db->DBDriver === 'SQLite3') {
$this->expectException(DatabaseException::class);
$this->expectExceptionMessage('Unsupported feature of the database platform you are using.');
$util->listDatabases();
}
}
public function testUtilsDatabaseExist(): void
{
$util = (new Database())->loadUtils($this->db);
if (in_array($this->db->DBDriver, ['MySQLi', 'Postgre', 'SQLSRV', 'OCI8'], true)) {
$exist = $util->databaseExists($this->db->getDatabase());
$this->assertTrue($exist);
} elseif ($this->db->DBDriver === 'SQLite3') {
$this->expectException(DatabaseException::class);
$this->expectExceptionMessage('Unsupported feature of the database platform you are using.');
$util->databaseExists($this->db->getDatabase());
}
}
public function testUtilsOptimizeDatabase(): void
{
$util = (new Database())->loadUtils($this->db);
if ($this->db->DBDriver === 'OCI8') {
$this->markTestSkipped(
'Unsupported feature of the oracle database platform.'
);
}
$d = $util->optimizeDatabase();
$this->assertTrue((bool) $d);
}
public function testUtilsOptimizeTableFalseOptimizeDatabaseDebugTrue(): void
{
$util = (new Database())->loadUtils($this->db);
$this->setPrivateProperty($util, 'optimizeTable', false);
$this->enableDBDebug();
$this->expectException(DatabaseException::class);
$this->expectExceptionMessage('Unsupported feature of the database platform you are using.');
$util->optimizeDatabase();
// this point in code execution will never be reached
}
public function testUtilsOptimizeTableFalseOptimizeDatabaseDebugFalse(): void
{
$util = (new Database())->loadUtils($this->db);
$this->setPrivateProperty($util, 'optimizeTable', false);
// WARNING this value will persist! take care to roll it back.
$this->disableDBDebug();
$result = $util->optimizeDatabase();
$this->assertFalse($result);
$this->enableDBDebug();
}
public function testUtilsOptimizeTable(): void
{
$util = (new Database())->loadUtils($this->db);
if ($this->db->DBDriver === 'OCI8') {
$this->markTestSkipped(
'Unsupported feature of the oracle database platform.'
);
}
$d = $util->optimizeTable('db_job');
$this->assertTrue($d);
}
public function testUtilsOptimizeTableFalseOptimizeTable(): void
{
$util = (new Database())->loadUtils($this->db);
$this->setPrivateProperty($util, 'optimizeTable', false);
$this->expectException(DatabaseException::class);
$this->expectExceptionMessage('Unsupported feature of the database platform you are using.');
$util->optimizeTable('db_job');
}
public function testUtilsRepairTable(): void
{
$util = (new Database())->loadUtils($this->db);
$this->expectException(DatabaseException::class);
$this->expectExceptionMessage('Unsupported feature of the database platform you are using.');
$util->repairTable('db_job');
}
public function testUtilsCSVFromResult(): void
{
$data = $this->db->table('job')->get();
$util = (new Database())->loadUtils($this->db);
$data = $util->getCSVFromResult($data);
$data = array_filter(preg_split('/(\r\n|\n|\r)/', $data));
$this->assertSame('"1","Developer","Awesome job, but sometimes makes you bored","","",""', $data[1]);
}
public function testUtilsXMLFromResult(): void
{
$data = $this->db->table('job')->where('id', 4)->get();
$util = (new Database())->loadUtils($this->db);
$data = $util->getXMLFromResult($data);
$expected = '<root><element><id>4</id><name>Musician</name><description>Only Coldplay can actually called Musician</description><created_at></created_at><updated_at></updated_at><deleted_at></deleted_at></element></root>';
$actual = preg_replace('#\R+#', '', $data);
$actual = preg_replace('/[ ]{2,}|[\t]/', '', $actual);
$this->assertSame($expected, $actual);
}
}