DbalFunctionalTestCase.php 2.7 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Doctrine\Tests;

class DbalFunctionalTestCase extends DbalTestCase
{
7 8 9
    /**
     * Shared connection when a TestCase is run alone (outside of it's functional suite)
     *
10
     * @var \Doctrine\DBAL\Connection
11
     */
12
    private static $_sharedConn;
13 14

    /**
15
     * @var \Doctrine\DBAL\Connection
16
     */
17 18
    protected $_conn;

19 20 21 22 23
    /**
     * @var \Doctrine\DBAL\Logging\DebugStack
     */
    protected $_sqlLoggerStack;

24 25
    protected function resetSharedConn()
    {
26 27 28 29
        if (self::$_sharedConn) {
            self::$_sharedConn->close();
            self::$_sharedConn = null;
        }
30 31
    }

32 33
    protected function setUp()
    {
34 35
        if ( ! isset(self::$_sharedConn)) {
            self::$_sharedConn = TestUtil::getConnection();
36
        }
37
        $this->_conn = self::$_sharedConn;
38 39 40 41 42

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

43 44 45 46 47 48 49
    protected function tearDown()
    {
        while ($this->_conn->isTransactionActive()) {
            $this->_conn->rollBack();
        }
    }

Luís Cobucci's avatar
Luís Cobucci committed
50
    protected function onNotSuccessfulTest(\Throwable $t)
51
    {
Luís Cobucci's avatar
Luís Cobucci committed
52 53
        if ($t instanceof \PHPUnit\Framework\AssertionFailedError) {
            throw $t;
54 55 56 57
        }

        if(isset($this->_sqlLoggerStack->queries) && count($this->_sqlLoggerStack->queries)) {
            $queries = "";
58
            $i = count($this->_sqlLoggerStack->queries);
jeroendedauw's avatar
jeroendedauw committed
59
            foreach (array_reverse($this->_sqlLoggerStack->queries) as $query) {
60 61 62 63 64 65
                $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
66 67

                    return var_export($p, true);
68
                }, $query['params'] ?: array());
69
                $queries .= $i.". SQL: '".$query['sql']."' Params: ".implode(", ", $params).PHP_EOL;
70
                $i--;
71 72
            }

Luís Cobucci's avatar
Luís Cobucci committed
73
            $trace = $t->getTrace();
74
            $traceMsg = "";
jeroendedauw's avatar
jeroendedauw committed
75
            foreach($trace as $part) {
76 77 78 79 80 81 82 83 84 85
                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
86
            $message = "[".get_class($t)."] ".$t->getMessage().PHP_EOL.PHP_EOL."With queries:".PHP_EOL.$queries.PHP_EOL."Trace:".PHP_EOL.$traceMsg;
87

88
            throw new \Exception($message, (int) $t->getCode(), $t);
89
        }
Luís Cobucci's avatar
Luís Cobucci committed
90
        throw $t;
91
    }
92
}