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
|
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Tests;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException;
use Doctrine\DBAL\Platforms\Exception\NotSupported;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use PHPUnit\Framework\Attributes\After;
use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\TestCase;
use function count;
abstract class FunctionalTestCase extends TestCase
{
/**
* Shared connection when a TestCase is run alone (outside of it's functional suite)
*/
private static ?Connection $sharedConnection = null;
protected Connection $connection;
/**
* Whether the shared connection could be reused by subsequent tests.
*/
private bool $isConnectionReusable = true;
/**
* Mark shared connection not reusable for subsequent tests.
*
* Should be called by the tests that modify configuration
* or alter the connection state in another way that may impact other tests.
*/
protected function markConnectionNotReusable(): void
{
$this->isConnectionReusable = false;
}
#[Before]
final protected function connect(): void
{
if (self::$sharedConnection === null) {
self::$sharedConnection = TestUtil::getConnection();
}
$this->connection = self::$sharedConnection;
}
#[After]
final protected function disconnect(): void
{
while ($this->connection->isTransactionActive()) {
$this->connection->rollBack();
}
if ($this->isConnectionReusable) {
return;
}
if (self::$sharedConnection !== null) {
self::$sharedConnection->close();
self::$sharedConnection = null;
}
// Make sure the connection is no longer available to the test.
// Otherwise, there is a chance that a teardown method of the test will reconnect
// (e.g. to drop a table), and then this reopened connection will remain open and attached to the PHPUnit result
// until the end of the suite leaking connection resources, while subsequent tests will use
// the newly established shared connection.
unset($this->connection);
$this->isConnectionReusable = true;
}
/**
* Drops the table with the specified name, if it exists.
*
* @throws Exception
*/
public function dropTableIfExists(string $name): void
{
$schemaManager = $this->connection->createSchemaManager();
try {
$schemaManager->dropTable($name);
} catch (DatabaseObjectNotFoundException) {
}
}
/**
* Drops and creates a new table.
*
* @throws Exception
*/
public function dropAndCreateTable(Table $table): void
{
$schemaManager = $this->connection->createSchemaManager();
$platform = $this->connection->getDatabasePlatform();
$tableName = $table->getQuotedName($platform);
$this->dropTableIfExists($tableName);
$schemaManager->createTable($table);
}
/**
* Drops the schema with the specified name, if it exists.
*
* @throws Exception
*/
public function dropSchemaIfExists(string $schemaName): void
{
$platform = $this->connection->getDatabasePlatform();
if (! $platform->supportsSchemas()) {
throw NotSupported::new(__METHOD__);
}
$schemaName = (new Identifier($schemaName))->getName();
$schemaManager = $this->connection->createSchemaManager();
$databaseSchema = $schemaManager->introspectSchema();
$sequencesToDrop = [];
foreach ($databaseSchema->getSequences() as $sequence) {
if ($sequence->getNamespaceName() !== $schemaName) {
continue;
}
$sequencesToDrop[] = $sequence;
}
$tablesToDrop = [];
foreach ($databaseSchema->getTables() as $table) {
if ($table->getNamespaceName() !== $schemaName) {
continue;
}
$tablesToDrop[] = $table;
}
if (count($sequencesToDrop) > 0 || count($tablesToDrop) > 0) {
$schemaManager->dropSchemaObjects(new Schema($tablesToDrop, $sequencesToDrop));
}
try {
$quotedSchemaName = (new Identifier($schemaName))->getQuotedName($platform);
$schemaManager->dropSchema($quotedSchemaName);
} catch (DatabaseObjectNotFoundException) {
}
}
/**
* Drops and creates a new schema.
*
* @throws Exception
*/
public function dropAndCreateSchema(string $schemaName): void
{
$platform = $this->connection->getDatabasePlatform();
if (! $platform->supportsSchemas()) {
throw NotSupported::new(__METHOD__);
}
$schemaManager = $this->connection->createSchemaManager();
$quotedSchemaName = (new Identifier($schemaName))->getQuotedName($platform);
$schemaToCreate = new Schema([], [], null, [$quotedSchemaName]);
$this->dropSchemaIfExists($quotedSchemaName);
$schemaManager->createSchemaObjects($schemaToCreate);
}
}
|