DbalFunctionalTestCase.php 3.17 KB
Newer Older
1 2
<?php

Michael Moravec's avatar
Michael Moravec committed
3 4
declare(strict_types=1);

5
namespace Doctrine\Tests;
Sergei Morozov's avatar
Sergei Morozov committed
6 7 8 9 10 11

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Logging\DebugStack;
use Exception;
use PHPUnit\Framework\AssertionFailedError;
use Throwable;
12 13 14 15 16 17 18 19 20 21
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;
22

23
abstract class DbalFunctionalTestCase extends DbalTestCase
24
{
25 26 27
    /**
     * Shared connection when a TestCase is run alone (outside of it's functional suite)
     *
28
     * @var Connection|null
29
     */
Sergei Morozov's avatar
Sergei Morozov committed
30
    private static $sharedConnection;
31

Sergei Morozov's avatar
Sergei Morozov committed
32
    /** @var Connection */
Sergei Morozov's avatar
Sergei Morozov committed
33
    protected $connection;
34

Sergei Morozov's avatar
Sergei Morozov committed
35
    /** @var DebugStack */
Sergei Morozov's avatar
Sergei Morozov committed
36
    protected $sqlLoggerStack;
37

38
    protected function resetSharedConn() : void
39
    {
40
        if (self::$sharedConnection === null) {
Sergei Morozov's avatar
Sergei Morozov committed
41
            return;
42
        }
Sergei Morozov's avatar
Sergei Morozov committed
43

Sergei Morozov's avatar
Sergei Morozov committed
44 45
        self::$sharedConnection->close();
        self::$sharedConnection = null;
46 47
    }

48
    protected function setUp() : void
49
    {
Sergei Morozov's avatar
Sergei Morozov committed
50 51
        if (! isset(self::$sharedConnection)) {
            self::$sharedConnection = TestUtil::getConnection();
52
        }
Sergei Morozov's avatar
Sergei Morozov committed
53
        $this->connection = self::$sharedConnection;
54

Sergei Morozov's avatar
Sergei Morozov committed
55 56
        $this->sqlLoggerStack = new DebugStack();
        $this->connection->getConfiguration()->setSQLLogger($this->sqlLoggerStack);
57 58
    }

59
    protected function tearDown() : void
60
    {
Sergei Morozov's avatar
Sergei Morozov committed
61 62
        while ($this->connection->isTransactionActive()) {
            $this->connection->rollBack();
63 64 65
        }
    }

66
    protected function onNotSuccessfulTest(Throwable $t) : void
67
    {
Sergei Morozov's avatar
Sergei Morozov committed
68
        if ($t instanceof AssertionFailedError) {
Luís Cobucci's avatar
Luís Cobucci committed
69
            throw $t;
70 71
        }

Sergei Morozov's avatar
Sergei Morozov committed
72
        if (isset($this->sqlLoggerStack->queries) && count($this->sqlLoggerStack->queries)) {
Sergei Morozov's avatar
Sergei Morozov committed
73
            $queries = '';
Sergei Morozov's avatar
Sergei Morozov committed
74 75
            $i       = count($this->sqlLoggerStack->queries);
            foreach (array_reverse($this->sqlLoggerStack->queries) as $query) {
Sergei Morozov's avatar
Sergei Morozov committed
76
                $params   = array_map(static function ($p) {
77 78
                    if (is_object($p)) {
                        return get_class($p);
79 80 81
                    }

                    if (is_scalar($p)) {
Sergei Morozov's avatar
Sergei Morozov committed
82
                        return "'" . $p . "'";
83
                    }
Gabriel Caruso's avatar
Gabriel Caruso committed
84 85

                    return var_export($p, true);
Sergei Morozov's avatar
Sergei Morozov committed
86 87
                }, $query['params'] ?: []);
                $queries .= $i . ". SQL: '" . $query['sql'] . "' Params: " . implode(', ', $params) . PHP_EOL;
88
                $i--;
89 90
            }

Sergei Morozov's avatar
Sergei Morozov committed
91 92 93 94 95 96
            $trace    = $t->getTrace();
            $traceMsg = '';
            foreach ($trace as $part) {
                if (! isset($part['file'])) {
                    continue;
                }
97

Sergei Morozov's avatar
Sergei Morozov committed
98 99 100
                if (strpos($part['file'], 'PHPUnit/') !== false) {
                    // Beginning with PHPUnit files we don't print the trace anymore.
                    break;
101
                }
Sergei Morozov's avatar
Sergei Morozov committed
102 103

                $traceMsg .= $part['file'] . ':' . $part['line'] . PHP_EOL;
104 105
            }

Sergei Morozov's avatar
Sergei Morozov committed
106
            $message = '[' . get_class($t) . '] ' . $t->getMessage() . PHP_EOL . PHP_EOL . 'With queries:' . PHP_EOL . $queries . PHP_EOL . 'Trace:' . PHP_EOL . $traceMsg;
107

Sergei Morozov's avatar
Sergei Morozov committed
108
            throw new Exception($message, (int) $t->getCode(), $t);
109
        }
Luís Cobucci's avatar
Luís Cobucci committed
110
        throw $t;
111
    }
112
}