Passed
Push — amo/scrutinizer-new-engine ( 927449 )
by Andreas
10:16
created

DBALFunctionalTestCase::onNotSuccessfulTest()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 33
rs 7.6666
cc 10
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
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
4
 * license agreements.  See the NOTICE file distributed with this work for
5
 * additional information regarding copyright ownership.  Crate licenses
6
 * this file to you under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.  You may
8
 * obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15
 * License for the specific language governing permissions and limitations
16
 * under the License.
17
 *
18
 * However, if you have executed another commercial license agreement
19
 * with Crate these terms will supersede the license and you may use the
20
 * software solely pursuant to the terms of the relevant commercial agreement.
21
 */
22
namespace Crate\Test\DBAL;
23
24
use Doctrine\DBAL\DriverManager;
25
use Doctrine\DBAL\Logging\DebugStack;
26
use PHPUnit\Framework\AssertionFailedError;
27
use PHPUnit\Framework\TestCase;
28
use Throwable;
29
30
abstract class DBALFunctionalTestCase extends TestCase
31
{
32
    /**
33
     * Shared connection when a TestCase is run alone (outside of it's functional suite)
34
     *
35
     * @var \Doctrine\DBAL\Connection
36
     */
37
    private static $_sharedConn;
38
39
    /**
40
     * @var \Doctrine\DBAL\Connection
41
     */
42
    protected $_conn;
43
44
    /**
45
     * @var \Doctrine\DBAL\Logging\DebugStack
46
     */
47
    protected $_sqlLoggerStack;
48
49
    protected function resetSharedConn()
50
    {
51
        if (self::$_sharedConn) {
52
            self::$_sharedConn->close();
53
            self::$_sharedConn = null;
54
        }
55
    }
56
57
    public function setUp() : void
58
    {
59
        if ( ! isset(self::$_sharedConn)) {
60
            $params = array(
61
                'driverClass' => 'Crate\DBAL\Driver\PDOCrate\Driver',
62
                'host' => 'localhost',
63
                'port' => 4200
64
            );
65
            self::$_sharedConn = DriverManager::getConnection($params);
66
        }
67
        $this->_conn = self::$_sharedConn;
68
69
        $this->_sqlLoggerStack = new DebugStack();
70
        $this->_conn->getConfiguration()->setSQLLogger($this->_sqlLoggerStack);
71
    }
72
73
    protected function onNotSuccessfulTest(Throwable $e) : void
74
    {
75
        if ($e instanceof AssertionFailedError) {
76
            throw $e;
77
        }
78
79
        if(isset($this->_sqlLoggerStack->queries) && count($this->_sqlLoggerStack->queries)) {
80
            $queries = "";
81
            $i = count($this->_sqlLoggerStack->queries);
82
            foreach (array_reverse($this->_sqlLoggerStack->queries) AS $query) {
83
                $params = array_map(function($p) { if (is_object($p)) return get_class($p); else return "'".print_r($p, true)."'"; }, $query['params'] ?: array());
0 ignored issues
show
Bug introduced by
Are you sure print_r($p, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
                $params = array_map(function($p) { if (is_object($p)) return get_class($p); else return "'"./** @scrutinizer ignore-type */ print_r($p, true)."'"; }, $query['params'] ?: array());
Loading history...
84
                $queries .= ($i+1).". SQL: '".$query['sql']."' Params: ".implode(", ", $params).PHP_EOL;
85
                $i--;
86
            }
87
88
            $trace = $e->getTrace();
89
            $traceMsg = "";
90
            foreach($trace AS $part) {
91
                if(isset($part['file'])) {
92
                    if(strpos($part['file'], "PHPUnit/") !== false) {
93
                        // Beginning with PHPUnit files we don't print the trace anymore.
94
                        break;
95
                    }
96
97
                    $traceMsg .= $part['file'].":".$part['line'].PHP_EOL;
98
                }
99
            }
100
101
            $message = "[".get_class($e)."] ".$e->getMessage().PHP_EOL.PHP_EOL."With queries:".PHP_EOL.$queries.PHP_EOL."Trace:".PHP_EOL.$traceMsg;
102
103
            throw new \Exception($message, (int)$e->getCode(), $e);
104
        }
105
        throw $e;
106
    }
107
108
    public function execute($stmt)
109
    {
110
        return $this->_conn->query($stmt);
111
    }
112
113
    public function refresh($table_name)
114
    {
115
        $this->_conn->query('REFRESH TABLE ' . $table_name);
116
    }
117
118
    public function prepareStatement($sql)
119
    {
120
        return $this->_conn->prepare($sql);
121
    }
122
}
123