DbalFunctionalTestCase.php 3.08 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 22

class DbalFunctionalTestCase extends DbalTestCase
{
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 37
    protected function resetSharedConn()
    {
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 47
    protected function setUp()
    {
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 58
    protected function tearDown()
    {
Sergei Morozov's avatar
Sergei Morozov committed
59 60
        while ($this->connection->isTransactionActive()) {
            $this->connection->rollBack();
61 62 63
        }
    }

Sergei Morozov's avatar
Sergei Morozov committed
64
    protected function onNotSuccessfulTest(Throwable $t)
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 77
                    if (is_object($p)) {
                        return get_class($p);
                    } elseif (is_scalar($p)) {
Sergei Morozov's avatar
Sergei Morozov committed
78
                        return "'" . $p . "'";
79
                    }
Gabriel Caruso's avatar
Gabriel Caruso committed
80 81

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

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

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

                $traceMsg .= $part['file'] . ':' . $part['line'] . PHP_EOL;
100 101
            }

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

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