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
<?php
namespace Doctrine\DBAL\Tests;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Logging\DebugStack;
use Exception;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;
use Throwable;
use const PHP_EOL;
use function array_map;
use function array_reverse;
use function count;
use function get_class;
use function implode;
use function is_object;
use function is_scalar;
use function strpos;
use function var_export;
abstract class FunctionalTestCase extends TestCase
{
/**
* Shared connection when a TestCase is run alone (outside of it's functional suite)
*
* @var Connection
*/
private static $sharedConnection;
/** @var Connection */
protected $connection;
/** @var DebugStack */
protected $sqlLoggerStack;
protected function resetSharedConn() : void
{
if (! self::$sharedConnection) {
return;
}
self::$sharedConnection->close();
self::$sharedConnection = null;
}
protected function setUp() : void
{
if (! isset(self::$sharedConnection)) {
self::$sharedConnection = TestUtil::getConnection();
}
$this->connection = self::$sharedConnection;
$this->sqlLoggerStack = new DebugStack();
$this->connection->getConfiguration()->setSQLLogger($this->sqlLoggerStack);
}
protected function tearDown() : void
{
while ($this->connection->isTransactionActive()) {
$this->connection->rollBack();
}
}
protected function onNotSuccessfulTest(Throwable $t) : void
{
if ($t instanceof AssertionFailedError) {
throw $t;
}
if (count($this->sqlLoggerStack->queries) > 0) {
$queries = '';
$i = count($this->sqlLoggerStack->queries);
foreach (array_reverse($this->sqlLoggerStack->queries) as $query) {
$params = array_map(static function ($p) {
if (is_object($p)) {
return get_class($p);
}
if (is_scalar($p)) {
return "'" . $p . "'";
}
return var_export($p, true);
}, $query['params'] ?? []);
$queries .= $i . ". SQL: '" . $query['sql'] . "' Params: " . implode(', ', $params) . PHP_EOL;
$i--;
}
$trace = $t->getTrace();
$traceMsg = '';
foreach ($trace as $part) {
if (! isset($part['file'])) {
continue;
}
if (strpos($part['file'], 'PHPUnit/') !== false) {
// Beginning with PHPUnit files we don't print the trace anymore.
break;
}
$traceMsg .= $part['file'] . ':' . $part['line'] . PHP_EOL;
}
$message = '[' . get_class($t) . '] ' . $t->getMessage() . PHP_EOL . PHP_EOL . 'With queries:' . PHP_EOL . $queries . PHP_EOL . 'Trace:' . PHP_EOL . $traceMsg;
throw new Exception($message, (int) $t->getCode(), $t);
}
throw $t;
}
}