X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=tests%2FDatabaseTest.php;h=4f0275493c8f659d28734c99052cd3145080ff2a;hb=10395429a7d88a86b21230aa7719a4f1855778d1;hp=70caba71221996361129f479f7fe7328ef569894;hpb=5a02e39a65f8f685440228cc1d36738cbe15f32b;p=friendica.git diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index 70caba7122..4f0275493c 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -5,66 +5,56 @@ namespace Friendica\Test; -use Friendica\App; -use Friendica\BaseObject; -use Friendica\Core\Config; -use Friendica\Database\DBA; -use PHPUnit\DbUnit\DataSet\YamlDataSet; -use PHPUnit\DbUnit\TestCaseTrait; -use PHPUnit\Framework\TestCase; -use PHPUnit_Extensions_Database_DB_IDatabaseConnection; +use Friendica\Database\Database; +use Friendica\Test\Util\Database\StaticDatabase; /** * Abstract class used by tests that need a database. */ -abstract class DatabaseTest extends TestCase +abstract class DatabaseTest extends MockedTest { - /** - * @var App The Friendica App - */ - protected $app; - protected function setUp() { - // Reusable App object - $this->app = BaseObject::getApp(); + parent::setUp(); + + StaticDatabase::statConnect($_SERVER); + // Rollbacks every DB usage (in case the test couldn't call tearDown) + StaticDatabase::statRollback(); + // Start the first, outer transaction + StaticDatabase::getGlobConnection()->beginTransaction(); + } + + protected function tearDown() + { + // Rollbacks every DB usage so we don't commit anything into the DB + StaticDatabase::statRollback(); - Config::set('system', 'url', 'http://localhost'); - Config::set('system', 'hostname', 'localhost'); - Config::set('system', 'worker_dont_fork', true); + parent::tearDown(); } /** - * Get database connection. - * - * This function is executed before each test in order to get a database connection that can be used by tests. - * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables. + * Loads a given DB fixture for this DB test * - * If it could not connect to the database, the test is skipped. + * @param string $fixture The path to the fixture + * @param Database $dba The DB connection * - * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection - * @see https://phpunit.de/manual/5.7/en/database.html + * @throws \Exception */ - protected function getConnection() + protected function loadFixture(string $fixture, Database $dba) { - if (!getenv('MYSQL_DATABASE')) { - $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.'); - } + $this->assertFileExists($fixture); - if (!DBA::connected()) { - $this->markTestSkipped('Could not connect to the database.'); - } + $data = include $fixture; - return $this->createDefaultDBConnection(DBA::getConnection(), getenv('MYSQL_DATABASE')); - } + foreach ($data as $tableName => $rows) { + if (!is_array($rows)) { + $dba->p('TRUNCATE TABLE `' . $tableName . '``'); + continue; + } - /** - * Get dataset to populate the database with. - * @return YamlDataSet - * @see https://phpunit.de/manual/5.7/en/database.html - */ - protected function getDataSet() - { - return new YamlDataSet(__DIR__ . '/datasets/api.yml'); + foreach ($rows as $row) { + $dba->insert($tableName, $row); + } + } } }