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.

DatabaseTestTrait.php 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * DatabaseTest class.
  4. */
  5. namespace Friendica\Test;
  6. use Friendica\Database\Database;
  7. use Friendica\Test\Util\Database\StaticDatabase;
  8. /**
  9. * Abstract class used by tests that need a database.
  10. */
  11. trait DatabaseTestTrait
  12. {
  13. protected function setUp()
  14. {
  15. StaticDatabase::statConnect($_SERVER);
  16. // Rollbacks every DB usage (in case the test couldn't call tearDown)
  17. StaticDatabase::statRollback();
  18. // Start the first, outer transaction
  19. StaticDatabase::getGlobConnection()->beginTransaction();
  20. parent::setUp();
  21. }
  22. protected function tearDown()
  23. {
  24. // Rollbacks every DB usage so we don't commit anything into the DB
  25. StaticDatabase::statRollback();
  26. parent::tearDown();
  27. }
  28. /**
  29. * Loads a given DB fixture for this DB test
  30. *
  31. * @param string $fixture The path to the fixture
  32. * @param Database $dba The DB connection
  33. *
  34. * @throws \Exception
  35. */
  36. protected function loadFixture(string $fixture, Database $dba)
  37. {
  38. $data = include $fixture;
  39. foreach ($data as $tableName => $rows) {
  40. if (!is_array($rows)) {
  41. $dba->p('TRUNCATE TABLE `' . $tableName . '``');
  42. continue;
  43. }
  44. foreach ($rows as $row) {
  45. $dba->insert($tableName, $row);
  46. }
  47. }
  48. }
  49. }