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