TestUtil.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Doctrine\Tests;
  3. /**
  4. * TestUtil is a class with static utility methods used during tests.
  5. *
  6. * @author robo
  7. */
  8. class TestUtil
  9. {
  10. /**
  11. * Gets a <b>real</b> database connection using the following parameters
  12. * of the $GLOBALS array:
  13. *
  14. * 'db_type' : The name of the Doctrine DBAL database driver to use.
  15. * 'db_username' : The username to use for connecting.
  16. * 'db_password' : The password to use for connecting.
  17. * 'db_host' : The hostname of the database to connect to.
  18. * 'db_name' : The name of the database to connect to.
  19. * 'db_port' : The port of the database to connect to.
  20. *
  21. * Usually these variables of the $GLOBALS array are filled by PHPUnit based
  22. * on an XML configuration file. If no such parameters exist, an SQLite
  23. * in-memory database is used.
  24. *
  25. * IMPORTANT:
  26. * 1) Each invocation of this method returns a NEW database connection.
  27. * 2) The database is dropped and recreated to ensure it's clean.
  28. *
  29. * @return Doctrine\DBAL\Connection The database connection instance.
  30. */
  31. public static function getConnection()
  32. {
  33. if (isset($GLOBALS['db_type'], $GLOBALS['db_username'], $GLOBALS['db_password'],
  34. $GLOBALS['db_host'], $GLOBALS['db_name'], $GLOBALS['db_port']) &&
  35. isset($GLOBALS['tmpdb_type'], $GLOBALS['tmpdb_username'], $GLOBALS['tmpdb_password'],
  36. $GLOBALS['tmpdb_host'], $GLOBALS['tmpdb_name'], $GLOBALS['tmpdb_port'])) {
  37. $realDbParams = array(
  38. 'driver' => $GLOBALS['db_type'],
  39. 'user' => $GLOBALS['db_username'],
  40. 'password' => $GLOBALS['db_password'],
  41. 'host' => $GLOBALS['db_host'],
  42. 'dbname' => $GLOBALS['db_name'],
  43. 'port' => $GLOBALS['db_port']
  44. );
  45. $tmpDbParams = array(
  46. 'driver' => $GLOBALS['tmpdb_type'],
  47. 'user' => $GLOBALS['tmpdb_username'],
  48. 'password' => $GLOBALS['tmpdb_password'],
  49. 'host' => $GLOBALS['tmpdb_host'],
  50. 'dbname' => $GLOBALS['tmpdb_name'],
  51. 'port' => $GLOBALS['tmpdb_port']
  52. );
  53. $realConn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams);
  54. $platform = $realConn->getDatabasePlatform();
  55. if ($platform->supportsCreateDropDatabase()) {
  56. $dbname = $realConn->getDatabase();
  57. // Connect to tmpdb in order to drop and create the real test db.
  58. $tmpConn = \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams);
  59. $realConn->close();
  60. $tmpConn->getSchemaManager()->dropDatabase($dbname);
  61. $tmpConn->getSchemaManager()->createDatabase($dbname);
  62. $tmpConn->close();
  63. } else {
  64. $sm = $realConn->getSchemaManager();
  65. /* @var $schema Schema */
  66. $schema = $sm->createSchema();
  67. $stmts = $schema->toDropSql($realConn->getDatabasePlatform());
  68. foreach ($stmts AS $stmt) {
  69. try {
  70. $realConn->exec($stmt);
  71. } catch(\Exception $e) {
  72. // TODO: Now is this a real good idea?
  73. }
  74. }
  75. }
  76. $conn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams, null, null);
  77. } else {
  78. $params = array(
  79. 'driver' => 'pdo_sqlite',
  80. 'memory' => true
  81. );
  82. if (isset($GLOBALS['db_path'])) {
  83. $params['path'] = $GLOBALS['db_path'];
  84. unlink($GLOBALS['db_path']);
  85. }
  86. $conn = \Doctrine\DBAL\DriverManager::getConnection($params);
  87. }
  88. return $conn;
  89. }
  90. /**
  91. * @return \Doctrine\DBAL\Connection
  92. */
  93. public static function getTempConnection()
  94. {
  95. $tmpDbParams = array(
  96. 'driver' => $GLOBALS['tmpdb_type'],
  97. 'user' => $GLOBALS['tmpdb_username'],
  98. 'password' => $GLOBALS['tmpdb_password'],
  99. 'host' => $GLOBALS['tmpdb_host'],
  100. 'dbname' => $GLOBALS['tmpdb_name'],
  101. 'port' => $GLOBALS['tmpdb_port']
  102. );
  103. // Connect to tmpdb in order to drop and create the real test db.
  104. return \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams);
  105. }
  106. }