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

namespace Doctrine\Tests;
4 5 6 7 8 9 10 11 12 13
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;
14 15 16

class DbalFunctionalTestCase extends DbalTestCase
{
17 18 19
    /**
     * Shared connection when a TestCase is run alone (outside of it's functional suite)
     *
20
     * @var \Doctrine\DBAL\Connection
21
     */
22
    private static $_sharedConn;
23 24

    /**
25
     * @var \Doctrine\DBAL\Connection
26
     */
27 28
    protected $_conn;

29 30 31 32 33
    /**
     * @var \Doctrine\DBAL\Logging\DebugStack
     */
    protected $_sqlLoggerStack;

34 35
    protected function resetSharedConn()
    {
36 37 38 39
        if (self::$_sharedConn) {
            self::$_sharedConn->close();
            self::$_sharedConn = null;
        }
40 41
    }

42 43
    protected function setUp()
    {
44 45
        if ( ! isset(self::$_sharedConn)) {
            self::$_sharedConn = TestUtil::getConnection();
46
        }
47
        $this->_conn = self::$_sharedConn;
48 49 50 51 52

        $this->_sqlLoggerStack = new \Doctrine\DBAL\Logging\DebugStack();
        $this->_conn->getConfiguration()->setSQLLogger($this->_sqlLoggerStack);
    }

53 54 55 56 57 58 59
    protected function tearDown()
    {
        while ($this->_conn->isTransactionActive()) {
            $this->_conn->rollBack();
        }
    }

Luís Cobucci's avatar
Luís Cobucci committed
60
    protected function onNotSuccessfulTest(\Throwable $t)
61
    {
Luís Cobucci's avatar
Luís Cobucci committed
62 63
        if ($t instanceof \PHPUnit\Framework\AssertionFailedError) {
            throw $t;
64 65 66 67
        }

        if(isset($this->_sqlLoggerStack->queries) && count($this->_sqlLoggerStack->queries)) {
            $queries = "";
68
            $i = count($this->_sqlLoggerStack->queries);
jeroendedauw's avatar
jeroendedauw committed
69
            foreach (array_reverse($this->_sqlLoggerStack->queries) as $query) {
70 71 72 73 74 75
                $params = array_map(function($p) {
                    if (is_object($p)) {
                        return get_class($p);
                    } elseif (is_scalar($p)) {
                        return "'".$p."'";
                    }
Gabriel Caruso's avatar
Gabriel Caruso committed
76 77

                    return var_export($p, true);
78
                }, $query['params'] ?: array());
79
                $queries .= $i.". SQL: '".$query['sql']."' Params: ".implode(", ", $params).PHP_EOL;
80
                $i--;
81 82
            }

Luís Cobucci's avatar
Luís Cobucci committed
83
            $trace = $t->getTrace();
84
            $traceMsg = "";
jeroendedauw's avatar
jeroendedauw committed
85
            foreach($trace as $part) {
86 87 88 89 90 91 92 93 94 95
                if(isset($part['file'])) {
                    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;
                }
            }

Luís Cobucci's avatar
Luís Cobucci committed
96
            $message = "[".get_class($t)."] ".$t->getMessage().PHP_EOL.PHP_EOL."With queries:".PHP_EOL.$queries.PHP_EOL."Trace:".PHP_EOL.$traceMsg;
97

98
            throw new \Exception($message, (int) $t->getCode(), $t);
99
        }
Luís Cobucci's avatar
Luís Cobucci committed
100
        throw $t;
101
    }
102
}