DbalFunctionalTestCase.php 3.13 KB
Newer Older
1 2 3
<?php

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

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

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

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

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

36
    protected function resetSharedConn() : void
37
    {
Sergei Morozov's avatar
Sergei Morozov committed
38
        if (! self::$sharedConnection) {
Sergei Morozov's avatar
Sergei Morozov committed
39
            return;
40
        }
Sergei Morozov's avatar
Sergei Morozov committed
41

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

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

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

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

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

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

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

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

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

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

                $traceMsg .= $part['file'] . ':' . $part['line'] . PHP_EOL;
102 103
            }

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

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