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.

63 lines
1.6KB

  1. <?php
  2. /**
  3. * DatabaseTest class.
  4. */
  5. namespace Friendica\Test;
  6. use Friendica\Database\DBA;
  7. use PHPUnit\DbUnit\DataSet\YamlDataSet;
  8. use PHPUnit\DbUnit\TestCaseTrait;
  9. use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
  10. require_once __DIR__ . '/../boot.php';
  11. /**
  12. * Abstract class used by tests that need a database.
  13. */
  14. abstract class DatabaseTest extends MockedTest
  15. {
  16. use TestCaseTrait;
  17. /**
  18. * Get database connection.
  19. *
  20. * This function is executed before each test in order to get a database connection that can be used by tests.
  21. * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
  22. *
  23. * If it could not connect to the database, the test is skipped.
  24. *
  25. * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
  26. * @see https://phpunit.de/manual/5.7/en/database.html
  27. */
  28. protected function getConnection()
  29. {
  30. if (!getenv('MYSQL_DATABASE')) {
  31. $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
  32. }
  33. DBA::connect(
  34. __DIR__,
  35. getenv('MYSQL_HOST'),
  36. getenv('MYSQL_USERNAME'),
  37. getenv('MYSQL_PASSWORD'),
  38. getenv('MYSQL_DATABASE'));
  39. if (!DBA::connected()) {
  40. $this->markTestSkipped('Could not connect to the database.');
  41. }
  42. return $this->createDefaultDBConnection(DBA::getConnection(), getenv('MYSQL_DATABASE'));
  43. }
  44. /**
  45. * Get dataset to populate the database with.
  46. * @return YamlDataSet
  47. * @see https://phpunit.de/manual/5.7/en/database.html
  48. */
  49. protected function getDataSet()
  50. {
  51. return new YamlDataSet(__DIR__ . '/datasets/api.yml');
  52. }
  53. }