Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.6 KiB

3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <?php
  2. /**
  3. * DatabaseTest class.
  4. */
  5. namespace Friendica\Test;
  6. use Friendica\App\Mode;
  7. use Friendica\Core\Config\Cache\ConfigCache;
  8. use Friendica\Database\Database;
  9. use Friendica\Factory\ConfigFactory;
  10. use Friendica\Factory\DBFactory;
  11. use Friendica\Factory\ProfilerFactory;
  12. use Friendica\Util\BasePath;
  13. use Friendica\Util\ConfigFileLoader;
  14. use Friendica\Util\Profiler;
  15. use PHPUnit\DbUnit\DataSet\YamlDataSet;
  16. use PHPUnit\DbUnit\TestCaseTrait;
  17. use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
  18. use Psr\Log\NullLogger;
  19. require_once __DIR__ . '/../boot.php';
  20. /**
  21. * Abstract class used by tests that need a database.
  22. */
  23. abstract class DatabaseTest extends MockedTest
  24. {
  25. use TestCaseTrait;
  26. /** @var Database */
  27. protected static $dba;
  28. /** @var BasePath */
  29. protected static $basePath;
  30. /** @var Mode */
  31. protected static $mode;
  32. /** @var ConfigCache */
  33. protected static $configCache;
  34. /** @var Profiler */
  35. protected static $profiler;
  36. public static function setUpBeforeClass()
  37. {
  38. parent::setUpBeforeClass();
  39. self::$basePath = new BasePath(dirname(__DIR__));
  40. $configLoader = new ConfigFileLoader(self::$basePath->getPath());
  41. $configFactory = new ConfigFactory();
  42. self::$configCache = $configFactory->createCache($configLoader);
  43. self::$profiler = new Profiler(self::$configCache);
  44. self::$dba = new Database(self::$configCache, self::$profiler, new NullLogger(), $_SERVER);
  45. self::$mode = new Mode(self::$basePath, self::$dba, self::$configCache);
  46. }
  47. /**
  48. * Get database connection.
  49. *
  50. * This function is executed before each test in order to get a database connection that can be used by tests.
  51. * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
  52. *
  53. * If it could not connect to the database, the test is skipped.
  54. *
  55. * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
  56. * @see https://phpunit.de/manual/5.7/en/database.html
  57. */
  58. protected function getConnection()
  59. {
  60. if (!getenv('MYSQL_DATABASE')) {
  61. $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
  62. }
  63. if (!self::$dba->isConnected()) {
  64. if (!self::$dba->connect()) {
  65. $this->markTestSkipped('Could not connect to the database.');
  66. }
  67. }
  68. return $this->createDefaultDBConnection(self::$dba->getConnection(), getenv('MYSQL_DATABASE'));
  69. }
  70. /**
  71. * Get dataset to populate the database with.
  72. * @return YamlDataSet
  73. * @see https://phpunit.de/manual/5.7/en/database.html
  74. */
  75. protected function getDataSet()
  76. {
  77. return new YamlDataSet(__DIR__ . '/datasets/api.yml');
  78. }
  79. }