Passed
Pull Request — master (#3120)
by Sergei
12:25
created

DbalFunctionalTestCase::onNotSuccessfulTest()   C

Complexity

Conditions 11
Paths 10

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 25
nc 10
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Doctrine\Tests;
4
use const PHP_EOL;
5
use function array_map;
6
use function array_reverse;
7
use function count;
8
use function get_class;
9
use function implode;
10
use function is_object;
11
use function is_scalar;
12
use function strpos;
13
use function var_export;
14
15
class DbalFunctionalTestCase extends DbalTestCase
16
{
17
    /**
18
     * Shared connection when a TestCase is run alone (outside of it's functional suite)
19
     *
20
     * @var \Doctrine\DBAL\Connection
21
     */
22
    private static $_sharedConn;
23
24
    /**
25
     * @var \Doctrine\DBAL\Connection
26
     */
27
    protected $_conn;
28
29
    /**
30
     * @var \Doctrine\DBAL\Logging\DebugStack
31
     */
32
    protected $_sqlLoggerStack;
33
34
    protected function resetSharedConn()
35
    {
36
        if (self::$_sharedConn) {
37
            self::$_sharedConn->close();
38
            self::$_sharedConn = null;
39
        }
40
    }
41
42
    protected function setUp()
43
    {
44
        if ( ! isset(self::$_sharedConn)) {
45
            self::$_sharedConn = TestUtil::getConnection();
46
        }
47
        $this->_conn = self::$_sharedConn;
48
49
        $this->_sqlLoggerStack = new \Doctrine\DBAL\Logging\DebugStack();
50
        $this->_conn->getConfiguration()->setSQLLogger($this->_sqlLoggerStack);
51
    }
52
53
    protected function tearDown()
54
    {
55
        while ($this->_conn->isTransactionActive()) {
56
            $this->_conn->rollBack();
57
        }
58
    }
59
60
    protected function onNotSuccessfulTest(\Throwable $t)
61
    {
62
        if ($t instanceof \PHPUnit\Framework\AssertionFailedError) {
63
            throw $t;
64
        }
65
66
        if(isset($this->_sqlLoggerStack->queries) && count($this->_sqlLoggerStack->queries)) {
67
            $queries = "";
68
            $i = count($this->_sqlLoggerStack->queries);
69
            foreach (array_reverse($this->_sqlLoggerStack->queries) as $query) {
70
                $params = array_map(function($p) {
71
                    if (is_object($p)) {
72
                        return get_class($p);
73
                    } elseif (is_scalar($p)) {
74
                        return "'".$p."'";
75
                    }
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
83
            $trace = $t->getTrace();
84
            $traceMsg = "";
85
            foreach($trace as $part) {
86
                if(isset($part['file'])) {
87
                    if(strpos($part['file'], "PHPUnit/") !== false) {
88
                        // Beginning with PHPUnit files we don't print the trace anymore.
89
                        break;
90
                    }
91
92
                    $traceMsg .= $part['file'].":".$part['line'].PHP_EOL;
93
                }
94
            }
95
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
        }
100
        throw $t;
101
    }
102
}
103