<?php
declare(strict_types=1);

// Static/no-connection gate for the private ODBC subclass only.
require_once __DIR__ . '/../../../../zhctproject/ai_api/vendor/autoload.php';
require_once __DIR__ . '/PrivateOdbcDm.php';

$checks = 0;
$same = static function ($actual, $expected, string $label) use (&$checks): void {
    if ($actual !== $expected) {
        throw new RuntimeException($label . ': expected '
            . var_export($expected, true) . ', got ' . var_export($actual, true));
    }
    ++$checks;
};

$connection = new PrivateOdbcDm([
    'hostname' => '127.0.0.1',
    'hostport' => 5238,
    'database' => 'AIZHCT_APP',
    'username' => '',
    'password' => '',
]);
$same($connection instanceof \app\common\database\Dm, true,
    'prototype inherits actual AI DM connector');
$same($connection->getBuilderClass(), \app\common\database\DmBuilder::class,
    'prototype keeps actual DM builder');
$same($connection->getPdo(), false, 'construction creates no PDO connection');

$parse = new ReflectionMethod(PrivateOdbcDm::class, 'parseDsn');
$parse->setAccessible(true);
$same($parse->invoke($connection, [
    'hostname' => '127.0.0.1', 'hostport' => 5238,
    'database' => 'AIZHCT_APP',
]), 'odbc:CDX_PRIVATE_AI', 'only private DSN output changes');
$same($connection->getPdo(), false, 'DSN validation still creates no PDO connection');
try {
    $parse->invoke($connection, [
        'hostname' => '127.0.0.1', 'hostport' => 5238,
        'database' => 'bad schema!',
    ]);
    throw new RuntimeException('invalid schema should retain Dm validation');
} catch (InvalidArgumentException $error) {
    ++$checks;
}
$create = new ReflectionMethod(PrivateOdbcDm::class, 'createPdo');
$same($create->getDeclaringClass()->getName(), PrivateOdbcDm::class,
    'private class owns PDO statement installation');

echo 'ODBC_APP_CONNECTOR_NO_CONNECTION_PASS: ' . $checks
    . ' checks; no database or extension' . PHP_EOL;
