-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ExceptionTest.php
261 lines (211 loc) · 8.27 KB
/
ExceptionTest.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Tests\Functional;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;
use Doctrine\DBAL\Types\Types;
use function array_merge;
use function chmod;
use function exec;
use function extension_loaded;
use function file_exists;
use function func_get_args;
use function posix_geteuid;
use function restore_error_handler;
use function set_error_handler;
use function sprintf;
use function sys_get_temp_dir;
use function touch;
use function unlink;
use const E_WARNING;
use const PHP_OS_FAMILY;
/** @psalm-import-type Params from DriverManager */
class ExceptionTest extends FunctionalTestCase
{
public function testPrimaryConstraintViolationException(): void
{
$table = new Table('duplicatekey_table');
$table->addColumn('id', Types::INTEGER, []);
$table->setPrimaryKey(['id']);
$this->dropAndCreateTable($table);
$this->connection->insert('duplicatekey_table', ['id' => 1]);
$this->expectException(Exception\UniqueConstraintViolationException::class);
$this->connection->insert('duplicatekey_table', ['id' => 1]);
}
public function testTableNotFoundException(): void
{
$sql = 'SELECT * FROM unknown_table';
$this->expectException(Exception\TableNotFoundException::class);
$this->connection->executeQuery($sql);
}
public function testTableExistsException(): void
{
$schemaManager = $this->connection->createSchemaManager();
$table = new Table('alreadyexist_table');
$table->addColumn('id', Types::INTEGER, []);
$table->setPrimaryKey(['id']);
$this->expectException(Exception\TableExistsException::class);
$schemaManager->createTable($table);
$schemaManager->createTable($table);
}
public function testNotNullConstraintViolationException(): void
{
$table = new Table('notnull_table');
$table->addColumn('id', Types::INTEGER, []);
$table->addColumn('val', Types::INTEGER, ['notnull' => true]);
$table->setPrimaryKey(['id']);
$this->dropAndCreateTable($table);
$this->expectException(Exception\NotNullConstraintViolationException::class);
$this->connection->insert('notnull_table', ['id' => 1, 'val' => null]);
}
public function testInvalidFieldNameException(): void
{
$table = new Table('bad_columnname_table');
$table->addColumn('id', Types::INTEGER, []);
$this->dropAndCreateTable($table);
$this->expectException(Exception\InvalidFieldNameException::class);
// prevent the PHPUnit error handler from handling the warning that db2_bind_param() may trigger
$previous = null;
$previous = set_error_handler(static function (int $errno) use (&$previous): bool {
if (($errno & ~E_WARNING) === 0) {
return true;
}
return $previous !== null && $previous(...func_get_args());
});
try {
$this->connection->insert('bad_columnname_table', ['name' => 5]);
} finally {
restore_error_handler();
}
}
public function testNonUniqueFieldNameException(): void
{
$table1 = new Table('ambiguous_list_table_1');
$table1->addColumn('id', Types::INTEGER);
$this->dropAndCreateTable($table1);
$table2 = new Table('ambiguous_list_table_2');
$table2->addColumn('id', Types::INTEGER);
$this->dropAndCreateTable($table2);
$sql = 'SELECT id FROM ambiguous_list_table_1, ambiguous_list_table_2';
$this->expectException(Exception\NonUniqueFieldNameException::class);
$this->connection->executeQuery($sql);
}
public function testUniqueConstraintViolationException(): void
{
$table = new Table('unique_column_table');
$table->addColumn('id', Types::INTEGER);
$table->addUniqueIndex(['id']);
$this->dropAndCreateTable($table);
$this->connection->insert('unique_column_table', ['id' => 5]);
$this->expectException(Exception\UniqueConstraintViolationException::class);
$this->connection->insert('unique_column_table', ['id' => 5]);
}
public function testSyntaxErrorException(): void
{
$table = new Table('syntax_error_table');
$table->addColumn('id', Types::INTEGER, []);
$table->setPrimaryKey(['id']);
$this->dropAndCreateTable($table);
$sql = 'SELECT id FRO syntax_error_table';
$this->expectException(Exception\SyntaxErrorException::class);
$this->connection->executeQuery($sql);
}
public function testConnectionExceptionSqLite(): void
{
if (! ($this->connection->getDatabasePlatform() instanceof SQLitePlatform)) {
self::markTestSkipped('Only fails this way on sqlite');
}
// mode 0 is considered read-only on Windows
$mode = PHP_OS_FAMILY !== 'Windows' ? 0444 : 0000;
$filename = sprintf('%s/%s', sys_get_temp_dir(), 'doctrine_failed_connection_' . $mode . '.db');
if (file_exists($filename)) {
$this->cleanupReadOnlyFile($filename);
}
touch($filename);
chmod($filename, $mode);
if ($this->isPosixSuperUser()) {
exec(sprintf('chattr +i %s', $filename));
}
$params = [
'driver' => 'pdo_sqlite',
'path' => $filename,
];
$conn = DriverManager::getConnection($params);
$schema = new Schema();
$table = $schema->createTable('no_connection');
$table->addColumn('id', Types::INTEGER);
$this->expectException(Exception\ReadOnlyException::class);
$this->expectExceptionMessage(
'An exception occurred while executing a query: SQLSTATE[HY000]: ' .
'General error: 8 attempt to write a readonly database',
);
try {
foreach ($schema->toSql($conn->getDatabasePlatform()) as $sql) {
$conn->executeStatement($sql);
}
} finally {
$this->cleanupReadOnlyFile($filename);
}
}
public function testInvalidUserName(): void
{
$this->testConnectionException(['user' => 'not_existing']);
}
public function testInvalidPassword(): void
{
$this->testConnectionException(['password' => 'really_not']);
}
public function testInvalidHost(): void
{
if (TestUtil::isDriverOneOf('pdo_sqlsrv', 'sqlsrv')) {
self::markTestSkipped(
'Some sqlsrv and pdo_sqlsrv versions do not provide the exception code or SQLSTATE for login timeout',
);
}
$this->testConnectionException(['host' => 'localnope']);
}
/**
* @param array<string, mixed> $params
* @psalm-param Params $params
* @phpstan-param array<string,mixed> $params
*
* @dataProvider getConnectionParams
*/
private function testConnectionException(array $params): void
{
$platform = $this->connection->getDatabasePlatform();
if ($platform instanceof SQLitePlatform) {
self::markTestSkipped('The SQLite driver does not use a network connection');
}
$params = array_merge(TestUtil::getConnectionParams(), $params);
$conn = DriverManager::getConnection($params);
$this->expectException(Exception\ConnectionException::class);
$conn->executeQuery($platform->getDummySelectSQL());
}
/** @return array<int, array<int, mixed>> */
public static function getConnectionParams(): iterable
{
return [
[['user' => 'not_existing']],
[['password' => 'really_not']],
[['host' => 'localnope']],
];
}
private function isPosixSuperUser(): bool
{
return extension_loaded('posix') && posix_geteuid() === 0;
}
private function cleanupReadOnlyFile(string $filename): void
{
if ($this->isPosixSuperUser()) {
exec(sprintf('chattr -i %s', $filename));
}
chmod($filename, 0200); // make the file writable again, so it can be removed on Windows
unlink($filename);
}
}