PageRenderTime 23ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/core/case/database/postgresql.php

https://github.com/Hackwar/joomla-platform
PHP | 141 lines | 70 code | 15 blank | 56 comment | 5 complexity | a97362844d66d5e3a63f5656591bdff2 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Test
  4. *
  5. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE
  7. */
  8. /**
  9. * Abstract test case class for PostgreSQL database testing.
  10. *
  11. * @package Joomla.Test
  12. * @since 12.1
  13. */
  14. abstract class TestCaseDatabasePostgresql extends TestCaseDatabase
  15. {
  16. /**
  17. * @var JDatabaseDriver The active database driver being used for the tests.
  18. * @since 12.1
  19. */
  20. protected static $driver;
  21. /**
  22. * @var array The JDatabaseDriver options for the connection.
  23. * @since 12.1
  24. */
  25. private static $_options = array('driver' => 'postgresql');
  26. /**
  27. * @var JDatabaseDriver The saved database driver to be restored after these tests.
  28. * @since 12.1
  29. */
  30. private static $_stash;
  31. /**
  32. * This method is called before the first test of this test class is run.
  33. *
  34. * An example DSN would be: host=localhost;port=5432;dbname=joomla_ut;user=utuser;pass=ut1234
  35. *
  36. * @return void
  37. *
  38. * @since 12.1
  39. */
  40. public static function setUpBeforeClass()
  41. {
  42. // First let's look to see if we have a DSN defined or in the environment variables.
  43. if (defined('JTEST_DATABASE_POSTGRESQL_DSN') || getenv('JTEST_DATABASE_POSTGRESQL_DSN'))
  44. {
  45. $dsn = defined('JTEST_DATABASE_POSTGRESQL_DSN') ? JTEST_DATABASE_POSTGRESQL_DSN : getenv('JTEST_DATABASE_POSTGRESQL_DSN');
  46. }
  47. else
  48. {
  49. return;
  50. }
  51. // First let's trim the pgsql: part off the front of the DSN if it exists.
  52. if (strpos($dsn, 'pgsql:') === 0)
  53. {
  54. $dsn = substr($dsn, 6);
  55. }
  56. // Split the DSN into its parts over semicolons.
  57. $parts = explode(';', $dsn);
  58. // Parse each part and populate the options array.
  59. foreach ($parts as $part)
  60. {
  61. list ($k, $v) = explode('=', $part, 2);
  62. switch ($k)
  63. {
  64. case 'host':
  65. self::$_options['host'] = $v;
  66. break;
  67. case 'port':
  68. self::$_options['port'] = $v;
  69. break;
  70. case 'dbname':
  71. self::$_options['database'] = $v;
  72. break;
  73. case 'user':
  74. self::$_options['user'] = $v;
  75. break;
  76. case 'pass':
  77. self::$_options['password'] = $v;
  78. break;
  79. }
  80. }
  81. try
  82. {
  83. // Attempt to instantiate the driver.
  84. self::$driver = JDatabaseDriver::getInstance(self::$_options);
  85. }
  86. catch (RuntimeException $e)
  87. {
  88. self::$driver = null;
  89. }
  90. // If for some reason an exception object was returned set our database object to null.
  91. if (self::$driver instanceof Exception)
  92. {
  93. self::$driver = null;
  94. }
  95. // Setup the factory pointer for the driver and stash the old one.
  96. self::$_stash = JFactory::$database;
  97. JFactory::$database = self::$driver;
  98. }
  99. /**
  100. * This method is called after the last test of this test class is run.
  101. *
  102. * @return void
  103. *
  104. * @since 12.1
  105. */
  106. public static function tearDownAfterClass()
  107. {
  108. JFactory::$database = self::$_stash;
  109. self::$driver = null;
  110. }
  111. /**
  112. * Returns the default database connection for running the tests.
  113. *
  114. * @return PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection
  115. *
  116. * @since 12.1
  117. */
  118. protected function getConnection()
  119. {
  120. // Compile the connection DSN.
  121. $dsn = 'pgsql:host=' . self::$_options['host'] . ';port=' . self::$_options['port'] . ';dbname=' . self::$_options['database'];
  122. // Create the PDO object from the DSN and options.
  123. $pdo = new PDO($dsn, self::$_options['user'], self::$_options['password']);
  124. return $this->createDefaultDBConnection($pdo, self::$_options['database']);
  125. }
  126. }