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.

64 lines
1.7 KiB

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