<?php

namespace Unit\Controller;

use App\Controller\ConfigController;
use App\Controller\DatabaseConnection;
use PDO;
use PDOException;

/**
 * @covers \App\Controller\DatabaseConnection
 * @covers \App\Controller\ConfigController
 */
class DatabaseConnectionTest extends BindApiControllerTest
{
    private PDO $dbConnection;

    public function testGetConnection()
    {
       $configController = new ConfigController(test: true);

        $dbHost     = $configController->getConfig(configKey: 'dbHost');
        $dbPort     = $configController->getConfig(configKey: 'dbPort');
        $dbDatabase = $configController->getConfig(configKey: 'dbDatabase');
        $dbUser     = $configController->getConfig(configKey: 'dbUser');
        $dbPassword = $configController->getConfig(configKey: 'dbPassword');

        try {
            $this->dbConnection = new PDO(
                dsn: "mysql:host=$dbHost;port=$dbPort;charset=utf8mb4;dbname=$dbDatabase",
                username: $dbUser,
                password: $dbPassword
            );
        } catch (PDOException $e) {
            $this->fail(message: $e->getMessage());
        }

        $databaseConnection = new DatabaseConnection(configController: $configController);

        self::assertEquals(expected: $databaseConnection->getConnection(), actual: $this->dbConnection);
    }
}
