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()); |
|
|
|
|
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
|
|
|
|