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.

80 lines
2.2 KiB

7 months ago
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2010-2021, the Friendica project
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Friendica\Test;
  22. use Friendica\Database\Database;
  23. use Friendica\Test\Util\Database\StaticDatabase;
  24. /**
  25. * Abstract class used by tests that need a database.
  26. */
  27. trait DatabaseTestTrait
  28. {
  29. protected function setUpDb()
  30. {
  31. StaticDatabase::statConnect($_SERVER);
  32. // Rollbacks every DB usage (in case the test couldn't call tearDown)
  33. StaticDatabase::statRollback();
  34. // Rollback the first, outer transaction just 2 be sure
  35. StaticDatabase::getGlobConnection()->rollBack();
  36. // Start the first, outer transaction
  37. StaticDatabase::getGlobConnection()->beginTransaction();
  38. }
  39. protected function tearDownDb()
  40. {
  41. try {
  42. // Rollbacks every DB usage so we don't commit anything into the DB
  43. StaticDatabase::statRollback();
  44. } catch (\PDOException $exception) {
  45. print_r("Found already rolled back transaction");
  46. }
  47. }
  48. /**
  49. * Loads a given DB fixture for this DB test
  50. *
  51. * @param string $fixture The path to the fixture
  52. * @param Database $dba The DB connection
  53. *
  54. * @throws \Exception
  55. */
  56. protected function loadFixture(string $fixture, Database $dba)
  57. {
  58. $data = include $fixture;
  59. foreach ($data as $tableName => $rows) {
  60. if (is_numeric($tableName)) {
  61. continue;
  62. }
  63. if (!is_array($rows)) {
  64. $dba->e('TRUNCATE TABLE `' . $tableName . '``');
  65. continue;
  66. }
  67. foreach ($rows as $row) {
  68. $dba->insert($tableName, $row, true);
  69. }
  70. }
  71. }
  72. }