DbalFunctionalTestCase.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Doctrine\Tests;
  3. class DbalFunctionalTestCase extends DbalTestCase
  4. {
  5. /**
  6. * Shared connection when a TestCase is run alone (outside of it's functional suite)
  7. *
  8. * @var \Doctrine\DBAL\Connection
  9. */
  10. private static $_sharedConn;
  11. /**
  12. * @var \Doctrine\DBAL\Connection
  13. */
  14. protected $_conn;
  15. /**
  16. * @var \Doctrine\DBAL\Logging\DebugStack
  17. */
  18. protected $_sqlLoggerStack;
  19. protected function resetSharedConn()
  20. {
  21. if (self::$_sharedConn) {
  22. self::$_sharedConn->close();
  23. self::$_sharedConn = null;
  24. }
  25. }
  26. protected function setUp()
  27. {
  28. if ( ! isset(self::$_sharedConn)) {
  29. self::$_sharedConn = TestUtil::getConnection();
  30. }
  31. $this->_conn = self::$_sharedConn;
  32. $this->_sqlLoggerStack = new \Doctrine\DBAL\Logging\DebugStack();
  33. $this->_conn->getConfiguration()->setSQLLogger($this->_sqlLoggerStack);
  34. }
  35. protected function onNotSuccessfulTest(\Exception $e)
  36. {
  37. if ($e instanceof \PHPUnit_Framework_AssertionFailedError) {
  38. throw $e;
  39. }
  40. if(isset($this->_sqlLoggerStack->queries) && count($this->_sqlLoggerStack->queries)) {
  41. $queries = "";
  42. $i = count($this->_sqlLoggerStack->queries);
  43. foreach (array_reverse($this->_sqlLoggerStack->queries) AS $query) {
  44. $params = array_map(function($p) { if (is_object($p)) return get_class($p); else return "'".$p."'"; }, $query['params'] ?: array());
  45. $queries .= ($i+1).". SQL: '".$query['sql']."' Params: ".implode(", ", $params).PHP_EOL;
  46. $i--;
  47. }
  48. $trace = $e->getTrace();
  49. $traceMsg = "";
  50. foreach($trace AS $part) {
  51. if(isset($part['file'])) {
  52. if(strpos($part['file'], "PHPUnit/") !== false) {
  53. // Beginning with PHPUnit files we don't print the trace anymore.
  54. break;
  55. }
  56. $traceMsg .= $part['file'].":".$part['line'].PHP_EOL;
  57. }
  58. }
  59. $message = "[".get_class($e)."] ".$e->getMessage().PHP_EOL.PHP_EOL."With queries:".PHP_EOL.$queries.PHP_EOL."Trace:".PHP_EOL.$traceMsg;
  60. throw new \Exception($message, (int)$e->getCode(), $e);
  61. }
  62. throw $e;
  63. }
  64. }