Skip to content

Commit 0086a86

Browse files
committed
tests: adding platform specific cleanups
1 parent 2a1e832 commit 0086a86

2 files changed

Lines changed: 97 additions & 7 deletions

File tree

tests/Pest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,38 @@ function db($uuid = 'ID')
2222
}
2323
}
2424

25+
function getConnectionParams($useUUID = 'ID'): array
26+
{
27+
$dbConnection = $_ENV['DB_CONNECTION'] ?? 'sqlite';
28+
29+
$config = match ($dbConnection) {
30+
'mysql' => [
31+
'dbname' => $_ENV['DB_DATABASE'] ?? 'test',
32+
'user' => $_ENV['DB_USERNAME'] ?? 'arca_user',
33+
'password' => $_ENV['DB_PASSWORD'] ?? 'arca_pass',
34+
'host' => $_ENV['DB_HOST'] ?? '127.0.0.1',
35+
'port' => (int) ($_ENV['DB_PORT'] ?? 3306),
36+
'driver' => 'pdo_mysql',
37+
],
38+
'pgsql' => [
39+
'dbname' => $_ENV['DB_DATABASE'] ?? 'test',
40+
'user' => $_ENV['DB_USERNAME'] ?? 'arca_user',
41+
'password' => $_ENV['DB_PASSWORD'] ?? 'arca_pass',
42+
'host' => $_ENV['DB_HOST'] ?? '127.0.0.1',
43+
'port' => (int) ($_ENV['DB_PORT'] ?? 5432),
44+
'driver' => 'pdo_pgsql',
45+
],
46+
'sqlite' => [
47+
'path' => $_ENV['DB_DATABASE'] ?? ':memory:',
48+
'driver' => 'pdo_sqlite',
49+
],
50+
default => throw new InvalidArgumentException("Unsupported database connection: {$dbConnection}")
51+
};
52+
53+
$config['useUUID'] = 'UUID' === $useUUID;
54+
55+
return $config;
56+
}
2557

2658

2759
function populateRandomUser($uuid = 'ID'): void

tests/TestHelpers.php

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,16 @@ function compareTableSchemas(string $tableName, Doctrine\DBAL\Schema\Table $expe
291291
function cleanupTableManagerTestTables(): void
292292
{
293293
try {
294-
db()->getConnection()->executeStatement('DROP TABLE IF EXISTS user CASCADE; ');
294+
$connection = db()->getConnection();
295+
$platform = $connection->getDatabasePlatform();
296+
297+
if ($platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
298+
$connection->executeStatement('DROP TABLE IF EXISTS user;');
299+
} elseif ($platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform) {
300+
$connection->executeStatement('DROP TABLE IF EXISTS "user" CASCADE;');
301+
} else {
302+
$connection->executeStatement('DROP TABLE IF EXISTS user CASCADE;');
303+
}
295304
} catch (Exception) {
296305
// Ignore cleanup errors
297306
}
@@ -417,17 +426,42 @@ function cleanupTestTables(string $useUUID): void
417426
{
418427
try {
419428
$connection = db($useUUID)->getConnection();
420-
$connection->executeStatement('SET FOREIGN_KEY_CHECKS=0;');
429+
$platform = $connection->getDatabasePlatform();
430+
431+
// Disable foreign key checks if supported
432+
if ($platform instanceof \Doctrine\DBAL\Platforms\MySQLPlatform) {
433+
$connection->executeStatement('SET FOREIGN_KEY_CHECKS=0;');
434+
} elseif ($platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
435+
$connection->executeStatement('PRAGMA foreign_keys = OFF;');
436+
} elseif ($platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform) {
437+
// PostgreSQL doesn't have a global foreign key disable,
438+
// but CASCADE in DROP statements handles dependencies
439+
}
421440

422441
// Get all existing tables first
423442
$tables = $connection->createSchemaManager()->listTableNames();
424443

425444
// Drop all tables to ensure complete cleanup
426445
foreach ($tables as $table) {
427-
$connection->executeStatement("DROP TABLE IF EXISTS {$table} CASCADE;");
446+
if ($platform instanceof \Doctrine\DBAL\Platforms\MySQLPlatform) {
447+
$connection->executeStatement("DROP TABLE IF EXISTS {$table} CASCADE;");
448+
} elseif ($platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
449+
$connection->executeStatement("DROP TABLE IF EXISTS {$table};");
450+
} elseif ($platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform) {
451+
$connection->executeStatement("DROP TABLE IF EXISTS \"{$table}\" CASCADE;");
452+
} else {
453+
$connection->executeStatement("DROP TABLE IF EXISTS {$table} CASCADE;");
454+
}
428455
}
429456

430-
$connection->executeStatement('SET FOREIGN_KEY_CHECKS=1;');
457+
// Re-enable foreign key checks if supported
458+
if ($platform instanceof \Doctrine\DBAL\Platforms\MySQLPlatform) {
459+
$connection->executeStatement('SET FOREIGN_KEY_CHECKS=1;');
460+
} elseif ($platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
461+
$connection->executeStatement('PRAGMA foreign_keys = ON;');
462+
} elseif ($platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform) {
463+
// No global setting to re-enable for PostgreSQL
464+
}
431465
} catch (Exception) {
432466
// Ignore cleanup errors
433467
}
@@ -442,17 +476,41 @@ function cleanupAllTestTables(): void
442476
foreach (['UUID', 'ID'] as $config) {
443477
try {
444478
$connection = db($config)->getConnection();
445-
$connection->executeStatement('SET FOREIGN_KEY_CHECKS=0;');
479+
$platform = $connection->getDatabasePlatform();
480+
481+
// Disable foreign key checks if supported
482+
if ($platform instanceof \Doctrine\DBAL\Platforms\MySQLPlatform) {
483+
$connection->executeStatement('SET FOREIGN_KEY_CHECKS=0;');
484+
} elseif ($platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
485+
$connection->executeStatement('PRAGMA foreign_keys = OFF;');
486+
} elseif ($platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform) {
487+
// PostgreSQL doesn't have a global foreign key disable
488+
}
446489

447490
// Get all existing tables first
448491
$tables = $connection->createSchemaManager()->listTableNames();
449492

450493
// Drop all tables to ensure complete cleanup
451494
foreach ($tables as $table) {
452-
$connection->executeStatement("DROP TABLE IF EXISTS {$table} CASCADE;");
495+
if ($platform instanceof \Doctrine\DBAL\Platforms\MySQLPlatform) {
496+
$connection->executeStatement("DROP TABLE IF EXISTS {$table} CASCADE;");
497+
} elseif ($platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
498+
$connection->executeStatement("DROP TABLE IF EXISTS {$table};");
499+
} elseif ($platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform) {
500+
$connection->executeStatement("DROP TABLE IF EXISTS \"{$table}\" CASCADE;");
501+
} else {
502+
$connection->executeStatement("DROP TABLE IF EXISTS {$table} CASCADE;");
503+
}
453504
}
454505

455-
$connection->executeStatement('SET FOREIGN_KEY_CHECKS=1;');
506+
// Re-enable foreign key checks if supported
507+
if ($platform instanceof \Doctrine\DBAL\Platforms\MySQLPlatform) {
508+
$connection->executeStatement('SET FOREIGN_KEY_CHECKS=1;');
509+
} elseif ($platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
510+
$connection->executeStatement('PRAGMA foreign_keys = ON;');
511+
} elseif ($platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform) {
512+
// No global setting to re-enable for PostgreSQL
513+
}
456514
} catch (Exception) {
457515
// Ignore cleanup errors
458516
}

0 commit comments

Comments
 (0)