connected ) { $msg = "Unable to connect to db! "; if(file_exists('dbfail.out')) { $msg .= file_get_contents('dbfail.out'); } throw new \Exception($msg); } \DBA::$dba->dbg(true); } } protected function setUp() : void { if ( \DBA::$dba->connected ) { // 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(); } // Make sure app config is reset and loaded from fixtures \App::$config = array(); \Zotlabs\Lib\Config::Load('system'); } protected function tearDown() : void { if ( \DBA::$dba->connected && $this->in_transaction ) { // Roll back the transaction, restoring the db to the // state it was before the test was run. if ( \DBA::$dba->db->rollBack() ) { $this->in_transaction = false; } else { throw new \Exception( "Transaction rollback failed! Error is: " . \DBA::$dba->db->errorInfo()); } } } private static function dbtype(string $type): int { if (trim(strtolower($type)) === 'postgres') { return DBTYPE_POSTGRES; } else { return DBTYPE_MYSQL; } } 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_parse_file($file)[$table_name]; 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))) . ')'; q($query); } } }