TestUtil.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. if (isset($GLOBALS['db_unix_socket'])) {
  54. $realDbParams['unix_socket'] = $GLOBALS['db_unix_socket'];
  55. }
  56. if (isset($GLOBALS['tmpdb_unix_socket'])) {
  57. $tmpDbParams['unix_socket'] = $GLOBALS['tmpdb_unix_socket'];
  58. }
  59. $realConn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams);
  60. $platform = $realConn->getDatabasePlatform();
  61. if ($platform->supportsCreateDropDatabase()) {
  62. $dbname = $realConn->getDatabase();
  63. // Connect to tmpdb in order to drop and create the real test db.
  64. $tmpConn = \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams);
  65. $realConn->close();
  66. $tmpConn->getSchemaManager()->dropAndCreateDatabase($dbname);
  67. $tmpConn->close();
  68. } else {
  69. $sm = $realConn->getSchemaManager();
  70. /* @var $schema Schema */
  71. $schema = $sm->createSchema();
  72. $stmts = $schema->toDropSql($realConn->getDatabasePlatform());
  73. foreach ($stmts AS $stmt) {
  74. $realConn->exec($stmt);
  75. }
  76. }
  77. $conn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams, null, null);
  78. } else {
  79. $params = array(
  80. 'driver' => 'pdo_sqlite',
  81. 'memory' => true
  82. );
  83. if (isset($GLOBALS['db_path'])) {
  84. $params['path'] = $GLOBALS['db_path'];
  85. unlink($GLOBALS['db_path']);
  86. }
  87. $conn = \Doctrine\DBAL\DriverManager::getConnection($params);
  88. }
  89. if (isset($GLOBALS['db_event_subscribers'])) {
  90. $evm = $conn->getEventManager();
  91. foreach (explode(",", $GLOBALS['db_event_subscribers']) AS $subscriberClass) {
  92. $subscriberInstance = new $subscriberClass();
  93. $evm->addEventSubscriber($subscriberInstance);
  94. }
  95. }
  96. return $conn;
  97. }
  98. /**
  99. * @return \Doctrine\DBAL\Connection
  100. */
  101. public static function getTempConnection()
  102. {
  103. $tmpDbParams = array(
  104. 'driver' => $GLOBALS['tmpdb_type'],
  105. 'user' => $GLOBALS['tmpdb_username'],
  106. 'password' => $GLOBALS['tmpdb_password'],
  107. 'host' => $GLOBALS['tmpdb_host'],
  108. 'dbname' => $GLOBALS['tmpdb_name'],
  109. 'port' => $GLOBALS['tmpdb_port']
  110. );
  111. // Connect to tmpdb in order to drop and create the real test db.
  112. return \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams);
  113. }
  114. }