<?php
// DbToolsTest.php
// Created: 2021-04-28
// Updated: 2024-07-31

declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use Index\Data\DbTools;
use Index\Data\DbType;
use Index\Data\MariaDB\MariaDBBackend;
use Index\Data\NullDb\NullDbConnection;
use Index\Data\SQLite\SQLiteBackend;
use Index\IO\MemoryStream;

#[CoversClass(DbTools::class)]
#[CoversClass(DbType::class)]
#[CoversClass(NullDbConnection::class)]
#[CoversClass(MariaDBBackend::class)]
#[CoversClass(SQLiteBackend::class)]
#[UsesClass(MemoryStream::class)]
final class DbToolsTest extends TestCase {
    public function testDetectType(): void {
        $this->assertEquals(DbType::NULL, DbTools::detectType(null));
        $this->assertEquals(DbType::INTEGER, DbTools::detectType(12345));
        $this->assertEquals(DbType::FLOAT, DbTools::detectType(123.45));
        $this->assertEquals(DbType::STRING, DbTools::detectType('This is a string.'));
        $this->assertEquals(DbType::BLOB, DbTools::detectType(MemoryStream::fromString('This is a string inside a memory stream.')));
    }

    public function testDSN(): void {
        $nullDbConn1 = DbTools::create('null:');
        $this->assertInstanceOf(NullDbConnection::class, $nullDbConn1);

        $nullDbConn2 = DbTools::create('Index-Data-NullDb-NullDbBackend:');
        $this->assertInstanceOf(NullDbConnection::class, $nullDbConn2);

        $maria = new MariaDBBackend;
        $sqlite = new SQLiteBackend;

        // flashii style misuzu connections details
        $mci1 = $maria->parseDsn('mariadb://flashii:TiFGzCTxzx0n2HYzmNZpa98j255X7W4B@:unix:/flashii/misuzu?socket=/var/run/mysqld/mysqld.sock&charset=utf8mb4&init=SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\'');

        $this->assertTrue($mci1->isUnixSocket());
        $this->assertEquals('/var/run/mysqld/mysqld.sock', (string)$mci1->getEndPoint());
        $this->assertEquals('', $mci1->getHost());
        $this->assertEquals(-1, $mci1->getPort());
        $this->assertEquals('/var/run/mysqld/mysqld.sock', $mci1->getSocketPath());
        $this->assertEquals('flashii', $mci1->getUserName());
        $this->assertEquals('TiFGzCTxzx0n2HYzmNZpa98j255X7W4B', $mci1->getPassword());
        $this->assertEquals('flashii_misuzu', $mci1->getDatabaseName());
        $this->assertTrue($mci1->hasCharacterSet());
        $this->assertEquals('utf8mb4', $mci1->getCharacterSet());
        $this->assertTrue($mci1->hasInitCommand());
        $this->assertEquals('SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\'', $mci1->getInitCommand());
        $this->assertFalse($mci1->isSecure());
        $this->assertNull($mci1->getKeyPath());
        $this->assertNull($mci1->getCertificatePath());
        $this->assertNull($mci1->getCertificateAuthorityPath());
        $this->assertNull($mci1->getTrustedCertificatesPath());
        $this->assertNull($mci1->getCipherAlgorithms());
        $this->assertTrue($mci1->shouldVerifyCertificate());
        $this->assertFalse($mci1->shouldUseCompression());

        // generic ipv4
        $mci2 = $maria->parseDsn('mariadb://username:password@127.0.0.1/database');
        $this->assertEquals('127.0.0.1', (string)$mci2->getEndPoint());
        $this->assertEquals('127.0.0.1', $mci2->getHost());
        $this->assertEquals(3306, $mci2->getPort());
        $this->assertEquals('', $mci2->getSocketPath());
        $this->assertEquals('username', $mci2->getUserName());
        $this->assertEquals('password', $mci2->getPassword());
        $this->assertEquals('database', $mci2->getDatabaseName());
        $this->assertFalse($mci2->hasCharacterSet());
        $this->assertNull($mci2->getCharacterSet());
        $this->assertFalse($mci2->hasInitCommand());
        $this->assertNull($mci2->getInitCommand());
        $this->assertFalse($mci2->isSecure());
        $this->assertNull($mci2->getKeyPath());
        $this->assertNull($mci2->getCertificatePath());
        $this->assertNull($mci2->getCertificateAuthorityPath());
        $this->assertNull($mci2->getTrustedCertificatesPath());
        $this->assertNull($mci2->getCipherAlgorithms());
        $this->assertTrue($mci2->shouldVerifyCertificate());
        $this->assertFalse($mci2->shouldUseCompression());

        // generic ipv6 with port
        $mci3 = $maria->parseDsn('mysql://username:password@[::1]:9001/database');
        $this->assertEquals('[::1]:9001', (string)$mci3->getEndPoint());
        $this->assertEquals('::1', $mci3->getHost());
        $this->assertEquals(9001, $mci3->getPort());
        $this->assertEquals('', $mci3->getSocketPath());
        $this->assertEquals('username', $mci3->getUserName());
        $this->assertEquals('password', $mci3->getPassword());
        $this->assertEquals('database', $mci3->getDatabaseName());
        $this->assertFalse($mci3->hasCharacterSet());
        $this->assertNull($mci3->getCharacterSet());
        $this->assertFalse($mci3->hasInitCommand());
        $this->assertNull($mci3->getInitCommand());
        $this->assertFalse($mci3->isSecure());
        $this->assertNull($mci3->getKeyPath());
        $this->assertNull($mci3->getCertificatePath());
        $this->assertNull($mci3->getCertificateAuthorityPath());
        $this->assertNull($mci3->getTrustedCertificatesPath());
        $this->assertNull($mci3->getCipherAlgorithms());
        $this->assertTrue($mci3->shouldVerifyCertificate());
        $this->assertFalse($mci3->shouldUseCompression());

        // sqlite normal
        $sql1 = $sqlite->parseDsn('sqlite:/path/to/database.db?key=ebwOKzGkLYIxDGXk&openOnly');
        $this->assertEquals('/path/to/database.db', $sql1->getFileName());
        $this->assertEquals('ebwOKzGkLYIxDGXk', $sql1->getEncryptionKey());
        $this->assertFalse($sql1->shouldReadOnly());
        $this->assertFalse($sql1->shouldCreate());

        // sqlite temp file
        $sql1 = $sqlite->parseDsn('sqlite:?key=Y63vrCttK8bEdPUIXVCOiLQKIgdbUn8V&readOnly');
        $this->assertEquals('', $sql1->getFileName());
        $this->assertEquals('Y63vrCttK8bEdPUIXVCOiLQKIgdbUn8V', $sql1->getEncryptionKey());
        $this->assertTrue($sql1->shouldReadOnly());
        $this->assertTrue($sql1->shouldCreate());

        // sqlite memory
        $sql1 = $sqlite->parseDsn('sqlite::memory:');
        $this->assertEquals(':memory:', $sql1->getFileName());
        $this->assertEquals('', $sql1->getEncryptionKey());
        $this->assertFalse($sql1->shouldReadOnly());
        $this->assertTrue($sql1->shouldCreate());
    }
}