/** * On charge les différente connexions à la BDD */ public function initDatabase() { $configDatabase = Config::get('database'); if ($configDatabase) { $databases = $configDatabase->config; foreach ($databases as $name => $database) { $type = $this->getConfigField($database, 'type', 'mysql'); $host = $this->getConfigField($database, 'host', 'localhost'); $port = $this->getConfigField($database, 'port', 3306); $encoding = $this->getConfigField($database, 'encoding', 'utf8'); $user = $this->getConfigField($database, 'user', 'root'); $pass = $this->getConfigField($database, 'pass', 'secret'); $name = $this->getConfigField($database, 'name', 'dev'); $prefixDbname = 'dbname='; $resultPrefixShow = in_array($type, ['sqlite', 'sqlite2']); if ($resultPrefixShow) { $prefixDbname = ''; } $dsn = "{$type}:{$prefixDbname}{$name}"; if (!$resultPrefixShow) { $dsn .= ";host={$host};charset={$encoding}"; if ($port != 3306) { $dsn .= ";port={$port}"; } } try { $connection = new Connection($dsn, $user, $pass); Connections::add($name, $connection); } catch (PDOException $exception) { } } } }
/** * Test du chargement des tables */ public function testLoadTable() { require_once __DIR__ . '/../Test/Table/NewsTable.php'; Config::add(__DIR__ . '/../Test/test.php', 'app'); Connections::add('dev', new Connection('sqlite::memory:')); $controller = new Controller(); $this->assertCount(0, $controller->tables); $this->assertInstanceOf(NewsTable::class, $controller->news); $this->assertCount(1, $controller->tables); }