forked from doctrine/dbal
/
FunctionalTestCase.php
71 lines (58 loc) · 1.99 KB
/
FunctionalTestCase.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
<?php
namespace Doctrine\DBAL\Tests;
use Doctrine\DBAL\Connection;
use PHPUnit\Framework\TestCase;
abstract class FunctionalTestCase extends TestCase
{
/**
* Shared connection when a TestCase is run alone (outside of it's functional suite)
*
* @var Connection|null
*/
private static $sharedConnection;
/** @var Connection */
protected $connection;
/**
* Whether the shared connection could be reused by subsequent tests.
*
* @var bool
*/
private $isConnectionReusable = true;
/**
* Mark shared connection not reusable for subsequent tests.
*
* Should be called by the tests that modify configuration configuration
* or alter the connection state in another way that may impact other tests.
*/
protected function markConnectionNotReusable(): void
{
$this->isConnectionReusable = false;
}
protected function setUp(): void
{
if (self::$sharedConnection === null) {
self::$sharedConnection = TestUtil::getConnection();
}
$this->connection = self::$sharedConnection;
}
protected function tearDown(): 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;
}
}