aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2023-08-13 18:23:45 +0200
committerHarald Eilertsen <haraldei@anduin.net>2023-08-13 18:23:45 +0200
commit5bbdbadda0651a85031a831234ba7e9c992c8fa0 (patch)
tree28f63d16052983436fc76685e7226dc4e27ce7cd
parentfd8aca2cba2530265a58d8966babdbbcb01d64e1 (diff)
downloadvolse-hubzilla-5bbdbadda0651a85031a831234ba7e9c992c8fa0.tar.gz
volse-hubzilla-5bbdbadda0651a85031a831234ba7e9c992c8fa0.tar.bz2
volse-hubzilla-5bbdbadda0651a85031a831234ba7e9c992c8fa0.zip
tests: Populate test db with fixtures before test run.
This probably needs to be optimized a bit to avoid loading large amounts of db fixtures before each test. But it's getting somewhere!
-rw-r--r--tests/unit/UnitTestCase.php37
1 files changed, 36 insertions, 1 deletions
diff --git a/tests/unit/UnitTestCase.php b/tests/unit/UnitTestCase.php
index afdaf1c9a..8f104e6d0 100644
--- a/tests/unit/UnitTestCase.php
+++ b/tests/unit/UnitTestCase.php
@@ -23,13 +23,14 @@
namespace Zotlabs\Tests\Unit;
use PHPUnit\Framework\TestCase;
+use Symfony\Component\Yaml\Yaml;
/*
* Make sure global constants and the global App object is available to the
* tests.
*/
require_once __DIR__ . '/../../boot.php';
-require_once('include/dba/dba_driver.php');
+require_once 'include/dba/dba_driver.php' ;
/**
* @brief Base class for our Unit Tests.
@@ -42,6 +43,7 @@ require_once('include/dba/dba_driver.php');
*/
class UnitTestCase extends TestCase {
private bool $in_transaction = false;
+ protected array $fixtures = array();
public static function setUpBeforeClass() : void {
if ( !\DBA::$dba ) {
@@ -60,6 +62,8 @@ class UnitTestCase extends TestCase {
"Unable to connect to db! Error is: "
. \DBA::$dba->db->errorInfo());
}
+
+ \DBA::$dba->dbg(true);
}
}
@@ -68,6 +72,9 @@ class UnitTestCase extends TestCase {
// Create a transaction, so that any actions taken by the
// tests does not change the actual contents of the database.
$this->in_transaction = \DBA::$dba->db->beginTransaction();
+
+ $this->loadFixtures();
+
}
}
@@ -84,4 +91,32 @@ class UnitTestCase extends TestCase {
}
}
}
+
+ private function loadFixtures() : void {
+ $files = glob(__DIR__ . '/includes/dba/_files/*.yml');
+ if ($files === false || empty($files)) {
+ error_log('[-] ' . __METHOD__ . ': No fixtures found! :(');
+ }
+ array_walk($files, fn($file) => $this->loadFixture($file));
+ }
+
+ private function loadFixture($file) : void {
+ $table_name = basename($file, '.yml');
+ $this->fixtures[$table_name] = Yaml::parseFile($file)[$table_name];
+
+ //echo "\n[*] Loaded fixture '{$table_name}':\n";
+ // . print_r($this->fixtures[$table_name], true)
+ // . PHP_EOL;
+
+ foreach ($this->fixtures[$table_name] as $entry) {
+ $query = 'INSERT INTO ' . dbesc($table_name) . '('
+ . implode(',', array_keys($entry))
+ . ') VALUES('
+ . implode(',', array_map(fn($val) => "'{$val}'", array_values($entry)))
+ . ')';
+
+ //print_r($query);
+ q($query);
+ }
+ }
}