TestUtil.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. $tableNames = $sm->listTableNames();
  66. foreach ($tableNames AS $tableName) {
  67. $sm->dropTable($tableName);
  68. }
  69. }
  70. $conn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams, null, null);
  71. } else {
  72. $params = array(
  73. 'driver' => 'pdo_sqlite',
  74. 'memory' => true
  75. );
  76. if (isset($GLOBALS['db_path'])) {
  77. $params['path'] = $GLOBALS['db_path'];
  78. unlink($GLOBALS['db_path']);
  79. }
  80. $conn = \Doctrine\DBAL\DriverManager::getConnection($params);
  81. }
  82. return $conn;
  83. }
  84. /**
  85. * @return \Doctrine\DBAL\Connection
  86. */
  87. public static function getTempConnection()
  88. {
  89. $tmpDbParams = array(
  90. 'driver' => $GLOBALS['tmpdb_type'],
  91. 'user' => $GLOBALS['tmpdb_username'],
  92. 'password' => $GLOBALS['tmpdb_password'],
  93. 'host' => $GLOBALS['tmpdb_host'],
  94. 'dbname' => $GLOBALS['tmpdb_name'],
  95. 'port' => $GLOBALS['tmpdb_port']
  96. );
  97. // Connect to tmpdb in order to drop and create the real test db.
  98. return \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams);
  99. }
  100. }