3 namespace Doctrine\Tests;
5 class DbalFunctionalTestCase extends DbalTestCase
8 * Shared connection when a TestCase is run alone (outside of it's functional suite)
10 * @var \Doctrine\DBAL\Connection
12 private static $_sharedConn;
15 * @var \Doctrine\DBAL\Connection
20 * @var \Doctrine\DBAL\Logging\DebugStack
22 protected $_sqlLoggerStack;
24 protected function resetSharedConn()
26 if (self::$_sharedConn) {
27 self::$_sharedConn->close();
28 self::$_sharedConn = null;
32 protected function setUp()
34 if ( ! isset(self::$_sharedConn)) {
35 self::$_sharedConn = TestUtil::getConnection();
37 $this->_conn = self::$_sharedConn;
39 $this->_sqlLoggerStack = new \Doctrine\DBAL\Logging\DebugStack();
40 $this->_conn->getConfiguration()->setSQLLogger($this->_sqlLoggerStack);
43 protected function onNotSuccessfulTest(\Exception $e)
45 if ($e instanceof \PHPUnit_Framework_AssertionFailedError) {
49 if(isset($this->_sqlLoggerStack->queries) && count($this->_sqlLoggerStack->queries)) {
51 $i = count($this->_sqlLoggerStack->queries);
52 foreach (array_reverse($this->_sqlLoggerStack->queries) AS $query) {
53 $params = array_map(function($p) { if (is_object($p)) return get_class($p); else return "'".$p."'"; }, $query['params'] ?: array());
54 $queries .= ($i+1).". SQL: '".$query['sql']."' Params: ".implode(", ", $params).PHP_EOL;
58 $trace = $e->getTrace();
60 foreach($trace AS $part) {
61 if(isset($part['file'])) {
62 if(strpos($part['file'], "PHPUnit/") !== false) {
63 // Beginning with PHPUnit files we don't print the trace anymore.
67 $traceMsg .= $part['file'].":".$part['line'].PHP_EOL;
71 $message = "[".get_class($e)."] ".$e->getMessage().PHP_EOL.PHP_EOL."With queries:".PHP_EOL.$queries.PHP_EOL."Trace:".PHP_EOL.$traceMsg;
73 throw new \Exception($message, (int)$e->getCode(), $e);