Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / tests / Doctrine / Tests / DbalFunctionalTestCase.php
1 <?php
2
3 namespace Doctrine\Tests;
4
5 class DbalFunctionalTestCase extends DbalTestCase
6 {
7     /**
8      * Shared connection when a TestCase is run alone (outside of it's functional suite)
9      *
10      * @var \Doctrine\DBAL\Connection
11      */
12     private static $_sharedConn;
13
14     /**
15      * @var \Doctrine\DBAL\Connection
16      */
17     protected $_conn;
18
19     /**
20      * @var \Doctrine\DBAL\Logging\DebugStack
21      */
22     protected $_sqlLoggerStack;
23
24     protected function resetSharedConn()
25     {
26         if (self::$_sharedConn) {
27             self::$_sharedConn->close();
28             self::$_sharedConn = null;
29         }
30     }
31
32     protected function setUp()
33     {
34         if ( ! isset(self::$_sharedConn)) {
35             self::$_sharedConn = TestUtil::getConnection();
36         }
37         $this->_conn = self::$_sharedConn;
38
39         $this->_sqlLoggerStack = new \Doctrine\DBAL\Logging\DebugStack();
40         $this->_conn->getConfiguration()->setSQLLogger($this->_sqlLoggerStack);
41     }
42
43     protected function onNotSuccessfulTest(\Exception $e)
44     {
45         if ($e instanceof \PHPUnit_Framework_AssertionFailedError) {
46             throw $e;
47         }
48
49         if(isset($this->_sqlLoggerStack->queries) && count($this->_sqlLoggerStack->queries)) {
50             $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;
55                 $i--;
56             }
57
58             $trace = $e->getTrace();
59             $traceMsg = "";
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.
64                         break;
65                     }
66
67                     $traceMsg .= $part['file'].":".$part['line'].PHP_EOL;
68                 }
69             }
70
71             $message = "[".get_class($e)."] ".$e->getMessage().PHP_EOL.PHP_EOL."With queries:".PHP_EOL.$queries.PHP_EOL."Trace:".PHP_EOL.$traceMsg;
72
73             throw new \Exception($message, (int)$e->getCode(), $e);
74         }
75         throw $e;
76     }
77 }