DbalFunctionalTestCase.php 3.14 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
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;
Grégoire Paris's avatar
Grégoire Paris committed
19
use const PHP_EOL;
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
        }
Grégoire Paris's avatar
Grégoire Paris committed
51

Sergei Morozov's avatar
Sergei Morozov committed
52
        $this->connection = self::$sharedConnection;
53

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

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

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

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

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

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

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

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

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

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

Sergei Morozov's avatar
Sergei Morozov committed
107
            throw new Exception($message, (int) $t->getCode(), $t);
108
        }
Grégoire Paris's avatar
Grégoire Paris committed
109

Luís Cobucci's avatar
Luís Cobucci committed
110
        throw $t;
111
    }
112
}