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
|
<?php
namespace Doctrine\DBAL\Tests;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException;
use Doctrine\DBAL\Schema\Table;
use PHPUnit\Framework\TestCase;
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 $e) {
}
}
/**
* 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);
}
}
|